123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- package com.vaadin.tests.components.treegrid;
-
- import static org.hamcrest.Matchers.greaterThanOrEqualTo;
- import static org.hamcrest.number.IsCloseTo.closeTo;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertThat;
-
- import java.util.List;
-
- import org.junit.Test;
- import org.openqa.selenium.StaleElementReferenceException;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.support.ui.ExpectedCondition;
- import org.openqa.selenium.support.ui.ExpectedConditions;
-
- import com.vaadin.testbench.By;
- import com.vaadin.testbench.elements.ButtonElement;
- import com.vaadin.testbench.elements.TreeGridElement;
- import com.vaadin.tests.tb3.MultiBrowserTest;
-
- public class TreeGridBigDetailsManagerTest extends MultiBrowserTest {
-
- private static final String CLASSNAME_ERROR = "v-Notification-error";
- private static final String CLASSNAME_LABEL = "v-label";
- private static final String CLASSNAME_LEAF = "v-treegrid-row-depth-1";
- private static final String CLASSNAME_SPACER = "v-treegrid-spacer";
- private static final String CLASSNAME_TREEGRID = "v-treegrid";
-
- private static final String EXPAND_ALL = "expandAll";
- private static final String COLLAPSE_ALL = "collapseAll";
- private static final String SHOW_DETAILS = "showDetails";
- private static final String HIDE_DETAILS = "hideDetails";
- private static final String ADD_GRID = "addGrid";
- private static final String SCROLL_TO_55 = "scrollTo55";
-
- private TreeGridElement treeGrid;
- private int expectedSpacerHeight = 0;
- private int expectedRowHeight = 0;
-
- private ExpectedCondition<Boolean> expectedConditionDetails(final int root,
- final int branch, final int leaf) {
- return new ExpectedCondition<Boolean>() {
- @Override
- public Boolean apply(WebDriver arg0) {
- return getSpacer(root, branch, leaf) != null;
- }
-
- @Override
- public String toString() {
- // waiting for...
- return String.format(
- "Leaf %s/%s/%s details row contents to be found", root,
- branch, leaf);
- }
- };
- }
-
- private WebElement getSpacer(final int root, final Integer branch,
- final Integer leaf) {
- String text;
- if (leaf == null) {
- if (branch == null) {
- text = "details for Root %s";
- } else {
- text = "details for Branch %s/%s";
- }
- } else {
- text = "details for Leaf %s/%s/%s";
- }
- try {
- List<WebElement> spacers = treeGrid
- .findElements(By.className(CLASSNAME_SPACER));
- for (WebElement spacer : spacers) {
- List<WebElement> labels = spacer
- .findElements(By.className(CLASSNAME_LABEL));
- for (WebElement label : labels) {
- if (String.format(text, root, branch, leaf)
- .equals(label.getText())) {
- return spacer;
- }
- }
- }
- } catch (StaleElementReferenceException e) {
- treeGrid = $(TreeGridElement.class).first();
- }
- return null;
- }
-
- private void ensureExpectedSpacerHeightSet() {
- if (expectedSpacerHeight == 0) {
- expectedSpacerHeight = treeGrid
- .findElement(By.className(CLASSNAME_SPACER)).getSize()
- .getHeight();
- assertThat((double) expectedSpacerHeight, closeTo(27d, 2d));
- }
- if (expectedRowHeight == 0) {
- expectedRowHeight = treeGrid.getRow(0).getSize().getHeight();
- }
- }
-
- private void assertSpacerCount(int expectedSpacerCount) {
- assertEquals("Unexpected amount of spacers.", expectedSpacerCount,
- treeGrid.findElements(By.className(CLASSNAME_SPACER)).size());
- }
-
- /**
- * Asserts that every spacer has the same height.
- */
- private void assertSpacerHeights() {
- List<WebElement> spacers = treeGrid
- .findElements(By.className(CLASSNAME_SPACER));
- for (WebElement spacer : spacers) {
- assertEquals("Unexpected spacer height.", expectedSpacerHeight,
- spacer.getSize().getHeight());
- }
- }
-
- /**
- * Asserts that every spacer is at least a row height from the previous one.
- * Doesn't check that the spacers are in correct order or rendered properly.
- */
- private void assertSpacerPositions() {
- List<WebElement> spacers = treeGrid
- .findElements(By.className(CLASSNAME_SPACER));
- WebElement previousSpacer = null;
- for (WebElement spacer : spacers) {
- if (previousSpacer == null) {
- previousSpacer = spacer;
- continue;
- }
- if (spacer.getLocation().y == 0) {
- // FIXME: find out why there are cases like this out of order
- continue;
- }
- // -1 should be enough, but increased tolerance to -3 for FireFox
- // and IE11 since a few pixels' discrepancy isn't relevant for this
- // fix
- assertThat("Unexpected spacer position.", spacer.getLocation().y,
- greaterThanOrEqualTo(previousSpacer.getLocation().y
- + expectedSpacerHeight + expectedRowHeight - 3));
- previousSpacer = spacer;
- }
- }
-
- private void assertNoErrors() {
- assertEquals("Error notification detected.", 0,
- treeGrid.findElements(By.className(CLASSNAME_ERROR)).size());
- }
-
- @Test
- public void expandAllOpenAllInitialDetails_toggleOneTwice_hideAll() {
- openTestURL();
- $(ButtonElement.class).id(EXPAND_ALL).click();
- $(ButtonElement.class).id(SHOW_DETAILS).click();
- $(ButtonElement.class).id(ADD_GRID).click();
-
- waitForElementPresent(By.className(CLASSNAME_TREEGRID));
-
- treeGrid = $(TreeGridElement.class).first();
-
- waitUntil(expectedConditionDetails(0, 0, 0));
- ensureExpectedSpacerHeightSet();
- int spacerCount = treeGrid.findElements(By.className(CLASSNAME_SPACER))
- .size();
- assertSpacerPositions();
-
- treeGrid.collapseWithClick(0);
-
- // collapsing one shouldn't affect spacer count, just update the cache
- waitUntil(ExpectedConditions.not(expectedConditionDetails(0, 0, 0)));
- assertSpacerHeights();
- assertSpacerPositions();
- assertSpacerCount(spacerCount);
-
- treeGrid.expandWithClick(0);
-
- // expanding back shouldn't affect spacer count, just update the cache
- waitUntil(expectedConditionDetails(0, 0, 0));
- assertSpacerHeights();
- assertSpacerPositions();
- assertSpacerCount(spacerCount);
-
- // test that repeating the toggle still doesn't change anything
- treeGrid.collapseWithClick(0);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(0, 0, 0)));
- assertSpacerHeights();
- assertSpacerPositions();
- assertSpacerCount(spacerCount);
-
- treeGrid.expandWithClick(0);
-
- waitUntil(expectedConditionDetails(0, 0, 0));
- assertSpacerHeights();
- assertSpacerPositions();
- assertSpacerCount(spacerCount);
-
- // test that hiding all still won't break things
- $(ButtonElement.class).id(HIDE_DETAILS).click();
- waitForElementNotPresent(By.className(CLASSNAME_SPACER));
-
- assertNoErrors();
- }
-
- @Test
- public void expandAllOpenAllInitialDetails_toggleAll() {
- openTestURL();
- $(ButtonElement.class).id(EXPAND_ALL).click();
- $(ButtonElement.class).id(SHOW_DETAILS).click();
- $(ButtonElement.class).id(ADD_GRID).click();
-
- waitForElementPresent(By.className(CLASSNAME_TREEGRID));
-
- treeGrid = $(TreeGridElement.class).first();
-
- waitUntil(expectedConditionDetails(0, 0, 0));
- ensureExpectedSpacerHeightSet();
-
- int spacerCount = treeGrid.findElements(By.className(CLASSNAME_SPACER))
- .size();
- assertSpacerPositions();
-
- $(ButtonElement.class).id(COLLAPSE_ALL).click();
-
- // There should still be a full cache's worth of details rows open,
- // just not the same rows than before collapsing all.
- waitForElementNotPresent(By.className(CLASSNAME_LEAF));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- // FIXME: TreeGrid fails to update cache correctly when you expand all
- // and after a long, long wait you end up with 3321 open details rows
- // and row 63/8/0 in view instead of 95 and 0/0/0 as expected.
- // WaitUntil timeouts by then.
- if (true) {// remove this block after fixed
- return;
- }
-
- $(ButtonElement.class).id(EXPAND_ALL).click();
-
- // State should have returned to what it was before collapsing.
- waitUntil(expectedConditionDetails(0, 0, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- assertNoErrors();
- }
-
- @Test
- public void expandAllOpenNoInitialDetails_showSeveral_toggleOneByOne() {
- openTestURL();
- $(ButtonElement.class).id(EXPAND_ALL).click();
- $(ButtonElement.class).id(ADD_GRID).click();
-
- waitForElementPresent(By.className(CLASSNAME_TREEGRID));
-
- treeGrid = $(TreeGridElement.class).first();
-
- // open details for several rows, leave one out from the hierarchy that
- // is to be collapsed
- treeGrid.getCell(0, 0).click();
- treeGrid.getCell(1, 0).click();
- treeGrid.getCell(2, 0).click();
- // no click for cell (3, 0)
- treeGrid.getCell(4, 0).click();
- treeGrid.getCell(5, 0).click();
- treeGrid.getCell(6, 0).click();
- treeGrid.getCell(7, 0).click();
- treeGrid.getCell(8, 0).click();
- int spacerCount = 8;
-
- waitUntil(expectedConditionDetails(0, 0, 0));
- assertSpacerCount(spacerCount);
- ensureExpectedSpacerHeightSet();
- assertSpacerPositions();
-
- // toggle the root with open details rows
- treeGrid.collapseWithClick(0);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(0, 0, 0)));
- assertSpacerCount(1);
- assertSpacerHeights();
-
- treeGrid.expandWithClick(0);
-
- waitUntil(expectedConditionDetails(0, 0, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- // toggle one of the branches with open details rows
- treeGrid.collapseWithClick(5);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(0, 1, 0)));
- assertSpacerCount(spacerCount - 3);
- assertSpacerHeights();
- assertSpacerPositions();
-
- treeGrid.expandWithClick(5);
-
- waitUntil(expectedConditionDetails(0, 1, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- assertNoErrors();
- }
-
- @Test
- public void expandAllOpenAllInitialDetailsScrolled_toggleOne_hideAll() {
- openTestURL();
- $(ButtonElement.class).id(EXPAND_ALL).click();
- $(ButtonElement.class).id(SHOW_DETAILS).click();
- $(ButtonElement.class).id(ADD_GRID).click();
-
- waitForElementPresent(By.className(CLASSNAME_TREEGRID));
- $(ButtonElement.class).id(SCROLL_TO_55).click();
-
- treeGrid = $(TreeGridElement.class).first();
-
- waitUntil(expectedConditionDetails(1, 2, 0));
- ensureExpectedSpacerHeightSet();
- int spacerCount = treeGrid.findElements(By.className(CLASSNAME_SPACER))
- .size();
- assertSpacerPositions();
-
- treeGrid.collapseWithClick(50);
-
- // collapsing one shouldn't affect spacer count, just update the cache
- waitUntil(ExpectedConditions.not(expectedConditionDetails(1, 2, 0)));
- assertSpacerHeights();
- assertSpacerPositions();
- // FIXME: gives 128, not 90 as expected
- // assertSpacerCount(spacerCount);
-
- treeGrid.expandWithClick(50);
-
- // expanding back shouldn't affect spacer count, just update the cache
- waitUntil(expectedConditionDetails(1, 2, 0));
- assertSpacerHeights();
- assertSpacerPositions();
- // FIXME: gives 131, not 90 as expected
- // assertSpacerCount(spacerCount);
-
- // test that repeating the toggle still doesn't change anything
-
- treeGrid.collapseWithClick(50);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(1, 2, 0)));
- assertSpacerHeights();
- assertSpacerPositions();
- // FIXME: gives 128, not 90 as expected
- // assertSpacerCount(spacerCount);
-
- treeGrid.expandWithClick(50);
-
- waitUntil(expectedConditionDetails(1, 2, 0));
- assertSpacerHeights();
- assertSpacerPositions();
- // FIXME: gives 131, not 90 as expected
- // assertSpacerCount(spacerCount);
-
- // test that hiding all still won't break things
-
- $(ButtonElement.class).id(HIDE_DETAILS).click();
- waitForElementNotPresent(By.className(CLASSNAME_SPACER));
-
- assertNoErrors();
- }
-
- @Test
- public void expandAllOpenAllInitialDetailsScrolled_toggleAll() {
- openTestURL();
- $(ButtonElement.class).id(EXPAND_ALL).click();
- $(ButtonElement.class).id(SHOW_DETAILS).click();
- $(ButtonElement.class).id(ADD_GRID).click();
-
- waitForElementPresent(By.className(CLASSNAME_TREEGRID));
- $(ButtonElement.class).id(SCROLL_TO_55).click();
-
- treeGrid = $(TreeGridElement.class).first();
-
- waitUntil(expectedConditionDetails(1, 1, 0));
- ensureExpectedSpacerHeightSet();
-
- int spacerCount = treeGrid.findElements(By.className(CLASSNAME_SPACER))
- .size();
- assertSpacerPositions();
-
- $(ButtonElement.class).id(COLLAPSE_ALL).click();
-
- waitForElementNotPresent(By.className(CLASSNAME_LEAF));
-
- // There should still be a full cache's worth of details rows open,
- // just not the same rows than before collapsing all.
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- // FIXME: collapsing too many rows after scrolling still causes a chaos
- if (true) { // remove this block after fixed
- return;
- }
-
- $(ButtonElement.class).id(EXPAND_ALL).click();
-
- // State should have returned to what it was before collapsing.
- waitUntil(expectedConditionDetails(1, 1, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- assertNoErrors();
- }
-
- @Test
- public void expandAllOpenNoInitialDetailsScrolled_showSeveral_toggleOneByOne() {
- openTestURL();
- $(ButtonElement.class).id(EXPAND_ALL).click();
- $(ButtonElement.class).id(ADD_GRID).click();
-
- waitForElementPresent(By.className(CLASSNAME_TREEGRID));
- $(ButtonElement.class).id(SCROLL_TO_55).click();
-
- treeGrid = $(TreeGridElement.class).first();
- assertSpacerCount(0);
-
- // open details for several rows, leave one out from the hierarchy that
- // is to be collapsed
- treeGrid.getCell(50, 0).click();
- treeGrid.getCell(51, 0).click();
- treeGrid.getCell(52, 0).click();
- // no click for cell (53, 0)
- treeGrid.getCell(54, 0).click();
- treeGrid.getCell(55, 0).click();
- treeGrid.getCell(56, 0).click();
- treeGrid.getCell(57, 0).click();
- treeGrid.getCell(58, 0).click();
- int spacerCount = 8;
-
- waitUntil(expectedConditionDetails(1, 2, 0));
- assertSpacerCount(spacerCount);
- ensureExpectedSpacerHeightSet();
- assertSpacerPositions();
-
- // toggle the branch with partially open details rows
- treeGrid.collapseWithClick(50);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(1, 2, 0)));
- assertSpacerCount(spacerCount - 2);
- assertSpacerHeights();
- assertSpacerPositions();
-
- treeGrid.expandWithClick(50);
-
- waitUntil(expectedConditionDetails(1, 2, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- // toggle the branch with fully open details rows
- treeGrid.collapseWithClick(54);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(1, 3, 0)));
- assertSpacerCount(spacerCount - 3);
- assertSpacerHeights();
- assertSpacerPositions();
-
- treeGrid.expandWithClick(54);
-
- waitUntil(expectedConditionDetails(1, 3, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- // repeat both toggles to ensure still no errors
- treeGrid.collapseWithClick(50);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(1, 2, 0)));
- assertSpacerCount(spacerCount - 2);
- assertSpacerHeights();
- assertSpacerPositions();
-
- treeGrid.expandWithClick(50);
-
- waitUntil(expectedConditionDetails(1, 2, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
- treeGrid.collapseWithClick(54);
-
- waitUntil(ExpectedConditions.not(expectedConditionDetails(1, 3, 0)));
- assertSpacerCount(spacerCount - 3);
- assertSpacerHeights();
- assertSpacerPositions();
-
- treeGrid.expandWithClick(54);
-
- waitUntil(expectedConditionDetails(1, 3, 0));
- assertSpacerCount(spacerCount);
- assertSpacerHeights();
- assertSpacerPositions();
-
- assertNoErrors();
- }
-
- }
|