Example #1
0
  public static void main(String... args) throws Exception {
    Robot robot = new Robot();

    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            popup = new JPopupMenu();
            popup.add(new JMenuItem("item"));
            popup.setVisible(true);
          }
        });

    robot.waitForIdle();

    if (!popup.isShowing()) {
      throw new RuntimeException("Where is my popup ?");
    }

    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            popup.setVisible(false);
            popup.removeAll();
            popup.setVisible(true);
          }
        });

    robot.waitForIdle();

    if (popup.isShowing()) {
      throw new RuntimeException("Empty popup is shown");
    }

    popup.setVisible(false);
  }
Example #2
0
  public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    robot.setAutoDelay(350);

    SwingUtilities.invokeAndWait(
        new Runnable() {

          public void run() {
            createAndShowGUI();
          }
        });

    robot.waitForIdle();

    SwingUtilities.invokeAndWait(
        new Runnable() {

          public void run() {
            spane.requestFocus();
            sbar.setValue(sbar.getMaximum());
          }
        });

    robot.waitForIdle();

    Point point = getClickPoint(0.5, 0.5);
    robot.mouseMove(point.x, point.y);
    robot.mousePress(InputEvent.BUTTON1_MASK);

    robot.waitForIdle();

    SwingUtilities.invokeAndWait(
        new Runnable() {

          public void run() {
            final int oldValue = sbar.getValue();
            sbar.addAdjustmentListener(
                new AdjustmentListener() {

                  public void adjustmentValueChanged(AdjustmentEvent e) {
                    if (e.getValue() >= oldValue) {
                      passed = false;
                    }
                    do_test = true;
                  }
                });
          }
        });

    robot.waitForIdle();

    point = getClickPoint(0.5, 0.2);
    robot.mouseMove(point.x, point.y);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.waitForIdle();

    if (!do_test || !passed) {
      throw new Exception("The scrollbar moved with incorrect direction");
    }
  }
  public static void main(String[] args) throws Exception {
    robot = new Robot();
    robot.setAutoDelay(50);

    try {
      SwingUtilities.invokeAndWait(
          new Runnable() {
            @Override
            public void run() {
              createAndShowGUI();
            }
          });

      robot.waitForIdle();

      checkFocusOwner(textField);

      robot.keyPress(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_TAB);
      robot.waitForIdle();

      checkFocusOwner(button);

      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      robot.waitForIdle();

      checkFocusOwner(textField);

      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      robot.waitForIdle();

      checkFocusOwner(button);
    } finally {
      SwingUtilities.invokeLater(
          new Runnable() {
            @Override
            public void run() {
              if (frame != null) {
                frame.dispose();
              }
            }
          });
    }
    System.out.println("Test Passed!");
  }
Example #4
0
  public static void main(String[] args) throws Throwable {
    Robot robot = new Robot();
    robot.setAutoDelay(250);

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    SwingUtilities.invokeAndWait(
        new Runnable() {

          @Override
          public void run() {
            JFrame frame = new JFrame("Test");
            frame.setContentPane(createPanel(frame));
            frame.setJMenuBar(createMenuBar());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
          }
        });

    robot.waitForIdle();

    // Change the default button to
    // force a call to BasicRootPaneUI.updateDefaultButtonBindings()
    Util.hitKeys(robot, KeyEvent.VK_TAB);

    // If the bug exists, then as soon as the menu appears,
    // the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no
    // effect.
    Util.hitMnemonics(robot, KeyEvent.VK_U);
    Util.hitKeys(robot, KeyEvent.VK_ENTER);
    robot.waitForIdle();

    checkAction();

    Util.hitMnemonics(robot, KeyEvent.VK_U);
    Util.hitKeys(robot, KeyEvent.VK_DOWN);
    Util.hitKeys(robot, KeyEvent.VK_ENTER);
    robot.waitForIdle();

    checkAction();
  }
Example #5
0
  public void oneDrag(int pixels) {
    robot.mouseMove(fp.x + frame.getWidth() / 2, fp.y + frame.getHeight() / 2);
    // drag for a short distance
    robot.mousePress(InputEvent.BUTTON1_MASK);
    for (int i = 1; i < pixels; i++) {
      robot.mouseMove(fp.x + frame.getWidth() / 2 + i, fp.y + frame.getHeight() / 2);
    }
    robot.waitForIdle();
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.waitForIdle();

    if (dragged && clicked) {
      throw new RuntimeException(
          "Test failed. Clicked event follows by Dragged. Dragged = "
              + dragged
              + ". Clicked = "
              + clicked
              + " : distance = "
              + pixels);
    }
  }
Example #6
0
  public void runTest() throws Exception {
    try {
      SwingUtilities.invokeAndWait(this::setupUI);

      robot.waitForIdle();

      SwingUtilities.invokeAndWait(
          () -> scrollPane.getViewport().scrollRectToVisible(comboBox.getBounds()));
      robot.waitForIdle();

      // Move mouse pointer to the center of the combo box
      Point p = comboBox.getLocationOnScreen();
      Dimension d = comboBox.getSize();
      robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);

      // The currently visible rectangle in scrollPane
      Rectangle viewRect0 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);

      // Scroll the scrollPane with mouse wheel
      robot.mouseWheel(1);
      robot.waitForIdle();

      // The updated rectangle
      Rectangle viewRect1 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);

      if (viewRect0.y == viewRect1.y) {
        throw new RuntimeException("Mouse wheel should have scrolled the JScrollPane");
      }
    } finally {
      if (frame != null) {
        frame.dispose();
      }
    }

    System.out.println("Test passed");
  }
Example #7
0
  public static void main(String[] args) throws Exception {

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    Robot robot = new Robot();
    robot.setAutoDelay(20);
    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            // just to quickly grab a column model
            JTable table = new JTable(10, 5);
            header = new JTableHeader(table.getColumnModel());
            checkColumn(0, "A");
            JFrame frame = new JFrame("standalone header");
            frame.add(header);
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
        });
    robot.waitForIdle();
    Point point = header.getLocationOnScreen();
    robot.mouseMove(point.x + 3, point.y + 3);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    for (int i = 0; i < header.getWidth() - 3; i++) {
      robot.mouseMove(point.x + i, point.y + 3);
    }
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            TableColumnModel model = header.getColumnModel();
            checkColumn(model.getColumnCount() - 1, "A");
          }
        });
  }
 public static void main(String[] args) throws Exception {
   final bug6274267 test = new bug6274267();
   Robot robot = new Robot();
   try {
     SwingUtilities.invokeAndWait(
         new Runnable() {
           public void run() {
             test.setupUI1();
           }
         });
     robot.waitForIdle();
     SwingUtilities.invokeAndWait(
         new Runnable() {
           public void run() {
             test.setupUI2();
           }
         });
     test.test();
   } finally {
     if (test.frame != null) {
       test.frame.dispose();
     }
   }
 }
  void test() {

    robot.waitForIdle();

    if (!text.isFocusOwner()) {
      robot.mouseMove(text.getLocationOnScreen().x + 5, text.getLocationOnScreen().y + 5);
      robot.delay(100);
      robot.mousePress(MouseEvent.BUTTON1_MASK);
      robot.delay(100);
      robot.mouseRelease(MouseEvent.BUTTON1_MASK);

      int iter = 10;
      while (!text.isFocusOwner() && iter-- > 0) {
        robot.delay(200);
      }
      if (iter <= 0) {
        Sysout.println("Test: text field couldn't be focused!");
        return;
      }
    }

    robot.keyPress(KeyEvent.VK_A);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_A);

    robot.waitForIdle();

    String charA = text.getText();
    System.err.println("Test: character typed with VK_A: " + charA);

    robot.keyPress(KeyEvent.VK_BACK_SPACE);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_BACK_SPACE);

    robot.waitForIdle();

    if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) {
      robot.keyPress(KeyEvent.VK_CONTROL);
    }
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_F);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_F);
    robot.keyRelease(KeyEvent.VK_ALT);
    if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) {
      robot.keyRelease(KeyEvent.VK_CONTROL);
    }

    robot.waitForIdle();

    String string = text.getText();

    robot.keyPress(KeyEvent.VK_I);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_I);

    robot.waitForIdle();

    Sysout.println("Test: character typed after mnemonic key press: " + text.getText());

    if (!text.getText().equals(string)) {
      throw new RuntimeException("Test failed!");
    }

    robot.keyPress(KeyEvent.VK_A);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_A);

    robot.waitForIdle();

    System.err.println("Test: chracter typed with VK_A: " + text.getText());

    if (!charA.equals(text.getText())) {
      throw new RuntimeException("Test failed!");
    }

    Sysout.println("Test passed.");
  }
Example #10
0
  private void test(TestState currentState) throws InterruptedException, InvocationTargetException {

    synchronized (LOCK) {
      this.currentState = currentState;
      System.out.println(this.currentState);

      List list;
      if (currentState.getMultiple()) {
        list = multiple;
      } else {
        list = single;
      }

      Robot r;
      try {
        r = new Robot();
      } catch (AWTException e) {
        throw new RuntimeException(e.getMessage());
      }

      r.delay(10);
      Point loc = this.getLocationOnScreen();

      r.mouseMove(loc.x + 10, loc.y + 10);
      r.mousePress(InputEvent.BUTTON1_MASK);
      r.delay(10);
      r.mouseRelease(InputEvent.BUTTON1_MASK);
      r.delay(10);

      list.requestFocusInWindow();
      LOCK.wait(ACTION_TIMEOUT);
      if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) {
        throw new RuntimeException("Test failed - list isn't focus owner.");
      }

      list.deselect(0);
      list.deselect(1);
      list.deselect(2);
      list.deselect(3);
      list.deselect(4);
      list.deselect(5);
      list.deselect(6);
      list.deselect(7);
      list.deselect(8);

      int selectIndex = 0;
      int visibleIndex = 0;

      if (currentState.getScrollMoved()) {

        if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP
            || currentState.getKeyID() == KeyEvent.VK_HOME) {
          selectIndex = 8;
          visibleIndex = 8;
        } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN
            || currentState.getKeyID() == KeyEvent.VK_END) {
          selectIndex = 0;
          visibleIndex = 0;
        }

      } else {

        if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP
            || currentState.getKeyID() == KeyEvent.VK_HOME) {

          if (currentState.getSelectedMoved()) {
            selectIndex = 1;
            visibleIndex = 0;
          } else {
            selectIndex = 0;
            visibleIndex = 0;
          }

        } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN
            || currentState.getKeyID() == KeyEvent.VK_END) {

          if (currentState.getSelectedMoved()) {
            selectIndex = 7;
            visibleIndex = 8;
          } else {
            selectIndex = 8;
            visibleIndex = 8;
          }
        }
      }

      list.select(selectIndex);
      list.makeVisible(visibleIndex);

      r.delay(10);

      if (currentState.getKeyID() == KeyEvent.VK_HOME
          || currentState.getKeyID() == KeyEvent.VK_END) {
        r.keyPress(KeyEvent.VK_CONTROL);
      }

      r.delay(10);
      r.keyPress(currentState.getKeyID());
      r.delay(10);
      r.keyRelease(currentState.getKeyID());
      r.delay(10);

      if (currentState.getKeyID() == KeyEvent.VK_HOME
          || currentState.getKeyID() == KeyEvent.VK_END) {
        r.keyRelease(KeyEvent.VK_CONTROL);
      }

      r.waitForIdle();
      r.delay(200);

      if (currentState.getTemplate() != currentState.getAction())
        throw new RuntimeException("Test failed.");
    }
  }
Example #11
0
  /**
   * Run through our command list and invoke each command.
   *
   * @see papertoolkit.actions.Action#invoke()
   */
  public void invoke() {
    // need to get a local Robot every time, because we may have sent this across the wire...
    final Robot rob = getRobot();

    // for each method in our list, invoke it with the correct arguments
    for (RobotCommand command : commandsToRun) {
      Object[] arguments = command.arguments;
      RobotMethod method = command.method;

      // System.out.println("RobotAction: Invoking " + method + " [" +
      // ArrayUtils.toString(arguments));
      switch (method) {
        case CREATE_SCREEN_CAPTURE: // args: rectangle, file
          BufferedImage image = rob.createScreenCapture((Rectangle) arguments[0]);
          // save the file locally
          ImageUtils.writeImageToJPEG(image, 100, (File) arguments[1]);
          // System.out.println("RobotAction :: Screen Cap Saved");
          break;
        case DELAY: // args: int milliseconds
          rob.delay((Integer) arguments[0]);
          break;
        case GET_PIXEL_COLOR: // args: int x, int y
          final Color pixelColor =
              rob.getPixelColor((Integer) arguments[0], (Integer) arguments[1]);
          System.out.println(
              "RobotAction :: Pixel Color at "
                  + arguments[0]
                  + ","
                  + arguments[1]
                  + " is "
                  + pixelColor);
          // TODO: Do something more interesting with this data
          break;
        case KEY_PRESS: // arg: int keycode to press
          rob.keyPress((Integer) arguments[0]);
          break;
        case KEY_RELEASE: // arg: int keycode to release
          rob.keyRelease((Integer) arguments[0]);
          break;
        case MOUSE_MOVE: // args: int x, int y
          // System.out.println("RobotAction :: Moving Mouse... to " + arguments[0] + "," +
          // arguments[1] + "!");
          rob.mouseMove((Integer) arguments[0], (Integer) arguments[1]);
          break;
        case MOUSE_PRESS: // int mouse button
          rob.mousePress((Integer) arguments[0]);
          break;
        case MOUSE_RELEASE: // int mouse button
          rob.mouseRelease((Integer) arguments[0]);
          break;
        case MOUSE_WHEEL: // int wheel roll amount
          rob.mouseWheel((Integer) arguments[0]);
          break;
        case SET_AUTO_DELAY: // int delay millis
          rob.setAutoDelay((Integer) arguments[0]);
          break;
        case SET_AUTO_WAIT_FOR_IDLE: // boolean wait?
          rob.setAutoWaitForIdle((Boolean) arguments[0]);
          break;
        case WAIT_FOR_IDLE:
          rob.waitForIdle();
          break;
      }
    }
  }