public static void main(String[] s) { JFrame frame = new JFrame("A test frame"); JPanel outputBox = new JPanel(); JButton jButton = new JButton(); frame.setSize(200, 200); frame.addMouseWheelListener( new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent e) { System.out.println("Wheel moved on FRAME : " + e); actualEvents++; } }); outputBox.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { System.out.println("MousePressed on OUTBOX : " + e); } }); frame.add(outputBox); outputBox.add(jButton); frame.setVisible(true); Util.waitForIdle(robot); Util.pointOnComp(jButton, robot); Util.waitForIdle(robot); for (int i = 0; i < MOVE_COUNT; i++) { robot.mouseWheel(1); robot.delay(10); } for (int i = 0; i < MOVE_COUNT; i++) { robot.mouseWheel(-1); robot.delay(10); } Util.waitForIdle(robot); // Not fair to check for multiplier 4 as it's not specified actual number of WheelEvents // result in a single wheel rotation. if (actualEvents != EXPECTED_COUNT) { AbstractTest.fail( "Expected events count: " + EXPECTED_COUNT + " Actual events count: " + actualEvents); } }
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"); }
public void mouseWheel(int wheelAmt) { robot.mouseWheel(wheelAmt); }
/** * Release a mouse button(s). * * @param rotation wheel rotation (could be negative or positive depending on the direction). * @see java.awt.Robot#mouseWheel(int wheelAmt) */ public void mouseWheel(int rotation) { robot.mouseWheel(rotation); }
/** * 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; } } }