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