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