2 * Copyright 2000-2016 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.tests.components.combobox;
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.hamcrest.MatcherAssert.assertThat;
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;
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;
32 public class ComboBoxSelectingWithNewItemsAllowedTest extends MultiBrowserTest {
33 private ComboBoxElement comboBoxElement;
34 private LabelElement labelElement;
37 public void setup() throws Exception {
41 waitForElementPresent(By.className("v-filterselect"));
42 comboBoxElement = $(ComboBoxElement.class).first();
43 labelElement = $(LabelElement.class).id("count");
47 public void checkDefaults() {
48 assertInitialItemCount();
52 public void itemIsAddedWithEnter() {
53 typeInputAndHitEnter("a");
55 assertOneMoreThanInitial();
56 assertThatSelectedValueIs("a");
60 public void itemIsAddedWithTab() {
61 typeInputAndHitTab("a");
63 assertOneMoreThanInitial();
64 assertThatSelectedValueIs("a");
68 public void itemIsAddedWhenClickingOutside() {
69 clearInputAndType("foo");
70 findElement(By.tagName("body")).click();
71 assertOneMoreThanInitial();
72 assertThatSelectedValueIs("foo");
76 public void matchingSuggestionIsSelectedWithEnter() {
77 typeInputAndHitEnter("a0");
79 assertInitialItemCount();
80 assertThatSelectedValueIs("a0");
84 public void matchingSuggestionIsSelectedWithTab() {
85 typeInputAndHitTab("a0");
87 assertInitialItemCount();
88 assertThatSelectedValueIs("a0");
92 public void nullIsSelected() {
93 typeInputAndHitEnter("a");
94 assertOneMoreThanInitial();
95 assertThatSelectedValueIs("a");
97 clearInputAndHitEnter();
99 assertOneMoreThanInitial();
100 assertThatSelectedValueIs("", "null");
104 public void itemFromSecondPageIsSelected() {
105 typeInputAndHitEnter("a20");
107 assertInitialItemCount();
108 assertThatSelectedValueIs("a20");
112 public void selectingNullFromSecondPage() {
113 typeInputAndHitEnter("a20");
114 assertInitialItemCount();
115 assertThatSelectedValueIs("a20");
117 clearInputAndHitEnter();
118 assertInitialItemCount();
119 assertThatSelectedValueIs("", "null");
123 public void selectionRemainsAfterOpeningPopup() {
124 typeInputAndHitEnter("a20");
125 assertInitialItemCount();
126 assertThatSelectedValueIs("a20");
129 assertThatSelectedValueIs("a20");
133 public void selectionOnMouseOut() {
134 typeInputAndHitEnter("a20");
135 comboBoxElement.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN);
137 findElement(By.className("v-app")).click();
139 assertInitialItemCount();
140 assertThatSelectedValueIs("a21");
144 public void cancelResetsSelection() {
145 sendKeysToInput("a20");
148 assertInitialItemCount();
149 assertThatSelectedValueIs("");
153 public void inputFieldResetsToSelectedText() {
154 typeInputAndHitEnter("z5");
156 sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE);
159 assertInitialItemCount();
160 assertThatSelectedValueIs("z5");
164 public void emptyValueIsSelectedWithTab() {
165 typeInputAndHitEnter("z5");
167 assertInitialItemCount();
168 assertThatSelectedValueIs("z5");
169 // longer delay for this one because otherwise it keeps failing when run
171 comboBoxElement.sendKeys(200, Keys.BACK_SPACE, Keys.BACK_SPACE,
173 assertInitialItemCount();
174 assertThatSelectedValueIs("", "null");
176 sendKeysToInput("z5");
178 assertInitialItemCount();
179 assertThatSelectedValueIs("", "null");
183 public void arrowNavigatedValueIsSelectedWithEnter() {
184 sendKeysToInput("z");
185 sendKeysToInput(Keys.DOWN, Keys.DOWN, getReturn());
187 assertInitialItemCount();
188 assertThatSelectedValueIs("z1");
192 public void arrowNavigatedValueIsSelectedWithTab() {
193 sendKeysToInput("z");
194 sendKeysToInput(Keys.DOWN, Keys.DOWN, Keys.TAB);
196 assertInitialItemCount();
197 assertThatSelectedValueIs("z1");
200 private void clearInputAndHitEnter() {
201 sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE, Keys.BACK_SPACE);
202 sendKeysToInput(getReturn());
205 private void typeInputAndHitEnter(String input) {
206 clearInputAndType(input);
207 sendKeysToInput(getReturn());
210 private void typeInputAndHitTab(String input) {
211 clearInputAndType(input);
212 sendKeysToInput(Keys.TAB);
215 private void clearInputAndType(String input) {
216 comboBoxElement.clear();
217 sendKeysToInput(input);
220 private void sendKeysToInput(CharSequence... keys) {
221 comboBoxElement.sendKeys(keys);
224 private Keys getReturn() {
225 if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
232 private void openPopup() {
233 // Need to wait to make sure popup is closed first.
236 } catch (InterruptedException e) {
239 comboBoxElement.openPopup();
242 private void cancelSelection() {
243 if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
244 findElement(By.className("v-app")).click();
246 sendKeysToInput(Keys.ESCAPE);
250 private void assertThatSelectedValueIs(final String value) {
251 assertThatSelectedValueIs(value, value);
254 private void assertThatSelectedValueIs(final String value,
255 final String labelValue) {
256 assertThat(comboBoxElement.getText(), is(value));
258 waitUntil(new ExpectedCondition<Boolean>() {
259 private String actualValue;
262 public Boolean apply(WebDriver input) {
263 actualValue = $(LabelElement.class).id("value").getText();
264 return actualValue.equals(labelValue);
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);
276 private void assertInitialItemCount() {
277 // wait for a bit in case the count is updating
279 assertThat("Wrong initial item count.", labelElement.getText(),
283 private void assertOneMoreThanInitial() {
284 waitUntil(new ExpectedCondition<Boolean>() {
286 public Boolean apply(WebDriver input) {
287 return "2601".equals(labelElement.getText());
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());