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.elements.LabelElement;
28 import com.vaadin.testbench.parallel.BrowserUtil;
29 import com.vaadin.tests.tb3.MultiBrowserTest;
30 import com.vaadin.v7.testbench.customelements.ComboBoxElement;
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 matchingSuggestionIsSelectedWithEnter() {
69 typeInputAndHitEnter("a0");
71 assertInitialItemCount();
72 assertThatSelectedValueIs("a0");
76 public void matchingSuggestionIsSelectedWithTab() {
77 typeInputAndHitTab("a0");
79 assertInitialItemCount();
80 assertThatSelectedValueIs("a0");
84 public void nullIsSelected() {
85 typeInputAndHitEnter("a");
86 assertOneMoreThanInitial();
87 assertThatSelectedValueIs("a");
89 clearInputAndHitEnter();
91 assertOneMoreThanInitial();
92 assertThatSelectedValueIs("", "null");
96 public void itemFromSecondPageIsSelected() {
97 typeInputAndHitEnter("a20");
99 assertInitialItemCount();
100 assertThatSelectedValueIs("a20");
104 public void selectingNullFromSecondPage() {
105 typeInputAndHitEnter("a20");
106 assertInitialItemCount();
107 assertThatSelectedValueIs("a20");
109 clearInputAndHitEnter();
110 assertInitialItemCount();
111 assertThatSelectedValueIs("", "null");
115 public void selectionRemainsAfterOpeningPopup() {
116 typeInputAndHitEnter("a20");
117 assertInitialItemCount();
118 assertThatSelectedValueIs("a20");
121 assertThatSelectedValueIs("a20");
125 public void noSelectionAfterMouseOut() {
126 typeInputAndHitEnter("a20");
127 comboBoxElement.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN);
129 findElement(By.className("v-app")).click();
131 assertInitialItemCount();
132 assertThatSelectedValueIs("a20");
136 public void cancelResetsSelection() {
137 sendKeysToInput("a20");
140 assertInitialItemCount();
141 assertThatSelectedValueIs("");
145 public void inputFieldResetsToSelectedText() {
146 typeInputAndHitEnter("z5");
148 sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE);
151 assertInitialItemCount();
152 assertThatSelectedValueIs("z5");
156 public void emptyValueIsSelectedWithTab() {
157 typeInputAndHitEnter("z5");
159 assertInitialItemCount();
160 assertThatSelectedValueIs("z5");
161 // longer delay for this one because otherwise it keeps failing when run
163 comboBoxElement.sendKeys(200, Keys.BACK_SPACE, Keys.BACK_SPACE,
165 assertInitialItemCount();
166 assertThatSelectedValueIs("", "null");
168 sendKeysToInput("z5");
170 assertInitialItemCount();
171 assertThatSelectedValueIs("", "null");
175 public void arrowNavigatedValueIsSelectedWithEnter() {
176 sendKeysToInput("z");
177 sendKeysToInput(Keys.DOWN, Keys.DOWN, getReturn());
179 assertInitialItemCount();
180 assertThatSelectedValueIs("z1");
184 public void arrowNavigatedValueIsSelectedWithTab() {
185 sendKeysToInput("z");
186 sendKeysToInput(Keys.DOWN, Keys.DOWN, Keys.TAB);
188 assertInitialItemCount();
189 assertThatSelectedValueIs("z1");
192 private void clearInputAndHitEnter() {
193 sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE, Keys.BACK_SPACE);
194 sendKeysToInput(getReturn());
197 private void typeInputAndHitEnter(String input) {
198 clearInputAndType(input);
199 sendKeysToInput(getReturn());
202 private void typeInputAndHitTab(String input) {
203 clearInputAndType(input);
204 sendKeysToInput(Keys.TAB);
207 private void clearInputAndType(String input) {
208 comboBoxElement.clear();
209 sendKeysToInput(input);
212 private void sendKeysToInput(CharSequence... keys) {
213 comboBoxElement.sendKeys(keys);
216 private Keys getReturn() {
217 if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
224 private void openPopup() {
225 // Need to wait to make sure popup is closed first.
228 } catch (InterruptedException e) {
231 comboBoxElement.openPopup();
234 private void cancelSelection() {
235 if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
236 findElement(By.className("v-app")).click();
238 sendKeysToInput(Keys.ESCAPE);
242 private void assertThatSelectedValueIs(final String value) {
243 assertThatSelectedValueIs(value, value);
246 private void assertThatSelectedValueIs(final String value,
247 final String labelValue) {
248 assertThat(comboBoxElement.getText(), is(value));
250 waitUntil(new ExpectedCondition<Boolean>() {
251 private String actualValue;
254 public Boolean apply(WebDriver input) {
255 actualValue = $(LabelElement.class).id("value").getText();
256 return actualValue.equals(labelValue);
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);
268 private void assertInitialItemCount() {
269 // wait for a bit in case the count is updating
271 assertThat("Wrong initial item count.", labelElement.getText(),
275 private void assertOneMoreThanInitial() {
276 waitUntil(new ExpectedCondition<Boolean>() {
278 public Boolean apply(WebDriver input) {
279 return "2601".equals(labelElement.getText());
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());