You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridClientRenderers.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.grid;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotEquals;
  20. import static org.junit.Assert.assertTrue;
  21. import org.junit.Test;
  22. import org.openqa.selenium.Keys;
  23. import org.openqa.selenium.WebElement;
  24. import org.openqa.selenium.interactions.Actions;
  25. import org.openqa.selenium.remote.DesiredCapabilities;
  26. import com.vaadin.testbench.By;
  27. import com.vaadin.testbench.TestBenchElement;
  28. import com.vaadin.testbench.elements.GridElement;
  29. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  30. import com.vaadin.testbench.elements.LabelElement;
  31. import com.vaadin.testbench.elements.NativeButtonElement;
  32. import com.vaadin.testbench.elements.NativeSelectElement;
  33. import com.vaadin.testbench.elementsbase.ServerClass;
  34. import com.vaadin.testbench.parallel.BrowserUtil;
  35. import com.vaadin.testbench.parallel.TestCategory;
  36. import com.vaadin.tests.tb3.MultiBrowserTest;
  37. import com.vaadin.tests.widgetset.client.grid.GridClientColumnRendererConnector.Renderers;
  38. import com.vaadin.tests.widgetset.server.grid.GridClientColumnRenderers;
  39. /**
  40. * Tests Grid client side renderers
  41. *
  42. * @since
  43. * @author Vaadin Ltd
  44. */
  45. @TestCategory("grid")
  46. public class GridClientRenderers extends MultiBrowserTest {
  47. private static final double SLEEP_MULTIPLIER = 1.2;
  48. private int latency = 0;
  49. @Override
  50. protected Class<?> getUIClass() {
  51. return GridClientColumnRenderers.class;
  52. }
  53. @Override
  54. protected String getDeploymentPath(Class<?> uiClass) {
  55. String path = super.getDeploymentPath(uiClass);
  56. if (latency > 0) {
  57. path += (path.contains("?") ? "&" : "?") + "latency=" + latency;
  58. }
  59. return path;
  60. }
  61. @ServerClass("com.vaadin.tests.widgetset.server.grid.GridClientColumnRenderers.GridController")
  62. public static class MyClientGridElement extends GridElement {
  63. }
  64. @Override
  65. public void setup() throws Exception {
  66. latency = 0; // reset
  67. super.setup();
  68. }
  69. @Test
  70. public void addWidgetRenderer() throws Exception {
  71. openTestURL();
  72. // Add widget renderer column
  73. $(NativeSelectElement.class).first().selectByText(
  74. Renderers.WIDGET_RENDERER.toString());
  75. $(NativeButtonElement.class).caption("Add").first().click();
  76. // Click the button in cell 1,1
  77. TestBenchElement cell = getGrid().getCell(1, 2);
  78. WebElement gwtButton = cell.findElement(By.tagName("button"));
  79. gwtButton.click();
  80. // Should be an alert visible
  81. assertEquals("Button did not contain text \"Clicked\"", "Clicked",
  82. gwtButton.getText());
  83. }
  84. @Test
  85. public void detachAndAttachGrid() {
  86. openTestURL();
  87. // Add widget renderer column
  88. $(NativeSelectElement.class).first().selectByText(
  89. Renderers.WIDGET_RENDERER.toString());
  90. $(NativeButtonElement.class).caption("Add").first().click();
  91. // Detach and re-attach the Grid
  92. $(NativeButtonElement.class).caption("DetachAttach").first().click();
  93. // Click the button in cell 1,1
  94. TestBenchElement cell = getGrid().getCell(1, 2);
  95. WebElement gwtButton = cell.findElement(By.tagName("button"));
  96. gwtButton.click();
  97. // Should be an alert visible
  98. assertEquals("Button did not contain text \"Clicked\"",
  99. gwtButton.getText(), "Clicked");
  100. }
  101. @Test
  102. public void rowsWithDataHasStyleName() throws Exception {
  103. testBench().disableWaitForVaadin();
  104. // Simulate network latency with 2000ms
  105. latency = 2000;
  106. openTestURL();
  107. sleep((int) (latency * SLEEP_MULTIPLIER));
  108. TestBenchElement row = getGrid().getRow(51);
  109. String className = row.getAttribute("class");
  110. assertFalse(
  111. "Row should not yet contain style name v-grid-row-has-data",
  112. className.contains("v-grid-row-has-data"));
  113. // Wait for data to arrive
  114. sleep((int) (latency * SLEEP_MULTIPLIER));
  115. row = getGrid().getRow(51);
  116. className = row.getAttribute("class");
  117. assertTrue("Row should now contain style name v-grid-row-has-data",
  118. className.contains("v-grid-row-has-data"));
  119. }
  120. @Test
  121. public void complexRendererSetVisibleContent() throws Exception {
  122. DesiredCapabilities desiredCapabilities = getDesiredCapabilities();
  123. // Simulate network latency with 2000ms
  124. latency = 2000;
  125. if (BrowserUtil.isIE8(desiredCapabilities)) {
  126. // IE8 is slower than other browsers. Bigger latency is needed for
  127. // stability in this test.
  128. latency = 3000;
  129. }
  130. // Chrome uses RGB instead of RGBA
  131. String colorRed = "rgba(255, 0, 0, 1)";
  132. String colorWhite = "rgba(255, 255, 255, 1)";
  133. String colorDark = "rgba(239, 240, 241, 1)";
  134. if (BrowserUtil.isChrome(desiredCapabilities)) {
  135. colorRed = "rgb(255, 0, 0)";
  136. colorWhite = "rgb(255, 255, 255)";
  137. colorDark = "rgb(239, 240, 241)";
  138. }
  139. openTestURL();
  140. getGrid();
  141. testBench().disableWaitForVaadin();
  142. // Test initial renderering with contentVisible = False
  143. TestBenchElement cell = getGrid().getCell(51, 1);
  144. String backgroundColor = cell.getCssValue("backgroundColor");
  145. assertEquals("Background color was not red.", colorRed, backgroundColor);
  146. // data arrives...
  147. sleep((int) (latency * SLEEP_MULTIPLIER));
  148. // Content becomes visible
  149. cell = getGrid().getCell(51, 1);
  150. backgroundColor = cell.getCssValue("backgroundColor");
  151. assertNotEquals("Background color was red.", colorRed, backgroundColor);
  152. // scroll down, new cells becomes contentVisible = False
  153. getGrid().scrollToRow(60);
  154. // Cell should be red (setContentVisible set cell red)
  155. cell = getGrid().getCell(55, 1);
  156. backgroundColor = cell.getCssValue("backgroundColor");
  157. assertEquals("Background color was not red.", colorRed, backgroundColor);
  158. // data arrives...
  159. sleep((int) (latency * SLEEP_MULTIPLIER));
  160. // Cell should no longer be red
  161. backgroundColor = cell.getCssValue("backgroundColor");
  162. assertTrue(
  163. "Background color was not reset",
  164. backgroundColor.equals(colorWhite)
  165. || backgroundColor.equals(colorDark));
  166. }
  167. @Test
  168. public void testSortingEvent() throws Exception {
  169. openTestURL();
  170. $(NativeButtonElement.class).caption("Trigger sorting event").first()
  171. .click();
  172. String consoleText = $(LabelElement.class).id("testDebugConsole")
  173. .getText();
  174. assertTrue("Console text as expected",
  175. consoleText.contains("Columns: 1, order: Column 1: ASCENDING"));
  176. }
  177. @Test
  178. public void testListSorter() throws Exception {
  179. openTestURL();
  180. $(NativeButtonElement.class).caption("Shuffle").first().click();
  181. GridElement gridElem = $(MyClientGridElement.class).first();
  182. // XXX: DANGER! We'll need to know how many rows the Grid has!
  183. // XXX: Currently, this is impossible; hence the hardcoded value of 70.
  184. boolean shuffled = false;
  185. for (int i = 1, l = 70; i < l; ++i) {
  186. String str_a = gridElem.getCell(i - 1, 0).getAttribute("innerHTML");
  187. String str_b = gridElem.getCell(i, 0).getAttribute("innerHTML");
  188. int value_a = Integer.parseInt(str_a);
  189. int value_b = Integer.parseInt(str_b);
  190. if (value_a > value_b) {
  191. shuffled = true;
  192. break;
  193. }
  194. }
  195. assertTrue("Grid shuffled", shuffled);
  196. $(NativeButtonElement.class).caption("Test sorting").first().click();
  197. for (int i = 1, l = 70; i < l; ++i) {
  198. String str_a = gridElem.getCell(i - 1, 0).getAttribute("innerHTML");
  199. String str_b = gridElem.getCell(i, 0).getAttribute("innerHTML");
  200. int value_a = Integer.parseInt(str_a);
  201. int value_b = Integer.parseInt(str_b);
  202. if (value_a > value_b) {
  203. assertTrue("Grid sorted", false);
  204. }
  205. }
  206. }
  207. @Test
  208. public void testComplexRendererOnActivate() {
  209. openTestURL();
  210. GridCellElement cell = getGrid().getCell(3, 1);
  211. cell.click();
  212. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  213. assertEquals("onActivate was not called on KeyDown Enter.",
  214. "Activated!", cell.getText());
  215. cell = getGrid().getCell(4, 1);
  216. cell.click();
  217. new Actions(getDriver()).moveToElement(cell).doubleClick().perform();
  218. assertEquals("onActivate was not called on double click.",
  219. "Activated!", cell.getText());
  220. }
  221. private GridElement getGrid() {
  222. return $(MyClientGridElement.class).first();
  223. }
  224. private void addColumn(Renderers renderer) {
  225. // Add widget renderer column
  226. $(NativeSelectElement.class).first().selectByText(renderer.toString());
  227. $(NativeButtonElement.class).caption("Add").first().click();
  228. }
  229. }