]> source.dussan.org Git - vaadin-framework.git/blob
c16a80856b9a5b1b5aeda2c9c285a259f74fe887
[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.combobox;
17
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.hamcrest.MatcherAssert.assertThat;
20
21 import org.junit.Test;
22 import org.openqa.selenium.Keys;
23 import org.openqa.selenium.WebDriver;
24 import org.openqa.selenium.support.ui.ExpectedCondition;
25
26 import com.vaadin.testbench.By;
27 import com.vaadin.testbench.customelements.ComboBoxElement;
28 import com.vaadin.testbench.elements.LabelElement;
29 import com.vaadin.testbench.parallel.BrowserUtil;
30 import com.vaadin.tests.tb3.MultiBrowserTest;
31
32 public class ComboBoxSelectingWithNewItemsAllowedTest extends MultiBrowserTest {
33     private ComboBoxElement comboBoxElement;
34     private LabelElement labelElement;
35
36     @Override
37     public void setup() throws Exception {
38         super.setup();
39
40         openTestURL();
41         waitForElementPresent(By.className("v-filterselect"));
42         comboBoxElement = $(ComboBoxElement.class).first();
43         labelElement = $(LabelElement.class).id("count");
44     }
45
46     @Test
47     public void checkDefaults() {
48         assertInitialItemCount();
49     }
50
51     @Test
52     public void itemIsAddedWithEnter() {
53         typeInputAndHitEnter("a");
54
55         assertOneMoreThanInitial();
56         assertThatSelectedValueIs("a");
57     }
58
59     @Test
60     public void itemIsAddedWithTab() {
61         typeInputAndHitTab("a");
62
63         assertOneMoreThanInitial();
64         assertThatSelectedValueIs("a");
65     }
66
67     @Test
68     public void itemIsAddedWhenClickingOutside() {
69         clearInputAndType("foo");
70         findElement(By.tagName("body")).click();
71         assertOneMoreThanInitial();
72         assertThatSelectedValueIs("foo");
73     }
74
75     @Test
76     public void matchingSuggestionIsSelectedWithEnter() {
77         typeInputAndHitEnter("a0");
78
79         assertInitialItemCount();
80         assertThatSelectedValueIs("a0");
81     }
82
83     @Test
84     public void matchingSuggestionIsSelectedWithTab() {
85         typeInputAndHitTab("a0");
86
87         assertInitialItemCount();
88         assertThatSelectedValueIs("a0");
89     }
90
91     @Test
92     public void nullIsSelected() {
93         typeInputAndHitEnter("a");
94         assertOneMoreThanInitial();
95         assertThatSelectedValueIs("a");
96
97         clearInputAndHitEnter();
98
99         assertOneMoreThanInitial();
100         assertThatSelectedValueIs("", "null");
101     }
102
103     @Test
104     public void itemFromSecondPageIsSelected() {
105         typeInputAndHitEnter("a20");
106
107         assertInitialItemCount();
108         assertThatSelectedValueIs("a20");
109     }
110
111     @Test
112     public void selectingNullFromSecondPage() {
113         typeInputAndHitEnter("a20");
114         assertInitialItemCount();
115         assertThatSelectedValueIs("a20");
116
117         clearInputAndHitEnter();
118         assertInitialItemCount();
119         assertThatSelectedValueIs("", "null");
120     }
121
122     @Test
123     public void selectionRemainsAfterOpeningPopup() {
124         typeInputAndHitEnter("a20");
125         assertInitialItemCount();
126         assertThatSelectedValueIs("a20");
127
128         openPopup();
129         assertThatSelectedValueIs("a20");
130     }
131
132     @Test
133     public void selectionOnMouseOut() {
134         typeInputAndHitEnter("a20");
135         comboBoxElement.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN);
136
137         findElement(By.className("v-app")).click();
138
139         assertInitialItemCount();
140         assertThatSelectedValueIs("a21");
141     }
142
143     @Test
144     public void cancelResetsSelection() {
145         sendKeysToInput("a20");
146         cancelSelection();
147
148         assertInitialItemCount();
149         assertThatSelectedValueIs("");
150     }
151
152     @Test
153     public void inputFieldResetsToSelectedText() {
154         typeInputAndHitEnter("z5");
155
156         sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE);
157         cancelSelection();
158
159         assertInitialItemCount();
160         assertThatSelectedValueIs("z5");
161     }
162
163     @Test
164     public void emptyValueIsSelectedWithTab() {
165         typeInputAndHitEnter("z5");
166
167         assertInitialItemCount();
168         assertThatSelectedValueIs("z5");
169         // longer delay for this one because otherwise it keeps failing when run
170         // on local machine
171         comboBoxElement.sendKeys(200, Keys.BACK_SPACE, Keys.BACK_SPACE,
172                 Keys.TAB);
173         assertInitialItemCount();
174         assertThatSelectedValueIs("", "null");
175
176         sendKeysToInput("z5");
177         cancelSelection();
178         assertInitialItemCount();
179         assertThatSelectedValueIs("", "null");
180     }
181
182     @Test
183     public void arrowNavigatedValueIsSelectedWithEnter() {
184         sendKeysToInput("z");
185         sendKeysToInput(Keys.DOWN, Keys.DOWN, getReturn());
186
187         assertInitialItemCount();
188         assertThatSelectedValueIs("z1");
189     }
190
191     @Test
192     public void arrowNavigatedValueIsSelectedWithTab() {
193         sendKeysToInput("z");
194         sendKeysToInput(Keys.DOWN, Keys.DOWN, Keys.TAB);
195
196         assertInitialItemCount();
197         assertThatSelectedValueIs("z1");
198     }
199
200     private void clearInputAndHitEnter() {
201         sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE, Keys.BACK_SPACE);
202         sendKeysToInput(getReturn());
203     }
204
205     private void typeInputAndHitEnter(String input) {
206         clearInputAndType(input);
207         sendKeysToInput(getReturn());
208     }
209
210     private void typeInputAndHitTab(String input) {
211         clearInputAndType(input);
212         sendKeysToInput(Keys.TAB);
213     }
214
215     private void clearInputAndType(String input) {
216         comboBoxElement.clear();
217         sendKeysToInput(input);
218     }
219
220     private void sendKeysToInput(CharSequence... keys) {
221         comboBoxElement.sendKeys(keys);
222     }
223
224     private Keys getReturn() {
225         if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
226             return Keys.ENTER;
227         } else {
228             return Keys.RETURN;
229         }
230     }
231
232     private void openPopup() {
233         // Need to wait to make sure popup is closed first.
234         try {
235             Thread.sleep(10);
236         } catch (InterruptedException e) {
237             e.printStackTrace();
238         }
239         comboBoxElement.openPopup();
240     }
241
242     private void cancelSelection() {
243         if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
244             findElement(By.className("v-app")).click();
245         } else {
246             sendKeysToInput(Keys.ESCAPE);
247         }
248     }
249
250     private void assertThatSelectedValueIs(final String value) {
251         assertThatSelectedValueIs(value, value);
252     }
253
254     private void assertThatSelectedValueIs(final String value,
255             final String labelValue) {
256         assertThat(comboBoxElement.getText(), is(value));
257
258         waitUntil(new ExpectedCondition<Boolean>() {
259             private String actualValue;
260
261             @Override
262             public Boolean apply(WebDriver input) {
263                 actualValue = $(LabelElement.class).id("value").getText();
264                 return actualValue.equals(labelValue);
265             }
266
267             @Override
268             public String toString() {
269                 // Timed out after 10 seconds waiting for ...
270                 return String.format("label value to match '%s' (was: '%s')",
271                         labelValue, actualValue);
272             }
273         });
274     }
275
276     private void assertInitialItemCount() {
277         // wait for a bit in case the count is updating
278         sleep(1000);
279         assertThat("Wrong initial item count.", labelElement.getText(),
280                 is("2600"));
281     }
282
283     private void assertOneMoreThanInitial() {
284         waitUntil(new ExpectedCondition<Boolean>() {
285             @Override
286             public Boolean apply(WebDriver input) {
287                 return "2601".equals(labelElement.getText());
288             }
289
290             @Override
291             public String toString() {
292                 // Timed out after 10 seconds waiting for ...
293                 return String.format("item count to become 2601 (was: %s)",
294                         labelElement.getText());
295             }
296         });
297     }
298 }