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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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.NoSuchElementException;
  27. import org.openqa.selenium.WebElement;
  28. import com.vaadin.testbench.By;
  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 now the last column in Grid.
  59. assertEquals("Unexpected column content", "(0, 0)", getGridElement()
  60. .getCell(0, 11).getText());
  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 org.openqa.selenium.By newRow = By
  152. .xpath("//td[text()='newcell: 0']");
  153. assertTrue("Unexpected initial state", !isElementPresent(newRow));
  154. selectMenuPath("Component", "Body rows", "Add first row");
  155. assertTrue("Add row failed", isElementPresent(newRow));
  156. selectMenuPath("Component", "Body rows", "Remove first row");
  157. assertTrue("Remove row failed", !isElementPresent(newRow));
  158. }
  159. /**
  160. * Test that the current view is updated when a property's value is reflect
  161. * to the client, when the value is modified server-side.
  162. */
  163. @Test
  164. public void testPropertyValueChangeEvent() throws Exception {
  165. openTestURL();
  166. assertEquals("Unexpected cell initial state", "(0, 0)",
  167. getGridElement().getCell(0, 0).getText());
  168. selectMenuPath("Component", "Body rows",
  169. "Modify first row (getItemProperty)");
  170. assertEquals("(First) modification with getItemProperty failed",
  171. "modified: 0", getGridElement().getCell(0, 0).getText());
  172. selectMenuPath("Component", "Body rows",
  173. "Modify first row (getContainerProperty)");
  174. assertEquals("(Second) modification with getItemProperty failed",
  175. "modified: Column 0", getGridElement().getCell(0, 0).getText());
  176. }
  177. @Test
  178. public void testRemovingAllItems() throws Exception {
  179. openTestURL();
  180. selectMenuPath("Component", "Body rows", "Remove all rows");
  181. assertEquals(0, getGridElement().findElement(By.tagName("tbody"))
  182. .findElements(By.tagName("tr")).size());
  183. }
  184. @Test
  185. public void testRemoveFirstRowTwice() {
  186. openTestURL();
  187. selectMenuPath("Component", "Body rows", "Remove first row");
  188. selectMenuPath("Component", "Body rows", "Remove first row");
  189. getGridElement().scrollToRow(50);
  190. assertFalse("Listener setup problem occurred.",
  191. logContainsText("AssertionError: Value change listeners"));
  192. }
  193. @Test
  194. public void testVerticalScrollBarVisibilityWhenEnoughRows()
  195. throws Exception {
  196. openTestURL();
  197. assertTrue(verticalScrollbarIsPresent());
  198. selectMenuPath("Component", "Body rows", "Remove all rows");
  199. assertFalse(verticalScrollbarIsPresent());
  200. selectMenuPath("Component", "Size", "HeightMode Row");
  201. selectMenuPath("Component", "Size", "Height by Rows", "2.33 rows");
  202. selectMenuPath("Component", "Body rows", "Add first row");
  203. selectMenuPath("Component", "Body rows", "Add first row");
  204. assertFalse(verticalScrollbarIsPresent());
  205. selectMenuPath("Component", "Body rows", "Add first row");
  206. assertTrue(verticalScrollbarIsPresent());
  207. }
  208. @Test
  209. public void testBareItemSetChange() throws Exception {
  210. openTestURL();
  211. filterSomeAndAssert();
  212. }
  213. @Test
  214. public void testBareItemSetChangeRemovingAllRows() throws Exception {
  215. openTestURL();
  216. selectMenuPath("Component", "Filter", "Add impassable filter");
  217. assertFalse("A notification shouldn't have been displayed",
  218. $(NotificationElement.class).exists());
  219. assertTrue("No body cells should've been found", getGridElement()
  220. .getBody().findElements(By.tagName("td")).isEmpty());
  221. }
  222. @Test
  223. public void testBareItemSetChangeWithMidScroll() throws Exception {
  224. openTestURL();
  225. getGridElement().scrollToRow(GridBasicFeatures.ROWS / 2);
  226. filterSomeAndAssert();
  227. }
  228. @Test
  229. public void testBareItemSetChangeWithBottomScroll() throws Exception {
  230. openTestURL();
  231. getGridElement().scrollToRow(GridBasicFeatures.ROWS);
  232. filterSomeAndAssert();
  233. }
  234. private void filterSomeAndAssert() {
  235. selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
  236. boolean foundElements = false;
  237. for (int row = 0; row < 100; row++) {
  238. try {
  239. GridCellElement cell = getGridElement().getCell(row, 1);
  240. foundElements = true;
  241. assertTrue("Unexpected cell contents. "
  242. + "Did the ItemSetChange work after all?", cell
  243. .getText().startsWith("(23"));
  244. } catch (NoSuchElementException e) {
  245. assertTrue("No rows were found", foundElements);
  246. return;
  247. }
  248. }
  249. fail("unexpected amount of rows post-filter. Did the ItemSetChange work after all?");
  250. }
  251. @Test
  252. public void testRemoveLastColumn() {
  253. setDebug(true);
  254. openTestURL();
  255. String columnName = "Column " + (GridBasicFeatures.COLUMNS - 1);
  256. assertTrue(columnName + " was not present in DOM",
  257. isElementPresent(By.xpath("//th[text()='" + columnName + "']")));
  258. selectMenuPath("Component", "Columns", columnName, "Add / Remove");
  259. assertFalse(isElementPresent(NotificationElement.class));
  260. assertFalse(columnName + " was still present in DOM",
  261. isElementPresent(By.xpath("//th[text()='" + columnName + "']")));
  262. }
  263. @Test
  264. public void testReverseColumns() {
  265. openTestURL();
  266. String[] gridData = new String[GridBasicFeatures.COLUMNS];
  267. GridElement grid = getGridElement();
  268. for (int i = 0; i < gridData.length; ++i) {
  269. gridData[i] = grid.getCell(0, i).getAttribute("innerHTML");
  270. }
  271. selectMenuPath("Component", "State", "Reverse Grid Columns");
  272. // Compare with reversed order
  273. for (int i = 0; i < gridData.length; ++i) {
  274. final int column = gridData.length - 1 - i;
  275. final String newText = grid.getCell(0, column).getAttribute(
  276. "innerHTML");
  277. assertEquals("Grid contained unexpected values. (0, " + column
  278. + ")", gridData[i], newText);
  279. }
  280. }
  281. @Test
  282. public void testAddingProperty() {
  283. setDebug(true);
  284. openTestURL();
  285. assertNotEquals("property value", getGridElement().getCell(0, 0)
  286. .getText());
  287. selectMenuPath("Component", "Properties", "Prepend property");
  288. assertEquals("property value", getGridElement().getCell(0, 0).getText());
  289. }
  290. @Test
  291. public void testRemovingAddedProperty() {
  292. openTestURL();
  293. assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
  294. assertNotEquals("property value", getGridElement().getCell(0, 0)
  295. .getText());
  296. selectMenuPath("Component", "Properties", "Prepend property");
  297. selectMenuPath("Component", "Properties", "Prepend property");
  298. assertNotEquals("property value", getGridElement().getCell(0, 0)
  299. .getText());
  300. assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
  301. }
  302. private boolean verticalScrollbarIsPresent() {
  303. return "scroll".equals(getGridVerticalScrollbar().getCssValue(
  304. "overflow-y"));
  305. }
  306. @Test
  307. public void testAddRowAboveViewport() {
  308. setDebug(true);
  309. openTestURL();
  310. GridCellElement cell = getGridElement().getCell(500, 1);
  311. String cellContent = cell.getText();
  312. selectMenuPath("Component", "Body rows", "Add first row");
  313. assertFalse("Error notification was present",
  314. isElementPresent(NotificationElement.class));
  315. assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
  316. }
  317. @Test
  318. public void testRemoveAndAddRowAboveViewport() {
  319. setDebug(true);
  320. openTestURL();
  321. GridCellElement cell = getGridElement().getCell(500, 1);
  322. String cellContent = cell.getText();
  323. selectMenuPath("Component", "Body rows", "Remove first row");
  324. assertFalse("Error notification was present after removing row",
  325. isElementPresent(NotificationElement.class));
  326. assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
  327. selectMenuPath("Component", "Body rows", "Add first row");
  328. assertFalse("Error notification was present after adding row",
  329. isElementPresent(NotificationElement.class));
  330. assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
  331. }
  332. @Test
  333. public void testScrollAndRemoveAll() {
  334. setDebug(true);
  335. openTestURL();
  336. getGridElement().scrollToRow(500);
  337. selectMenuPath("Component", "Body rows", "Remove all rows");
  338. assertFalse("Error notification was present after removing all rows",
  339. isElementPresent(NotificationElement.class));
  340. assertFalse(getGridElement().isElementPresent(By.vaadin("#cell[0][0]")));
  341. }
  342. private void assertPrimaryStylename(String stylename) {
  343. assertTrue(getGridElement().getAttribute("class").contains(stylename));
  344. String tableWrapperStyleName = getGridElement().getTableWrapper()
  345. .getAttribute("class");
  346. assertTrue(tableWrapperStyleName.contains(stylename + "-tablewrapper"));
  347. String hscrollStyleName = getGridElement().getHorizontalScroller()
  348. .getAttribute("class");
  349. assertTrue(hscrollStyleName.contains(stylename + "-scroller"));
  350. assertTrue(hscrollStyleName
  351. .contains(stylename + "-scroller-horizontal"));
  352. String vscrollStyleName = getGridElement().getVerticalScroller()
  353. .getAttribute("class");
  354. assertTrue(vscrollStyleName.contains(stylename + "-scroller"));
  355. assertTrue(vscrollStyleName.contains(stylename + "-scroller-vertical"));
  356. }
  357. @Test
  358. public void testScrollPosDoesNotChangeAfterStateChange() {
  359. openTestURL();
  360. scrollGridVerticallyTo(1000);
  361. int scrollPos = getGridVerticalScrollPos();
  362. selectMenuPath("Component", "Editor", "Enabled");
  363. assertEquals("Scroll position should've not have changed", scrollPos,
  364. getGridVerticalScrollPos());
  365. }
  366. @Test
  367. public void testReloadPage() throws InterruptedException {
  368. setDebug(true);
  369. openTestURL();
  370. reopenTestURL();
  371. // After opening the URL Grid can be stuck in a state where it thinks it
  372. // should wait for something that's not going to happen.
  373. testBench().disableWaitForVaadin();
  374. // Wait until page is loaded completely.
  375. int count = 0;
  376. while (!$(GridElement.class).exists()) {
  377. if (count == 100) {
  378. fail("Reloading page failed");
  379. }
  380. sleep(100);
  381. ++count;
  382. }
  383. // Wait a bit more for notification to occur.
  384. sleep(1000);
  385. assertFalse("Exception occurred when reloading page",
  386. isElementPresent(NotificationElement.class));
  387. }
  388. @Test
  389. public void testAddThirdRowToGrid() {
  390. openTestURL();
  391. selectMenuPath("Component", "Body rows", "Add third row");
  392. assertFalse(logContainsText("Exception occured"));
  393. }
  394. }