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.

GridDetailsClientTest.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright 2000-2016 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.client;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotEquals;
  20. import static org.junit.Assert.assertNotNull;
  21. import static org.junit.Assert.assertNull;
  22. import static org.junit.Assert.assertTrue;
  23. import static org.junit.Assert.fail;
  24. import java.util.List;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.openqa.selenium.NoSuchElementException;
  28. import org.openqa.selenium.WebElement;
  29. import com.vaadin.shared.ui.grid.Range;
  30. import com.vaadin.testbench.By;
  31. import com.vaadin.testbench.ElementQuery;
  32. import com.vaadin.testbench.TestBenchElement;
  33. import com.vaadin.testbench.customelements.FixedNotificationElement;
  34. import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
  35. import com.vaadin.v7.shared.ui.grid.ScrollDestination;
  36. public class GridDetailsClientTest extends GridBasicClientFeaturesTest {
  37. private static final String[] SET_GENERATOR = new String[] { "Component",
  38. "Row details", "Set generator" };
  39. private static final String[] SET_FAULTY_GENERATOR = new String[] {
  40. "Component", "Row details", "Set faulty generator" };
  41. private static final String[] SET_EMPTY_GENERATOR = new String[] {
  42. "Component", "Row details", "Set empty generator" };
  43. @Before
  44. public void setUp() {
  45. setDebug(true);
  46. openTestURL();
  47. }
  48. @Test(expected = NoSuchElementException.class)
  49. public void noDetailsByDefault() {
  50. assertNull("details for row 1 should not exist at the start",
  51. getGridElement().getDetails(1));
  52. }
  53. @Test(expected = NoSuchElementException.class)
  54. public void nullRendererDoesNotShowDetailsPlaceholder() {
  55. toggleDetailsFor(1);
  56. getGridElement().getDetails(1);
  57. }
  58. @Test
  59. public void applyRendererThenOpenDetails() {
  60. selectMenuPath(SET_GENERATOR);
  61. toggleDetailsFor(1);
  62. TestBenchElement details = getGridElement().getDetails(1);
  63. assertTrue("Unexpected details content",
  64. details.getText().startsWith("Row: 1."));
  65. }
  66. @Test(expected = NoSuchElementException.class)
  67. public void openDetailsThenAppyRendererShouldNotShowDetails() {
  68. toggleDetailsFor(1);
  69. selectMenuPath(SET_GENERATOR);
  70. getGridElement().getDetails(1);
  71. }
  72. @Test
  73. public void openHiddenDetailsThenScrollToIt() {
  74. try {
  75. getGridElement().getDetails(100);
  76. fail("details row for 100 was apparently found, while it shouldn't have been.");
  77. } catch (NoSuchElementException e) {
  78. // expected
  79. }
  80. selectMenuPath(SET_GENERATOR);
  81. toggleDetailsFor(100);
  82. // scroll a bit beyond so we see below.
  83. getGridElement().scrollToRow(101);
  84. TestBenchElement details = getGridElement().getDetails(100);
  85. assertTrue("Unexpected details content",
  86. details.getText().startsWith("Row: 100."));
  87. }
  88. @Test
  89. public void errorUpdaterShowsErrorNotification() {
  90. assertFalse("No notifications should've been at the start",
  91. $(FixedNotificationElement.class).exists());
  92. selectMenuPath(SET_FAULTY_GENERATOR);
  93. toggleDetailsFor(1);
  94. ElementQuery<FixedNotificationElement> notification = $(
  95. FixedNotificationElement.class);
  96. assertTrue("Was expecting an error notification here",
  97. notification.exists());
  98. notification.first().close();
  99. assertEquals("The error details element should be empty", "",
  100. getGridElement().getDetails(1).getText());
  101. }
  102. @Test(expected = NoSuchElementException.class)
  103. public void detailsClosedWhenResettingGenerator() {
  104. selectMenuPath(SET_GENERATOR);
  105. toggleDetailsFor(1);
  106. selectMenuPath(SET_FAULTY_GENERATOR);
  107. getGridElement().getDetails(1);
  108. }
  109. @Test
  110. public void settingNewGeneratorStillWorksAfterError() {
  111. selectMenuPath(SET_FAULTY_GENERATOR);
  112. toggleDetailsFor(1);
  113. $(FixedNotificationElement.class).first().close();
  114. toggleDetailsFor(1);
  115. selectMenuPath(SET_GENERATOR);
  116. toggleDetailsFor(1);
  117. assertNotEquals("New details should've been generated even after error",
  118. "", getGridElement().getDetails(1).getText());
  119. }
  120. @Test
  121. public void updaterRendersExpectedWidgets() {
  122. selectMenuPath(SET_GENERATOR);
  123. toggleDetailsFor(1);
  124. TestBenchElement detailsElement = getGridElement().getDetails(1);
  125. assertNotNull(detailsElement.findElement(By.className("gwt-Label")));
  126. assertNotNull(detailsElement.findElement(By.className("gwt-Button")));
  127. }
  128. @Test
  129. public void widgetsInUpdaterWorkAsExpected() {
  130. selectMenuPath(SET_GENERATOR);
  131. toggleDetailsFor(1);
  132. TestBenchElement detailsElement = getGridElement().getDetails(1);
  133. WebElement button = detailsElement
  134. .findElement(By.className("gwt-Button"));
  135. button.click();
  136. WebElement label = detailsElement
  137. .findElement(By.className("gwt-Label"));
  138. assertEquals("clicked", label.getText());
  139. }
  140. @Test
  141. public void emptyGenerator() {
  142. selectMenuPath(SET_EMPTY_GENERATOR);
  143. toggleDetailsFor(1);
  144. assertEquals("empty generator did not produce an empty details row", "",
  145. getGridElement().getDetails(1).getText());
  146. }
  147. @Test(expected = NoSuchElementException.class)
  148. public void removeDetailsRow() {
  149. selectMenuPath(SET_GENERATOR);
  150. toggleDetailsFor(1);
  151. toggleDetailsFor(1);
  152. getGridElement().getDetails(1);
  153. }
  154. @Test
  155. public void rowElementClassNames() {
  156. selectMenuPath(SET_GENERATOR);
  157. toggleDetailsFor(0);
  158. toggleDetailsFor(1);
  159. List<WebElement> elements = getGridElement()
  160. .findElements(By.className("v-grid-spacer"));
  161. assertEquals("v-grid-spacer", elements.get(0).getAttribute("class"));
  162. assertEquals("v-grid-spacer stripe",
  163. elements.get(1).getAttribute("class"));
  164. }
  165. @Test
  166. public void scrollDownToRowWithDetails() {
  167. selectMenuPath(SET_GENERATOR);
  168. toggleDetailsFor(100);
  169. scrollToRow(100, ScrollDestination.ANY);
  170. Range validScrollRange = Range.between(1691, 1706);
  171. assertTrue(validScrollRange.contains(getGridVerticalScrollPos()));
  172. }
  173. @Test
  174. public void scrollUpToRowWithDetails() {
  175. selectMenuPath(SET_GENERATOR);
  176. toggleDetailsFor(100);
  177. scrollGridVerticallyTo(999999);
  178. scrollToRow(100, ScrollDestination.ANY);
  179. Range validScrollRange = Range.between(1981, 2001);
  180. assertTrue(validScrollRange.contains(getGridVerticalScrollPos()));
  181. }
  182. @Test
  183. public void cannotScrollBeforeTop() {
  184. selectMenuPath(SET_GENERATOR);
  185. toggleDetailsFor(1);
  186. scrollToRow(0, ScrollDestination.END);
  187. assertEquals(0, getGridVerticalScrollPos());
  188. }
  189. @Test
  190. public void cannotScrollAfterBottom() {
  191. selectMenuPath(SET_GENERATOR);
  192. toggleDetailsFor(999);
  193. scrollToRow(999, ScrollDestination.START);
  194. Range expectedRange = Range.withLength(19671, 20);
  195. assertTrue(expectedRange.contains(getGridVerticalScrollPos()));
  196. }
  197. private void scrollToRow(int rowIndex, ScrollDestination destination) {
  198. selectMenuPath(new String[] { "Component", "State", "Scroll to...",
  199. "Row " + rowIndex + "...", "Destination " + destination });
  200. }
  201. private void toggleDetailsFor(int rowIndex) {
  202. selectMenuPath(new String[] { "Component", "Row details",
  203. "Toggle details for...", "Row " + rowIndex });
  204. }
  205. }