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 8.7KB

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