Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Robot/ManualInstructions/ManualInstructions.java
38828 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24test %W% %E% %I%, %G%25@bug 631571726@summary manual control over the Robot27@author Andrei Dmitriev : area=awt.robot28@run applet/manual=yesno ManualInstructions.html29*/3031import java.applet.Applet;32import java.awt.*;33import java.awt.event.*;34import java.util.Timer;35import java.util.TimerTask;3637public class ManualInstructions extends Applet38{39final static long SEND_DELAY = 1000;4041public static void main(String s[]){42ManualInstructions mi = new ManualInstructions();43mi.init();44mi.start();45}4647static Robot robot;48Point mouseLocation; //where mouse should be pressed each time49Panel target = new Panel();50Button pressOn = new Button("press on ...");51Button releaseOn = new Button("release on ...");52Button clickOn = new Button("click on ...");53Choice buttonNumber = new Choice();5455public void init()56{57try {58robot = new Robot();59} catch (AWTException ex) {60ex.printStackTrace();61throw new RuntimeException(ex);62}63this.setLayout (new BorderLayout ());6465target.setBackground(Color.green);66target.setName("GreenBox");//for the ease of debug67target.setPreferredSize(new Dimension(100, 100));68String toolkit = Toolkit.getDefaultToolkit().getClass().getName();6970// on X systems two buttons are reserved for wheel though they are countable by MouseInfo.71int buttonsNumber = toolkit.equals("sun.awt.windows.WToolkit")?MouseInfo.getNumberOfButtons():MouseInfo.getNumberOfButtons()-2;7273for (int i = 0; i < 8; i++){74buttonNumber.add("BUTTON"+(i+1)+"_MASK");75}7677pressOn.addActionListener(new ActionListener(){78public void actionPerformed(ActionEvent e){79System.out.println("Now pressing : " + (buttonNumber.getSelectedIndex()+1));8081Timer timer = new Timer();82TimerTask robotInteraction = new TimerTask(){83public void run(){84robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);85robot.mousePress(getMask(buttonNumber.getSelectedIndex()+1));86}87};88timer.schedule(robotInteraction, SEND_DELAY);89}90});9192releaseOn.addActionListener(new ActionListener(){93public void actionPerformed(ActionEvent e){94System.out.println("Now releasing : " + (buttonNumber.getSelectedIndex()+1));95Timer timer = new Timer();96TimerTask robotInteraction = new TimerTask(){97public void run(){98robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);99robot.mouseRelease(getMask(buttonNumber.getSelectedIndex()+1));100}101};102timer.schedule(robotInteraction, SEND_DELAY);103}104});105106clickOn.addActionListener(new ActionListener(){107public void actionPerformed(ActionEvent e){108System.out.println("Now clicking : " + (buttonNumber.getSelectedIndex()+1));109Timer timer = new Timer();110TimerTask robotInteraction = new TimerTask(){111public void run(){112robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);113robot.mousePress(getMask(buttonNumber.getSelectedIndex()+1));114robot.mouseRelease(getMask(buttonNumber.getSelectedIndex()+1));115}116};117timer.schedule(robotInteraction, SEND_DELAY);118}119120});121target.addMouseListener(new MouseAdapter(){122public void mousePressed(MouseEvent e){123Sysout.println(""+e);124}125public void mouseReleased(MouseEvent e){126Sysout.println(""+e);127}128public void mouseClicked(MouseEvent e){129Sysout.println(""+e);130}131});132133String[] instructions =134{135"Do provide an instruction to the robot by",136"choosing the button number to act and ",137"pressing appropriate java.awt.Button on the left.",138"Inspect an output in the TextArea below.",139"Please don't generate non-natural sequences like Release-Release, etc.",140"If you use keyboard be sure that you released the keyboard shortly.",141"If events are generated well press Pass, otherwise Fail."142};143Sysout.createDialogWithInstructions( instructions );144145}//End init()146147private int getMask(int button){148return InputEvent.getMaskForButton(button);149150/*151//this only works for standard buttons and for old JDK builds152int mask = 0;153switch (button){154case 1: {155mask = InputEvent.BUTTON1_MASK;156break;157}158case 2: {159mask = InputEvent.BUTTON2_MASK;160break;161}162case 3: {163mask = InputEvent.BUTTON3_MASK;164break;165}166}167return mask;168*/169}170171private Point updateTargetLocation() {172return new Point(target.getLocationOnScreen().x + target.getWidth()/2, target.getLocationOnScreen().y + target.getHeight()/2);173}174175public void start ()176{177//Get things going. Request focus, set size, et cetera178setSize (200,200);179setVisible(true);180validate();181Frame f = new Frame ("Set action for Robot here.");182f.setLayout(new FlowLayout());183f.add(buttonNumber);184f.add(pressOn);185f.add(releaseOn);186f.add(clickOn);187f.add(target);188f.pack();189f.setVisible(true);190}// start()191}// class192193/* Place other classes related to the test after this line */194195196/****************************************************197Standard Test Machinery198DO NOT modify anything below -- it's a standard199chunk of code whose purpose is to make user200interaction uniform, and thereby make it simpler201to read and understand someone else's test.202****************************************************/203204/**205This is part of the standard test machinery.206It creates a dialog (with the instructions), and is the interface207for sending text messages to the user.208To print the instructions, send an array of strings to Sysout.createDialog209WithInstructions method. Put one line of instructions per array entry.210To display a message for the tester to see, simply call Sysout.println211with the string to be displayed.212This mimics System.out.println but works within the test harness as well213as standalone.214*/215216class Sysout217{218private static TestDialog dialog;219220public static void createDialogWithInstructions( String[] instructions )221{222dialog = new TestDialog( new Frame(), "Instructions" );223dialog.printInstructions( instructions );224dialog.setVisible(true);225println( "Any messages for the tester will display here." );226}227228public static void createDialog( )229{230dialog = new TestDialog( new Frame(), "Instructions" );231String[] defInstr = { "Instructions will appear here. ", "" } ;232dialog.printInstructions( defInstr );233dialog.setVisible(true);234println( "Any messages for the tester will display here." );235}236237public static void printInstructions( String[] instructions )238{239dialog.printInstructions( instructions );240}241242243public static void println( String messageIn )244{245dialog.displayMessage( messageIn );246}247248}// Sysout class249250/**251This is part of the standard test machinery. It provides a place for the252test instructions to be displayed, and a place for interactive messages253to the user to be displayed.254To have the test instructions displayed, see Sysout.255To have a message to the user be displayed, see Sysout.256Do not call anything in this dialog directly.257*/258class TestDialog extends Dialog259{260261TextArea instructionsText;262TextArea messageText;263int maxStringLength = 120;264265//DO NOT call this directly, go through Sysout266public TestDialog( Frame frame, String name )267{268super( frame, name );269int scrollBoth = TextArea.SCROLLBARS_BOTH;270instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );271add( "North", instructionsText );272273messageText = new TextArea( "", 5, maxStringLength, scrollBoth );274add("Center", messageText);275276pack();277278setVisible(true);279}// TestDialog()280281//DO NOT call this directly, go through Sysout282public void printInstructions( String[] instructions )283{284//Clear out any current instructions285instructionsText.setText( "" );286287//Go down array of instruction strings288289String printStr, remainingStr;290for( int i=0; i < instructions.length; i++ )291{292//chop up each into pieces maxSringLength long293remainingStr = instructions[ i ];294while( remainingStr.length() > 0 )295{296//if longer than max then chop off first max chars to print297if( remainingStr.length() >= maxStringLength )298{299//Try to chop on a word boundary300int posOfSpace = remainingStr.301lastIndexOf( ' ', maxStringLength - 1 );302303if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;304305printStr = remainingStr.substring( 0, posOfSpace + 1 );306remainingStr = remainingStr.substring( posOfSpace + 1 );307}308//else just print309else310{311printStr = remainingStr;312remainingStr = "";313}314315instructionsText.append( printStr + "\n" );316}// while317}// for318}//printInstructions()319320//DO NOT call this directly, go through Sysout321public void displayMessage( String messageIn )322{323messageText.append( messageIn + "\n" );324System.out.println(messageIn);325}326327}// TestDialog class328329330