diff options
author | Ansku <Ansku@users.noreply.github.com> | 2017-08-22 14:59:49 +0300 |
---|---|---|
committer | Aleksi Hietanen <aleksi@vaadin.com> | 2017-08-22 14:59:49 +0300 |
commit | 3b2bea5236718fb13c9885141c0a4dddb323a868 (patch) | |
tree | c701551cf723e5ca4424574f96db627fb3457b14 /uitest | |
parent | 5ef925daa91b1253f170f244e2a992f4f92979e1 (diff) | |
download | vaadin-framework-3b2bea5236718fb13c9885141c0a4dddb323a868.tar.gz vaadin-framework-3b2bea5236718fb13c9885141c0a4dddb323a868.zip |
Resize should work within Grid details row (#9808)
Fixes #7341
Diffstat (limited to 'uitest')
4 files changed, 610 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridLayoutDetailsRowResize.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridLayoutDetailsRowResize.java new file mode 100644 index 0000000000..db4ebd1ae8 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridLayoutDetailsRowResize.java @@ -0,0 +1,121 @@ +/* + * Copyright 2000-2017 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.grid; + +import com.vaadin.data.provider.ListDataProvider; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.data.bean.Person; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Grid; +import com.vaadin.ui.GridLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.themes.ValoTheme; +import com.vaadin.v7.ui.themes.Reindeer; + +/** + * Tests that details row resizes along with the contents properly. + * + * @author Vaadin Ltd + */ +@SuppressWarnings("deprecation") +public class GridLayoutDetailsRowResize extends SimpleGridUI { + + @Override + protected void setup(VaadinRequest request) { + final Grid<Person> grid = createGrid(); + grid.setSizeFull(); + + addComponent(new Button("Toggle theme", new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + if (ValoTheme.THEME_NAME.equals(getUI().getTheme())) { + getUI().setTheme(Reindeer.THEME_NAME); + } else { + getUI().setTheme(ValoTheme.THEME_NAME); + } + } + })); + + addComponent(grid); + + grid.setDetailsGenerator(item -> { + final GridLayout detailsLayout = new GridLayout(); + detailsLayout.setSizeFull(); + detailsLayout.setHeightUndefined(); + + final Label lbl1 = new Label("test1"); + lbl1.setId("lbl1"); + lbl1.setWidth("200px"); + detailsLayout.addComponent(lbl1); + + final Label lbl2 = new Label("test2"); + lbl2.setId("lbl2"); + detailsLayout.addComponent(lbl2); + + final Label lbl3 = new Label("test3"); + lbl3.setId("lbl3"); + detailsLayout.addComponent(lbl3); + + final Label lbl4 = new Label("test4"); + lbl4.setId("lbl4"); + lbl4.setVisible(false); + detailsLayout.addComponent(lbl4); + + final Button button = new Button("Toggle visibility", + new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + lbl4.setVisible(!lbl4.isVisible()); + } + }); + button.setId("btn"); + detailsLayout.addComponent(button); + + return detailsLayout; + }); + + grid.addItemClickListener(click -> { + final Person person = click.getItem(); + grid.setDetailsVisible(person, !grid.isDetailsVisible(person)); + }); + + addComponent(new Button("Open details", new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + for (Object itemId : ((ListDataProvider<?>) grid + .getDataProvider()).getItems()) { + if (itemId instanceof Person) { + grid.setDetailsVisible((Person) itemId, true); + } + } + } + })); + } + + @Override + protected String getTestDescription() { + return "Detail row should be correctly resized when its contents change."; + } + + @Override + protected Integer getTicketNumber() { + return 7341; + } +} diff --git a/uitest/src/main/java/com/vaadin/v7/tests/components/grid/GridLayoutDetailsRowResize.java b/uitest/src/main/java/com/vaadin/v7/tests/components/grid/GridLayoutDetailsRowResize.java new file mode 100644 index 0000000000..3c8aac9dcf --- /dev/null +++ b/uitest/src/main/java/com/vaadin/v7/tests/components/grid/GridLayoutDetailsRowResize.java @@ -0,0 +1,136 @@ +/* + * Copyright 2000-2017 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.v7.tests.components.grid; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Component; +import com.vaadin.ui.themes.ValoTheme; +import com.vaadin.v7.event.ItemClickEvent; +import com.vaadin.v7.event.ItemClickEvent.ItemClickListener; +import com.vaadin.v7.ui.Grid; +import com.vaadin.v7.ui.Grid.DetailsGenerator; +import com.vaadin.v7.ui.Grid.RowReference; +import com.vaadin.v7.ui.Label; +import com.vaadin.v7.ui.VerticalLayout; +import com.vaadin.v7.ui.themes.Reindeer; + +/** + * Tests that details row resizes along with the contents properly. + * + * @author Vaadin Ltd + */ +@SuppressWarnings("deprecation") +public class GridLayoutDetailsRowResize extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + final Grid grid = new Grid(); + grid.setSizeFull(); + grid.addColumn("name", String.class); + grid.addColumn("born", Integer.class); + + grid.addRow("Nicolaus Copernicus", 1543); + grid.addRow("Galileo Galilei", 1564); + grid.addRow("Johannes Kepler", 1571); + + addComponent(grid); + + grid.setDetailsGenerator(new DetailsGenerator() { + @Override + public Component getDetails(final RowReference rowReference) { + final VerticalLayout detailsLayout = new VerticalLayout(); + detailsLayout.setId("details"); + detailsLayout.setSizeFull(); + detailsLayout.setHeightUndefined(); + + final Label lbl1 = new Label("test1"); + lbl1.setId("lbl1"); + lbl1.setWidth("200px"); + detailsLayout.addComponent(lbl1); + + final Label lbl2 = new Label("test2"); + lbl2.setId("lbl2"); + detailsLayout.addComponent(lbl2); + + final Label lbl3 = new Label("test3"); + lbl3.setId("lbl3"); + detailsLayout.addComponent(lbl3); + + final Label lbl4 = new Label("test4"); + lbl4.setId("lbl4"); + lbl4.setVisible(false); + detailsLayout.addComponent(lbl4); + + final Button button = new Button("Toggle visibility", + new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + lbl4.setVisible(!lbl4.isVisible()); + } + }); + button.setId("btn"); + detailsLayout.addComponent(button); + + return detailsLayout; + } + }); + + grid.addItemClickListener(new ItemClickListener() { + @Override + public void itemClick(final ItemClickEvent event) { + final Object itemId = event.getItemId(); + grid.setDetailsVisible(itemId, !grid.isDetailsVisible(itemId)); + } + }); + + addComponent(new Button("Toggle theme", new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + if (ValoTheme.THEME_NAME.equals(getUI().getTheme())) { + getUI().setTheme(Reindeer.THEME_NAME); + } else { + getUI().setTheme(ValoTheme.THEME_NAME); + } + } + })); + + addComponent(new Button("Open details", new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + for (Object itemId : grid.getContainerDataSource() + .getItemIds()) { + grid.setDetailsVisible(itemId, true); + } + } + })); + } + + @Override + protected String getTestDescription() { + return "Detail row should be correctly resized when its contents change."; + } + + @Override + protected Integer getTicketNumber() { + return 7341; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridLayoutDetailsRowResizeTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridLayoutDetailsRowResizeTest.java new file mode 100644 index 0000000000..d0a7bc7f6b --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridLayoutDetailsRowResizeTest.java @@ -0,0 +1,193 @@ +/* + * Copyright 2000-2017 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.grid; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.number.IsCloseTo.closeTo; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedCondition; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.testbench.elements.LabelElement; +import com.vaadin.testbench.parallel.Browser; +import com.vaadin.testbench.parallel.TestCategory; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Tests that details row resizes along with the contents properly. + * + * @author Vaadin Ltd + */ +@TestCategory("grid") +public class GridLayoutDetailsRowResizeTest extends MultiBrowserTest { + + @Test + public void testLabelHeights() { + openTestURL(); + waitForElementPresent(By.className("v-grid")); + + GridElement grid = $(GridElement.class).first(); + + grid.getCell(2, 0).click(); + waitForElementPresent(By.id("lbl2")); + + WebElement layout = grid.findElement(By.className("v-gridlayout")); + int layoutHeight = layout.getSize().height; + + ButtonElement button = $(ButtonElement.class).id("btn"); + int buttonHeight = button.getSize().height; + + // height should be divided equally + double expectedLabelHeight = (layoutHeight - buttonHeight) / 3; + assertLabelHeight("lbl1", expectedLabelHeight); + assertLabelHeight("lbl2", expectedLabelHeight); + assertLabelHeight("lbl3", expectedLabelHeight); + + assertDetailsRowHeight(layoutHeight); + + // ensure fourth label isn't present yet + assertElementNotPresent(By.id("lbl4")); + + button.click(); + waitForElementPresent(By.id("lbl4")); + + // get layout height after the new label has been added + layoutHeight = layout.getSize().height; + + expectedLabelHeight = (layoutHeight - buttonHeight) / 4; + assertLabelHeight("lbl1", expectedLabelHeight); + assertLabelHeight("lbl2", expectedLabelHeight); + assertLabelHeight("lbl3", expectedLabelHeight); + assertLabelHeight("lbl4", expectedLabelHeight); + + assertDetailsRowHeight(layoutHeight); + } + + @Test + public void testMultipleDetailsRows() { + if (Browser.PHANTOMJS.name() + .equalsIgnoreCase(getDesiredCapabilities().getBrowserName())) { + // For some inexplicable reason PhantomJS fails to click that + // button, even if similar button clicks work just fine in other + // tests. Didn't disable PhantomJS altogether so that the other test + // at least could work in the initial pre-merge regression check. + return; + } + + setDebug(true); + openTestURL(); + waitForElementPresent(By.className("v-grid")); + + // all rows won't fit if using Valo, toggle back to reindeer + ButtonElement themeButton = $(ButtonElement.class) + .caption("Toggle theme").first(); + int buttonHeight = themeButton.getSize().height; + + themeButton.click(); + + // wait for the theme change to take hold + waitUntil(new ExpectedCondition<Boolean>() { + @Override + public Boolean apply(WebDriver arg0) { + return buttonHeight > $(ButtonElement.class) + .caption("Toggle theme").first().getSize().height; + } + + @Override + public String toString() { + // Expected condition failed: waiting for ... + return "button's theme to change"; + } + }); + + ButtonElement detailsButton = $(ButtonElement.class) + .caption("Open details").first(); + + detailsButton.click(); + waitForElementPresent(By.id("lbl2")); + + List<ButtonElement> buttons = $(ButtonElement.class) + .caption("Toggle visibility").all(); + assertThat("Unexpected amount of details rows.", buttons.size(), is(3)); + + Map<ButtonElement, Integer> positions = new LinkedHashMap<ButtonElement, Integer>(); + Map<Integer, ButtonElement> ordered = new TreeMap<Integer, ButtonElement>(); + for (ButtonElement button : buttons) { + positions.put(button, button.getLocation().getY()); + ordered.put(button.getLocation().getY(), button); + } + int labelHeight = 0; + for (LabelElement label : $(LabelElement.class).all()) { + if ("test1".equals(label.getText())) { + labelHeight = label.getSize().height; + } + } + + // toggle the contents + for (ButtonElement button : buttons) { + button.click(); + } + + int i = 0; + for (Entry<Integer, ButtonElement> entry : ordered.entrySet()) { + ++i; + ButtonElement button = entry.getValue(); + assertThat( + String.format("Unexpected button position: details row %s.", + i), + (double) button.getLocation().getY(), + closeTo(positions.get(button) + (i * labelHeight), 1d)); + } + + // toggle the contents + for (ButtonElement button : buttons) { + button.click(); + } + + // assert original positions back + for (ButtonElement button : buttons) { + assertThat(String.format("Unexpected button position."), + (double) button.getLocation().getY(), + closeTo(positions.get(button), 1d)); + } + } + + private void assertLabelHeight(String id, double expectedHeight) { + // 1px leeway for calculations + assertThat("Unexpected label height.", + (double) $(LabelElement.class).id(id).getSize().height, + closeTo(expectedHeight, 1d)); + } + + private void assertDetailsRowHeight(int layoutHeight) { + // check that details row height matches layout height (3px leeway) + WebElement detailsRow = findElement(By.className("v-grid-spacer")); + assertThat("Unexpected details row height", (double) layoutHeight, + closeTo(detailsRow.getSize().height, 3d)); + } +} diff --git a/uitest/src/test/java/com/vaadin/v7/tests/components/grid/GridLayoutDetailsRowResizeTest.java b/uitest/src/test/java/com/vaadin/v7/tests/components/grid/GridLayoutDetailsRowResizeTest.java new file mode 100644 index 0000000000..21bc766d59 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/v7/tests/components/grid/GridLayoutDetailsRowResizeTest.java @@ -0,0 +1,160 @@ +/* + * Copyright 2000-2017 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.v7.tests.components.grid; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.number.IsCloseTo.closeTo; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.testbench.elements.LabelElement; +import com.vaadin.testbench.elements.VerticalLayoutElement; +import com.vaadin.testbench.parallel.TestCategory; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Tests that details row resizes along with the contents properly. + * + * @author Vaadin Ltd + */ +@TestCategory("grid") +public class GridLayoutDetailsRowResizeTest extends MultiBrowserTest { + + @Test + public void testLabelHeights() { + openTestURL(); + waitForElementPresent(By.className("v-grid")); + + GridElement grid = $(GridElement.class).first(); + + grid.getCell(2, 0).click(); + waitForElementPresent(By.id("lbl2")); + + VerticalLayoutElement layout = $(VerticalLayoutElement.class) + .id("details"); + int layoutHeight = layout.getSize().height; + + ButtonElement button = $(ButtonElement.class).id("btn"); + int buttonHeight = button.getSize().height; + + // height should be divided equally + double expectedLabelHeight = (layoutHeight - buttonHeight) / 3; + assertLabelHeight("lbl1", expectedLabelHeight); + assertLabelHeight("lbl2", expectedLabelHeight); + assertLabelHeight("lbl3", expectedLabelHeight); + + assertDetailsRowHeight(layoutHeight); + + // ensure fourth label isn't present yet + assertElementNotPresent(By.id("lbl4")); + + button.click(); + waitForElementPresent(By.id("lbl4")); + + // get layout height after the new label has been added + layoutHeight = layout.getSize().height; + + expectedLabelHeight = (layoutHeight - buttonHeight) / 4; + assertLabelHeight("lbl1", expectedLabelHeight); + assertLabelHeight("lbl2", expectedLabelHeight); + assertLabelHeight("lbl3", expectedLabelHeight); + assertLabelHeight("lbl4", expectedLabelHeight); + + assertDetailsRowHeight(layoutHeight); + } + + @Test + public void testMultipleDetailsRows() { + openTestURL(); + waitForElementPresent(By.className("v-grid")); + + ButtonElement detailsButton = $(ButtonElement.class) + .caption("Open details").first(); + + detailsButton.click(); + waitForElementPresent(By.id("lbl2")); + + List<ButtonElement> buttons = $(ButtonElement.class) + .caption("Toggle visibility").all(); + assertThat("Unexpected amount of details rows.", buttons.size(), is(3)); + + Map<ButtonElement, Integer> positions = new LinkedHashMap<ButtonElement, Integer>(); + Map<Integer, ButtonElement> ordered = new TreeMap<Integer, ButtonElement>(); + for (ButtonElement button : buttons) { + positions.put(button, button.getLocation().getY()); + ordered.put(button.getLocation().getY(), button); + } + int labelHeight = 0; + for (LabelElement label : $(LabelElement.class).all()) { + if ("test1".equals(label.getText())) { + labelHeight = label.getSize().height; + } + } + + // toggle the contents + for (ButtonElement button : buttons) { + button.click(); + } + + int i = 0; + for (Entry<Integer, ButtonElement> entry : ordered.entrySet()) { + ++i; + ButtonElement button = entry.getValue(); + assertThat( + String.format("Unexpected button position: details row %s.", + i), + (double) button.getLocation().getY(), + closeTo(positions.get(button) + (i * labelHeight), 1d)); + } + + // toggle the contents + for (ButtonElement button : buttons) { + button.click(); + } + + // assert original positions back + for (ButtonElement button : buttons) { + assertThat(String.format("Unexpected button position."), + (double) button.getLocation().getY(), + closeTo(positions.get(button), 1d)); + } + } + + private void assertLabelHeight(String id, double expectedHeight) { + // 1px leeway for calculations + assertThat("Unexpected label height.", + (double) $(LabelElement.class).id(id).getSize().height, + closeTo(expectedHeight, 1d)); + } + + private void assertDetailsRowHeight(int layoutHeight) { + // check that details row height matches layout height (1px leeway) + WebElement detailsRow = findElement(By.className("v-grid-spacer")); + assertThat("Unexpected details row height", (double) layoutHeight, + closeTo(detailsRow.getSize().height, 1d)); + } +} |