0001: package abbot.tester;
0002:
0003: import javax.swing.event.MenuEvent;
0004:
0005: import java.awt.*;
0006: import java.awt.event.*;
0007: import java.io.*;
0008: import javax.swing.*;
0009: import javax.swing.border.EmptyBorder;
0010:
0011: import junit.extensions.abbot.*;
0012: import junit.extensions.abbot.Timer;
0013: import abbot.*;
0014: import abbot.util.AWT;
0015: import abbot.util.WeakAWTEventListener;
0016: import abbot.util.Bugs;
0017:
0018: /** Unit test to verify Robot operation. */
0019:
0020: public class RobotTest extends ComponentTestFixture {
0021:
0022: class Flag {
0023: volatile boolean flag;
0024: }
0025:
0026: public void testWaitForIdle() {
0027: final Flag flag = new Flag();
0028: SwingUtilities.invokeLater(new Runnable() {
0029: public void run() {
0030: try {
0031: Thread.sleep(1000);
0032: } catch (InterruptedException e) {
0033: }
0034: flag.flag = true;
0035: }
0036: });
0037: getRobot().waitForIdle();
0038: assertTrue("Did not wait for event dispatch to finish",
0039: flag.flag);
0040: }
0041:
0042: private volatile boolean pump;
0043: private volatile boolean waitStarted;
0044: private volatile boolean waitTerminated;
0045: private volatile boolean gotTimeout;
0046:
0047: // FIXME: on fast CPUs running linux (up through 1.5),
0048: // it has been found that waitForIdle will wait forever
0049: // when a dialog is shown. need to reproduce that scenario
0050: // here to ensure we don't hang.
0051: public void xtestWaitForIdleTimeout() throws Exception {
0052: Robot robot = new Robot() {
0053: protected boolean isQueueBlocked(EventQueue q) {
0054: return false;
0055: }
0056:
0057: protected boolean postInvocationEvent(EventQueue q,
0058: Toolkit toolkit, long timeout) {
0059: if (super .postInvocationEvent(q, toolkit, timeout)) {
0060: System.err.println("x");
0061: pump = false;
0062: gotTimeout = true;
0063: return true;
0064: }
0065: return false;
0066: }
0067: };
0068:
0069: final Runnable EMPTY = new Runnable() {
0070: public void run() {
0071: if (pump) {
0072: System.err.print(".");
0073: SwingUtilities.invokeLater(this );
0074: }
0075: }
0076: };
0077: JFrame frame = new JFrame(getName());
0078: showWindow(frame);
0079:
0080: pump = true;
0081: // Put wait for idle on a different thread in case it blocks
0082: new Thread("wait for idle") {
0083: public void run() {
0084: waitStarted = true;
0085: System.err.println("start pump");
0086: try {
0087: SwingUtilities.invokeAndWait(EMPTY);
0088: } catch (Throwable t) {
0089: }
0090: System.err.println("waitForIdle");
0091: getRobot().waitForIdle();
0092: waitTerminated = true;
0093: }
0094: }.start();
0095:
0096: while (!waitStarted) {
0097: Thread.sleep(100);
0098: }
0099: System.err.println("test thread wait");
0100: Timer timer = new Timer();
0101: while (!waitTerminated) {
0102: if (timer.elapsed() > 10000) {
0103: fail("Idle wait should not block unreasonably long");
0104: }
0105: System.err.print("+");
0106: Thread.sleep(10);
0107: }
0108: pump = false;
0109: assertTrue("Idle wait should time out", gotTimeout);
0110: }
0111:
0112: public void testEventPostDelay() throws Exception {
0113: if (Robot.getEventMode() != Robot.EM_ROBOT)
0114: return;
0115: JLabel label = new JLabel(getName());
0116: MouseWatcher ml = new MouseWatcher();
0117: label.addMouseListener(ml);
0118: showFrame(label);
0119: java.awt.Robot r = Robot.getRobot();
0120: Point pt = label.getLocationOnScreen();
0121: r.mouseMove(pt.x + label.getWidth() / 2, pt.y
0122: + label.getHeight() / 2);
0123: r.mousePress(MouseEvent.BUTTON1_MASK);
0124: r.mouseRelease(MouseEvent.BUTTON1_MASK);
0125: r.delay(Robot.getEventPostDelay());
0126: long now = System.currentTimeMillis();
0127: robot.waitForIdle();
0128: if (!ml.gotPress) {
0129: Timer timer = new Timer();
0130: while (!ml.gotPress) {
0131: robot.delay(5);
0132: if (timer.elapsed() > 5000)
0133: fail("Mouse press never registered");
0134: }
0135: long arrived = System.currentTimeMillis();
0136: fail("MOUSE_PRESSED event not yet generated, " + "after "
0137: + Robot.getEventPostDelay()
0138: + "ms, actual additional delay was "
0139: + (arrived - now) + "ms");
0140: }
0141: }
0142:
0143: /** Ensure image capture gets the right image. */
0144: public void testImageCapture() throws Throwable {
0145: // Don't need to test this under AWT mode, where we might get a false
0146: // negative anyway
0147: if (Robot.getEventMode() == Robot.EM_AWT)
0148: return;
0149:
0150: final int X = 100;
0151: final int Y = 100;
0152: File gif = new File("test/abbot/tester/image.gif");
0153: ImageIcon icon = new ImageIcon(gif.toURL());
0154: JLabel label1 = new JLabel(icon);
0155: JLabel label2 = new JLabel(icon);
0156: final JFrame frame = new JFrame(getName());
0157: frame.setLocation(X, Y);
0158: JPanel pane = (JPanel) frame.getContentPane();
0159: pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
0160: //Ensure OSX growbox doesn't obscure the picture
0161: pane.setBorder(new EmptyBorder(20, 20, 20, 20));
0162: pane.add(label1);
0163: pane.add(label2);
0164: frame.pack();
0165: frame.setResizable(false);
0166: frame.setLocation(X, Y);
0167: showWindow(frame);
0168: robot.focus(frame, true);
0169:
0170: ImageComparator ic = new ImageComparator();
0171: int status = icon.getImageLoadStatus();
0172: int DONE = MediaTracker.ERRORED | MediaTracker.ABORTED
0173: | MediaTracker.COMPLETE;
0174: Timer timer = new Timer();
0175: while ((status & DONE) == 0) {
0176: if (timer.elapsed() > 5000)
0177: fail("Icon image failed to load or error within 5s");
0178: robot.sleep();
0179: status = icon.getImageLoadStatus();
0180: }
0181: if ((status & DONE) != MediaTracker.COMPLETE) {
0182: fail("Icon image load failed: status=" + status);
0183: }
0184: robot.activate(frame);
0185: robot.waitForIdle();
0186:
0187: Image image = robot.capture(label1);
0188: assertEquals("Captured wrong image (1)", 0, ic.compare(image,
0189: gif));
0190: image = robot.capture(label2);
0191: assertEquals("Captured wrong image (2)", 0, ic.compare(image,
0192: gif));
0193: }
0194:
0195: private class MenuListener implements ActionListener {
0196: public int actionCount = 0;
0197: public boolean gotAction = false;
0198:
0199: public void actionPerformed(ActionEvent ev) {
0200: ++actionCount;
0201: gotAction = true;
0202: }
0203: }
0204:
0205: public void testSelectAWTMenuItem() {
0206: MenuBar mb = new MenuBar();
0207: Menu menu = new Menu("File");
0208: menu.add(new MenuItem("One"));
0209: MenuItem mi = new MenuItem("Two");
0210: MenuListener ml1 = new MenuListener();
0211: MenuListener ml2 = new MenuListener();
0212: mi.addActionListener(ml1);
0213: menu.add(mi);
0214: Menu sub = new Menu("Submenu");
0215: sub.add(new MenuItem("Sub one"));
0216: MenuItem mi2 = new MenuItem("Sub two");
0217: mi2.addActionListener(ml2);
0218: sub.add(mi2);
0219: menu.add(sub);
0220: mb.add(menu);
0221: JFrame frame = new JFrame(getName());
0222: frame.setMenuBar(mb);
0223: // Ensure we have a proper size for linux
0224: showWindow(frame, new Dimension(200, 100));
0225: /*
0226: robot.selectAWTMenuItem(mi);
0227: robot.waitForIdle();
0228: assertTrue("AWT Menu item in menubar menu not hit", ml1.gotAction);
0229: robot.selectAWTMenuItem(mi2);
0230: robot.waitForIdle();
0231: assertTrue("AWT Menu item in menubar submenu not hit", ml2.gotAction);
0232:
0233: ml1.gotAction = ml2.gotAction = false;
0234: robot.selectAWTMenuItem(frame, mi.getLabel());
0235: robot.waitForIdle();
0236: assertTrue("AWT Menu item in menubar menu not hit", ml1.gotAction);
0237: robot.selectAWTMenuItem(frame, mi2.getLabel());
0238: robot.waitForIdle();
0239: assertTrue("AWT Menu item in menubar submenu not hit", ml2.gotAction);
0240: */
0241: ml1.gotAction = false;
0242: String PATH = "File|Two";
0243: robot.selectAWTMenuItem(frame, PATH);
0244: robot.waitForIdle();
0245: assertTrue("AWT Menu item identified by path '" + PATH
0246: + "' not hit", ml1.gotAction);
0247: }
0248:
0249: public void testAWTPopupMenuSelection() {
0250: if (Robot.getEventMode() == Robot.EM_AWT
0251: && Bugs.showAWTPopupMenuBlocks()) {
0252: //fail("This test would block");
0253: return;
0254: }
0255:
0256: PopupMenu popup = new PopupMenu();
0257: popup.add(new MenuItem("One"));
0258: MenuItem mi = new MenuItem("Two");
0259: MenuListener ml1 = new MenuListener();
0260: MenuListener ml2 = new MenuListener();
0261: mi.addActionListener(ml1);
0262: popup.add(mi);
0263: Menu sub = new Menu("Submenu");
0264: sub.add(new MenuItem("Sub one"));
0265: MenuItem mi2 = new MenuItem("Sub two");
0266: mi2.addActionListener(ml2);
0267: sub.add(mi2);
0268: popup.add(sub);
0269: JFrame frame = new JFrame(getName());
0270: frame.add(popup);
0271: showWindow(frame, new Dimension(200, 200));
0272:
0273: robot.selectAWTPopupMenuItem(mi);
0274: robot.waitForIdle();
0275: assertTrue("AWT PopupMenu item not hit", ml1.gotAction);
0276: robot.selectAWTPopupMenuItem(mi2);
0277: robot.waitForIdle();
0278: assertTrue("AWT PopupMenu item in submenu not hit",
0279: ml2.gotAction);
0280:
0281: ml1.gotAction = ml2.gotAction = false;
0282: robot.selectAWTPopupMenuItem(frame, mi.getLabel());
0283: robot.waitForIdle();
0284: assertTrue("AWT PopupMenu item not hit", ml1.gotAction);
0285: robot.selectAWTPopupMenuItem(frame, mi2.getLabel());
0286: robot.waitForIdle();
0287: assertTrue("AWT PopupMenu item in submenu not hit",
0288: ml2.gotAction);
0289: }
0290:
0291: public void testSelectMenuItem() {
0292: JMenuBar mb = new JMenuBar();
0293: JMenu menu = new JMenu("File");
0294: JMenuItem mi0 = new JMenuItem("One");
0295: MenuListener ml0 = new MenuListener();
0296: mi0.addActionListener(ml0);
0297: menu.add(mi0);
0298: JMenuItem mi1 = new JMenuItem("Two");
0299: MenuListener ml1 = new MenuListener();
0300: mi1.addActionListener(ml1);
0301: menu.add(mi1);
0302: JMenu sub = new JMenu("Submenu");
0303: sub.add(new JMenuItem("Sub one"));
0304: JMenuItem mi2 = new JMenuItem("Sub two");
0305: MenuListener ml2 = new MenuListener();
0306: mi2.addActionListener(ml2);
0307: sub.add(mi2);
0308: menu.add(sub);
0309: mb.add(menu);
0310: JFrame frame = new JFrame(getName());
0311: frame.getContentPane().add(new JLabel(getName()));
0312: frame.setJMenuBar(mb);
0313: showWindow(frame);
0314:
0315: robot.selectMenuItem(mi0);
0316: robot.waitForIdle();
0317: assertTrue("Standard menu item 0 not selected", ml0.gotAction);
0318:
0319: robot.selectMenuItem(mi1);
0320: robot.waitForIdle();
0321: assertTrue("Standard menu item 1 not selected", ml1.gotAction);
0322: }
0323:
0324: public void testSelectMenuItemByPath() {
0325: JMenuBar mb = new JMenuBar();
0326: JMenu menu = new JMenu("File");
0327:
0328: JMenuItem mi1 = new JMenuItem("Two");
0329: JMenu sub = new JMenu("Submenu");
0330: JMenuItem mi2 = new JMenuItem("File");
0331: MenuListener ml = new MenuListener();
0332: mi2.addActionListener(ml);
0333:
0334: mb.add(menu);
0335: menu.add(new JMenuItem("One"));
0336: menu.add(sub);
0337: sub.add(new JMenuItem("Sub one"));
0338: sub.add(mi2);
0339:
0340: JFrame frame = new JFrame(getName());
0341: frame.getContentPane().add(new JLabel(getName()));
0342: frame.setJMenuBar(mb);
0343: showWindow(frame);
0344:
0345: String path = menu.getText() + "|" + sub.getText() + "|"
0346: + mi2.getText();
0347: robot.selectMenuItem(frame, path);
0348: assertTrue("Select menu item by path failed", ml.gotAction);
0349: }
0350:
0351: public void testSelectMenuItemByPathLazyLoad() {
0352: JMenuBar mb = new JMenuBar();
0353: JMenu menu = new JMenu("File");
0354:
0355: JMenuItem mi1 = new JMenuItem("Two");
0356: final JMenu sub = new JMenu("Submenu");
0357: final JMenuItem mi2 = new JMenuItem("File");
0358: MenuListener ml = new MenuListener();
0359: mi2.addActionListener(ml);
0360:
0361: mb.add(menu);
0362: menu.add(new JMenuItem("One"));
0363: menu.add(sub);
0364:
0365: sub.addMenuListener(new javax.swing.event.MenuListener() {
0366: public void menuCanceled(MenuEvent e) {
0367: }
0368:
0369: public void menuDeselected(MenuEvent e) {
0370: }
0371:
0372: public void menuSelected(MenuEvent e) {
0373: sub.add(new JMenuItem("Sub one"));
0374: sub.add(mi2);
0375: }
0376: });
0377:
0378: JFrame frame = new JFrame(getName());
0379: frame.getContentPane().add(new JLabel(getName()));
0380: frame.setJMenuBar(mb);
0381: showWindow(frame);
0382:
0383: String path = menu.getText() + "|" + sub.getText() + "|"
0384: + mi2.getText();
0385: robot.selectMenuItem(frame, path);
0386: assertTrue("Select menu item by path failed", ml.gotAction);
0387: }
0388:
0389: public void testSelectPopupMenuItemByPathLazyLoad() {
0390: final JPopupMenu popup = new JPopupMenu();
0391: JMenu menu = new JMenu("File");
0392:
0393: JMenuItem mi1 = new JMenuItem("Two");
0394: final JMenu sub = new JMenu("Submenu");
0395: final JMenuItem mi2 = new JMenuItem("File");
0396: MenuListener ml = new MenuListener();
0397: mi2.addActionListener(ml);
0398:
0399: popup.add(menu);
0400: menu.add(new JMenuItem("One"));
0401: menu.add(sub);
0402:
0403: sub.addMenuListener(new javax.swing.event.MenuListener() {
0404: public void menuCanceled(MenuEvent e) {
0405: }
0406:
0407: public void menuDeselected(MenuEvent e) {
0408: }
0409:
0410: public void menuSelected(MenuEvent e) {
0411: sub.add(new JMenuItem("Sub one"));
0412: sub.add(mi2);
0413: }
0414: });
0415:
0416: JFrame frame = new JFrame(getName());
0417: JLabel component = new JLabel(getName());
0418: component.addMouseListener(new MouseAdapter() {
0419: public void mouseClicked(MouseEvent e) {
0420: test(e);
0421: }
0422:
0423: public void mousePressed(MouseEvent e) {
0424: test(e);
0425: }
0426:
0427: public void mouseReleased(MouseEvent e) {
0428: test(e);
0429: }
0430:
0431: private void test(MouseEvent e) {
0432: if (e.isPopupTrigger()) {
0433: popup.show((Component) e.getSource(), e.getX(), e
0434: .getY());
0435: }
0436: }
0437:
0438: });
0439: frame.getContentPane().add(component);
0440: showWindow(frame);
0441:
0442: String path = menu.getText() + "|" + sub.getText() + "|"
0443: + mi2.getText();
0444: robot.selectPopupMenuItem(component, new ComponentLocation(
0445: new Point(10, 10)), path);
0446: assertTrue("Select menu item by path failed", ml.gotAction);
0447: }
0448:
0449: public void testSelectMenuItemLongItemTraversal() {
0450: // The following should not cause problems:
0451: // ------------
0452: // |menu|menu1|
0453: // ------------------------
0454: // |really long menu item |
0455: // ------------------------
0456: JMenuBar mb = new JMenuBar();
0457: JMenu menu1 = new JMenu("A");
0458: JMenuItem mi = new JMenuItem(
0459: "This is a really long menu item such that the center of the item is way out there");
0460: menu1.add(mi);
0461: JMenu menu2 = new JMenu("B");
0462: menu2.add(new JMenuItem("About"));
0463: mb.add(menu1);
0464: mb.add(menu2);
0465: MenuListener ml = new MenuListener();
0466: mi.addActionListener(ml);
0467: JFrame frame = new JFrame(getName());
0468: frame.getContentPane().add(new JLabel(getName()));
0469: frame.setJMenuBar(mb);
0470: showWindow(frame);
0471:
0472: robot.selectMenuItem(mi);
0473: assertTrue("Long menu item missed", ml.gotAction);
0474: }
0475:
0476: public void testSelectSubmenuItem() {
0477: JMenuBar mb = new JMenuBar();
0478: JMenu menu = new JMenu("File");
0479: menu.add(new JMenuItem("One"));
0480: JMenuItem mi1 = new JMenuItem("Two");
0481: MenuListener ml1 = new MenuListener();
0482: mi1.addActionListener(ml1);
0483: menu.add(mi1);
0484: JMenu sub = new JMenu("Submenu");
0485: sub.add(new JMenuItem("Sub one"));
0486: JMenuItem mi2 = new JMenuItem("Sub two");
0487: MenuListener ml2 = new MenuListener();
0488: mi2.addActionListener(ml2);
0489: sub.add(mi2);
0490: menu.add(sub);
0491: mb.add(menu);
0492: JFrame frame = new JFrame(getName());
0493: frame.getContentPane().add(new JLabel(getName()));
0494: frame.setJMenuBar(mb);
0495: showWindow(frame);
0496:
0497: robot.selectMenuItem(mi2);
0498: assertTrue("Submenu item not selected", ml2.gotAction);
0499: }
0500:
0501: public void testMenuSelectionWithParentShowing() {
0502: JMenuBar mb = new JMenuBar();
0503: JMenu menu = new JMenu("File");
0504: menu.add(new JMenuItem("One"));
0505: JMenuItem mi1 = new JMenuItem("Two");
0506: MenuListener ml1 = new MenuListener();
0507: mi1.addActionListener(ml1);
0508: menu.add(mi1);
0509: mb.add(menu);
0510:
0511: JFrame frame = new JFrame(getName());
0512: frame.getContentPane().add(new JLabel(getName()));
0513: frame.setJMenuBar(mb);
0514: showWindow(frame);
0515:
0516: robot.click(menu);
0517: robot.waitForIdle();
0518: robot.selectMenuItem(mi1);
0519: robot.waitForIdle();
0520: assertTrue(
0521: "Menu it main menu not hit when parent already open",
0522: ml1.gotAction);
0523: }
0524:
0525: public void testSelectNestedMenuItem() {
0526: // Repeatedly select a nested menu item
0527: JMenuBar mb = new JMenuBar();
0528: mb.add(new JMenu("Action Management"));
0529: JMenu menu = new JMenu("Risk Management");
0530: mb.add(menu);
0531: mb.add(new JMenu("BPComs"));
0532: mb.add(new JMenu("Tools"));
0533: mb.add(new JMenu("Actions"));
0534: mb.add(new JMenu("Window"));
0535: mb.add(new JMenu("Help"));
0536: JMenu sub1 = new JMenu("Position Enquiry");
0537: menu.add(sub1);
0538: menu.add(new JMenu("Static Data Management"));
0539: menu.add(new JMenu("Excession Management"));
0540: sub1.add(new JMenu("Cash Account"));
0541: JMenu sub2 = new JMenu("Safekeeping Account");
0542: sub1.add(sub2);
0543: sub1.add(new JMenu("Client"));
0544: sub1.add(new JMenu("RCA"));
0545: JMenuItem mi = new JMenuItem("Search...");
0546: sub2.add(mi);
0547: MenuListener ml = new MenuListener();
0548: mi.addActionListener(ml);
0549: JFrame frame = new JFrame(getName());
0550: frame.setJMenuBar(mb);
0551: showWindow(frame);
0552:
0553: int REPEAT = 5;
0554: for (int i = 0; i < REPEAT; i++) {
0555: robot.selectMenuItem(mi);
0556: robot.waitForIdle();
0557: }
0558: assertEquals("Some menu activations failed", REPEAT,
0559: ml.actionCount);
0560: }
0561:
0562: private class MouseWatcher extends MouseAdapter {
0563: public volatile boolean gotPress = false;
0564: public volatile boolean gotRelease = false;
0565: public volatile boolean gotClick = false;
0566: public volatile int clickCount = 0;
0567: public volatile boolean popupTrigger = false;
0568: public volatile Component source = null;
0569: public volatile int modifiers = 0;
0570: public volatile Point where = null;
0571:
0572: public void mousePressed(MouseEvent me) {
0573: gotPress = true;
0574: source = me.getComponent();
0575: popupTrigger = me.isPopupTrigger();
0576: modifiers = me.getModifiers();
0577: where = me.getPoint();
0578: }
0579:
0580: public void mouseReleased(MouseEvent me) {
0581: gotRelease = true;
0582: popupTrigger = popupTrigger || me.isPopupTrigger();
0583: }
0584:
0585: public void mouseClicked(MouseEvent me) {
0586: gotClick = true;
0587: clickCount = me.getClickCount();
0588: }
0589: }
0590:
0591: /** Verify proper click operation. You may need to set
0592: Robot.mouseReleaseDelay if this fails intermittently. */
0593: public void testClick() {
0594: MouseWatcher mw = new MouseWatcher();
0595: JFrame frame = new JFrame(getName());
0596: frame.addMouseListener(mw);
0597: showWindow(frame, new Dimension(200, 200), true);
0598: robot.click(frame);
0599: robot.waitForIdle();
0600: assertTrue("Never received press", mw.gotPress);
0601: assertEquals("Wrong event source", frame, mw.source);
0602: assertTrue("Never received release", mw.gotRelease);
0603: assertTrue("Never received click", mw.gotClick);
0604: assertEquals("No modifiers expected", AWT
0605: .getMouseModifiers(MouseEvent.BUTTON1_MASK), AWT
0606: .getMouseModifiers(mw.modifiers));
0607: assertTrue("Unexpected popup trigger", !mw.popupTrigger);
0608: }
0609:
0610: /** Double clicks. */
0611: public void testMultipleClicks() {
0612: MouseWatcher mw = new MouseWatcher();
0613: JFrame frame = new JFrame(getName());
0614: showWindow(frame, new Dimension(200, 200));
0615: JPanel pane = (JPanel) frame.getContentPane();
0616: pane.addMouseListener(mw);
0617: robot.click(pane, pane.getWidth() / 2, pane.getHeight() / 2,
0618: InputEvent.BUTTON1_MASK, 2);
0619: Timer timer = new Timer();
0620: while (mw.clickCount < 2) {
0621: if (timer.elapsed() > EVENT_GENERATION_DELAY)
0622: fail("Never received a double click");
0623: robot.sleep();
0624: }
0625: }
0626:
0627: public void testClickAt() {
0628: MouseWatcher mw = new MouseWatcher();
0629: JFrame frame = new JFrame(getName());
0630: frame.addMouseListener(mw);
0631: showWindow(frame, new Dimension(200, 200));
0632: // Make sure we don't click in the title or border...
0633: Insets insets = frame.getInsets();
0634: Point at = new Point(insets.left, insets.top);
0635: robot.click(frame, at.x, at.y);
0636: robot.waitForIdle();
0637: assertTrue("Never received press", mw.gotPress);
0638: assertEquals("Wrong event source", frame, mw.source);
0639: assertTrue("Never received release", mw.gotRelease);
0640: assertTrue("Never received click", mw.gotClick);
0641: assertEquals("Wrong mouse coordinates", at, mw.where);
0642: assertEquals("No modifiers expected", AWT
0643: .getMouseModifiers(MouseEvent.BUTTON1_MASK), AWT
0644: .getMouseModifiers(mw.modifiers));
0645: assertTrue("Unexpected popup trigger", !mw.popupTrigger);
0646: }
0647:
0648: public void testClickPopup() {
0649: MouseWatcher mw = new MouseWatcher();
0650: JFrame frame = new JFrame(getName());
0651: frame.addMouseListener(mw);
0652: showWindow(frame, new Dimension(200, 200));
0653: robot.click(frame, AWTConstants.POPUP_MASK);
0654: robot.waitForIdle();
0655: assertTrue("Never received press", mw.gotPress);
0656: assertEquals("Wrong event source", frame, mw.source);
0657: assertTrue("Never received release", mw.gotRelease);
0658: assertTrue("Never received click", mw.gotClick);
0659: assertEquals("Wrong modifiers", AWT
0660: .getMouseModifiers(AWTConstants.POPUP_MASK), AWT
0661: .getMouseModifiers(mw.modifiers));
0662: assertTrue("Should be popup trigger", mw.popupTrigger);
0663: }
0664:
0665: public void testClickTertiary() {
0666: MouseWatcher mw = new MouseWatcher();
0667: JFrame frame = new JFrame(getName());
0668: frame.addMouseListener(mw);
0669: showWindow(frame, new Dimension(200, 200));
0670: int mask = AWTConstants.TERTIARY_MASK;
0671: robot.click(frame, mask);
0672: robot.waitForIdle();
0673: // w32 uses CTRL as a modifier prior to 1.4, I guess in case you don't
0674: // have a 3-button mouse.
0675: if (Platform.isWindows()
0676: && Platform.JAVA_VERSION < Platform.JAVA_1_4) {
0677: mask |= InputEvent.CTRL_MASK;
0678: }
0679:
0680: assertTrue("Never received press", mw.gotPress);
0681: assertEquals("Wrong event source", frame, mw.source);
0682: assertTrue("Never received release", mw.gotRelease);
0683: assertTrue("Never received click", mw.gotClick);
0684: assertEquals("Wrong modifiers", AWT.getMouseModifiers(mask),
0685: AWT.getMouseModifiers(mw.modifiers));
0686: assertTrue("Unexpected popup trigger", !mw.popupTrigger);
0687: }
0688:
0689: public void testFocusSingleFrame() {
0690: JTextField tf = new JTextField("Default focus goes here");
0691: JTextField tf2 = new JTextField("Requested focus goes here");
0692: JPanel pane = new JPanel();
0693: pane.add(tf);
0694: pane.add(tf2);
0695: showFrame(pane);
0696: robot.focus(tf2, true);
0697: assertTrue("Default TextField should not have focus", !tf
0698: .hasFocus());
0699: assertTrue("Next TextField should have focus", tf2.hasFocus());
0700: robot.focus(tf, true);
0701: assertTrue("First TextField should have focus", tf.hasFocus());
0702: }
0703:
0704: // FIXME sporadic failures on linux (AWT mode)
0705: public void testFocusMultipleFrames() {
0706: JTextField tf = new JTextField("Default focus goes here");
0707: JTextField tf2 = new JTextField("Requested focus goes here");
0708: JTextField tf3 = new JTextField("Next it goes here");
0709: JPanel pane = new JPanel();
0710: pane.add(tf);
0711: pane.add(tf2);
0712: showFrame(pane);
0713: JFrame frame = new JFrame(getName() + " 2");
0714: frame.getContentPane().add(tf3);
0715: showWindow(frame);
0716: robot.focus(tf2, true);
0717: assertTrue("Second text field didn't get focus", tf2.hasFocus());
0718: robot.focus(tf3, true);
0719: assertTrue("Third text field didn't get focus", tf3.hasFocus());
0720: }
0721:
0722: private class KeyWatcher extends KeyAdapter {
0723: public volatile boolean gotPress = false;
0724: public volatile boolean gotRelease = false;
0725: public volatile boolean gotTyped = false;
0726: public volatile int keyCode = KeyEvent.VK_UNDEFINED;
0727: public volatile int modifiers = 0;
0728:
0729: public void keyPressed(KeyEvent ke) {
0730: gotPress = true;
0731: keyCode = ke.getKeyCode();
0732: modifiers = ke.getModifiers();
0733: }
0734:
0735: public void keyReleased(KeyEvent ke) {
0736: gotRelease = true;
0737: }
0738:
0739: public void keyTyped(KeyEvent ke) {
0740: gotTyped = true;
0741: }
0742: }
0743:
0744: public void testPlainKey() {
0745: KeyWatcher kw = new KeyWatcher();
0746: JTextField tf = new JTextField();
0747: tf.setColumns(10);
0748: tf.addKeyListener(kw);
0749: JPanel pane = new JPanel();
0750: pane.add(tf);
0751: showFrame(pane);
0752: robot.focus(tf, true);
0753: int code = KeyEvent.VK_A;
0754: robot.key(code);
0755: robot.waitForIdle();
0756: assertTrue("Never received key press", kw.gotPress);
0757: assertTrue("Never received release", kw.gotRelease);
0758: assertTrue("Never received type", kw.gotTyped);
0759: assertEquals("Wrong key code", code, kw.keyCode);
0760: assertEquals("Wrong modifiers", 0, kw.modifiers);
0761: }
0762:
0763: public void testKey() {
0764: JTextField tf1 = new JTextField();
0765: KeyWatcher kw = new KeyWatcher();
0766: tf1.addKeyListener(kw);
0767: showFrame(tf1);
0768: robot.focus(tf1, true);
0769: robot.key(KeyEvent.VK_A);
0770: robot.waitForIdle();
0771: assertTrue("Never received key press", kw.gotPress);
0772: assertEquals("Wrong key code", KeyEvent.VK_A, kw.keyCode);
0773: assertEquals("Wrong modifiers", 0, kw.modifiers);
0774:
0775: kw.gotPress = false;
0776: robot.key(KeyEvent.VK_A, KeyEvent.SHIFT_MASK);
0777: robot.waitForIdle();
0778: assertTrue("Never received key press", kw.gotPress);
0779: assertEquals("Wrong key code", KeyEvent.VK_A, kw.keyCode);
0780: assertEquals("Wrong modifiers", KeyEvent.SHIFT_MASK,
0781: kw.modifiers);
0782: }
0783:
0784: /** Generate some things which we know we don't have key mappings for. */
0785: public void testKeyString() {
0786: JPanel pane = new JPanel();
0787: JTextField tf1 = new JTextField();
0788: JTextField tf2 = new JTextField();
0789: tf1.setColumns(10);
0790: tf2.setColumns(10);
0791: pane.add(tf1);
0792: pane.add(tf2);
0793: Frame frame = showFrame(pane);
0794: robot.activate(frame);
0795: robot.waitForIdle();
0796: robot.focus(tf1, true);
0797: String keymap = "The quick brown fox jumped over the lazy dog"
0798: + "`1234567890-=~!@#$%^&*()_+[]\\{}|;':\",./<>?";
0799: robot.keyString(keymap);
0800: robot.waitForIdle();
0801: assertEquals("Wrong text typed", keymap, tf1.getText());
0802:
0803: // Throw in some Unicode; should work just as well for any other...
0804: String string = "Un \u00e9l\u00e9ment gr\u00e2ce \u00e0 l'index";
0805: robot.focus(tf2, true);
0806: robot.keyString(string);
0807: robot.waitForIdle();
0808: assertEquals("Wrong non-keymap text typed", string, tf2
0809: .getText());
0810: }
0811:
0812: public void testEndKey() {
0813: TextField tf = new TextField(getName());
0814: KeyWatcher watcher = new KeyWatcher();
0815: tf.addKeyListener(watcher);
0816: showFrame(tf);
0817: robot.focus(tf, true);
0818: robot.key(KeyEvent.VK_END);
0819: robot.waitForIdle();
0820: Timer timer = new Timer();
0821: while (!watcher.gotPress) {
0822: if (timer.elapsed() > EVENT_GENERATION_DELAY)
0823: fail("Never got key press");
0824: robot.sleep();
0825: }
0826: assertEquals("Wrong keycode was generated", KeyEvent.VK_END,
0827: watcher.keyCode);
0828: while (!watcher.gotRelease) {
0829: if (timer.elapsed() > EVENT_GENERATION_DELAY)
0830: fail("Never got key release");
0831: robot.sleep();
0832: }
0833: }
0834:
0835: public void testFindFocusOwner() {
0836: JPanel pane = new JPanel();
0837: JTextField tf1 = new JTextField("tf 1");
0838: tf1.setName("tf 1");
0839: JTextField tf2 = new JTextField("tf 2");
0840: tf2.setName("tf 2");
0841: pane.add(tf1);
0842: pane.add(tf2);
0843: showFrame(pane);
0844: robot.focus(tf2, true);
0845: assertEquals("Wrong focus detected", Robot.toString(tf2), Robot
0846: .toString(robot.findFocusOwner()));
0847: }
0848:
0849: public void testIconify() throws Throwable {
0850: Frame frame = showFrame(new JLabel(getName()));
0851: robot.iconify(frame);
0852: // Give the WM time to put the window away
0853: Timer timer = new Timer();
0854: while (frame.getState() != Frame.ICONIFIED) {
0855: if (timer.elapsed() > EVENT_GENERATION_DELAY)
0856: fail("Frame not iconified, state " + frame.getState());
0857: robot.sleep();
0858: }
0859: robot.deiconify(frame);
0860: timer.reset();
0861: while (frame.getState() != Frame.NORMAL) {
0862: if (timer.elapsed() > EVENT_GENERATION_DELAY)
0863: fail("Frame not restored, state " + frame.getState());
0864: robot.sleep();
0865: }
0866: }
0867:
0868: public void testMaximize() {
0869: Frame frame = showFrame(new JLabel(getName()));
0870: Dimension size = frame.getSize();
0871: robot.maximize(frame);
0872: Timer timer = new Timer();
0873: while (frame.getSize().equals(size)) {
0874: if (timer.elapsed() > EVENT_GENERATION_DELAY)
0875: fail("Frame size not changed");
0876: robot.sleep();
0877: }
0878: robot.normalize(frame);
0879: // Don't bother testing normalize (it won't work on 1.3.1)
0880: }
0881:
0882: private class EnterExitListener implements AWTEventListener {
0883: public AWTEvent exited;
0884: public AWTEvent entered;
0885:
0886: public void eventDispatched(AWTEvent event) {
0887: if (event.getID() == MouseEvent.MOUSE_ENTERED) {
0888: entered = event;
0889: } else if (event.getID() == MouseEvent.MOUSE_EXITED) {
0890: exited = event;
0891: }
0892: }
0893: }
0894:
0895: public void testEnterExitGeneration() {
0896: JPanel panel = new JPanel();
0897: JLabel label1 = new JLabel("Source");
0898: label1.setName("label 1");
0899: JLabel label2 = new JLabel("Destination");
0900: label2.setName("label 2");
0901:
0902: panel.add(label1);
0903: panel.add(label2);
0904: Frame f = showFrame(panel, new Dimension(200, 200));
0905: f.setName(getName());
0906: // Ensure we have a state to start with
0907: robot.mouseMove(label1);
0908: robot.waitForIdle();
0909: // Add the listener *after* the mouse is within the first component
0910: EnterExitListener eel = new EnterExitListener();
0911: new WeakAWTEventListener(eel, MouseEvent.MOUSE_EVENT_MASK);
0912: robot.mouseMove(label2);
0913: robot.waitForIdle();
0914:
0915: assertEquals("Expect no exit event w/o motion listeners", null,
0916: eel.exited);
0917: assertEquals("Expect no enter event w/o motion listeners",
0918: null, eel.entered);
0919:
0920: MouseListener ml = new MouseAdapter() {
0921: };
0922: label1.addMouseListener(ml);
0923: label2.addMouseListener(ml);
0924:
0925: robot.mouseMove(label1);
0926: robot.waitForIdle();
0927:
0928: assertEquals("Expect no exit event (last component was frame)",
0929: null, eel.exited);
0930: assertEquals("Enter event was expected", label1, eel.entered
0931: .getSource());
0932:
0933: eel.entered = eel.exited = null;
0934:
0935: robot.mouseMove(label2);
0936: robot.waitForIdle();
0937: assertEquals("Exit event was expected", label1, eel.exited
0938: .getSource());
0939: assertEquals("Enter event was expected", label2, eel.entered
0940: .getSource());
0941: }
0942:
0943: public void testGetModifiers() {
0944: Object[][] modifiers = {
0945: { "ALT", new Integer(InputEvent.ALT_MASK) },
0946: { "ALT_GRAPH", new Integer(InputEvent.ALT_GRAPH_MASK) },
0947: { "SHIFT", new Integer(InputEvent.SHIFT_MASK) },
0948: { "CTRL", new Integer(InputEvent.CTRL_MASK) },
0949: { "META", new Integer(InputEvent.META_MASK) },
0950: { "BUTTON1", new Integer(InputEvent.BUTTON1_MASK) },
0951: { "BUTTON2", new Integer(InputEvent.BUTTON2_MASK) },
0952: { "BUTTON3", new Integer(InputEvent.BUTTON3_MASK) },
0953: { "POPUP", new Integer(AWTConstants.POPUP_MASK) },
0954: { "TERTIARY", new Integer(AWTConstants.TERTIARY_MASK) },
0955: {
0956: "ALT|CTRL|SHIFT",
0957: new Integer(InputEvent.ALT_MASK
0958: | InputEvent.CTRL_MASK
0959: | InputEvent.SHIFT_MASK) }, };
0960: for (int i = 0; i < modifiers.length; i++) {
0961: String mod = (String) modifiers[i][0];
0962: assertEquals("Wrong value for string modifier " + mod,
0963: modifiers[i][1], new Integer(AWT.getModifiers(mod)));
0964: }
0965: for (int i = 0; i < modifiers.length; i++) {
0966: String mod = modifiers[i][0].toString() + "_MASK";
0967: assertEquals("Wrong value for string modifier " + mod,
0968: modifiers[i][1], new Integer(AWT.getModifiers(mod)));
0969: }
0970: }
0971:
0972: public void testMouselessModifierMask() {
0973: final JButton b = new JButton("Push Me");
0974: b.setMnemonic(KeyEvent.VK_P);
0975: showFrame(b);
0976: final Flag flag = new Flag();
0977: b.addActionListener(new ActionListener() {
0978: public void actionPerformed(ActionEvent e) {
0979: flag.flag = true;
0980: }
0981: });
0982: invokeAndWait(new Runnable() {
0983: public void run() {
0984: b.requestFocus();
0985: }
0986: });
0987: robot.key(KeyEvent.VK_P, Robot.MOUSELESS_MODIFIER_MASK);
0988: robot.waitForIdle();
0989: assertTrue(
0990: "Button not activated, probably incorrect mouseless modifier, or not supported",
0991: flag.flag);
0992: }
0993:
0994: // FIXME X11 hangs here when run with all tests on a non-primary display
0995: // 6 errors on non-primary display, 3 on primary display
0996: public void testSampleComponentLocation() {
0997: // Sampling requires java.awt.Robot
0998: if (Robot.getEventMode() != Robot.EM_ROBOT)
0999: return;
1000:
1001: if (!Platform.isX11()) {
1002: JLabel label = new JLabel(" ");
1003: label.setBackground(Color.red);
1004: label.setOpaque(true);
1005: showFrame(label);
1006: assertEquals("Sample of component location failed",
1007: Color.red, robot
1008: .sample(label, label.getWidth() / 2, label
1009: .getHeight() / 2));
1010: }
1011: }
1012:
1013: /** Create a new test case with the given name. */
1014: public RobotTest(String name) {
1015: super (name);
1016: }
1017:
1018: /** Note the event mode when reporting this test's name. */
1019: public String getName() {
1020: return Robot.getEventMode() == Robot.EM_AWT ? super .getName()
1021: + " (AWT mode)" : super .getName();
1022: }
1023:
1024: private Robot robot;
1025:
1026: protected void setUp() {
1027: robot = getRobot();
1028: }
1029:
1030: // /** Provide for repetitive testing on individual tests. */
1031: // public static void main(String[] args) {
1032: // RepeatHelper.runTests(args, RobotTest.class);
1033: // }
1034: }
|