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.

GridSortingTest.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.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import java.util.Collections;
  22. import java.util.Comparator;
  23. import java.util.List;
  24. import org.junit.Test;
  25. import org.openqa.selenium.Keys;
  26. import org.openqa.selenium.WebElement;
  27. import org.openqa.selenium.interactions.Actions;
  28. import com.vaadin.shared.data.sort.SortDirection;
  29. import com.vaadin.testbench.By;
  30. import com.vaadin.testbench.elements.GridElement;
  31. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  32. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
  33. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
  34. public class GridSortingTest extends GridBasicFeaturesTest {
  35. private static class SortInfo {
  36. public final int sortOrder;
  37. public final SortDirection sortDirection;
  38. private SortInfo(int sortOrder, SortDirection sortDirection) {
  39. this.sortOrder = sortOrder;
  40. this.sortDirection = sortDirection;
  41. }
  42. }
  43. private static class SortInfoWithColumn extends SortInfo {
  44. public final int columnIndex;
  45. private SortInfoWithColumn(int columnIndex, int sortOrder,
  46. SortDirection sortDirection) {
  47. super(sortOrder, sortDirection);
  48. this.columnIndex = columnIndex;
  49. }
  50. }
  51. private static SortInfo _(int sortOrder, SortDirection sortDirection) {
  52. return new SortInfo(sortOrder, sortDirection);
  53. }
  54. private static SortInfoWithColumn _(int columnIndex, int sortOrder,
  55. SortDirection sortDirection) {
  56. return new SortInfoWithColumn(columnIndex, sortOrder, sortDirection);
  57. }
  58. @Test
  59. public void testProgrammaticSorting() throws Exception {
  60. openTestURL();
  61. // Sorting by column 9 is sorting by row index that is represented as a
  62. // String.
  63. // First cells for first 3 rows are (9, 0), (99, 0) and (999, 0)
  64. sortBy("Column 9, DESC");
  65. assertLastSortIsUserOriginated(false);
  66. // Verify that programmatic sorting calls are identified as originating
  67. // from API
  68. assertColumnsAreSortedAs(_(9, 1, SortDirection.DESCENDING));
  69. String row = "";
  70. for (int i = 0; i < 3; ++i) {
  71. row += "9";
  72. String expected = "(" + row + ", 0)";
  73. String cellValue = getGridElement().getCell(i, 0).getText();
  74. assertEquals("Grid is not sorted by Column 9 "
  75. + "using descending direction.", expected, cellValue);
  76. }
  77. // Column 10 is random numbers from Random with seed 13334
  78. sortBy("Column 10, ASC");
  79. assertFalse("Column 9 should no longer have the sort-desc stylename",
  80. getGridElement().getHeaderCell(0, 9).getAttribute("class")
  81. .contains("sort-desc"));
  82. assertColumnsAreSortedAs(_(10, 1, SortDirection.ASCENDING));
  83. for (int i = 0; i < 5; ++i) {
  84. Integer firstRow = Integer.valueOf(getGridElement().getCell(i + 1,
  85. 10).getText());
  86. Integer secondRow = Integer.valueOf(getGridElement().getCell(i, 10)
  87. .getText());
  88. assertGreater("Grid is not sorted by Column 10 using"
  89. + " ascending direction", firstRow, secondRow);
  90. }
  91. // Column 7 is row index as a number. Last three row are original rows
  92. // 2, 1 and 0.
  93. sortBy("Column 7, DESC");
  94. for (int i = 0; i < 3; ++i) {
  95. String expected = "(" + i + ", 0)";
  96. String cellContent = getGridElement().getCell(
  97. GridBasicFeatures.ROWS - (i + 1), 0).getText();
  98. assertEquals("Grid is not sorted by Column 7 using "
  99. + "descending direction", expected, cellContent);
  100. }
  101. assertFalse("Column 10 should no longer have the sort-asc stylename",
  102. getGridElement().getHeaderCell(0, 10).getAttribute("class")
  103. .contains("sort-asc"));
  104. assertColumnsAreSortedAs(_(7, 1, SortDirection.DESCENDING));
  105. }
  106. @Test
  107. public void testMouseSorting() throws Exception {
  108. setDebug(true);
  109. openTestURL();
  110. GridElement grid = getGridElement();
  111. selectMenuPath("Component", "Columns", "Column 9", "Column 9 Width",
  112. "Auto");
  113. // Sorting by column 9 is sorting by row index that is represented as a
  114. // String.
  115. // Click header twice to sort descending
  116. clickHeader(grid.getHeaderCell(0, 9));
  117. assertLastSortIsUserOriginated(true);
  118. assertColumnsAreSortedAs(_(9, 1, SortDirection.ASCENDING));
  119. clickHeader(grid.getHeaderCell(0, 9));
  120. assertColumnsAreSortedAs(_(9, 1, SortDirection.DESCENDING));
  121. // First cells for first 3 rows are (9, 0), (99, 0) and (999, 0)
  122. String row = "";
  123. for (int i = 0; i < 3; ++i) {
  124. row += "9";
  125. String expected = "(" + row + ", 0)";
  126. String actual = grid.getCell(i, 0).getText();
  127. assertEquals("Grid is not sorted by Column 9"
  128. + " using descending direction.", expected, actual);
  129. }
  130. selectMenuPath("Component", "Columns", "Column 10", "Column 10 Width",
  131. "Auto");
  132. // Column 10 is random numbers from Random with seed 13334
  133. // Click header to sort ascending
  134. clickHeader(grid.getHeaderCell(0, 10));
  135. assertColumnsAreSortedAs(_(10, 1, SortDirection.ASCENDING));
  136. for (int i = 0; i < 5; ++i) {
  137. Integer firstRow = Integer.valueOf(grid.getCell(i + 1, 10)
  138. .getText());
  139. Integer secondRow = Integer.valueOf(grid.getCell(i, 10).getText());
  140. assertGreater(
  141. "Grid is not sorted by Column 10 using ascending direction",
  142. firstRow, secondRow);
  143. }
  144. selectMenuPath("Component", "Columns", "Column 7", "Column 7 Width",
  145. "Auto");
  146. // Column 7 is row index as a number. Last three row are original rows
  147. // 2, 1 and 0.
  148. // Click header twice to sort descending
  149. clickHeader(grid.getHeaderCell(0, 7));
  150. assertColumnsAreSortedAs(_(7, 1, SortDirection.ASCENDING));
  151. clickHeader(grid.getHeaderCell(0, 7));
  152. assertColumnsAreSortedAs(_(7, 1, SortDirection.DESCENDING));
  153. for (int i = 0; i < 3; ++i) {
  154. assertEquals(
  155. "Grid is not sorted by Column 7 using descending direction",
  156. "(" + i + ", 0)",
  157. grid.getCell(GridBasicFeatures.ROWS - (i + 1), 0).getText());
  158. }
  159. }
  160. private void clickHeader(GridCellElement headerCell) {
  161. new Actions(getDriver()).moveToElement(headerCell, 5, 5).click()
  162. .perform();
  163. }
  164. private void sendKey(Keys seq) {
  165. new Actions(getDriver()).sendKeys(seq).perform();
  166. }
  167. private void holdKey(Keys key) {
  168. new Actions(getDriver()).keyDown(key).perform();
  169. }
  170. private void releaseKey(Keys key) {
  171. new Actions(getDriver()).keyUp(key).perform();
  172. }
  173. @Test
  174. public void testKeyboardSorting() {
  175. openTestURL();
  176. /*
  177. * We can't click on the header directly, since it will sort the header
  178. * immediately. We need to focus some other column first, and only then
  179. * navigate there.
  180. */
  181. getGridElement().getCell(0, 0).click();
  182. sendKey(Keys.ARROW_UP);
  183. // Sort ASCENDING on first column
  184. sendKey(Keys.ENTER);
  185. assertLastSortIsUserOriginated(true);
  186. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING));
  187. // Move to next column
  188. sendKey(Keys.RIGHT);
  189. // Add this column to the existing sorting group
  190. holdKey(Keys.SHIFT);
  191. sendKey(Keys.ENTER);
  192. releaseKey(Keys.SHIFT);
  193. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
  194. _(2, SortDirection.ASCENDING));
  195. // Move to next column
  196. sendKey(Keys.RIGHT);
  197. // Add a third column to the sorting group
  198. holdKey(Keys.SHIFT);
  199. sendKey(Keys.ENTER);
  200. releaseKey(Keys.SHIFT);
  201. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
  202. _(2, SortDirection.ASCENDING), _(3, SortDirection.ASCENDING));
  203. // Move back to the second column
  204. sendKey(Keys.LEFT);
  205. // Change sort direction of the second column to DESCENDING
  206. holdKey(Keys.SHIFT);
  207. sendKey(Keys.ENTER);
  208. releaseKey(Keys.SHIFT);
  209. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
  210. _(2, SortDirection.DESCENDING), _(3, SortDirection.ASCENDING));
  211. // Move back to the third column
  212. sendKey(Keys.RIGHT);
  213. // Set sorting to third column, ASCENDING
  214. sendKey(Keys.ENTER);
  215. assertColumnsAreSortedAs(_(2, 1, SortDirection.ASCENDING));
  216. // Move to the fourth column
  217. sendKey(Keys.RIGHT);
  218. // Make sure that single-column sorting also works as expected
  219. sendKey(Keys.ENTER);
  220. assertColumnsAreSortedAs(_(3, 1, SortDirection.ASCENDING));
  221. }
  222. private void assertColumnsAreSortedAs(SortInfoWithColumn... sortInfos) {
  223. for (SortInfoWithColumn sortInfo : sortInfos) {
  224. assertSort(sortInfo, sortInfo.columnIndex,
  225. onlyOneColumnIsSorted(sortInfos));
  226. }
  227. }
  228. /**
  229. * @param sortDirections
  230. * <code>null</code> if not interested in that index, otherwise a
  231. * direction that the column needs to be sorted as
  232. */
  233. private void assertColumnsAreSortedAs(SortInfo... sortInfos) {
  234. for (int column = 0; column < sortInfos.length; column++) {
  235. SortInfo sortInfo = sortInfos[column];
  236. assertSort(sortInfo, column, onlyOneColumnIsSorted(sortInfos));
  237. }
  238. }
  239. private void assertSort(SortInfo sortInfo, int column,
  240. boolean onlyOneColumnIsSorted) {
  241. if (sortInfo == null) {
  242. return;
  243. }
  244. GridCellElement headerCell = getGridElement().getHeaderCell(0, column);
  245. String classValue = headerCell.getAttribute("class");
  246. boolean isSortedAscending = sortInfo.sortDirection == SortDirection.ASCENDING
  247. && classValue.contains("sort-asc");
  248. boolean isSortedDescending = sortInfo.sortDirection == SortDirection.DESCENDING
  249. && classValue.contains("sort-desc");
  250. if (isSortedAscending || isSortedDescending) {
  251. String sortOrderAttribute = headerCell.getAttribute("sort-order");
  252. if (sortOrderAttribute == null) {
  253. if (!(sortInfo.sortOrder == 1 && onlyOneColumnIsSorted)) {
  254. fail("missing sort-order element attribute from column "
  255. + column);
  256. }
  257. } else {
  258. assertEquals("sort order was not as expected",
  259. String.valueOf(sortInfo.sortOrder), sortOrderAttribute);
  260. }
  261. } else {
  262. fail("column index " + column + " was not sorted as "
  263. + sortInfo.sortDirection + " (class: " + classValue + ")");
  264. }
  265. }
  266. private static boolean onlyOneColumnIsSorted(SortInfo[] sortInfos) {
  267. boolean foundSortedColumn = false;
  268. for (SortInfo sortInfo : sortInfos) {
  269. if (sortInfo == null) {
  270. continue;
  271. }
  272. if (!foundSortedColumn) {
  273. foundSortedColumn = true;
  274. } else {
  275. // two columns were sorted
  276. return false;
  277. }
  278. }
  279. return foundSortedColumn;
  280. }
  281. private void sortBy(String column) {
  282. selectMenuPath("Component", "State", "Sort by column", column);
  283. }
  284. private void assertLastSortIsUserOriginated(boolean isUserOriginated) {
  285. List<WebElement> userOriginatedMessages = getDriver()
  286. .findElements(
  287. By.xpath("//*[contains(text(),'SortOrderChangeEvent: isUserOriginated')]"));
  288. Collections.sort(userOriginatedMessages, new Comparator<WebElement>() {
  289. @Override
  290. public int compare(WebElement o1, WebElement o2) {
  291. return o1.getText().compareTo(o2.getText());
  292. }
  293. });
  294. String newestEntry = userOriginatedMessages.get(
  295. userOriginatedMessages.size() - 1).getText();
  296. String[] parts = newestEntry.split(" ");
  297. boolean wasUserOriginated = Boolean
  298. .parseBoolean(parts[parts.length - 1]);
  299. if (isUserOriginated) {
  300. assertTrue("expected the sort to be user originated, but wasn't",
  301. wasUserOriginated);
  302. } else {
  303. assertFalse(
  304. "expected the sort not to be user originated, but it was",
  305. wasUserOriginated);
  306. }
  307. }
  308. }