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.

GridScrollDestinationTest.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.vaadin.tests.components.grid;
  2. import static org.hamcrest.number.IsCloseTo.closeTo;
  3. import static org.junit.Assert.assertEquals;
  4. import static org.junit.Assert.assertThat;
  5. import java.util.List;
  6. import org.junit.Test;
  7. import org.openqa.selenium.WebElement;
  8. import com.vaadin.shared.ui.grid.ScrollDestination;
  9. import com.vaadin.testbench.By;
  10. import com.vaadin.testbench.TestBenchElement;
  11. import com.vaadin.testbench.elements.ButtonElement;
  12. import com.vaadin.testbench.elements.GridElement;
  13. import com.vaadin.testbench.elements.NativeSelectElement;
  14. import com.vaadin.tests.tb3.SingleBrowserTest;
  15. public class GridScrollDestinationTest extends SingleBrowserTest {
  16. private ButtonElement button;
  17. private GridElement grid;
  18. private TestBenchElement header;
  19. private TestBenchElement tableWrapper;
  20. @Override
  21. public void setup() throws Exception {
  22. super.setup();
  23. openTestURL();
  24. button = $(ButtonElement.class).first();
  25. grid = $(GridElement.class).first();
  26. header = grid.getHeader();
  27. tableWrapper = grid.getTableWrapper();
  28. }
  29. private void assertElementAtTop(WebElement row) {
  30. assertThat((double) row.getLocation().getY(), closeTo(
  31. header.getLocation().getY() + header.getSize().getHeight(),
  32. 1d));
  33. }
  34. private void assertElementAtBottom(WebElement row) {
  35. assertThat(
  36. (double) row.getLocation().getY() + row.getSize().getHeight(),
  37. closeTo((double) tableWrapper.getLocation().getY()
  38. + tableWrapper.getSize().getHeight(), 1d));
  39. }
  40. private void assertElementAtMiddle(WebElement row) {
  41. assertThat((double) row.getLocation()
  42. .getY() + (row.getSize().getHeight() / 2), closeTo(
  43. (double) tableWrapper.getLocation().getY()
  44. + header.getSize().getHeight()
  45. + ((tableWrapper.getSize().getHeight()
  46. - header.getSize().getHeight()) / 2),
  47. 1d));
  48. }
  49. @Test
  50. public void destinationAny() {
  51. // ScrollDestination.ANY selected by default
  52. // scroll down
  53. button.click();
  54. // expect the row at the bottom of the viewport
  55. List<WebElement> rows = grid.getBody()
  56. .findElements(By.className("v-grid-row"));
  57. // last rendered row is a buffer row, inspect second to last
  58. WebElement row = rows.get(rows.size() - 2);
  59. assertEquals("50", row.getText());
  60. assertElementAtBottom(row);
  61. // scroll to end
  62. grid.scrollToRow((int) grid.getRowCount() - 1);
  63. // ensure row 50 is out of visual range, first two rows are out of view
  64. // and getText can't find the contents so inspect the third row
  65. rows = grid.getBody().findElements(By.className("v-grid-row"));
  66. row = rows.get(2);
  67. assertGreater(row.getText() + " is not greater than 52",
  68. Integer.valueOf(row.getText()), 52);
  69. // scroll up
  70. button.click();
  71. // expect the row at the top of the viewport
  72. rows = grid.getBody().findElements(By.className("v-grid-row"));
  73. // first rendered row is a buffer row, inspect second
  74. row = rows.get(1);
  75. assertEquals("50", row.getText());
  76. assertElementAtTop(row);
  77. // scroll up by a few rows
  78. grid.scrollToRow(45);
  79. // refresh row references
  80. rows = grid.getBody().findElements(By.className("v-grid-row"));
  81. row = rows.get(6);
  82. assertEquals("50", row.getText());
  83. // scroll while already within viewport
  84. button.click();
  85. // expect no change since the row is still within viewport
  86. rows = grid.getBody().findElements(By.className("v-grid-row"));
  87. row = rows.get(6);
  88. assertEquals("50", row.getText());
  89. }
  90. @Test
  91. public void destinationEnd() {
  92. $(NativeSelectElement.class).first()
  93. .selectByText(ScrollDestination.END.name());
  94. // scroll down
  95. button.click();
  96. // expect the row at the bottom of the viewport
  97. List<WebElement> rows = grid.getBody()
  98. .findElements(By.className("v-grid-row"));
  99. // last rendered row is a buffer row, inspect second to last
  100. WebElement row = rows.get(rows.size() - 2);
  101. assertEquals("50", row.getText());
  102. assertElementAtBottom(row);
  103. // scroll to end
  104. grid.scrollToRow((int) grid.getRowCount() - 1);
  105. // ensure row 50 is out of visual range, first two rows are out of view
  106. // and getText can't find the contents so inspect the third row
  107. rows = grid.getBody().findElements(By.className("v-grid-row"));
  108. row = rows.get(2);
  109. assertGreater(row.getText() + " is not greater than 52",
  110. Integer.valueOf(row.getText()), 52);
  111. // scroll up
  112. button.click();
  113. // expect the row at the bottom of the viewport
  114. rows = grid.getBody().findElements(By.className("v-grid-row"));
  115. // last rendered row is a buffer row, inspect second to last
  116. row = rows.get(rows.size() - 2);
  117. assertEquals("50", row.getText());
  118. assertElementAtBottom(row);
  119. // scroll down by a few rows
  120. grid.scrollToRow(55);
  121. // refresh row references
  122. rows = grid.getBody().findElements(By.className("v-grid-row"));
  123. row = rows.get(rows.size() - 7);
  124. assertEquals("50", row.getText());
  125. // scroll while already within viewport
  126. button.click();
  127. // expect the row at the bottom of the viewport again
  128. rows = grid.getBody().findElements(By.className("v-grid-row"));
  129. row = rows.get(rows.size() - 2);
  130. assertEquals("50", row.getText());
  131. assertElementAtBottom(row);
  132. }
  133. @Test
  134. public void destinationStart() {
  135. $(NativeSelectElement.class).first()
  136. .selectByText(ScrollDestination.START.name());
  137. // scroll down
  138. button.click();
  139. // expect the row at the top of the viewport
  140. List<WebElement> rows = grid.getBody()
  141. .findElements(By.className("v-grid-row"));
  142. // first rendered row is a buffer row, inspect second
  143. WebElement row = rows.get(1);
  144. assertEquals("50", row.getText());
  145. assertElementAtTop(row);
  146. // scroll to end
  147. grid.scrollToRow((int) grid.getRowCount() - 1);
  148. // ensure row 50 is out of visual range, first two rows are out of view
  149. // and getText can't find the contents so inspect the third row
  150. rows = grid.getBody().findElements(By.className("v-grid-row"));
  151. row = rows.get(2);
  152. assertGreater(row.getText() + " is not greater than 52",
  153. Integer.valueOf(row.getText()), 52);
  154. // scroll up
  155. button.click();
  156. // expect the row at the top of the viewport
  157. rows = grid.getBody().findElements(By.className("v-grid-row"));
  158. // first rendered row is a buffer row, inspect second
  159. row = rows.get(1);
  160. assertEquals("50", row.getText());
  161. assertElementAtTop(row);
  162. // scroll up by a few rows
  163. grid.scrollToRow(45);
  164. // refresh row references
  165. rows = grid.getBody().findElements(By.className("v-grid-row"));
  166. row = rows.get(6);
  167. assertEquals("50", row.getText());
  168. // scroll while already within viewport
  169. button.click();
  170. // expect the row at the top of the viewport again
  171. rows = grid.getBody().findElements(By.className("v-grid-row"));
  172. row = rows.get(1);
  173. assertEquals("50", row.getText());
  174. assertElementAtTop(row);
  175. }
  176. @Test
  177. public void destinationMiddle() {
  178. NativeSelectElement destinationSelect = $(NativeSelectElement.class)
  179. .first();
  180. destinationSelect.selectByText(ScrollDestination.MIDDLE.name());
  181. // scroll down
  182. button.click();
  183. // expect the row at the middle of the viewport
  184. List<WebElement> rows = grid.getBody()
  185. .findElements(By.className("v-grid-row"));
  186. // inspect the middle row
  187. WebElement row = rows.get(rows.size() / 2);
  188. assertEquals("50", row.getText());
  189. assertElementAtMiddle(row);
  190. // scroll to end
  191. grid.scrollToRow((int) grid.getRowCount() - 1);
  192. // ensure row 50 is out of visual range, first two rows are out of view
  193. // and getText can't find the contents so inspect the third row
  194. rows = grid.getBody().findElements(By.className("v-grid-row"));
  195. row = rows.get(2);
  196. assertGreater(row.getText() + " is not greater than 52",
  197. Integer.valueOf(row.getText()), 52);
  198. // scroll up
  199. button.click();
  200. // expect the row at the middle of the viewport
  201. rows = grid.getBody().findElements(By.className("v-grid-row"));
  202. // first rendered row is a buffer row, inspect second
  203. row = rows.get(rows.size() / 2);
  204. assertEquals("50", row.getText());
  205. assertElementAtMiddle(row);
  206. // scroll down by a few rows
  207. destinationSelect.selectByText(ScrollDestination.START.name());
  208. button.click();
  209. // refresh row references
  210. rows = grid.getBody().findElements(By.className("v-grid-row"));
  211. row = rows.get(1);
  212. assertEquals("50", row.getText());
  213. // scroll while already within viewport
  214. destinationSelect.selectByText(ScrollDestination.MIDDLE.name());
  215. button.click();
  216. // expect the row at the top of the viewport again
  217. rows = grid.getBody().findElements(By.className("v-grid-row"));
  218. row = rows.get(rows.size() / 2);
  219. assertEquals("50", row.getText());
  220. assertElementAtMiddle(row);
  221. }
  222. }