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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Copyright 2000-2016 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.v7.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.customelements.GridElement;
  31. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  32. import com.vaadin.testbench.elements.NotificationElement;
  33. import com.vaadin.v7.tests.components.grid.basicfeatures.GridBasicFeatures;
  34. import com.vaadin.v7.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,
  46. getGridHeaderRowCells().size());
  47. }
  48. @Test
  49. public void testRemoveAndAddColumn() {
  50. setDebug(true);
  51. openTestURL();
  52. assertEquals("column 0",
  53. getGridElement().getHeaderCell(0, 0).getText().toLowerCase());
  54. selectMenuPath("Component", "Columns", "Column 0", "Add / Remove");
  55. assertEquals("column 1",
  56. getGridElement().getHeaderCell(0, 0).getText().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)",
  60. getGridElement().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", "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. @Test
  235. public void testBareItemSetChangeWithBottomScrollAndSmallViewport()
  236. throws Exception {
  237. openTestURL();
  238. selectMenuPath("Component", "Size", "HeightMode Row");
  239. getGridElement().getRow(GridBasicFeatures.ROWS - 1);
  240. // filter
  241. selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
  242. String text = getGridElement().getCell(10, 0).getText();
  243. assertFalse(text.isEmpty());
  244. }
  245. private void filterSomeAndAssert() {
  246. selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
  247. boolean foundElements = false;
  248. for (int row = 0; row < 100; row++) {
  249. try {
  250. GridCellElement cell = getGridElement().getCell(row, 1);
  251. foundElements = true;
  252. assertTrue(
  253. "Unexpected cell contents. "
  254. + "Did the ItemSetChange work after all?",
  255. cell.getText().startsWith("(23"));
  256. } catch (NoSuchElementException e) {
  257. assertTrue("No rows were found", foundElements);
  258. return;
  259. }
  260. }
  261. fail("unexpected amount of rows post-filter. Did the ItemSetChange work after all?");
  262. }
  263. @Test
  264. public void testRemoveLastColumn() {
  265. setDebug(true);
  266. openTestURL();
  267. int col = GridBasicFeatures.COLUMNS;
  268. String columnName = "Column " + (GridBasicFeatures.COLUMNS - 1);
  269. assertTrue(columnName + " was not present in DOM",
  270. isElementPresent(By.xpath("//th[" + col + "]/div[1]")));
  271. selectMenuPath("Component", "Columns", columnName, "Add / Remove");
  272. assertFalse(isElementPresent(NotificationElement.class));
  273. assertFalse(columnName + " was still present in DOM",
  274. isElementPresent(By.xpath("//th[" + col + "]/div[1]")));
  275. }
  276. @Test
  277. public void testReverseColumns() {
  278. openTestURL();
  279. String[] gridData = new String[GridBasicFeatures.COLUMNS];
  280. GridElement grid = getGridElement();
  281. for (int i = 0; i < gridData.length; ++i) {
  282. gridData[i] = grid.getCell(0, i).getAttribute("innerHTML");
  283. }
  284. selectMenuPath("Component", "State", "Reverse Grid Columns");
  285. // Compare with reversed order
  286. for (int i = 0; i < gridData.length; ++i) {
  287. final int column = gridData.length - 1 - i;
  288. final String newText = grid.getCell(0, column)
  289. .getAttribute("innerHTML");
  290. assertEquals(
  291. "Grid contained unexpected values. (0, " + column + ")",
  292. gridData[i], newText);
  293. }
  294. }
  295. @Test
  296. public void testAddingProperty() {
  297. setDebug(true);
  298. openTestURL();
  299. assertNotEquals("property value",
  300. getGridElement().getCell(0, 0).getText());
  301. selectMenuPath("Component", "Properties", "Prepend property");
  302. assertEquals("property value",
  303. getGridElement().getCell(0, 0).getText());
  304. }
  305. @Test
  306. public void testRemovingAddedProperty() {
  307. openTestURL();
  308. assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
  309. assertNotEquals("property value",
  310. getGridElement().getCell(0, 0).getText());
  311. selectMenuPath("Component", "Properties", "Prepend property");
  312. selectMenuPath("Component", "Properties", "Prepend property");
  313. assertNotEquals("property value",
  314. getGridElement().getCell(0, 0).getText());
  315. assertEquals("(0, 0)", getGridElement().getCell(0, 0).getText());
  316. }
  317. private boolean verticalScrollbarIsPresent() {
  318. return "scroll"
  319. .equals(getGridVerticalScrollbar().getCssValue("overflow-y"));
  320. }
  321. @Test
  322. public void testAddRowAboveViewport() {
  323. setDebug(true);
  324. openTestURL();
  325. GridCellElement cell = getGridElement().getCell(500, 1);
  326. String cellContent = cell.getText();
  327. selectMenuPath("Component", "Body rows", "Add first row");
  328. assertFalse("Error notification was present",
  329. isElementPresent(NotificationElement.class));
  330. assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
  331. }
  332. @Test
  333. public void testRemoveAndAddRowAboveViewport() {
  334. setDebug(true);
  335. openTestURL();
  336. GridCellElement cell = getGridElement().getCell(500, 1);
  337. String cellContent = cell.getText();
  338. selectMenuPath("Component", "Body rows", "Remove first row");
  339. assertFalse("Error notification was present after removing row",
  340. isElementPresent(NotificationElement.class));
  341. assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
  342. selectMenuPath("Component", "Body rows", "Add first row");
  343. assertFalse("Error notification was present after adding row",
  344. isElementPresent(NotificationElement.class));
  345. assertEquals("Grid scrolled unexpectedly", cellContent, cell.getText());
  346. }
  347. @Test
  348. public void testScrollAndRemoveAll() {
  349. setDebug(true);
  350. openTestURL();
  351. getGridElement().scrollToRow(500);
  352. selectMenuPath("Component", "Body rows", "Remove all rows");
  353. assertFalse("Error notification was present after removing all rows",
  354. isElementPresent(NotificationElement.class));
  355. assertFalse(
  356. getGridElement().isElementPresent(By.vaadin("#cell[0][0]")));
  357. }
  358. private void assertPrimaryStylename(String stylename) {
  359. assertTrue(getGridElement().getAttribute("class").contains(stylename));
  360. String tableWrapperStyleName = getGridElement().getTableWrapper()
  361. .getAttribute("class");
  362. assertTrue(tableWrapperStyleName.contains(stylename + "-tablewrapper"));
  363. String hscrollStyleName = getGridElement().getHorizontalScroller()
  364. .getAttribute("class");
  365. assertTrue(hscrollStyleName.contains(stylename + "-scroller"));
  366. assertTrue(
  367. hscrollStyleName.contains(stylename + "-scroller-horizontal"));
  368. String vscrollStyleName = getGridElement().getVerticalScroller()
  369. .getAttribute("class");
  370. assertTrue(vscrollStyleName.contains(stylename + "-scroller"));
  371. assertTrue(vscrollStyleName.contains(stylename + "-scroller-vertical"));
  372. }
  373. @Test
  374. public void testScrollPosDoesNotChangeAfterStateChange() {
  375. openTestURL();
  376. scrollGridVerticallyTo(1000);
  377. int scrollPos = getGridVerticalScrollPos();
  378. selectMenuPath("Component", "Editor", "Enabled");
  379. assertEquals("Scroll position should've not have changed", scrollPos,
  380. getGridVerticalScrollPos());
  381. }
  382. @Test
  383. public void testReloadPage() throws InterruptedException {
  384. setDebug(true);
  385. openTestURL();
  386. reopenTestURL();
  387. // After opening the URL Grid can be stuck in a state where it thinks it
  388. // should wait for something that's not going to happen.
  389. testBench().disableWaitForVaadin();
  390. // Wait until page is loaded completely.
  391. int count = 0;
  392. while (!$(GridElement.class).exists()) {
  393. if (count == 100) {
  394. fail("Reloading page failed");
  395. }
  396. sleep(100);
  397. ++count;
  398. }
  399. // Wait a bit more for notification to occur.
  400. sleep(1000);
  401. assertFalse("Exception occurred when reloading page",
  402. isElementPresent(NotificationElement.class));
  403. }
  404. @Test
  405. public void testAddThirdRowToGrid() {
  406. openTestURL();
  407. selectMenuPath("Component", "Body rows", "Add third row");
  408. assertFalse(logContainsText("Exception occured"));
  409. }
  410. @Test
  411. public void getBodyRowCountJS() {
  412. openTestURL();
  413. GridElement grid = $(GridElement.class).first();
  414. assertEquals(1000L,
  415. executeScript("return arguments[0].getBodyRowCount()", grid));
  416. selectMenuPath("Component", "Body rows", "Remove all rows");
  417. assertEquals(0L,
  418. executeScript("return arguments[0].getBodyRowCount()", grid));
  419. selectMenuPath("Component", "Body rows", "Add first row");
  420. assertEquals(1L,
  421. executeScript("return arguments[0].getBodyRowCount()", grid));
  422. }
  423. }