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.

ComboBoxCaretNavigationTest.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.vaadin.tests.components.combobox;
  2. import com.vaadin.testbench.elements.ComboBoxElement;
  3. import com.vaadin.tests.tb3.SingleBrowserTest;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.openqa.selenium.Keys;
  7. import org.openqa.selenium.WebElement;
  8. import java.util.List;
  9. import static org.junit.Assert.assertArrayEquals;
  10. public class ComboBoxCaretNavigationTest extends SingleBrowserTest {
  11. @Override
  12. @Before
  13. public void setup() throws Exception {
  14. super.setup();
  15. openTestURL();
  16. }
  17. @Test
  18. public void testHomeAndEndKeys() {
  19. ComboBoxElement comboBox = $(ComboBoxElement.class).first();
  20. String text = comboBox.getPopupSuggestions().get(1);
  21. comboBox.selectByText(text);
  22. comboBox.sendKeys(Keys.HOME);
  23. assertCaretPosition("Home key didn't work well.", 0, comboBox);
  24. comboBox.sendKeys(Keys.END);
  25. assertCaretPosition("End key didn't work well.", text.length(),
  26. comboBox);
  27. }
  28. @Test
  29. public void testLeftAndRightKeys() {
  30. ComboBoxElement comboBox = $(ComboBoxElement.class).first();
  31. String text = comboBox.getPopupSuggestions().get(1);
  32. comboBox.selectByText(text);
  33. comboBox.sendKeys(Keys.ARROW_LEFT);
  34. assertCaretPosition("Left Arrow key didn't work well.",
  35. text.length() - 1, comboBox);
  36. comboBox.sendKeys(Keys.ARROW_RIGHT);
  37. assertCaretPosition("Right Arrow key didn't work well.", text.length(),
  38. comboBox);
  39. }
  40. @Test
  41. public void testHomeAndRightKeys() {
  42. ComboBoxElement comboBox = $(ComboBoxElement.class).first();
  43. String text = comboBox.getPopupSuggestions().get(1);
  44. comboBox.selectByText(text);
  45. comboBox.sendKeys(Keys.HOME);
  46. assertCaretPosition("Home key didn't work well.", 0, comboBox);
  47. comboBox.sendKeys(Keys.ARROW_RIGHT);
  48. assertCaretPosition("Right Arrow key didn't work well.", 1, comboBox);
  49. }
  50. @Test
  51. public void testLeftAndEndKeys() {
  52. ComboBoxElement comboBox = $(ComboBoxElement.class).first();
  53. String text = comboBox.getPopupSuggestions().get(1);
  54. comboBox.selectByText(text);
  55. comboBox.sendKeys(Keys.ARROW_LEFT);
  56. assertCaretPosition("Left Arrow key didn't work well.",
  57. text.length() - 1, comboBox);
  58. comboBox.sendKeys(Keys.END);
  59. assertCaretPosition("End key didn't work well.", text.length(),
  60. comboBox);
  61. }
  62. private void assertCaretPosition(String message, int position,
  63. ComboBoxElement comboBox) {
  64. assertArrayEquals(message, new int[] { position, position },
  65. getSelection(comboBox.getInputField()));
  66. }
  67. private int[] getSelection(WebElement element) {
  68. @SuppressWarnings("unchecked")
  69. List<Long> range = (List<Long>) executeScript(
  70. "return [arguments[0].selectionStart,arguments[0].selectionEnd]",
  71. element);
  72. return new int[] { range.get(0).intValue(), range.get(1).intValue() };
  73. }
  74. }