]> source.dussan.org Git - vaadin-framework.git/blob
759476c67aa644c18ca15ff387cc43dfc845e18b
[vaadin-framework.git] /
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.server;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openqa.selenium.NoSuchElementException;
27
28 import com.vaadin.testbench.By;
29 import com.vaadin.testbench.TestBenchElement;
30 import com.vaadin.testbench.elements.NotificationElement;
31 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
32
33 public class GridDetailsServerTest extends GridBasicFeaturesTest {
34     /**
35      * The reason to why last item details wasn't selected is that since it will
36      * exist only after the viewport has been scrolled into view, we wouldn't be
37      * able to scroll that particular details row into view, making tests
38      * awkward with two scroll commands back to back.
39      */
40     private static final int ALMOST_LAST_INDEX = 995;
41     private static final String[] OPEN_ALMOST_LAST_ITEM_DETAILS = new String[] {
42             "Component", "Details", "Open " + ALMOST_LAST_INDEX };
43     private static final String[] OPEN_FIRST_ITEM_DETAILS = new String[] {
44             "Component", "Details", "Open firstItemId" };
45     private static final String[] TOGGLE_FIRST_ITEM_DETAILS = new String[] {
46             "Component", "Details", "Toggle firstItemId" };
47     private static final String[] DETAILS_GENERATOR_NULL = new String[] {
48             "Component", "Details", "Generators", "NULL" };
49     private static final String[] DETAILS_GENERATOR_WATCHING = new String[] {
50             "Component", "Details", "Generators", "\"Watching\"" };
51     private static final String[] DETAILS_GENERATOR_PERSISTING = new String[] {
52             "Component", "Details", "Generators", "Persisting" };
53     private static final String[] CHANGE_HIERARCHY = new String[] { "Component",
54             "Details", "Generators", "- Change Component" };
55
56     @Before
57     public void setUp() {
58         openTestURL();
59     }
60
61     @Test(expected = NoSuchElementException.class)
62     public void openWithNoGenerator() {
63         try {
64             getGridElement().getDetails(0);
65             fail("Expected NoSuchElementException");
66         } catch (NoSuchElementException ignore) {
67             // expected
68         }
69         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
70         getGridElement().getDetails(0);
71     }
72
73     @Test
74     public void openVisiblePopulatedDetails() {
75         selectMenuPath(DETAILS_GENERATOR_WATCHING);
76         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
77         assertNotNull("details should've populated", getGridElement()
78                 .getDetails(0).findElement(By.className("v-widget")));
79     }
80
81     @Test(expected = NoSuchElementException.class)
82     public void closeVisiblePopulatedDetails() {
83         selectMenuPath(DETAILS_GENERATOR_WATCHING);
84         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
85         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
86         getGridElement().getDetails(0);
87     }
88
89     @Test
90     public void openDetailsOutsideOfActiveRange() throws InterruptedException {
91         getGridElement().scroll(10000);
92         selectMenuPath(DETAILS_GENERATOR_WATCHING);
93         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
94         getGridElement().scroll(0);
95         Thread.sleep(50);
96         assertNotNull("details should've been opened",
97                 getGridElement().getDetails(0));
98     }
99
100     @Test(expected = NoSuchElementException.class)
101     public void closeDetailsOutsideOfActiveRange() {
102         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
103         getGridElement().scroll(10000);
104         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
105         getGridElement().scroll(0);
106         getGridElement().getDetails(0);
107     }
108
109     @Test
110     public void componentIsVisibleClientSide() {
111         selectMenuPath(DETAILS_GENERATOR_WATCHING);
112         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
113
114         TestBenchElement details = getGridElement().getDetails(0);
115         assertNotNull("No widget detected inside details",
116                 details.findElement(By.className("v-widget")));
117     }
118
119     @Test
120     public void openingDetailsTwice() {
121         selectMenuPath(DETAILS_GENERATOR_WATCHING);
122         selectMenuPath(OPEN_FIRST_ITEM_DETAILS); // open
123         selectMenuPath(OPEN_FIRST_ITEM_DETAILS); // close
124         selectMenuPath(OPEN_FIRST_ITEM_DETAILS); // open
125
126         TestBenchElement details = getGridElement().getDetails(0);
127         assertNotNull("No widget detected inside details",
128                 details.findElement(By.className("v-widget")));
129     }
130
131     @Test(expected = NoSuchElementException.class)
132     public void scrollingDoesNotCreateAFloodOfDetailsRows() {
133         selectMenuPath(DETAILS_GENERATOR_WATCHING);
134
135         // scroll somewhere to hit uncached rows
136         getGridElement().scrollToRow(101);
137
138         // this should throw
139         getGridElement().getDetails(100);
140     }
141
142     @Test
143     public void openingDetailsOutOfView() {
144         getGridElement().scrollToRow(500);
145
146         selectMenuPath(DETAILS_GENERATOR_WATCHING);
147         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
148
149         getGridElement().scrollToRow(0);
150
151         // if this fails, it'll fail before the assertNotNull
152         assertNotNull("unexpected null details row",
153                 getGridElement().getDetails(0));
154     }
155
156     @Test
157     public void togglingAVisibleDetailsRowWithOneRoundtrip() {
158         selectMenuPath(DETAILS_GENERATOR_WATCHING);
159         selectMenuPath(OPEN_FIRST_ITEM_DETAILS); // open
160
161         assertTrue("Unexpected generator content",
162                 getGridElement().getDetails(0).getText().endsWith("(0)"));
163         selectMenuPath(TOGGLE_FIRST_ITEM_DETAILS);
164         assertTrue("New component was not displayed in the client",
165                 getGridElement().getDetails(0).getText().endsWith("(1)"));
166     }
167
168     @Test
169     public void almostLastItemIdIsRendered() {
170         selectMenuPath(DETAILS_GENERATOR_WATCHING);
171         selectMenuPath(OPEN_ALMOST_LAST_ITEM_DETAILS);
172         scrollGridVerticallyTo(100000);
173
174         TestBenchElement details = getGridElement()
175                 .getDetails(ALMOST_LAST_INDEX);
176         assertNotNull(details);
177         assertTrue("Unexpected details content",
178                 details.getText().endsWith(ALMOST_LAST_INDEX + " (0)"));
179     }
180
181     @Test
182     public void persistingChangesWorkInDetails() {
183         selectMenuPath(DETAILS_GENERATOR_PERSISTING);
184         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
185         assertEquals("One", getGridElement().getDetails(0).getText());
186         selectMenuPath(CHANGE_HIERARCHY);
187         assertEquals("Two", getGridElement().getDetails(0).getText());
188     }
189
190     @Test
191     public void persistingChangesWorkInDetailsWhileOutOfView() {
192         selectMenuPath(DETAILS_GENERATOR_PERSISTING);
193         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
194         assertEquals("One", getGridElement().getDetails(0).getText());
195         scrollGridVerticallyTo(10000);
196         selectMenuPath(CHANGE_HIERARCHY);
197         scrollGridVerticallyTo(0);
198         assertEquals("Two", getGridElement().getDetails(0).getText());
199     }
200
201     @Test
202     public void persistingChangesWorkInDetailsWhenNotAttached() {
203         selectMenuPath(DETAILS_GENERATOR_PERSISTING);
204         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
205         assertEquals("One", getGridElement().getDetails(0).getText());
206
207         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
208         assertFalse("Details should be detached",
209                 getGridElement().isElementPresent(By.vaadin("#details[0]")));
210
211         selectMenuPath(CHANGE_HIERARCHY);
212         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
213
214         assertEquals("Two", getGridElement().getDetails(0).getText());
215     }
216
217     @Test
218     public void swappingDetailsGenerators_noDetailsShown() {
219         selectMenuPath(DETAILS_GENERATOR_WATCHING);
220         selectMenuPath(DETAILS_GENERATOR_NULL);
221         assertFalse("Got some errors", $(NotificationElement.class).exists());
222     }
223
224     @Test
225     public void swappingDetailsGenerators_shownDetails() {
226         selectMenuPath(DETAILS_GENERATOR_PERSISTING);
227         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
228         assertTrue("Details should contain 'One' at first",
229                 getGridElement().getDetails(0).getText().contains("One"));
230
231         selectMenuPath(DETAILS_GENERATOR_WATCHING);
232         assertFalse(
233                 "Details should contain 'Watching' after swapping generator",
234                 getGridElement().getDetails(0).getText().contains("Watching"));
235     }
236
237     @Test
238     public void swappingDetailsGenerators_whileDetailsScrolledOut_showNever() {
239         scrollGridVerticallyTo(1000);
240         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
241         selectMenuPath(DETAILS_GENERATOR_WATCHING);
242         assertFalse("Got some errors", $(NotificationElement.class).exists());
243     }
244
245     @Test
246     public void swappingDetailsGenerators_whileDetailsScrolledOut_showAfter() {
247         scrollGridVerticallyTo(1000);
248         selectMenuPath(DETAILS_GENERATOR_PERSISTING);
249         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
250         selectMenuPath(DETAILS_GENERATOR_WATCHING);
251         scrollGridVerticallyTo(0);
252
253         assertFalse("Got some errors", $(NotificationElement.class).exists());
254         assertNotNull("Could not find a details",
255                 getGridElement().getDetails(0));
256     }
257
258     @Test
259     public void swappingDetailsGenerators_whileDetailsScrolledOut_showBefore() {
260         selectMenuPath(DETAILS_GENERATOR_PERSISTING);
261         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
262         selectMenuPath(DETAILS_GENERATOR_WATCHING);
263         scrollGridVerticallyTo(1000);
264
265         assertFalse("Got some errors", $(NotificationElement.class).exists());
266         assertNotNull("Could not find a details",
267                 getGridElement().getDetails(0));
268     }
269
270     @Test
271     public void swappingDetailsGenerators_whileDetailsScrolledOut_showBeforeAndAfter() {
272         selectMenuPath(DETAILS_GENERATOR_PERSISTING);
273         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
274         selectMenuPath(DETAILS_GENERATOR_WATCHING);
275         scrollGridVerticallyTo(1000);
276         scrollGridVerticallyTo(0);
277
278         assertFalse("Got some errors", $(NotificationElement.class).exists());
279         assertNotNull("Could not find a details",
280                 getGridElement().getDetails(0));
281     }
282
283     @Test
284     public void noAssertErrorsOnEmptyDetailsAndScrollDown() {
285         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
286         scrollGridVerticallyTo(500);
287         assertFalse(logContainsText("AssertionError"));
288     }
289
290     @Test
291     public void noAssertErrorsOnPopulatedDetailsAndScrollDown() {
292         selectMenuPath(DETAILS_GENERATOR_WATCHING);
293         selectMenuPath(OPEN_FIRST_ITEM_DETAILS);
294         scrollGridVerticallyTo(500);
295         assertFalse(logContainsText("AssertionError"));
296     }
297 }