Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java
47626 views
/*1* Copyright (c) 2002, 2014, 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*/22/*23test 1.3 02/06/2524@bug 490293325@summary Test that selecting the current item sends an ItemEvent26@author bchristi : area= Choice27@run applet SelectCurrentItemTest.html28*/2930// Note there is no @ in front of test above. This is so that the31// harness will not mistake this file as a test file. It should32// only see the html file as a test file. (the harness runs all33// valid test files, so it would run this test twice if this file34// were valid as well as the html file.)35// Also, note the area= after Your Name in the author tag. Here, you36// should put which functional area the test falls in. See the37// AWT-core home page -> test areas and/or -> AWT team for a list of38// areas.39// Note also the 'SelectCurrentItemTest.html' in the run tag. This should40// be changed to the name of the test.414243/**44* SelectCurrentItemTest.java45*46* summary:47*/4849import java.applet.Applet;50import java.awt.*;51import java.awt.event.*;5253//Automated tests should run as applet tests if possible because they54// get their environments cleaned up, including AWT threads, any55// test created threads, and any system resources used by the test56// such as file descriptors. (This is normally not a problem as57// main tests usually run in a separate VM, however on some platforms58// such as the Mac, separate VMs are not possible and non-applet59// tests will cause problems). Also, you don't have to worry about60// synchronisation stuff in Applet tests they way you do in main61// tests...626364public class SelectCurrentItemTest extends Applet implements ItemListener,65WindowListener, Runnable66{67//Declare things used in the test, like buttons and labels here68Frame frame;69Choice theChoice;70Robot robot;7172Object lock = new Object();73boolean passed = false;7475public void init()76{77//Create instructions for the user here, as well as set up78// the environment -- set the layout manager, add buttons,79// etc.8081this.setLayout (new BorderLayout ());8283String[] instructions =84{85"This is an AUTOMATIC test",86"simply wait until it is done"87};88Sysout.createDialog( );89Sysout.printInstructions( instructions );9091frame = new Frame("SelectCurrentItemTest");92theChoice = new Choice();93for (int i = 0; i < 10; i++) {94theChoice.add(new String("Choice Item " + i));95}96theChoice.addItemListener(this);97frame.add(theChoice);98frame.addWindowListener(this);99100try {101robot = new Robot();102robot.setAutoDelay(500);103}104catch (AWTException e) {105throw new RuntimeException("Unable to create Robot. Test fails.");106}107108}//End init()109110public void start ()111{112//Get things going. Request focus, set size, et cetera113setSize (200,200);114setVisible(true);115validate();116117//What would normally go into main() will probably go here.118//Use System.out.println for diagnostic messages that you want119//to read after the test is done.120//Use Sysout.println for messages you want the tester to read.121122frame.setLocation(1,20);123robot.mouseMove(10, 30);124frame.pack();125frame.setVisible(true);126synchronized(lock) {127try {128lock.wait(120000);129}130catch(InterruptedException e) {}131}132robot.waitForIdle();133if (!passed) {134throw new RuntimeException("TEST FAILED!");135}136137// wait to make sure ItemEvent has been processed138139// try {Thread.sleep(10000);} catch (InterruptedException e){}140}// start()141142public void run() {143try {Thread.sleep(1000);} catch (InterruptedException e){}144// get loc of Choice on screen145Point loc = theChoice.getLocationOnScreen();146// get bounds of Choice147Dimension size = theChoice.getSize();148robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);149150robot.setAutoDelay(250);151robot.mousePress(InputEvent.BUTTON1_MASK);152robot.mouseRelease(InputEvent.BUTTON1_MASK);153154robot.setAutoDelay(1000);155robot.mouseMove(loc.x + size.width / 2, loc.y + size.height + size.height / 2);156robot.setAutoDelay(250);157robot.mousePress(InputEvent.BUTTON1_MASK);158robot.mouseRelease(InputEvent.BUTTON1_MASK);159robot.waitForIdle();160synchronized(lock) {161lock.notify();162}163}164165public void itemStateChanged(ItemEvent e) {166Sysout.println("ItemEvent received. Test passes");167passed = true;168}169170public void windowOpened(WindowEvent e) {171Sysout.println("windowActivated()");172Thread testThread = new Thread(this);173testThread.start();174}175public void windowActivated(WindowEvent e) {176}177public void windowDeactivated(WindowEvent e) {}178public void windowClosed(WindowEvent e) {}179public void windowClosing(WindowEvent e) {}180public void windowIconified(WindowEvent e) {}181public void windowDeiconified(WindowEvent e) {}182183}// class SelectCurrentItemTest184185186/****************************************************187Standard Test Machinery188DO NOT modify anything below -- it's a standard189chunk of code whose purpose is to make user190interaction uniform, and thereby make it simpler191to read and understand someone else's test.192****************************************************/193194/**195This is part of the standard test machinery.196It creates a dialog (with the instructions), and is the interface197for sending text messages to the user.198To print the instructions, send an array of strings to Sysout.createDialog199WithInstructions method. Put one line of instructions per array entry.200To display a message for the tester to see, simply call Sysout.println201with the string to be displayed.202This mimics System.out.println but works within the test harness as well203as standalone.204*/205206class Sysout207{208private static TestDialog dialog;209210public static void createDialogWithInstructions( String[] instructions )211{212dialog = new TestDialog( new Frame(), "Instructions" );213dialog.printInstructions( instructions );214dialog.setVisible(true);215println( "Any messages for the tester will display here." );216}217218public static void createDialog( )219{220dialog = new TestDialog( new Frame(), "Instructions" );221String[] defInstr = { "Instructions will appear here. ", "" } ;222dialog.printInstructions( defInstr );223dialog.setLocation(0, 400);224dialog.setVisible(true);225println( "Any messages for the tester will display here." );226}227228229public static void printInstructions( String[] instructions )230{231dialog.printInstructions( instructions );232}233234235public static void println( String messageIn )236{237dialog.displayMessage( messageIn );238}239240}// Sysout class241242/**243This is part of the standard test machinery. It provides a place for the244test instructions to be displayed, and a place for interactive messages245to the user to be displayed.246To have the test instructions displayed, see Sysout.247To have a message to the user be displayed, see Sysout.248Do not call anything in this dialog directly.249*/250class TestDialog extends Dialog251{252253TextArea instructionsText;254TextArea messageText;255int maxStringLength = 80;256257//DO NOT call this directly, go through Sysout258public TestDialog( Frame frame, String name )259{260super( frame, name );261int scrollBoth = TextArea.SCROLLBARS_BOTH;262instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );263add( "North", instructionsText );264265messageText = new TextArea( "", 5, maxStringLength, scrollBoth );266add("Center", messageText);267268pack();269270show();271}// TestDialog()272273//DO NOT call this directly, go through Sysout274public void printInstructions( String[] instructions )275{276//Clear out any current instructions277instructionsText.setText( "" );278279//Go down array of instruction strings280281String printStr, remainingStr;282for( int i=0; i < instructions.length; i++ )283{284//chop up each into pieces maxSringLength long285remainingStr = instructions[ i ];286while( remainingStr.length() > 0 )287{288//if longer than max then chop off first max chars to print289if( remainingStr.length() >= maxStringLength )290{291//Try to chop on a word boundary292int posOfSpace = remainingStr.293lastIndexOf( ' ', maxStringLength - 1 );294295if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;296297printStr = remainingStr.substring( 0, posOfSpace + 1 );298remainingStr = remainingStr.substring( posOfSpace + 1 );299}300//else just print301else302{303printStr = remainingStr;304remainingStr = "";305}306307instructionsText.append( printStr + "\n" );308309}// while310311}// for312313}//printInstructions()314315//DO NOT call this directly, go through Sysout316public void displayMessage( String messageIn )317{318messageText.append( messageIn + "\n" );319System.out.println(messageIn);320}321322}// TestDialog class323324325