/tags/fest-swing-1.0a3/src/test/java/org/fest/swing/core/RobotFixtureTest.java

http://fest.googlecode.com/ · Java · 310 lines · 248 code · 42 blank · 20 comment · 0 complexity · 5dfda26cf9ca2de41a107cd2f50b9bc0 MD5 · raw file

  1. /*
  2. * Created on Sep 5, 2007
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. *
  14. * Copyright @2007-2008 the original author or authors.
  15. */
  16. package org.fest.swing.core;
  17. import java.awt.Component;
  18. import java.awt.Dimension;
  19. import java.awt.Point;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import javax.swing.*;
  23. import org.testng.annotations.AfterMethod;
  24. import org.testng.annotations.BeforeMethod;
  25. import org.testng.annotations.DataProvider;
  26. import org.testng.annotations.Test;
  27. import org.fest.swing.exception.ComponentLookupException;
  28. import org.fest.swing.exception.WaitTimedOutError;
  29. import org.fest.swing.testing.ClickRecorder;
  30. import org.fest.swing.testing.KeyRecorder;
  31. import org.fest.swing.testing.TestFrame;
  32. import static java.awt.event.KeyEvent.*;
  33. import static org.fest.assertions.Assertions.assertThat;
  34. import static org.fest.assertions.Fail.fail;
  35. import static org.fest.swing.core.MouseButton.*;
  36. import static org.fest.swing.core.Pause.pause;
  37. import static org.fest.swing.testing.ClickRecorder.attachTo;
  38. import static org.fest.swing.testing.TestGroups.GUI;
  39. import static org.fest.swing.util.AWT.centerOf;
  40. /**
  41. * Tests for <code>{@link org.fest.swing.core.RobotFixture}</code>.
  42. *
  43. * @author Yvonne Wang
  44. */
  45. @Test(groups = GUI)
  46. public class RobotFixtureTest {
  47. private Robot robot;
  48. private MyFrame frame;
  49. private JTextField textFieldWithPopup;
  50. private JTextField textFieldWithoutPopup;
  51. @BeforeMethod public void setUp() {
  52. robot = RobotFixture.robotWithCurrentAwtHierarchy();
  53. frame = new MyFrame();
  54. textFieldWithPopup = frame.textFieldWithPopup;
  55. textFieldWithoutPopup = frame.textFieldWithoutPopup;
  56. robot.showWindow(frame); // implicitly test 'showWindow(Window)'
  57. assertThat(frame.isVisible()).isTrue();
  58. assertThat(frame.getLocationOnScreen()).isEqualTo(new Point(100, 100));
  59. }
  60. @AfterMethod public void tearDown() {
  61. robot.cleanUp();
  62. }
  63. public void shouldThrowErrorIfWindowNeverShown() {
  64. try {
  65. robot.showWindow(new JFrame() {
  66. private static final long serialVersionUID = 1L;
  67. @Override public void setVisible(boolean b) {
  68. super.setVisible(false);
  69. }
  70. });
  71. fail();
  72. } catch (WaitTimedOutError e) {
  73. assertThat(e).message().contains("Timed out waiting for Window to open");
  74. }
  75. }
  76. public void shouldNotPackWindowAsSpecified() {
  77. class MyWindow extends JWindow {
  78. private static final long serialVersionUID = 1L;
  79. private boolean packed;
  80. @Override public void pack() {
  81. packed = true;
  82. super.pack();
  83. }
  84. boolean packed() { return packed; }
  85. }
  86. Dimension size = new Dimension(100, 100);
  87. MyWindow window = new MyWindow();
  88. robot.showWindow(window, size, false);
  89. assertThat(window.getSize()).isEqualTo(size);
  90. assertThat(window.packed()).isFalse();
  91. assertThat(window.getLocationOnScreen()).isEqualTo(new Point(0, 0));
  92. }
  93. public void shouldClickComponent() {
  94. ClickRecorder recorder = attachTo(textFieldWithoutPopup);
  95. robot.click(textFieldWithoutPopup);
  96. assertThat(recorder).clicked(LEFT_BUTTON).timesClicked(1);
  97. }
  98. public void shouldRightClickComponent() {
  99. ClickRecorder recorder = attachTo(textFieldWithoutPopup);
  100. robot.rightClick(textFieldWithoutPopup);
  101. assertThat(recorder).clicked(RIGHT_BUTTON).timesClicked(1);
  102. }
  103. public void shouldDoubleClickComponent() {
  104. ClickRecorder recorder = attachTo(textFieldWithoutPopup);
  105. robot.doubleClick(textFieldWithoutPopup);
  106. assertThat(recorder).clicked(LEFT_BUTTON).timesClicked(2);
  107. }
  108. public void shouldClickComponentOnceWithLeftButtonAtGivenPoint() {
  109. Point p = new Point(10, 10);
  110. ClickRecorder recorder = attachTo(textFieldWithoutPopup);
  111. robot.click(textFieldWithoutPopup, p);
  112. assertThat(recorder).clicked(LEFT_BUTTON).timesClicked(1).clickedAt(p);
  113. }
  114. @Test(groups = GUI, dataProvider = "mouseButtons")
  115. public void shouldClickComponentOnceWithGivenButton(MouseButton button) {
  116. ClickRecorder recorder = attachTo(textFieldWithoutPopup);
  117. robot.click(textFieldWithoutPopup, button);
  118. assertThat(recorder).clicked(button).timesClicked(1);
  119. }
  120. @DataProvider(name = "mouseButtons")
  121. public Object[][] mouseButtons() {
  122. return new Object[][] {
  123. { LEFT_BUTTON },
  124. { MIDDLE_BUTTON },
  125. { RIGHT_BUTTON }
  126. };
  127. }
  128. @Test(groups = GUI, dataProvider = "clickingData")
  129. public void shouldClickComponentWithGivenMouseButtonAndGivenNumberOfTimes(MouseButton button, int times) {
  130. ClickRecorder recorder = attachTo(textFieldWithoutPopup);
  131. robot.click(textFieldWithoutPopup, button, times);
  132. assertThat(recorder).clicked(button).timesClicked(times).clickedAt(centerOf(textFieldWithoutPopup));
  133. }
  134. @Test(groups = GUI, dataProvider = "clickingData")
  135. public void shouldClickComponentWithGivenMouseButtonAndGivenNumberOfTimesAtGivenPoint(MouseButton button, int times) {
  136. ClickRecorder recorder = attachTo(textFieldWithoutPopup);
  137. Point point = new Point(10, 10);
  138. robot.click(textFieldWithoutPopup, point, button, times);
  139. assertThat(recorder).clicked(button).timesClicked(times).clickedAt(point);
  140. }
  141. @DataProvider(name = "clickingData")
  142. public Object[][] clickingData() {
  143. return new Object[][] {
  144. { LEFT_BUTTON, 1 },
  145. { LEFT_BUTTON, 2 },
  146. { MIDDLE_BUTTON, 1 },
  147. { MIDDLE_BUTTON, 2 },
  148. { RIGHT_BUTTON, 1 },
  149. { RIGHT_BUTTON, 2 },
  150. };
  151. }
  152. public void shouldPressAndReleaseGivenKeys() {
  153. textFieldWithPopup.requestFocusInWindow();
  154. KeyRecorder recorder = KeyRecorder.attachTo(textFieldWithPopup);
  155. int[] keys = { VK_A, VK_B, VK_Z };
  156. robot.pressAndReleaseKeys(keys);
  157. assertThat(recorder).keysPressed(keys).keysReleased(keys);
  158. }
  159. public void shouldPressGivenKeyWithoutReleasingIt() {
  160. textFieldWithPopup.requestFocusInWindow();
  161. KeyRecorder recorder = KeyRecorder.attachTo(textFieldWithPopup);
  162. robot.pressKey(VK_A);
  163. assertThat(recorder).keysPressed(VK_A).noKeysReleased();
  164. }
  165. public void shouldReleaseGivenKey() {
  166. textFieldWithPopup.requestFocusInWindow();
  167. KeyRecorder recorder = KeyRecorder.attachTo(textFieldWithPopup);
  168. robot.pressKey(VK_A);
  169. robot.releaseKey(VK_A);
  170. assertThat(recorder).keysReleased(VK_A);
  171. }
  172. public void shouldShowPopupMenu() {
  173. JPopupMenu menu = robot.showPopupMenu(textFieldWithPopup);
  174. assertThat(menu).isSameAs(popupMenu());
  175. assertThat(menu.isVisible()).isTrue();
  176. }
  177. public void shouldThrowErrorIfPopupNotFound() {
  178. try {
  179. robot.showPopupMenu(textFieldWithoutPopup);
  180. fail();
  181. } catch (ComponentLookupException expected) {
  182. assertThat(expected).message().contains("Unable to show popup")
  183. .contains("on javax.swing.JTextField")
  184. .contains("name='withoutPopup'");
  185. }
  186. }
  187. public void shouldReturnActivePopupMenu() {
  188. robot.showPopupMenu(textFieldWithPopup);
  189. JPopupMenu found = robot.findActivePopupMenu();
  190. assertThat(found).isSameAs(frame.popupMenu);
  191. }
  192. public void shouldReturnNullIfActivePopupMenuNotFound() {
  193. JPopupMenu found = robot.findActivePopupMenu();
  194. assertThat(found).isNull();
  195. }
  196. public void shouldCloseWindow() {
  197. final TestFrame w = new TestFrame(getClass());
  198. w.display();
  199. robot.close(w);
  200. pause(new Condition("Window closed") {
  201. @Override public boolean test() {
  202. return !w.isVisible();
  203. }
  204. });
  205. assertThat(w.isVisible()).isFalse();
  206. }
  207. public void shouldNotCloseWindowIfWindowNotShowing() {
  208. TestFrame w = new TestFrame(getClass());
  209. w.display();
  210. w.setVisible(false);
  211. assertThat(w.isShowing()).isFalse();
  212. robot.close(w);
  213. assertThat(w.isShowing()).isFalse();
  214. }
  215. public void shouldGiveFocus() {
  216. giveFocusAndVerifyThatHasFocus(textFieldWithPopup);
  217. giveFocusAndVerifyThatHasFocus(textFieldWithoutPopup);
  218. }
  219. private void giveFocusAndVerifyThatHasFocus(Component c) {
  220. robot.focus(c);
  221. pause(500);
  222. assertThat(c.isFocusOwner()).isTrue();
  223. }
  224. public void shouldGiveFocusAndWaitUntilComponentHasFocus() {
  225. robot.focusAndWaitForFocusGain(textFieldWithPopup);
  226. assertThat(textFieldWithPopup.isFocusOwner()).isTrue();
  227. robot.focusAndWaitForFocusGain(textFieldWithoutPopup);
  228. assertThat(textFieldWithoutPopup.isFocusOwner()).isTrue();
  229. }
  230. private JPopupMenu popupMenu() {
  231. return frame.popupMenu;
  232. }
  233. public void shouldPassIfNoJOptionPaneIsShowing() {
  234. robot.requireNoJOptionPaneIsShowing();
  235. }
  236. public void shouldFailIfJOptionPaneIsShowingAndExpectingNotShowing() throws Exception {
  237. robot.click(frame.button);
  238. pause(500);
  239. try {
  240. robot.requireNoJOptionPaneIsShowing();
  241. fail("Expecting AssertionError");
  242. } catch (AssertionError e) {
  243. assertThat(e).message().contains("Expecting no JOptionPane to be showing");
  244. }
  245. }
  246. private static class MyFrame extends TestFrame {
  247. private static final long serialVersionUID = 1L;
  248. final JTextField textFieldWithPopup = new JTextField("With Pop-up Menu");
  249. final JTextField textFieldWithoutPopup = new JTextField("Without Pop-up Menu");
  250. final JButton button = new JButton("Click Me");
  251. final JPopupMenu popupMenu = new JPopupMenu("Pop-up Menu");
  252. MyFrame() {
  253. super(RobotFixtureTest.class);
  254. add(textFieldWithPopup);
  255. add(textFieldWithoutPopup);
  256. add(button);
  257. button.addActionListener(new ActionListener() {
  258. public void actionPerformed(ActionEvent e) {
  259. JOptionPane.showMessageDialog(MyFrame.this, "A Message");
  260. }
  261. });
  262. textFieldWithPopup.setComponentPopupMenu(popupMenu);
  263. textFieldWithoutPopup.setName("withoutPopup");
  264. popupMenu.add(new JMenuItem("Luke"));
  265. popupMenu.add(new JMenuItem("Leia"));
  266. }
  267. }
  268. }