123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /*
- * Copyright 2000-2014 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.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertNotEquals;
- import static org.junit.Assert.assertTrue;
-
- import org.junit.Test;
- import org.openqa.selenium.Keys;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.interactions.Actions;
- import org.openqa.selenium.remote.DesiredCapabilities;
-
- import com.vaadin.testbench.By;
- import com.vaadin.testbench.TestBenchElement;
- import com.vaadin.testbench.elements.GridElement;
- import com.vaadin.testbench.elements.GridElement.GridCellElement;
- import com.vaadin.testbench.elements.LabelElement;
- import com.vaadin.testbench.elements.NativeButtonElement;
- import com.vaadin.testbench.elements.NativeSelectElement;
- import com.vaadin.testbench.elementsbase.ServerClass;
- import com.vaadin.testbench.parallel.BrowserUtil;
- import com.vaadin.testbench.parallel.TestCategory;
- import com.vaadin.tests.tb3.MultiBrowserTest;
- import com.vaadin.tests.widgetset.client.grid.GridClientColumnRendererConnector.Renderers;
- import com.vaadin.tests.widgetset.server.grid.GridClientColumnRenderers;
-
- /**
- * Tests Grid client side renderers
- *
- * @since
- * @author Vaadin Ltd
- */
- @TestCategory("grid")
- public class GridClientRenderers extends MultiBrowserTest {
-
- private static final double SLEEP_MULTIPLIER = 1.2;
- private int latency = 0;
-
- @Override
- protected Class<?> getUIClass() {
- return GridClientColumnRenderers.class;
- }
-
- @Override
- protected String getDeploymentPath(Class<?> uiClass) {
- String path = super.getDeploymentPath(uiClass);
- if (latency > 0) {
- path += (path.contains("?") ? "&" : "?") + "latency=" + latency;
- }
- return path;
- }
-
- @ServerClass("com.vaadin.tests.widgetset.server.grid.GridClientColumnRenderers.GridController")
- public static class MyClientGridElement extends GridElement {
- }
-
- @Override
- public void setup() throws Exception {
- latency = 0; // reset
- super.setup();
- }
-
- @Test
- public void addWidgetRenderer() throws Exception {
- openTestURL();
-
- // Add widget renderer column
- $(NativeSelectElement.class).first().selectByText(
- Renderers.WIDGET_RENDERER.toString());
- $(NativeButtonElement.class).caption("Add").first().click();
-
- // Click the button in cell 1,1
- TestBenchElement cell = getGrid().getCell(1, 2);
- WebElement gwtButton = cell.findElement(By.tagName("button"));
- gwtButton.click();
-
- // Should be an alert visible
- assertEquals("Button did not contain text \"Clicked\"", "Clicked",
- gwtButton.getText());
- }
-
- @Test
- public void detachAndAttachGrid() {
- openTestURL();
-
- // Add widget renderer column
- $(NativeSelectElement.class).first().selectByText(
- Renderers.WIDGET_RENDERER.toString());
- $(NativeButtonElement.class).caption("Add").first().click();
-
- // Detach and re-attach the Grid
- $(NativeButtonElement.class).caption("DetachAttach").first().click();
-
- // Click the button in cell 1,1
- TestBenchElement cell = getGrid().getCell(1, 2);
- WebElement gwtButton = cell.findElement(By.tagName("button"));
- gwtButton.click();
-
- // Should be an alert visible
- assertEquals("Button did not contain text \"Clicked\"",
- gwtButton.getText(), "Clicked");
- }
-
- @Test
- public void rowsWithDataHasStyleName() throws Exception {
-
- testBench().disableWaitForVaadin();
-
- // Simulate network latency with 2000ms
- latency = 2000;
-
- openTestURL();
-
- sleep((int) (latency * SLEEP_MULTIPLIER));
-
- TestBenchElement row = getGrid().getRow(51);
- String className = row.getAttribute("class");
- assertFalse(
- "Row should not yet contain style name v-grid-row-has-data",
- className.contains("v-grid-row-has-data"));
-
- // Wait for data to arrive
- sleep((int) (latency * SLEEP_MULTIPLIER));
-
- row = getGrid().getRow(51);
- className = row.getAttribute("class");
- assertTrue("Row should now contain style name v-grid-row-has-data",
- className.contains("v-grid-row-has-data"));
- }
-
- @Test
- public void complexRendererSetVisibleContent() throws Exception {
-
- DesiredCapabilities desiredCapabilities = getDesiredCapabilities();
-
- // Simulate network latency with 2000ms
- latency = 2000;
- if (BrowserUtil.isIE8(desiredCapabilities)) {
- // IE8 is slower than other browsers. Bigger latency is needed for
- // stability in this test.
- latency = 3000;
- }
-
- // Chrome uses RGB instead of RGBA
- String colorRed = "rgba(255, 0, 0, 1)";
- String colorWhite = "rgba(255, 255, 255, 1)";
- String colorDark = "rgba(239, 240, 241, 1)";
- if (BrowserUtil.isChrome(desiredCapabilities)) {
- colorRed = "rgb(255, 0, 0)";
- colorWhite = "rgb(255, 255, 255)";
- colorDark = "rgb(239, 240, 241)";
- }
-
- openTestURL();
-
- getGrid();
-
- testBench().disableWaitForVaadin();
-
- // Test initial renderering with contentVisible = False
- TestBenchElement cell = getGrid().getCell(51, 1);
- String backgroundColor = cell.getCssValue("backgroundColor");
- assertEquals("Background color was not red.", colorRed, backgroundColor);
-
- // data arrives...
- sleep((int) (latency * SLEEP_MULTIPLIER));
-
- // Content becomes visible
- cell = getGrid().getCell(51, 1);
- backgroundColor = cell.getCssValue("backgroundColor");
- assertNotEquals("Background color was red.", colorRed, backgroundColor);
-
- // scroll down, new cells becomes contentVisible = False
- getGrid().scrollToRow(60);
-
- // Cell should be red (setContentVisible set cell red)
- cell = getGrid().getCell(55, 1);
- backgroundColor = cell.getCssValue("backgroundColor");
- assertEquals("Background color was not red.", colorRed, backgroundColor);
-
- // data arrives...
- sleep((int) (latency * SLEEP_MULTIPLIER));
-
- // Cell should no longer be red
- backgroundColor = cell.getCssValue("backgroundColor");
- assertTrue(
- "Background color was not reset",
- backgroundColor.equals(colorWhite)
- || backgroundColor.equals(colorDark));
- }
-
- @Test
- public void testSortingEvent() throws Exception {
- openTestURL();
-
- $(NativeButtonElement.class).caption("Trigger sorting event").first()
- .click();
-
- String consoleText = $(LabelElement.class).id("testDebugConsole")
- .getText();
-
- assertTrue("Console text as expected",
- consoleText.contains("Columns: 1, order: Column 1: ASCENDING"));
-
- }
-
- @Test
- public void testListSorter() throws Exception {
- openTestURL();
-
- $(NativeButtonElement.class).caption("Shuffle").first().click();
-
- GridElement gridElem = $(MyClientGridElement.class).first();
-
- // XXX: DANGER! We'll need to know how many rows the Grid has!
- // XXX: Currently, this is impossible; hence the hardcoded value of 70.
-
- boolean shuffled = false;
- for (int i = 1, l = 70; i < l; ++i) {
-
- String str_a = gridElem.getCell(i - 1, 0).getAttribute("innerHTML");
- String str_b = gridElem.getCell(i, 0).getAttribute("innerHTML");
-
- int value_a = Integer.parseInt(str_a);
- int value_b = Integer.parseInt(str_b);
-
- if (value_a > value_b) {
- shuffled = true;
- break;
- }
- }
- assertTrue("Grid shuffled", shuffled);
-
- $(NativeButtonElement.class).caption("Test sorting").first().click();
-
- for (int i = 1, l = 70; i < l; ++i) {
-
- String str_a = gridElem.getCell(i - 1, 0).getAttribute("innerHTML");
- String str_b = gridElem.getCell(i, 0).getAttribute("innerHTML");
-
- int value_a = Integer.parseInt(str_a);
- int value_b = Integer.parseInt(str_b);
-
- if (value_a > value_b) {
- assertTrue("Grid sorted", false);
- }
- }
- }
-
- @Test
- public void testComplexRendererOnActivate() {
- openTestURL();
-
- GridCellElement cell = getGrid().getCell(3, 1);
- cell.click();
- new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
-
- assertEquals("onActivate was not called on KeyDown Enter.",
- "Activated!", cell.getText());
-
- cell = getGrid().getCell(4, 1);
- cell.click();
- new Actions(getDriver()).moveToElement(cell).doubleClick().perform();
- assertEquals("onActivate was not called on double click.",
- "Activated!", cell.getText());
- }
-
- private GridElement getGrid() {
- return $(MyClientGridElement.class).first();
- }
-
- private void addColumn(Renderers renderer) {
- // Add widget renderer column
- $(NativeSelectElement.class).first().selectByText(renderer.toString());
- $(NativeButtonElement.class).caption("Add").first().click();
- }
- }
|