From 9a70e9123384e78d6af8b716128895957692fca0 Mon Sep 17 00:00:00 2001 From: Artur Date: Thu, 13 Apr 2017 10:25:20 +0300 Subject: Provide GridLayout size and cell elements to JS and TB (#9019) --- .../java/com/vaadin/client/ui/VGridLayout.java | 44 +++++++++++++ .../testbench/elements/GridLayoutElement.java | 55 +++++++++++++++++ .../tests/elements/gridlayout/GridLayoutUI.java | 44 +++++++++++++ .../elements/gridlayout/GridLayoutUITest.java | 72 ++++++++++++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 uitest/src/main/java/com/vaadin/tests/elements/gridlayout/GridLayoutUI.java create mode 100644 uitest/src/test/java/com/vaadin/tests/elements/gridlayout/GridLayoutUITest.java diff --git a/client/src/main/java/com/vaadin/client/ui/VGridLayout.java b/client/src/main/java/com/vaadin/client/ui/VGridLayout.java index f34c7eabde..c8c6cce0f3 100644 --- a/client/src/main/java/com/vaadin/client/ui/VGridLayout.java +++ b/client/src/main/java/com/vaadin/client/ui/VGridLayout.java @@ -88,8 +88,24 @@ public class VGridLayout extends ComplexPanel { setStyleName(CLASSNAME); addStyleName(StyleConstants.UI_LAYOUT); + + publishJSHelpers(getElement()); } + private native void publishJSHelpers(Element root) + /*-{ + var self = this; + root.getRowCount = $entry(function () { + return self.@VGridLayout::getRowCount()(); + }); + root.getColumnCount = $entry(function () { + return self.@VGridLayout::getColumnCount()(); + }); + root.getCell = $entry(function (row,column) { + return self.@VGridLayout::getCellElement(*)(row, column); + }); + }-*/; + private GridLayoutConnector getConnector() { return (GridLayoutConnector) ConnectorMap.get(client) .getConnector(this); @@ -623,6 +639,10 @@ public class VGridLayout extends ComplexPanel { Cell[][] cells; + private int rowCount; + + private int columnCount; + /** * Private helper class. */ @@ -781,6 +801,20 @@ public class VGridLayout extends ComplexPanel { return cells[col][row]; } + private Element getCellElement(int row, int col) { + if (row < 0 || row >= getRowCount() || col < 0 + || col >= getColumnCount()) { + return null; + } + + Cell cell = cells[col][row]; + if (cell == null || cell.slot == null) { + return null; + } + + return cell.slot.getWrapperElement(); + } + /** * Creates a new Cell with the given coordinates. *

@@ -882,6 +916,8 @@ public class VGridLayout extends ComplexPanel { } public void setSize(int rows, int cols) { + rowCount = rows; + columnCount = cols; if (cells == null) { cells = new Cell[cols][rows]; } else if (cells.length != cols || cells[0].length != rows) { @@ -897,6 +933,14 @@ public class VGridLayout extends ComplexPanel { } } + private int getRowCount() { + return rowCount; + } + + private int getColumnCount() { + return columnCount; + } + @Override public boolean remove(Widget w) { boolean removed = super.remove(w); diff --git a/testbench-api/src/main/java/com/vaadin/testbench/elements/GridLayoutElement.java b/testbench-api/src/main/java/com/vaadin/testbench/elements/GridLayoutElement.java index 31ebca721d..4f3106356a 100644 --- a/testbench-api/src/main/java/com/vaadin/testbench/elements/GridLayoutElement.java +++ b/testbench-api/src/main/java/com/vaadin/testbench/elements/GridLayoutElement.java @@ -15,9 +15,64 @@ */ package com.vaadin.testbench.elements; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; + import com.vaadin.testbench.elementsbase.ServerClass; @ServerClass("com.vaadin.ui.GridLayout") public class GridLayoutElement extends AbstractLayoutElement { + /** + * Gets the total number of rows in the layout. + * + * @return the number of rows in the layout, + */ + public long getRowCount() { + Long res = (Long) getCommandExecutor() + .executeScript("return arguments[0].getRowCount()", this); + if (res == null) { + throw new IllegalStateException("getRowCount returned null"); + } + + return res.longValue(); + } + + /** + * Gets the total number of columns in the layout. + * + * @return the number of columns in the layout, + */ + public long getColumnCount() { + Long res = (Long) getCommandExecutor() + .executeScript("return arguments[0].getColumnCount()", this); + if (res == null) { + throw new IllegalStateException("getColumnCount returned null"); + } + + return res.longValue(); + } + + /** + * Gets the cell element at the given position. + * + * @param row + * the row coordinate + * @param column + * the column coordinate + * @return the cell element at the given position + * @throws NoSuchElementException + * if no cell was found at the given position + */ + public WebElement getCell(int row, int column) { + WebElement res = (WebElement) getCommandExecutor().executeScript( + "return arguments[0].getCell(" + row + "," + column + ")", + this); + if (res == null) { + throw new NoSuchElementException( + "No cell found at " + row + "," + column); + } + + return res; + } } diff --git a/uitest/src/main/java/com/vaadin/tests/elements/gridlayout/GridLayoutUI.java b/uitest/src/main/java/com/vaadin/tests/elements/gridlayout/GridLayoutUI.java new file mode 100644 index 0000000000..698a64f747 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/elements/gridlayout/GridLayoutUI.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2016 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.elements.gridlayout; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.GridLayout; +import com.vaadin.ui.Label; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class GridLayoutUI extends AbstractTestUI { + + public static final String ONE_ROW_ONE_COL = "oneRowOneCol"; + public static final String TEN_ROWS_TEN_COLS = "tenRowsTenCols"; + + @Override + protected void setup(VaadinRequest request) { + GridLayout oneRowZeroCols = new GridLayout(1, 1); + oneRowZeroCols.setId(ONE_ROW_ONE_COL); + addComponent(oneRowZeroCols); + + GridLayout tenTimesTen = new GridLayout(10, 10); + tenTimesTen.addComponent(new Label("5-5"), 5, 5); + tenTimesTen.addComponent(new Button("7-7 8-8"), 7, 7, 8, 8); + tenTimesTen.setId(TEN_ROWS_TEN_COLS); + addComponent(tenTimesTen); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/elements/gridlayout/GridLayoutUITest.java b/uitest/src/test/java/com/vaadin/tests/elements/gridlayout/GridLayoutUITest.java new file mode 100644 index 0000000000..78d1fe8deb --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/elements/gridlayout/GridLayoutUITest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2000-2016 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.elements.gridlayout; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.GridLayoutElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class GridLayoutUITest extends SingleBrowserTest { + + @Test + public void getRows() { + openTestURL(); + Assert.assertEquals(1, $(GridLayoutElement.class) + .id(GridLayoutUI.ONE_ROW_ONE_COL).getRowCount()); + Assert.assertEquals(10, $(GridLayoutElement.class) + .id(GridLayoutUI.TEN_ROWS_TEN_COLS).getRowCount()); + } + + @Test + public void getColumns() { + openTestURL(); + Assert.assertEquals(1, $(GridLayoutElement.class) + .id(GridLayoutUI.ONE_ROW_ONE_COL).getColumnCount()); + Assert.assertEquals(10, $(GridLayoutElement.class) + .id(GridLayoutUI.TEN_ROWS_TEN_COLS).getColumnCount()); + } + + @Test + public void getCell() { + openTestURL(); + GridLayoutElement grid = $(GridLayoutElement.class) + .id(GridLayoutUI.TEN_ROWS_TEN_COLS); + + WebElement cell55 = grid.getCell(5, 5); + Assert.assertEquals("v-gridlayout-slot", cell55.getAttribute("class")); + Assert.assertEquals("5-5", cell55.getText()); + + try { + grid.getCell(4, 4); + Assert.fail("Should throw for empty cell"); + } catch (NoSuchElementException e) { + } + + WebElement cell77 = grid.getCell(7, 7); + Assert.assertEquals("v-gridlayout-slot", cell77.getAttribute("class")); + Assert.assertEquals("7-7 8-8", cell77.getText()); + + try { + grid.getCell(7, 8); + Assert.fail("Should throw for merged cell"); + } catch (NoSuchElementException e) { + } + } +} -- cgit v1.2.3