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.

GridBasicFeaturesTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright 2000-2013 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.assertTrue;
  19. import java.util.List;
  20. import org.junit.Test;
  21. import org.openqa.selenium.By;
  22. import org.openqa.selenium.JavascriptExecutor;
  23. import org.openqa.selenium.NoSuchElementException;
  24. import org.openqa.selenium.WebDriver;
  25. import org.openqa.selenium.WebElement;
  26. import org.openqa.selenium.interactions.Actions;
  27. import com.vaadin.tests.tb3.MultiBrowserTest;
  28. /**
  29. *
  30. * @since
  31. * @author Vaadin Ltd
  32. */
  33. public class GridBasicFeaturesTest extends MultiBrowserTest {
  34. @Test
  35. public void testColumnHeaderCaptions() throws Exception {
  36. openTestURL();
  37. // Column headers should be visible
  38. List<WebElement> cells = getGridHeaderRowCells();
  39. assertEquals(10, cells.size());
  40. assertEquals("Column0", cells.get(0).getText());
  41. assertEquals("Column1", cells.get(1).getText());
  42. assertEquals("Column2", cells.get(2).getText());
  43. }
  44. @Test
  45. public void testColumnFooterCaptions() throws Exception {
  46. openTestURL();
  47. // footer row should by default be hidden
  48. List<WebElement> cells = getGridFooterRowCells();
  49. assertEquals(0, cells.size());
  50. // Open footer row
  51. selectMenuPath("Component", "Footers", "Visible");
  52. // Footers should now be visible
  53. cells = getGridFooterRowCells();
  54. assertEquals(10, cells.size());
  55. assertEquals("Footer 0", cells.get(0).getText());
  56. assertEquals("Footer 1", cells.get(1).getText());
  57. assertEquals("Footer 2", cells.get(2).getText());
  58. }
  59. @Test
  60. public void testColumnGroupHeaders() throws Exception {
  61. openTestURL();
  62. // Hide column headers for this test
  63. selectMenuPath("Component", "Headers", "Visible");
  64. List<WebElement> cells = getGridHeaderRowCells();
  65. // header row should be empty
  66. assertEquals(0, cells.size());
  67. // add a group row
  68. selectMenuPath("Component", "Column groups", "Add group row");
  69. // Empty group row cells should be present
  70. cells = getGridHeaderRowCells();
  71. assertEquals(10, cells.size());
  72. // Group columns 0 & 1
  73. selectMenuPath("Component", "Column groups", "Column group row 1",
  74. "Group Column 0 & 1");
  75. cells = getGridHeaderRowCells();
  76. assertEquals("Column 0 & 1", cells.get(0).getText());
  77. }
  78. @Test
  79. public void testColumnGroupFooters() throws Exception {
  80. openTestURL();
  81. // add a group row
  82. selectMenuPath("Component", "Column groups", "Add group row");
  83. // Set footer visible
  84. selectMenuPath("Component", "Column groups", "Column group row 1",
  85. "Footer Visible");
  86. // Group columns 0 & 1
  87. selectMenuPath("Component", "Column groups", "Column group row 1",
  88. "Group Column 0 & 1");
  89. List<WebElement> cells = getGridFooterRowCells();
  90. assertEquals("Column 0 & 1", cells.get(0).getText());
  91. }
  92. @Test
  93. public void testGroupingSameColumnsOnRowThrowsException() throws Exception {
  94. openTestURL();
  95. // add a group row
  96. selectMenuPath("Component", "Column groups", "Add group row");
  97. // Group columns 0 & 1
  98. selectMenuPath("Component", "Column groups", "Column group row 1",
  99. "Group Column 0 & 1");
  100. // Group columns 1 & 2 shoud fail
  101. selectMenuPath("Component", "Column groups", "Column group row 1",
  102. "Group Column 1 & 2");
  103. assertTrue(getLogRow(0)
  104. .contains(
  105. "Exception occured, java.lang.IllegalArgumentExceptionColumn Column1 already belongs to another group."));
  106. }
  107. @Test
  108. public void testHidingColumn() throws Exception {
  109. openTestURL();
  110. // Column 0 should be visible
  111. List<WebElement> cells = getGridHeaderRowCells();
  112. assertEquals("Column0", cells.get(0).getText());
  113. // Hide column 0
  114. selectMenuPath("Component", "Columns", "Column0", "Visible");
  115. // Column 1 should now be the first cell
  116. cells = getGridHeaderRowCells();
  117. assertEquals("Column1", cells.get(0).getText());
  118. }
  119. @Test
  120. public void testRemovingColumn() throws Exception {
  121. openTestURL();
  122. // Column 0 should be visible
  123. List<WebElement> cells = getGridHeaderRowCells();
  124. assertEquals("Column0", cells.get(0).getText());
  125. // Hide column 0
  126. selectMenuPath("Component", "Columns", "Column0", "Remove");
  127. // Column 1 should now be the first cell
  128. cells = getGridHeaderRowCells();
  129. assertEquals("Column1", cells.get(0).getText());
  130. }
  131. @Test
  132. public void testFreezingColumn() throws Exception {
  133. openTestURL();
  134. // Freeze column 2
  135. selectMenuPath("Component", "Columns", "Column2", "Freeze");
  136. WebElement cell = getBodyCellByRowAndColumn(1, 1);
  137. assertTrue(cell.getAttribute("class").contains("frozen"));
  138. cell = getBodyCellByRowAndColumn(1, 2);
  139. assertTrue(cell.getAttribute("class").contains("frozen"));
  140. }
  141. @Test
  142. public void testInitialColumnWidths() throws Exception {
  143. openTestURL();
  144. // Default borders and margins implemented by escalator
  145. int cellBorder = 1 + 1;
  146. int cellMargin = 2 + 2;
  147. WebElement cell = getBodyCellByRowAndColumn(1, 1);
  148. assertEquals((100 - cellBorder - cellMargin) + "px",
  149. cell.getCssValue("width"));
  150. cell = getBodyCellByRowAndColumn(1, 2);
  151. assertEquals((150 - cellBorder - cellMargin) + "px",
  152. cell.getCssValue("width"));
  153. cell = getBodyCellByRowAndColumn(1, 3);
  154. assertEquals((200 - cellBorder - cellMargin) + "px",
  155. cell.getCssValue("width"));
  156. }
  157. @Test
  158. public void testColumnWidths() throws Exception {
  159. openTestURL();
  160. // Default borders and margins implemented by escalator
  161. int cellBorder = 1 + 1;
  162. int cellMargin = 2 + 2;
  163. // Default column width is 100px
  164. WebElement cell = getBodyCellByRowAndColumn(1, 1);
  165. assertEquals((100 - cellBorder - cellMargin) + "px",
  166. cell.getCssValue("width"));
  167. // Set first column to be 200px wide
  168. selectMenuPath("Component", "Columns", "Column0", "Column0 Width",
  169. "200px");
  170. cell = getBodyCellByRowAndColumn(1, 1);
  171. assertEquals((200 - cellBorder - cellMargin) + "px",
  172. cell.getCssValue("width"));
  173. // Set second column to be 150px wide
  174. selectMenuPath("Component", "Columns", "Column1", "Column1 Width",
  175. "150px");
  176. cell = getBodyCellByRowAndColumn(1, 2);
  177. assertEquals((150 - cellBorder - cellMargin) + "px",
  178. cell.getCssValue("width"));
  179. // Set first column to be auto sized (defaults to 100px currently)
  180. selectMenuPath("Component", "Columns", "Column0", "Column0 Width",
  181. "Auto");
  182. cell = getBodyCellByRowAndColumn(1, 1);
  183. assertEquals((100 - cellBorder - cellMargin) + "px",
  184. cell.getCssValue("width"));
  185. }
  186. @Test
  187. public void testPrimaryStyleNames() throws Exception {
  188. openTestURL();
  189. // v-grid is default primary style namea
  190. assertPrimaryStylename("v-grid");
  191. selectMenuPath("Component", "State", "Primary style name",
  192. "v-escalator");
  193. assertPrimaryStylename("v-escalator");
  194. selectMenuPath("Component", "State", "Primary style name", "my-grid");
  195. assertPrimaryStylename("my-grid");
  196. selectMenuPath("Component", "State", "Primary style name", "v-grid");
  197. assertPrimaryStylename("v-grid");
  198. }
  199. /**
  200. * Test that the current view is updated when a server-side container change
  201. * occurs (without scrolling back and forth)
  202. */
  203. @Test
  204. public void testItemSetChangeEvent() throws Exception {
  205. openTestURL();
  206. final By newRow = By.xpath("//td[text()='newcell: 0']");
  207. assertTrue("Unexpected initial state", !elementIsFound(newRow));
  208. selectMenuPath("Component", "Body rows", "Add first row");
  209. assertTrue("Add row failed", elementIsFound(newRow));
  210. selectMenuPath("Component", "Body rows", "Remove first row");
  211. assertTrue("Remove row failed", !elementIsFound(newRow));
  212. }
  213. /**
  214. * Test that the current view is updated when a property's value is reflect
  215. * to the client, when the value is modified server-side.
  216. */
  217. @Test
  218. public void testPropertyValueChangeEvent() throws Exception {
  219. openTestURL();
  220. assertEquals("Unexpected cell initial state", "(0, 0)",
  221. getBodyCellByRowAndColumn(1, 1).getText());
  222. selectMenuPath("Component", "Body rows",
  223. "Modify first row (getItemProperty)");
  224. assertEquals("(First) modification with getItemProperty failed",
  225. "modified: 0", getBodyCellByRowAndColumn(1, 1).getText());
  226. selectMenuPath("Component", "Body rows",
  227. "Modify first row (getContainerProperty)");
  228. assertEquals("(Second) modification with getItemProperty failed",
  229. "modified: Column0", getBodyCellByRowAndColumn(1, 1).getText());
  230. }
  231. @Test
  232. public void testDataFetchingWorks() throws Exception {
  233. openTestURL();
  234. scrollGridVerticallyTo(200);
  235. /*
  236. * Give time for the data to be fetched.
  237. *
  238. * TODO TestBench currently doesn't know when Grid's DOM structure is
  239. * stable. There are some plans regarding implementing support for this,
  240. * so this test case can (should) be modified once that's implemented.
  241. */
  242. sleep(1000);
  243. /*
  244. * TODO this screenshot comparison could be done on the DOM level, if
  245. * the DOM would be always in order. This could be amended once DOM
  246. * reordering is merged into the Grid branch.
  247. */
  248. compareScreen("dataHasBeenLoaded");
  249. }
  250. private boolean elementIsFound(By locator) {
  251. try {
  252. return driver.findElement(locator) != null;
  253. } catch (NoSuchElementException e) {
  254. return false;
  255. }
  256. }
  257. private void assertPrimaryStylename(String stylename) {
  258. assertTrue(getGridElement().getAttribute("class").contains(stylename));
  259. String tableWrapperStyleName = getTableWrapper().getAttribute("class");
  260. assertTrue(tableWrapperStyleName.contains(stylename + "-tablewrapper"));
  261. String hscrollStyleName = getHorizontalScroller().getAttribute("class");
  262. assertTrue(hscrollStyleName.contains(stylename + "-scroller"));
  263. assertTrue(hscrollStyleName
  264. .contains(stylename + "-scroller-horizontal"));
  265. String vscrollStyleName = getVerticalScroller().getAttribute("class");
  266. assertTrue(vscrollStyleName.contains(stylename + "-scroller"));
  267. assertTrue(vscrollStyleName.contains(stylename + "-scroller-vertical"));
  268. }
  269. private WebElement getBodyCellByRowAndColumn(int row, int column) {
  270. return getDriver().findElement(
  271. By.xpath("//div[@id='testComponent']//tbody/tr[" + row
  272. + "]/td[" + column + "]"));
  273. }
  274. private void selectSubMenu(String menuCaption) {
  275. selectMenu(menuCaption);
  276. new Actions(getDriver()).moveByOffset(100, 0).build().perform();
  277. }
  278. private void selectMenu(String menuCaption) {
  279. getDriver().findElement(
  280. By.xpath("//span[text() = '" + menuCaption + "']")).click();
  281. }
  282. private void selectMenuPath(String... menuCaptions) {
  283. selectMenu(menuCaptions[0]);
  284. for (int i = 1; i < menuCaptions.length; i++) {
  285. selectSubMenu(menuCaptions[i]);
  286. }
  287. }
  288. private WebElement getVerticalScroller() {
  289. return getDriver().findElement(
  290. By.xpath("//div[@id='testComponent']/div[1]"));
  291. }
  292. private WebElement getHorizontalScroller() {
  293. return getDriver().findElement(
  294. By.xpath("//div[@id='testComponent']/div[2]"));
  295. }
  296. private WebElement getTableWrapper() {
  297. return getDriver().findElement(
  298. By.xpath("//div[@id='testComponent']/div[3]"));
  299. }
  300. private WebElement getGridElement() {
  301. return getDriver().findElement(By.id("testComponent"));
  302. }
  303. private List<WebElement> getGridHeaderRowCells() {
  304. return getDriver().findElements(
  305. By.xpath("//div[@id='testComponent']//thead//th"));
  306. }
  307. private List<WebElement> getGridFooterRowCells() {
  308. return getDriver().findElements(
  309. By.xpath("//div[@id='testComponent']//tfoot//td"));
  310. }
  311. private void scrollGridVerticallyTo(double px) {
  312. executeScript("arguments[0].scrollTop = " + px,
  313. getGridVerticalScrollbar());
  314. }
  315. private Object executeScript(String script, WebElement element) {
  316. @SuppressWarnings("hiding")
  317. final WebDriver driver = getDriver();
  318. if (driver instanceof JavascriptExecutor) {
  319. final JavascriptExecutor je = (JavascriptExecutor) driver;
  320. return je.executeScript(script, element);
  321. } else {
  322. throw new IllegalStateException("current driver "
  323. + getDriver().getClass().getName() + " is not a "
  324. + JavascriptExecutor.class.getSimpleName());
  325. }
  326. }
  327. private WebElement getGridVerticalScrollbar() {
  328. return getDriver()
  329. .findElement(
  330. By.xpath("//div[contains(@class, \"v-grid-scroller-vertical\")]"));
  331. }
  332. }