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.

GridStructureTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.basicfeatures.server;
  17. import static org.hamcrest.MatcherAssert.assertThat;
  18. import static org.hamcrest.core.IsNot.not;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertFalse;
  21. import static org.junit.Assert.assertNotEquals;
  22. import static org.junit.Assert.assertTrue;
  23. import static org.junit.Assert.fail;
  24. import java.util.List;
  25. import org.junit.Test;
  26. import org.openqa.selenium.By;
  27. import org.openqa.selenium.NoSuchElementException;
  28. import org.openqa.selenium.WebElement;
  29. import com.vaadin.testbench.TestBenchElement;
  30. import com.vaadin.testbench.elements.GridElement;
  31. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  32. import com.vaadin.testbench.elements.NotificationElement;
  33. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
  34. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
  35. public class GridStructureTest extends GridBasicFeaturesTest {
  36. @Test
  37. public void testRemovingAllColumns() {
  38. setDebug(true);
  39. openTestURL();
  40. for (int i = 0; i < GridBasicFeatures.COLUMNS; ++i) {
  41. selectMenuPath("Component", "Columns", "Column " + i,
  42. "Add / Remove");
  43. assertFalse(isElementPresent(NotificationElement.class));
  44. }
  45. assertEquals("Headers still visible.", 0, getGridHeaderRowCells()
  46. .size());
  47. }
  48. @Test
  49. public void testRemoveAndAddColumn() {
  50. setDebug(true);
  51. openTestURL();
  52. assertEquals("column 0", getGridElement().getHeaderCell(0, 0).getText()
  53. .toLowerCase());
  54. selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
  55. assertEquals("column 1", getGridElement().getHeaderCell(0, 0).getText()
  56. .toLowerCase());
  57. selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
  58. // Column 0 is appended to the end of grid
  59. assertEquals("column 0", getGridElement().getHeaderCell(0, 11)
  60. .getText().toLowerCase());
  61. }
  62. @Test
  63. public void testRemovingColumn() throws Exception {
  64. openTestURL();
  65. // Column 0 should be visible
  66. List<TestBenchElement> cells = getGridHeaderRowCells();
  67. assertEquals("column 0", cells.get(0).getText().toLowerCase());
  68. // Hide column 0
  69. selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
  70. // Column 1 should now be the first cell
  71. cells = getGridHeaderRowCells();
  72. assertEquals("column 1", cells.get(0).getText().toLowerCase());
  73. }
  74. @Test
  75. public void testDataLoadingAfterRowRemoval() throws Exception {
  76. openTestURL();
  77. // Remove columns 2,3,4
  78. selectMenuPath("Component", "Columns", "Column 2", "Add / Remove");
  79. selectMenuPath("Component", "Columns", "Column 3", "Add / Remove");
  80. selectMenuPath("Component", "Columns", "Column 4", "Add / Remove");
  81. // Scroll so new data is lazy loaded
  82. scrollGridVerticallyTo(1000);
  83. // Let lazy loading do its job
  84. sleep(1000);
  85. // Check that row is loaded
  86. assertThat(getGridElement().getCell(11, 0).getText(), not("..."));
  87. }
  88. @Test
  89. public void testFreezingColumn() throws Exception {
  90. openTestURL();
  91. // Freeze column 1
  92. selectMenuPath("Component", "State", "Frozen column count", "1");
  93. WebElement cell = getGridElement().getCell(0, 0);
  94. assertTrue(cell.getAttribute("class").contains("frozen"));
  95. cell = getGridElement().getCell(0, 1);
  96. assertFalse(cell.getAttribute("class").contains("frozen"));
  97. }
  98. @Test
  99. public void testInitialColumnWidths() throws Exception {
  100. openTestURL();
  101. WebElement cell = getGridElement().getCell(0, 0);
  102. assertEquals(100, cell.getSize().getWidth());
  103. cell = getGridElement().getCell(0, 1);
  104. assertEquals(150, cell.getSize().getWidth());
  105. cell = getGridElement().getCell(0, 2);
  106. assertEquals(200, cell.getSize().getWidth());
  107. }
  108. @Test
  109. public void testColumnWidths() throws Exception {
  110. openTestURL();
  111. // Default column width is 100px
  112. WebElement cell = getGridElement().getCell(0, 0);
  113. assertEquals(100, cell.getSize().getWidth());
  114. // Set first column to be 200px wide
  115. selectMenuPath("Component", "Columns", "Column 0", "Column 0 Width",
  116. "200px");
  117. cell = getGridElement().getCell(0, 0);
  118. assertEquals(200, cell.getSize().getWidth());
  119. // Set second column to be 150px wide
  120. selectMenuPath("Component", "Columns", "Column 1", "Column 1 Width",
  121. "150px");
  122. cell = getGridElement().getCell(0, 1);
  123. assertEquals(150, cell.getSize().getWidth());
  124. selectMenuPath("Component", "Columns", "Column 0", "Column 0 Width",
  125. "Auto");
  126. // since the column 0 was previously 200, it should've shrunk when
  127. // autoresizing.
  128. cell = getGridElement().getCell(0, 0);
  129. assertLessThan("", cell.getSize().getWidth(), 200);
  130. }
  131. @Test
  132. public void testPrimaryStyleNames() throws Exception {
  133. openTestURL();
  134. // v-grid is default primary style namea
  135. assertPrimaryStylename("v-grid");
  136. selectMenuPath("Component", "State", "Primary style name",
  137. "v-escalator");
  138. assertPrimaryStylename("v-escalator");
  139. selectMenuPath("Component", "State", "Primary style name", "my-grid");
  140. assertPrimaryStylename("my-grid");
  141. selectMenuPath("Component", "State", "Primary style name", "v-grid");
  142. assertPrimaryStylename("v-grid");
  143. }
  144. /**
  145. * Test that the current view is updated when a server-side container change
  146. * occurs (without scrolling back and forth)
  147. */
  148. @Test
  149. public void testItemSetChangeEvent() throws Exception {
  150. openTestURL();
  151. final By newRow = By.xpath("//td[text()='newcell: 0']");
  152. assertTrue("Unexpected initial state", !isElementPresent(newRow));
  153. selectMenuPath("Component", "Body rows", "Add first row");
  154. assertTrue("Add row failed", isElementPresent(newRow));
  155. selectMenuPath("Component", "Body rows", "Remove first row");
  156. assertTrue("Remove row failed", !isElementPresent(newRow));
  157. }
  158. /**
  159. * Test that the current view is updated when a property's value is reflect
  160. * to the client, when the value is modified server-side.
  161. */
  162. @Test
  163. public void testPropertyValueChangeEvent() throws Exception {
  164. openTestURL();
  165. assertEquals("Unexpected cell initial state", "(0, 0)",
  166. getGridElement().getCell(0, 0).getText());
  167. selectMenuPath("Component", "Body rows",
  168. "Modify first row (getItemProperty)");
  169. assertEquals("(First) modification with getItemProperty failed",
  170. "modified: 0", getGridElement().getCell(0, 0).getText());
  171. selectMenuPath("Component", "Body rows",
  172. "Modify first row (getContainerProperty)");
  173. assertEquals("(Second) modification with getItemProperty failed",
  174. "modified: Column 0", getGridElement().getCell(0, 0).getText());
  175. }
  176. @Test
  177. public void testRemovingAllItems() throws Exception {
  178. openTestURL();
  179. selectMenuPath("Component", "Body rows", "Remove all rows");
  180. assertEquals(0, getGridElement().findElement(By.tagName("tbody"))
  181. .findElements(By.tagName("tr")).size());
  182. }
  183. @Test
  184. public void testVerticalScrollBarVisibilityWhenEnoughRows()
  185. throws Exception {
  186. openTestURL();
  187. assertTrue(verticalScrollbarIsPresent());
  188. selectMenuPath("Component", "Body rows", "Remove all rows");
  189. assertFalse(verticalScrollbarIsPresent());
  190. selectMenuPath("Component", "Size", "HeightMode Row");
  191. selectMenuPath("Component", "Size", "Height by Rows", "2.33 rows");
  192. selectMenuPath("Component", "Body rows", "Add first row");
  193. selectMenuPath("Component", "Body rows", "Add first row");
  194. assertFalse(verticalScrollbarIsPresent());
  195. selectMenuPath("Component", "Body rows", "Add first row");
  196. assertTrue(verticalScrollbarIsPresent());
  197. }
  198. @Test
  199. public void testBareItemSetChange() throws Exception {
  200. openTestURL();
  201. filterAndAssert();
  202. }
  203. @Test
  204. public void testBareItemSetChangeWithMidScroll() throws Exception {
  205. openTestURL();
  206. getGridElement().scrollToRow(GridBasicFeatures.ROWS / 2);
  207. filterAndAssert();
  208. }
  209. @Test
  210. public void testBareItemSetChangeWithBottomScroll() throws Exception {
  211. openTestURL();
  212. getGridElement().scrollToRow(GridBasicFeatures.ROWS);
  213. filterAndAssert();
  214. }
  215. private void filterAndAssert() {
  216. selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
  217. boolean foundElements = false;
  218. for (int row = 0; row < 100; row++) {
  219. try {
  220. GridCellElement cell = getGridElement().getCell(row, 1);
  221. foundElements = true;
  222. assertTrue("Unexpected cell contents. "
  223. + "Did the ItemSetChange work after all?", cell
  224. .getText().startsWith("(23"));
  225. } catch (NoSuchElementException e) {
  226. assertTrue("No rows were found", foundElements);
  227. return;
  228. }
  229. }
  230. fail("unexpected amount of rows post-filter. Did the ItemSetChange work after all?");
  231. }
  232. @Test
  233. public void testRemoveLastColumn() {
  234. setDebug(true);
  235. openTestURL();
  236. String columnName = "Column " + (GridBasicFeatures.COLUMNS - 1);
  237. assertTrue(columnName + " was not present in DOM",
  238. isElementPresent(By.xpath("//th[text()='" + columnName + "']")));
  239. selectMenuPath("Component", "Columns", columnName, "Add / Remove");
  240. assertFalse(isElementPresent(NotificationElement.class));
  241. assertFalse(columnName + " was still present in DOM",
  242. isElementPresent(By.xpath("//th[text()='" + columnName + "']")));
  243. }
  244. @Test
  245. public void testReverseColumns() {
  246. openTestURL();
  247. String[] gridData = new String[GridBasicFeatures.COLUMNS];
  248. GridElement grid = getGridElement();
  249. for (int i = 0; i < gridData.length; ++i) {
  250. gridData[i] = grid.getCell(0, i).getAttribute("innerHTML");
  251. }
  252. selectMenuPath("Component", "State", "Reverse Grid Columns");
  253. // Compare with reversed order
  254. for (int i = 0; i < gridData.length; ++i) {
  255. final int column = gridData.length - 1 - i;
  256. final String newText = grid.getCell(0, column).getAttribute(
  257. "innerHTML");
  258. assertEquals("Grid contained unexpected values. (0, " + column
  259. + ")", gridData[i], newText);
  260. }
  261. }
  262. @Test
  263. public void testAddingProperty() {
  264. setDebug(true);
  265. openTestURL();
  266. assertNotEquals("property value", getGridElement().getCell(0, 0)
  267. .getText());
  268. selectMenuPath("Component", "Properties", "Prepend property");
  269. assertEquals("property value", getGridElement().getCell(0, 0).getText());
  270. }
  271. @Test
  272. public void testRemovingAddedProperty() {
  273. openTestURL();
  274. assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
  275. assertNotEquals("property value", getGridElement().getCell(0, 0)
  276. .getText());
  277. selectMenuPath("Component", "Properties", "Prepend property");
  278. selectMenuPath("Component", "Properties", "Prepend property");
  279. assertNotEquals("property value", getGridElement().getCell(0, 0)
  280. .getText());
  281. assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
  282. }
  283. private boolean verticalScrollbarIsPresent() {
  284. return "scroll".equals(getGridVerticalScrollbar().getCssValue(
  285. "overflow-y"));
  286. }
  287. private void assertPrimaryStylename(String stylename) {
  288. assertTrue(getGridElement().getAttribute("class").contains(stylename));
  289. String tableWrapperStyleName = getGridElement().getTableWrapper()
  290. .getAttribute("class");
  291. assertTrue(tableWrapperStyleName.contains(stylename + "-tablewrapper"));
  292. String hscrollStyleName = getGridElement().getHorizontalScroller()
  293. .getAttribute("class");
  294. assertTrue(hscrollStyleName.contains(stylename + "-scroller"));
  295. assertTrue(hscrollStyleName
  296. .contains(stylename + "-scroller-horizontal"));
  297. String vscrollStyleName = getGridElement().getVerticalScroller()
  298. .getAttribute("class");
  299. assertTrue(vscrollStyleName.contains(stylename + "-scroller"));
  300. assertTrue(vscrollStyleName.contains(stylename + "-scroller-vertical"));
  301. }
  302. }