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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.vaadin.tests.components.grid.basics;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7. import org.junit.Assert;
  8. import org.junit.Test;
  9. import org.openqa.selenium.Keys;
  10. import org.openqa.selenium.remote.DesiredCapabilities;
  11. import com.vaadin.testbench.parallel.Browser;
  12. public class GridSortingTest extends GridBasicsTest {
  13. public static final Comparator<DataObject> BIG_RANDOM = Comparator
  14. .comparing(DataObject::getBigRandom);
  15. public static final Comparator<DataObject> SMALL_RANDOM = Comparator
  16. .comparing(DataObject::getSmallRandom);
  17. @Override
  18. public List<DesiredCapabilities> getBrowsersToTest() {
  19. // Should be browsersSupportingShiftClick but for whatever reason IE11
  20. // fails to shift click
  21. return Collections
  22. .singletonList(Browser.CHROME.getDesiredCapabilities());
  23. }
  24. @Test
  25. public void testSortBySingleColumnByUser() {
  26. getGridElement().getHeaderCell(0, 6).click();
  27. int i = 0;
  28. for (Integer rowNumber : getTestData().sorted(BIG_RANDOM)
  29. .map(DataObject::getRowNumber).limit(5)
  30. .collect(Collectors.toList())) {
  31. Assert.assertEquals(
  32. "Grid was not sorted as expected, row number mismatch",
  33. rowNumber.toString(),
  34. getGridElement().getCell(i++, 3).getText());
  35. }
  36. }
  37. @Test
  38. public void testSortByMultipleColumnsByUser() {
  39. getGridElement().getHeaderCell(0, 7).click();
  40. getGridElement().getHeaderCell(0, 6).click(20, 20, Keys.SHIFT);
  41. int i = 0;
  42. for (Integer rowNumber : getTestData()
  43. .sorted(SMALL_RANDOM.thenComparing(BIG_RANDOM))
  44. .map(DataObject::getRowNumber).limit(5)
  45. .collect(Collectors.toList())) {
  46. Assert.assertEquals(
  47. "Grid was not sorted as expected, row number mismatch",
  48. rowNumber.toString(),
  49. getGridElement().getCell(i++, 3).getText());
  50. }
  51. }
  52. @Test
  53. public void serverSideOrderByColumn0() {
  54. selectMenuPath("Component", "Columns", "Column 0", "Sort ASC");
  55. Assert.assertEquals("1. SortEvent: isUserOriginated? false",
  56. getLogRow(0));
  57. Comparator<String> comparator = Comparator.naturalOrder();
  58. int i = 0;
  59. for (String coord : getTestData().map(DataObject::getCoordinates)
  60. .sorted(comparator).limit(5).collect(Collectors.toList())) {
  61. Assert.assertEquals(
  62. "Grid was not sorted as expected, row number mismatch",
  63. coord, getGridElement().getCell(i++, 0).getText());
  64. }
  65. // self-verification
  66. Assert.assertTrue(i > 0);
  67. selectMenuPath("Component", "Columns", "Column 0", "Sort DESC");
  68. Assert.assertEquals("2. SortEvent: isUserOriginated? false",
  69. getLogRow(0));
  70. i = 0;
  71. for (String coord : getTestData().map(DataObject::getCoordinates)
  72. .sorted(comparator.reversed()).limit(5)
  73. .collect(Collectors.toList())) {
  74. Assert.assertEquals(
  75. "Grid was not sorted as expected, row number mismatch",
  76. coord, getGridElement().getCell(i++, 0).getText());
  77. }
  78. }
  79. @Test
  80. public void serverSideOrderByDate() {
  81. selectMenuPath("Component", "Columns", "Date", "Sort ASC");
  82. Assert.assertEquals("1. SortEvent: isUserOriginated? false",
  83. getLogRow(0));
  84. Comparator<Date> comparator = Comparator.naturalOrder();
  85. int i = 0;
  86. for (Date date : getTestData().map(DataObject::getDate)
  87. .sorted(comparator).limit(5).collect(Collectors.toList())) {
  88. Assert.assertEquals(
  89. "Grid was not sorted as expected, row number mismatch",
  90. date.toString(),
  91. getGridElement().getCell(i++, 4).getText());
  92. }
  93. // self-verification
  94. Assert.assertTrue(i > 0);
  95. selectMenuPath("Component", "Columns", "Date", "Sort DESC");
  96. Assert.assertEquals("2. SortEvent: isUserOriginated? false",
  97. getLogRow(0));
  98. i = 0;
  99. for (Date date : getTestData().map(DataObject::getDate)
  100. .sorted(comparator.reversed()).limit(5)
  101. .collect(Collectors.toList())) {
  102. Assert.assertEquals(
  103. "Grid was not sorted as expected, row number mismatch",
  104. date.toString(),
  105. getGridElement().getCell(i++, 4).getText());
  106. }
  107. }
  108. @Test
  109. public void serverSideClearOrder() {
  110. selectMenuPath("Component", "Columns", "Column 0", "Sort ASC");
  111. selectMenuPath("Component", "Columns", "Clear sort");
  112. Assert.assertEquals("2. SortEvent: isUserOriginated? false",
  113. getLogRow(0));
  114. int i = 0;
  115. for (String coord : getTestData().map(DataObject::getCoordinates)
  116. .limit(5).collect(Collectors.toList())) {
  117. Assert.assertEquals(
  118. "Grid was not sorted as expected, row number mismatch",
  119. coord, getGridElement().getCell(i++, 0).getText());
  120. }
  121. // self-verification
  122. Assert.assertTrue(i > 0);
  123. }
  124. }