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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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.testbench.elements.TextFieldElement;
  15. import com.vaadin.tests.tb3.SingleBrowserTest;
  16. public class GridScrollDestinationTest extends SingleBrowserTest {
  17. private TextFieldElement textField;
  18. private ButtonElement button;
  19. private GridElement grid;
  20. private TestBenchElement header;
  21. private TestBenchElement tableWrapper;
  22. @Override
  23. public void setup() throws Exception {
  24. super.setup();
  25. openTestURL();
  26. textField = $(TextFieldElement.class).first();
  27. button = $(ButtonElement.class).first();
  28. grid = $(GridElement.class).first();
  29. header = grid.getHeader();
  30. tableWrapper = grid.getTableWrapper();
  31. }
  32. private void assertElementAtTop(WebElement row) {
  33. assertThat((double) row.getLocation().getY(), closeTo(
  34. header.getLocation().getY() + header.getSize().getHeight(),
  35. 1d));
  36. }
  37. private void assertElementAtBottom(WebElement row) {
  38. assertThat(
  39. (double) row.getLocation().getY() + row.getSize().getHeight(),
  40. closeTo((double) tableWrapper.getLocation().getY()
  41. + tableWrapper.getSize().getHeight(), 1d));
  42. }
  43. private void assertElementAtMiddle(WebElement row) {
  44. assertThat((double) row.getLocation()
  45. .getY() + (row.getSize().getHeight() / 2), closeTo(
  46. (double) tableWrapper.getLocation().getY()
  47. + header.getSize().getHeight()
  48. + ((tableWrapper.getSize().getHeight()
  49. - header.getSize().getHeight()) / 2),
  50. 1d));
  51. }
  52. @Test
  53. public void destinationAny() {
  54. // ScrollDestination.ANY selected by default
  55. // scroll down
  56. button.click();
  57. // expect the row at the bottom of the viewport
  58. List<WebElement> rows = grid.getBody()
  59. .findElements(By.className("v-grid-row"));
  60. // last rendered row is a buffer row, inspect second to last
  61. WebElement row = rows.get(rows.size() - 2);
  62. assertEquals("50", row.getText());
  63. assertElementAtBottom(row);
  64. // scroll to end
  65. grid.scrollToRow((int) grid.getRowCount() - 1);
  66. // ensure row 50 is out of visual range, first two rows are out of view
  67. // and getText can't find the contents so inspect the third row
  68. rows = grid.getBody().findElements(By.className("v-grid-row"));
  69. row = rows.get(2);
  70. assertGreater(row.getText() + " is not greater than 52",
  71. Integer.valueOf(row.getText()), 52);
  72. // scroll up
  73. button.click();
  74. // expect the row at the top of the viewport
  75. rows = grid.getBody().findElements(By.className("v-grid-row"));
  76. // first rendered row is a buffer row, inspect second
  77. row = rows.get(1);
  78. assertEquals("50", row.getText());
  79. assertElementAtTop(row);
  80. // scroll up by a few rows
  81. grid.scrollToRow(45);
  82. // refresh row references
  83. rows = grid.getBody().findElements(By.className("v-grid-row"));
  84. row = rows.get(6);
  85. assertEquals("50", row.getText());
  86. // scroll while already within viewport
  87. button.click();
  88. // expect no change since the row is still within viewport
  89. rows = grid.getBody().findElements(By.className("v-grid-row"));
  90. row = rows.get(6);
  91. assertEquals("50", row.getText());
  92. // scroll to beginning using scroll destination
  93. textField.setValue("0");
  94. button.click();
  95. // expect to be scrolled all the way up
  96. rows = grid.getBody().findElements(By.className("v-grid-row"));
  97. row = rows.get(0);
  98. assertEquals("0", row.getText());
  99. assertElementAtTop(row);
  100. // scroll to end using scroll destination
  101. textField.setValue("99");
  102. button.click();
  103. // expect to be scrolled all the way down
  104. rows = grid.getBody().findElements(By.className("v-grid-row"));
  105. row = rows.get(rows.size() - 1);
  106. assertEquals("99", row.getText());
  107. assertElementAtBottom(row);
  108. }
  109. @Test
  110. public void destinationEnd() {
  111. $(NativeSelectElement.class).first()
  112. .selectByText(ScrollDestination.END.name());
  113. // scroll down
  114. button.click();
  115. // expect the row at the bottom of the viewport
  116. List<WebElement> rows = grid.getBody()
  117. .findElements(By.className("v-grid-row"));
  118. // last rendered row is a buffer row, inspect second to last
  119. WebElement row = rows.get(rows.size() - 2);
  120. assertEquals("50", row.getText());
  121. assertElementAtBottom(row);
  122. // scroll to end
  123. grid.scrollToRow((int) grid.getRowCount() - 1);
  124. // ensure row 50 is out of visual range, first two rows are out of view
  125. // and getText can't find the contents so inspect the third row
  126. rows = grid.getBody().findElements(By.className("v-grid-row"));
  127. row = rows.get(2);
  128. assertGreater(row.getText() + " is not greater than 52",
  129. Integer.valueOf(row.getText()), 52);
  130. // scroll up
  131. button.click();
  132. // expect the row at the bottom of the viewport
  133. rows = grid.getBody().findElements(By.className("v-grid-row"));
  134. // last rendered row is a buffer row, inspect second to last
  135. row = rows.get(rows.size() - 2);
  136. assertEquals("50", row.getText());
  137. assertElementAtBottom(row);
  138. // scroll down by a few rows
  139. grid.scrollToRow(55);
  140. // refresh row references
  141. rows = grid.getBody().findElements(By.className("v-grid-row"));
  142. row = rows.get(rows.size() - 7);
  143. assertEquals("50", row.getText());
  144. // scroll while already within viewport
  145. button.click();
  146. // expect the row at the bottom of the viewport again
  147. rows = grid.getBody().findElements(By.className("v-grid-row"));
  148. row = rows.get(rows.size() - 2);
  149. assertEquals("50", row.getText());
  150. assertElementAtBottom(row);
  151. // scroll to beginning using scroll destination
  152. textField.setValue("0");
  153. button.click();
  154. button.click();
  155. // expect to be scrolled all the way up
  156. rows = grid.getBody().findElements(By.className("v-grid-row"));
  157. row = rows.get(0);
  158. assertEquals("0", row.getText());
  159. assertElementAtTop(row);
  160. // scroll to end using scroll destination
  161. textField.setValue("99");
  162. button.click();
  163. // expect to be scrolled all the way down
  164. rows = grid.getBody().findElements(By.className("v-grid-row"));
  165. row = rows.get(rows.size() - 1);
  166. assertEquals("99", row.getText());
  167. assertElementAtBottom(row);
  168. }
  169. @Test
  170. public void destinationStart() {
  171. $(NativeSelectElement.class).first()
  172. .selectByText(ScrollDestination.START.name());
  173. // scroll down
  174. button.click();
  175. // expect the row at the top of the viewport
  176. List<WebElement> rows = grid.getBody()
  177. .findElements(By.className("v-grid-row"));
  178. // first rendered row is a buffer row, inspect second
  179. WebElement row = rows.get(1);
  180. assertEquals("50", row.getText());
  181. assertElementAtTop(row);
  182. // scroll to end
  183. grid.scrollToRow((int) grid.getRowCount() - 1);
  184. // ensure row 50 is out of visual range, first two rows are out of view
  185. // and getText can't find the contents so inspect the third row
  186. rows = grid.getBody().findElements(By.className("v-grid-row"));
  187. row = rows.get(2);
  188. assertGreater(row.getText() + " is not greater than 52",
  189. Integer.valueOf(row.getText()), 52);
  190. // scroll up
  191. button.click();
  192. // expect the row at the top of the viewport
  193. rows = grid.getBody().findElements(By.className("v-grid-row"));
  194. // first rendered row is a buffer row, inspect second
  195. row = rows.get(1);
  196. assertEquals("50", row.getText());
  197. assertElementAtTop(row);
  198. // scroll up by a few rows
  199. grid.scrollToRow(45);
  200. // refresh row references
  201. rows = grid.getBody().findElements(By.className("v-grid-row"));
  202. row = rows.get(6);
  203. assertEquals("50", row.getText());
  204. // scroll while already within viewport
  205. button.click();
  206. // expect the row at the top of the viewport again
  207. rows = grid.getBody().findElements(By.className("v-grid-row"));
  208. row = rows.get(1);
  209. assertEquals("50", row.getText());
  210. assertElementAtTop(row);
  211. // scroll to beginning using scroll destination
  212. textField.setValue("0");
  213. button.click();
  214. // expect to be scrolled all the way up
  215. rows = grid.getBody().findElements(By.className("v-grid-row"));
  216. row = rows.get(0);
  217. assertEquals("0", row.getText());
  218. assertElementAtTop(row);
  219. // scroll to end using scroll destination
  220. textField.setValue("99");
  221. button.click();
  222. // expect to be scrolled all the way down
  223. rows = grid.getBody().findElements(By.className("v-grid-row"));
  224. row = rows.get(rows.size() - 1);
  225. assertEquals("99", row.getText());
  226. assertElementAtBottom(row);
  227. }
  228. @Test
  229. public void destinationMiddle() {
  230. NativeSelectElement destinationSelect = $(NativeSelectElement.class)
  231. .first();
  232. destinationSelect.selectByText(ScrollDestination.MIDDLE.name());
  233. // scroll down
  234. button.click();
  235. // expect the row at the middle of the viewport
  236. List<WebElement> rows = grid.getBody()
  237. .findElements(By.className("v-grid-row"));
  238. // inspect the middle row
  239. WebElement row = rows.get(rows.size() / 2);
  240. assertEquals("50", row.getText());
  241. assertElementAtMiddle(row);
  242. // scroll to end
  243. grid.scrollToRow((int) grid.getRowCount() - 1);
  244. // ensure row 50 is out of visual range, first two rows are out of view
  245. // and getText can't find the contents so inspect the third row
  246. rows = grid.getBody().findElements(By.className("v-grid-row"));
  247. row = rows.get(2);
  248. assertGreater(row.getText() + " is not greater than 52",
  249. Integer.valueOf(row.getText()), 52);
  250. // scroll up
  251. button.click();
  252. // expect the row at the middle of the viewport
  253. rows = grid.getBody().findElements(By.className("v-grid-row"));
  254. // first rendered row is a buffer row, inspect second
  255. row = rows.get(rows.size() / 2);
  256. assertEquals("50", row.getText());
  257. assertElementAtMiddle(row);
  258. // scroll down by a few rows
  259. destinationSelect.selectByText(ScrollDestination.START.name());
  260. button.click();
  261. // refresh row references
  262. rows = grid.getBody().findElements(By.className("v-grid-row"));
  263. row = rows.get(1);
  264. assertEquals("50", row.getText());
  265. // scroll while already within viewport
  266. destinationSelect.selectByText(ScrollDestination.MIDDLE.name());
  267. button.click();
  268. // expect the row at the top of the viewport again
  269. rows = grid.getBody().findElements(By.className("v-grid-row"));
  270. row = rows.get(rows.size() / 2);
  271. assertEquals("50", row.getText());
  272. assertElementAtMiddle(row);
  273. // scroll to beginning using scroll destination
  274. textField.setValue("0");
  275. button.click();
  276. // expect to be scrolled all the way up
  277. rows = grid.getBody().findElements(By.className("v-grid-row"));
  278. row = rows.get(0);
  279. assertEquals("0", row.getText());
  280. assertElementAtTop(row);
  281. // scroll to end using scroll destination
  282. textField.setValue("99");
  283. button.click();
  284. // expect to be scrolled all the way down
  285. rows = grid.getBody().findElements(By.className("v-grid-row"));
  286. row = rows.get(rows.size() - 1);
  287. assertEquals("99", row.getText());
  288. assertElementAtBottom(row);
  289. }
  290. }