diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2016-09-21 09:13:12 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-23 06:15:02 +0000 |
commit | 4d16c72c32ce67204ff49a5b45c5ce3e5288b86e (patch) | |
tree | 0110a3afd8157875bfa88118ffaddd9157dbd922 /uitest | |
parent | f12fbfb7044defc8442d05e6810c43875c04710c (diff) | |
download | vaadin-framework-4d16c72c32ce67204ff49a5b45c5ce3e5288b86e.tar.gz vaadin-framework-4d16c72c32ce67204ff49a5b45c5ce3e5288b86e.zip |
Reintroduce grid column sizing
Change-Id: Ie5e91c3e9c8f2c9d8c05415d5602e2eaf3bd960b
Diffstat (limited to 'uitest')
-rw-r--r-- | uitest/src/main/java/com/vaadin/tests/components/grid/GridColumnResizing.java | 52 | ||||
-rw-r--r-- | uitest/src/test/java/com/vaadin/tests/components/grid/GridColumnResizingTest.java | 150 |
2 files changed, 202 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridColumnResizing.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridColumnResizing.java new file mode 100644 index 0000000000..67e6473850 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridColumnResizing.java @@ -0,0 +1,52 @@ +package com.vaadin.tests.components.grid; + +import java.util.Arrays; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.data.bean.Person; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.Column; +import com.vaadin.ui.Label; +import com.vaadin.ui.TextField; +import com.vaadin.ui.renderers.NumberRenderer; + +public class GridColumnResizing extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + TextField input = new TextField(); + Label isResizedLabel = new Label("not resized"); + Grid<Person> grid = new Grid<>(); + Column<Person, String> nameColumn = grid.addColumn("Name", + Person::getFirstName); + Column<Person, Number> ageColumn = grid.addColumn("Age", Person::getAge, + new NumberRenderer()); + grid.addColumnResizeListener(event -> { + if (event.isUserOriginated()) { + isResizedLabel.setValue("client resized"); + } else { + isResizedLabel.setValue("server resized"); + } + }); + grid.setItems(Arrays.asList(Person.createTestPerson1(), + Person.createTestPerson2())); + + addComponent(input); + addButton("set width", event -> nameColumn + .setWidth(Double.parseDouble(input.getValue()))); + addButton("set expand ratio", event -> { + nameColumn.setExpandRatio(4); + ageColumn.setExpandRatio(1); + }); + addButton("set min width", event -> nameColumn + .setMinimumWidth(Double.parseDouble(input.getValue()))); + addButton("set max width", event -> nameColumn + .setMaximumWidth(Double.parseDouble(input.getValue()))); + addButton("toggle resizable", + event -> nameColumn.setResizable(!nameColumn.isResizable())); + + addComponents(grid, isResizedLabel); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridColumnResizingTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridColumnResizingTest.java new file mode 100644 index 0000000000..3f2a08d979 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridColumnResizingTest.java @@ -0,0 +1,150 @@ +package com.vaadin.tests.components.grid; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Dimension; +import org.openqa.selenium.Keys; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.testbench.elements.GridElement.GridCellElement; +import com.vaadin.testbench.elements.LabelElement; +import com.vaadin.testbench.elements.TextFieldElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class GridColumnResizingTest extends MultiBrowserTest { + + @Test + public void serverSetWidth() { + openTestURL(); + + serverSideSetWidth(50); + assertColumnWidth(50, 0); + + serverSideSetWidth(500); + assertColumnWidth(500, 0); + } + + @Test + public void setResizable() { + openTestURL(); + ButtonElement toggleResizableButton = $(ButtonElement.class).get(4); + GridCellElement cell = getGrid().getHeaderCell(0, 0); + + Assert.assertEquals(true, cell.isElementPresent( + By.cssSelector("div.v-grid-column-resize-handle"))); + + toggleResizableButton.click(); + Assert.assertEquals(false, cell.isElementPresent( + By.cssSelector("div.v-grid-column-resize-handle"))); + } + + @Test + public void setExpandRatio() { + openTestURL(); + ButtonElement setExpandRatioButton = $(ButtonElement.class).get(1); + + setExpandRatioButton.click(); + assertColumnWidthWithThreshold(375, 0, 2); + assertColumnWidthWithThreshold(125, 1, 2); + } + + @Test + public void setMinimumWidth() { + openTestURL(); + + setMinWidth(100); + serverSideSetWidth(50); + assertColumnWidth(100, 0); + + serverSideSetWidth(150); + dragResizeColumn(0, 0, -100); + assertColumnWidth(100, 0); + } + + @Test + public void setMaximumWidth() { + openTestURL(); + + serverSideSetWidth(50); + setMaxWidth(100); + + serverSideSetWidth(150); + assertColumnWidth(100, 0); + + // TODO add the following when grid column width recalculation has been + // fixed in the case where the sum of column widths exceeds the visible + // area + + // serverSideSetWidth(50); + // dragResizeColumn(0, 0, 200); + // assertColumnWidth(100, 0); + } + + @Test + public void resizeEventListener() { + openTestURL(); + + Assert.assertEquals("not resized", + $(LabelElement.class).get(1).getText()); + + serverSideSetWidth(150); + Assert.assertEquals("server resized", + $(LabelElement.class).get(1).getText()); + + dragResizeColumn(0, 0, 100); + Assert.assertEquals("client resized", + $(LabelElement.class).get(1).getText()); + } + + private GridElement getGrid() { + return $(GridElement.class).first(); + } + + private void serverSideSetWidth(double width) { + TextFieldElement textField = $(TextFieldElement.class).first(); + ButtonElement setWidthButton = $(ButtonElement.class).get(0); + textField.clear(); + textField.sendKeys(String.valueOf(width), Keys.ENTER); + setWidthButton.click(); + } + + private void setMinWidth(double minWidth) { + TextFieldElement textField = $(TextFieldElement.class).first(); + ButtonElement setMinWidthButton = $(ButtonElement.class).get(2); + textField.clear(); + textField.sendKeys(String.valueOf(minWidth), Keys.ENTER); + setMinWidthButton.click(); + } + + private void setMaxWidth(double maxWidth) { + TextFieldElement textField = $(TextFieldElement.class).first(); + ButtonElement setMaxWidthButton = $(ButtonElement.class).get(3); + textField.clear(); + textField.sendKeys(String.valueOf(maxWidth), Keys.ENTER); + setMaxWidthButton.click(); + } + + private void dragResizeColumn(int columnIndex, int posX, int offset) { + GridCellElement headerCell = getGrid().getHeaderCell(0, columnIndex); + Dimension size = headerCell.getSize(); + new Actions(getDriver()) + .moveToElement(headerCell, size.getWidth() + posX, + size.getHeight() / 2) + .clickAndHold().moveByOffset(offset, 0).release().perform(); + } + + private void assertColumnWidth(int width, int columnIndex) { + Assert.assertEquals(width, + getGrid().getCell(0, columnIndex).getSize().getWidth()); + } + + private void assertColumnWidthWithThreshold(int width, int columnIndex, + int threshold) { + Assert.assertTrue( + Math.abs(getGrid().getCell(0, columnIndex).getSize().getWidth() + - width) <= threshold); + } +} |