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.

CloseShortcutTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2000-2014 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.window;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertTrue;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import org.openqa.selenium.By;
  22. import org.openqa.selenium.Keys;
  23. import org.openqa.selenium.WebDriver;
  24. import org.openqa.selenium.interactions.Actions;
  25. import org.openqa.selenium.remote.DesiredCapabilities;
  26. import org.openqa.selenium.support.ui.ExpectedCondition;
  27. import com.vaadin.testbench.elements.CheckBoxElement;
  28. import com.vaadin.testbench.elements.TextFieldElement;
  29. import com.vaadin.testbench.elements.WindowElement;
  30. import com.vaadin.testbench.parallel.BrowserUtil;
  31. import com.vaadin.tests.tb3.MultiBrowserTest;
  32. /**
  33. * Tests close shortcuts for Window.
  34. *
  35. * @author Vaadin Ltd
  36. */
  37. public class CloseShortcutTest extends MultiBrowserTest {
  38. private WindowElement window;
  39. private CheckBoxElement cbDefault;
  40. private CheckBoxElement cbOther;
  41. private CheckBoxElement cbCtrl;
  42. private CheckBoxElement cbShift;
  43. @Override
  44. @Before
  45. public void setup() throws Exception {
  46. super.setup();
  47. openTestURL();
  48. waitForElementPresent(By.className("v-window"));
  49. window = $(WindowElement.class).first();
  50. cbDefault = $(CheckBoxElement.class).id("default");
  51. cbOther = $(CheckBoxElement.class).id("other");
  52. cbCtrl = $(CheckBoxElement.class).id("control");
  53. cbShift = $(CheckBoxElement.class).id("shift");
  54. }
  55. @Test
  56. public void testAllCheckBoxesSelected() {
  57. assertTrue("Default wasn't selected initially.", isChecked(cbDefault));
  58. assertTrue("Other wasn't selected initially.", isChecked(cbOther));
  59. assertTrue("Ctrl+A wasn't selected initially.", isChecked(cbCtrl));
  60. assertTrue("Shift+H wasn't selected initially.", isChecked(cbShift));
  61. }
  62. @Test
  63. public void testAllCheckBoxesClickable() {
  64. click(cbDefault);
  65. click(cbOther);
  66. click(cbCtrl);
  67. click(cbShift);
  68. assertFalse("Default was selected when it shouldn't have been.",
  69. isChecked(cbDefault));
  70. assertFalse("Other was selected when it shouldn't have been.",
  71. isChecked(cbOther));
  72. assertFalse("Ctrl+A was selected when it shouldn't have been.",
  73. isChecked(cbCtrl));
  74. assertFalse("Shift+H was selected when it shouldn't have been.",
  75. isChecked(cbShift));
  76. }
  77. @Test
  78. public void testDefaultWithAll() {
  79. attemptDefaultShortcut();
  80. ensureWindowClosed();
  81. }
  82. @Test
  83. public void testDefaultWithoutSelection() {
  84. click(cbDefault);
  85. attemptDefaultShortcut();
  86. ensureWindowOpen();
  87. }
  88. @Test
  89. public void testOtherWithAll() {
  90. attemptOtherShortcut();
  91. // TODO: remove this check once #14902 has been fixed
  92. DesiredCapabilities cap = getDesiredCapabilities();
  93. if ((BrowserUtil.isIE(cap) && !BrowserUtil.isIE8(cap))
  94. || BrowserUtil.isPhantomJS(cap)) {
  95. ensureWindowClosed();
  96. }
  97. }
  98. @Test
  99. public void testOtherWithoutSelection() {
  100. click(cbOther);
  101. attemptOtherShortcut();
  102. ensureWindowOpen();
  103. }
  104. @Test
  105. public void testCtrlWithAll() {
  106. attemptCtrlShortcut();
  107. // TODO: remove this check once #14902 has been fixed
  108. if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
  109. ensureWindowClosed();
  110. }
  111. }
  112. @Test
  113. public void testCtrlWithoutSelection() {
  114. click(cbCtrl);
  115. attemptCtrlShortcut();
  116. ensureWindowOpen();
  117. }
  118. @Test
  119. public void testShiftWithAll() {
  120. attemptShiftShortcut();
  121. // TODO: remove this check once #14902 has been fixed
  122. DesiredCapabilities capabilities = getDesiredCapabilities();
  123. if (!BrowserUtil.isIE(capabilities) || BrowserUtil.isIE8(capabilities)) {
  124. ensureWindowClosed();
  125. }
  126. }
  127. @Test
  128. public void testShiftWithoutSelection() {
  129. click(cbShift);
  130. attemptShiftShortcut();
  131. ensureWindowOpen();
  132. }
  133. private boolean isChecked(CheckBoxElement cb) {
  134. String checked = cb.findElement(By.tagName("input")).getAttribute(
  135. "checked");
  136. if ("true".equals(checked)) {
  137. return true;
  138. } else if (checked == null) {
  139. return false;
  140. }
  141. throw new IllegalStateException(
  142. "Unexpected attribute value for 'checked': " + checked);
  143. }
  144. @Override
  145. protected void click(final CheckBoxElement cb) {
  146. final boolean initial = isChecked(cb);
  147. super.click(cb);
  148. waitUntil(new ExpectedCondition<Boolean>() {
  149. @Override
  150. public Boolean apply(WebDriver input) {
  151. return initial != isChecked(cb);
  152. }
  153. @Override
  154. public String toString() {
  155. // Timed out after 10 seconds waiting for ...
  156. return "checked state to change";
  157. }
  158. });
  159. }
  160. private void attemptDefaultShortcut() {
  161. window.focus();
  162. $(TextFieldElement.class).first().sendKeys(Keys.ESCAPE);
  163. }
  164. private void attemptOtherShortcut() {
  165. window.focus();
  166. $(TextFieldElement.class).first().sendKeys("R");
  167. }
  168. private void attemptCtrlShortcut() {
  169. window.focus();
  170. new Actions(driver).keyDown(Keys.CONTROL).perform();
  171. $(TextFieldElement.class).first().sendKeys("A");
  172. new Actions(driver).keyUp(Keys.CONTROL).perform();
  173. }
  174. private void attemptShiftShortcut() {
  175. window.focus();
  176. new Actions(driver).keyDown(Keys.SHIFT).perform();
  177. $(TextFieldElement.class).first().sendKeys("H");
  178. new Actions(driver).keyUp(Keys.SHIFT).perform();
  179. }
  180. private void ensureWindowClosed() {
  181. assertTrue("Window didn't close as expected.", $(WindowElement.class)
  182. .all().isEmpty());
  183. }
  184. private void ensureWindowOpen() {
  185. assertFalse("Window closed when it shouldn't have.",
  186. $(WindowElement.class).all().isEmpty());
  187. }
  188. }