You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComboBoxSelectingNewItemValueChangeTest.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package com.vaadin.tests.components.combobox;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import org.junit.Test;
  5. import org.openqa.selenium.Keys;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.interactions.Actions;
  8. import org.openqa.selenium.support.ui.ExpectedCondition;
  9. import com.vaadin.testbench.By;
  10. import com.vaadin.testbench.elements.ButtonElement;
  11. import com.vaadin.testbench.elements.CheckBoxElement;
  12. import com.vaadin.testbench.elements.ComboBoxElement;
  13. import com.vaadin.testbench.elements.LabelElement;
  14. import com.vaadin.testbench.parallel.BrowserUtil;
  15. import com.vaadin.tests.tb3.MultiBrowserTest;
  16. public class ComboBoxSelectingNewItemValueChangeTest extends MultiBrowserTest {
  17. protected enum SelectionType {
  18. ENTER, TAB, CLICK_OUT;
  19. }
  20. private ComboBoxElement comboBoxElement;
  21. private LabelElement valueLabelElement;
  22. private LabelElement changeLabelElement;
  23. private String[] defaultInputs = new String[] { "foo", "bar", "baz",
  24. "fie" };
  25. private String[] shortInputs = new String[] { "a", "b", "c", "d" };
  26. @Override
  27. public void setup() throws Exception {
  28. super.setup();
  29. openTestURL();
  30. waitForElementPresent(By.className("v-filterselect"));
  31. comboBoxElement = $(ComboBoxElement.class).first();
  32. valueLabelElement = $(LabelElement.class).id("value");
  33. changeLabelElement = $(LabelElement.class).id("change");
  34. }
  35. @Test
  36. public void newItemHandlingWithEnter() {
  37. itemHandling(SelectionType.ENTER, defaultInputs);
  38. }
  39. @Test
  40. public void newItemHandlingWithTab() {
  41. itemHandling(SelectionType.TAB, defaultInputs);
  42. }
  43. @Test
  44. public void newItemHandlingWithClickingOut() {
  45. itemHandling(SelectionType.CLICK_OUT, defaultInputs);
  46. }
  47. @Test
  48. public void slowNewItemHandlingWithEnter() {
  49. delay(true);
  50. itemHandling(SelectionType.ENTER, defaultInputs);
  51. }
  52. @Test
  53. public void slowNewItemHandlingWithTab() {
  54. delay(true);
  55. itemHandling(SelectionType.TAB, defaultInputs);
  56. }
  57. @Test
  58. public void slowNewItemHandlingWithClickingOut() {
  59. delay(true);
  60. itemHandling(SelectionType.CLICK_OUT, defaultInputs);
  61. }
  62. @Test
  63. public void shortNewItemHandlingWithEnter() {
  64. itemHandling(SelectionType.ENTER, shortInputs);
  65. }
  66. @Test
  67. public void shortNewItemHandlingWithTab() {
  68. itemHandling(SelectionType.TAB, shortInputs);
  69. }
  70. @Test
  71. public void shortNewItemHandlingWithClickingOut() {
  72. itemHandling(SelectionType.CLICK_OUT, shortInputs);
  73. }
  74. public void itemHandling(SelectionType selectionType, String[] inputs) {
  75. assertThatSelectedValueIs("");
  76. // new item, no existing selection
  77. typeInputAndSelect(inputs[0], selectionType);
  78. assertThatSelectedValueIs(inputs[0]);
  79. assertValueChange(1);
  80. // new item, existing selection
  81. typeInputAndSelect(inputs[1], selectionType);
  82. assertThatSelectedValueIs(inputs[1]);
  83. assertValueChange(2);
  84. reject(true);
  85. // item adding blocked, existing selection
  86. typeInputAndSelect(inputs[2], selectionType);
  87. assertThatSelectedValueIs(inputs[1]);
  88. assertRejected(inputs[2]);
  89. reset();
  90. // item adding blocked, no existing selection
  91. typeInputAndSelect(inputs[2], selectionType);
  92. assertThatSelectedValueIs("");
  93. assertRejected(inputs[2]);
  94. reject(false);
  95. blockSelection(true);
  96. // item adding allowed, selection blocked, no existing selection
  97. typeInputAndSelect(inputs[2], selectionType);
  98. assertThatSelectedValueIs("");
  99. assertItemCount(2601);
  100. // second attempt selects
  101. typeInputAndSelect(inputs[2], selectionType);
  102. assertThatSelectedValueIs(inputs[2]);
  103. assertValueChange(1);
  104. // item adding allowed, selection blocked, existing selection
  105. typeInputAndSelect(inputs[3], selectionType);
  106. assertThatSelectedValueIs(inputs[2]);
  107. assertItemCount(2602);
  108. }
  109. protected void typeInputAndSelect(String input,
  110. SelectionType selectionType) {
  111. // clear() would cause an additional value change in chrome 70+
  112. // since it always makes blur after clear()
  113. comboBoxElement.sendKeys(Keys.BACK_SPACE, Keys.BACK_SPACE, Keys.BACK_SPACE);
  114. sendKeysToInput(input);
  115. switch (selectionType) {
  116. case ENTER:
  117. sendKeysToInput(getReturn());
  118. break;
  119. case TAB:
  120. sendKeysToInput(Keys.TAB);
  121. break;
  122. case CLICK_OUT:
  123. new Actions(getDriver()).moveToElement(comboBoxElement, 10, 10)
  124. .moveByOffset(comboBoxElement.getSize().getWidth(), 0)
  125. .click().perform();
  126. break;
  127. }
  128. }
  129. private void sendKeysToInput(CharSequence... keys) {
  130. // ensure mouse is located over the ComboBox to avoid hover issues
  131. new Actions(getDriver()).moveToElement(comboBoxElement).perform();
  132. comboBoxElement.sendKeys(keys);
  133. }
  134. private Keys getReturn() {
  135. if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
  136. return Keys.ENTER;
  137. } else {
  138. return Keys.RETURN;
  139. }
  140. }
  141. protected void assertThatSelectedValueIs(final String value) {
  142. waitUntil(new ExpectedCondition<Boolean>() {
  143. private String actualComboBoxValue;
  144. private String actualLabelValue;
  145. @Override
  146. public Boolean apply(WebDriver input) {
  147. actualLabelValue = valueLabelElement.getText();
  148. actualComboBoxValue = comboBoxElement.getText();
  149. return actualComboBoxValue.equals(value)
  150. && actualLabelValue.equals(value);
  151. }
  152. @Override
  153. public String toString() {
  154. // Timed out after 10 seconds waiting for ...
  155. return String.format(
  156. "combobox and label value to match '%s' (was: '%s' and '%s')",
  157. value, actualComboBoxValue, actualLabelValue);
  158. }
  159. });
  160. }
  161. protected void assertValueChange(int count) {
  162. assertEquals(String.format(
  163. "Value change count: %s Selection change count: %s user originated: true",
  164. count, count), changeLabelElement.getText());
  165. }
  166. private void assertRejected(String value) {
  167. assertEquals(String.format("item %s discarded", value),
  168. changeLabelElement.getText());
  169. }
  170. private void assertItemCount(int count) {
  171. assertEquals(String.format("adding new item... count: %s", count),
  172. changeLabelElement.getText());
  173. }
  174. private void reject(boolean reject) {
  175. CheckBoxElement checkBox = $(CheckBoxElement.class).id("reject");
  176. if (reject != checkBox.isChecked()) {
  177. checkBox.click();
  178. }
  179. }
  180. private void delay(boolean delay) {
  181. CheckBoxElement checkBox = $(CheckBoxElement.class).id("delay");
  182. if (delay != checkBox.isChecked()) {
  183. checkBox.click();
  184. }
  185. }
  186. private void blockSelection(boolean noSelection) {
  187. CheckBoxElement checkBox = $(CheckBoxElement.class).id("noSelection");
  188. if (noSelection != checkBox.isChecked()) {
  189. checkBox.click();
  190. }
  191. }
  192. protected void reset() {
  193. $(ButtonElement.class).id("reset").click();
  194. }
  195. }