Browse Source

Provide GridLayout size and cell elements to JS and TB (#9019)

tags/8.1.0.alpha5
Artur 7 years ago
parent
commit
4454e6bdc1

+ 44
- 0
client/src/main/java/com/vaadin/client/ui/VGridLayout.java View File

@@ -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.
* <p>
@@ -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);

+ 55
- 0
testbench-api/src/main/java/com/vaadin/testbench/elements/GridLayoutElement.java View File

@@ -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;

}
}

+ 44
- 0
uitest/src/main/java/com/vaadin/tests/elements/gridlayout/GridLayoutUI.java View File

@@ -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);
}

}

+ 72
- 0
uitest/src/test/java/com/vaadin/tests/elements/gridlayout/GridLayoutUITest.java View File

@@ -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) {
}
}
}

Loading…
Cancel
Save