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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. GridCellElement header = grid.getHeaderCell(0, 9);
  117. header.click();
  118. assertLastSortIsUserOriginated(true);
  119. assertColumnsAreSortedAs(_(9, 1, SortDirection.ASCENDING));
  120. grid.getHeaderCell(0, 9).click();
  121. assertColumnsAreSortedAs(_(9, 1, SortDirection.DESCENDING));
  122. // First cells for first 3 rows are (9, 0), (99, 0) and (999, 0)
  123. String row = "";
  124. for (int i = 0; i < 3; ++i) {
  125. row += "9";
  126. String expected = "(" + row + ", 0)";
  127. String actual = grid.getCell(i, 0).getText();
  128. assertEquals("Grid is not sorted by Column 9"
  129. + " using descending direction.", expected, actual);
  130. }
  131. selectMenuPath("Component", "Columns", "Column 10", "Column 10 Width",
  132. "Auto");
  133. // Column 10 is random numbers from Random with seed 13334
  134. // Click header to sort ascending
  135. grid.getHeaderCell(0, 10).click();
  136. assertColumnsAreSortedAs(_(10, 1, SortDirection.ASCENDING));
  137. for (int i = 0; i < 5; ++i) {
  138. Integer firstRow = Integer.valueOf(grid.getCell(i + 1, 10)
  139. .getText());
  140. Integer secondRow = Integer.valueOf(grid.getCell(i, 10).getText());
  141. assertGreater(
  142. "Grid is not sorted by Column 10 using ascending direction",
  143. firstRow, secondRow);
  144. }
  145. selectMenuPath("Component", "Columns", "Column 7", "Column 7 Width",
  146. "Auto");
  147. // Column 7 is row index as a number. Last three row are original rows
  148. // 2, 1 and 0.
  149. // Click header twice to sort descending
  150. grid.getHeaderCell(0, 7).click();
  151. assertColumnsAreSortedAs(_(7, 1, SortDirection.ASCENDING));
  152. grid.getHeaderCell(0, 7).click();
  153. assertColumnsAreSortedAs(_(7, 1, SortDirection.DESCENDING));
  154. for (int i = 0; i < 3; ++i) {
  155. assertEquals(
  156. "Grid is not sorted by Column 7 using descending direction",
  157. "(" + i + ", 0)",
  158. grid.getCell(GridBasicFeatures.ROWS - (i + 1), 0).getText());
  159. }
  160. }
  161. private void sendKey(Keys seq) {
  162. new Actions(getDriver()).sendKeys(seq).perform();
  163. }
  164. private void holdKey(Keys key) {
  165. new Actions(getDriver()).keyDown(key).perform();
  166. }
  167. private void releaseKey(Keys key) {
  168. new Actions(getDriver()).keyUp(key).perform();
  169. }
  170. @Test
  171. public void testKeyboardSorting() {
  172. openTestURL();
  173. /*
  174. * We can't click on the header directly, since it will sort the header
  175. * immediately. We need to focus some other column first, and only then
  176. * navigate there.
  177. */
  178. getGridElement().getCell(0, 0).click();
  179. sendKey(Keys.ARROW_UP);
  180. // Sort ASCENDING on first column
  181. sendKey(Keys.ENTER);
  182. assertLastSortIsUserOriginated(true);
  183. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING));
  184. // Move to next column
  185. sendKey(Keys.RIGHT);
  186. // Add this column to the existing sorting group
  187. holdKey(Keys.SHIFT);
  188. sendKey(Keys.ENTER);
  189. releaseKey(Keys.SHIFT);
  190. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
  191. _(2, SortDirection.ASCENDING));
  192. // Move to next column
  193. sendKey(Keys.RIGHT);
  194. // Add a third column to the sorting group
  195. holdKey(Keys.SHIFT);
  196. sendKey(Keys.ENTER);
  197. releaseKey(Keys.SHIFT);
  198. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
  199. _(2, SortDirection.ASCENDING), _(3, SortDirection.ASCENDING));
  200. // Move back to the second column
  201. sendKey(Keys.LEFT);
  202. // Change sort direction of the second column to DESCENDING
  203. holdKey(Keys.SHIFT);
  204. sendKey(Keys.ENTER);
  205. releaseKey(Keys.SHIFT);
  206. assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
  207. _(2, SortDirection.DESCENDING), _(3, SortDirection.ASCENDING));
  208. // Move back to the third column
  209. sendKey(Keys.RIGHT);
  210. // Set sorting to third column, ASCENDING
  211. sendKey(Keys.ENTER);
  212. assertColumnsAreSortedAs(_(2, 1, SortDirection.ASCENDING));
  213. // Move to the fourth column
  214. sendKey(Keys.RIGHT);
  215. // Make sure that single-column sorting also works as expected
  216. sendKey(Keys.ENTER);
  217. assertColumnsAreSortedAs(_(3, 1, SortDirection.ASCENDING));
  218. }
  219. private void assertColumnsAreSortedAs(SortInfoWithColumn... sortInfos) {
  220. for (SortInfoWithColumn sortInfo : sortInfos) {
  221. assertSort(sortInfo, sortInfo.columnIndex,
  222. onlyOneColumnIsSorted(sortInfos));
  223. }
  224. }
  225. /**
  226. * @param sortDirections
  227. * <code>null</code> if not interested in that index, otherwise a
  228. * direction that the column needs to be sorted as
  229. */
  230. private void assertColumnsAreSortedAs(SortInfo... sortInfos) {
  231. for (int column = 0; column < sortInfos.length; column++) {
  232. SortInfo sortInfo = sortInfos[column];
  233. assertSort(sortInfo, column, onlyOneColumnIsSorted(sortInfos));
  234. }
  235. }
  236. private void assertSort(SortInfo sortInfo, int column,
  237. boolean onlyOneColumnIsSorted) {
  238. if (sortInfo == null) {
  239. return;
  240. }
  241. GridCellElement headerCell = getGridElement().getHeaderCell(0, column);
  242. String classValue = headerCell.getAttribute("class");
  243. boolean isSortedAscending = sortInfo.sortDirection == SortDirection.ASCENDING
  244. && classValue.contains("sort-asc");
  245. boolean isSortedDescending = sortInfo.sortDirection == SortDirection.DESCENDING
  246. && classValue.contains("sort-desc");
  247. if (isSortedAscending || isSortedDescending) {
  248. String sortOrderAttribute = headerCell.getAttribute("sort-order");
  249. if (sortOrderAttribute == null) {
  250. if (!(sortInfo.sortOrder == 1 && onlyOneColumnIsSorted)) {
  251. fail("missing sort-order element attribute from column "
  252. + column);
  253. }
  254. } else {
  255. assertEquals("sort order was not as expected",
  256. String.valueOf(sortInfo.sortOrder), sortOrderAttribute);
  257. }
  258. } else {
  259. fail("column index " + column + " was not sorted as "
  260. + sortInfo.sortDirection + " (class: " + classValue + ")");
  261. }
  262. }
  263. private static boolean onlyOneColumnIsSorted(SortInfo[] sortInfos) {
  264. boolean foundSortedColumn = false;
  265. for (SortInfo sortInfo : sortInfos) {
  266. if (sortInfo == null) {
  267. continue;
  268. }
  269. if (!foundSortedColumn) {
  270. foundSortedColumn = true;
  271. } else {
  272. // two columns were sorted
  273. return false;
  274. }
  275. }
  276. return foundSortedColumn;
  277. }
  278. private void sortBy(String column) {
  279. selectMenuPath("Component", "State", "Sort by column", column);
  280. }
  281. private void assertLastSortIsUserOriginated(boolean isUserOriginated) {
  282. List<WebElement> userOriginatedMessages = getDriver()
  283. .findElements(
  284. By.xpath("//*[contains(text(),'SortOrderChangeEvent: isUserOriginated')]"));
  285. Collections.sort(userOriginatedMessages, new Comparator<WebElement>() {
  286. @Override
  287. public int compare(WebElement o1, WebElement o2) {
  288. return o1.getText().compareTo(o2.getText());
  289. }
  290. });
  291. String newestEntry = userOriginatedMessages.get(
  292. userOriginatedMessages.size() - 1).getText();
  293. String[] parts = newestEntry.split(" ");
  294. boolean wasUserOriginated = Boolean
  295. .parseBoolean(parts[parts.length - 1]);
  296. if (isUserOriginated) {
  297. assertTrue("expected the sort to be user originated, but wasn't",
  298. wasUserOriginated);
  299. } else {
  300. assertFalse(
  301. "expected the sort not to be user originated, but it was",
  302. wasUserOriginated);
  303. }
  304. }
  305. }