< prev index next >

test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java

Print this page


   1 /*
   2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  64 
  65 public class SelectCurrentItemTest extends Applet implements ItemListener,
  66  WindowListener, Runnable
  67 {
  68     //Declare things used in the test, like buttons and labels here
  69     Frame frame;
  70     Choice theChoice;
  71     Robot robot;
  72 
  73     Object lock = new Object();
  74     boolean passed = false;
  75 
  76     public void init()
  77     {
  78         //Create instructions for the user here, as well as set up
  79         // the environment -- set the layout manager, add buttons,
  80         // etc.
  81 
  82         this.setLayout (new BorderLayout ());
  83 
  84         String[] instructions =
  85         {
  86             "This is an AUTOMATIC test",
  87             "simply wait until it is done"
  88         };
  89         Sysout.createDialog( );
  90         Sysout.printInstructions( instructions );
  91 
  92         frame = new Frame("SelectCurrentItemTest");
  93         theChoice = new Choice();
  94         for (int i = 0; i < 10; i++) {
  95             theChoice.add(new String("Choice Item " + i));
  96         }
  97         theChoice.addItemListener(this);
  98         frame.add(theChoice);
  99         frame.addWindowListener(this);
 100 
 101         try {
 102             robot = new Robot();
 103             robot.setAutoDelay(500);
 104         }
 105         catch (AWTException e) {
 106             throw new RuntimeException("Unable to create Robot.  Test fails.");
 107         }
 108 
 109     }//End  init()
 110 
 111     public void start ()
 112     {
 113         //Get things going.  Request focus, set size, et cetera
 114         setSize (200,200);
 115         setVisible(true);
 116         validate();
 117 
 118         //What would normally go into main() will probably go here.
 119         //Use System.out.println for diagnostic messages that you want
 120         //to read after the test is done.
 121         //Use Sysout.println for messages you want the tester to read.
 122 
 123         frame.setLocation(1,20);
 124         robot.mouseMove(10, 30);
 125         frame.pack();
 126         frame.setVisible(true);
 127         synchronized(lock) {
 128         try {
 129         lock.wait(120000);
 130         }
 131         catch(InterruptedException e) {}
 132         }
 133         robot.waitForIdle();
 134         if (!passed) {
 135             throw new RuntimeException("TEST FAILED!");
 136         }
 137 
 138         // wait to make sure ItemEvent has been processed
 139 
 140 //        try {Thread.sleep(10000);} catch (InterruptedException e){}
 141     }// start()


 147         // get bounds of Choice
 148         Dimension size = theChoice.getSize();
 149         robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);
 150 
 151         robot.setAutoDelay(250);
 152         robot.mousePress(InputEvent.BUTTON1_MASK);
 153         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 154 
 155         robot.setAutoDelay(1000);
 156         robot.mouseMove(loc.x + size.width / 2, loc.y + size.height + size.height / 2);
 157         robot.setAutoDelay(250);
 158         robot.mousePress(InputEvent.BUTTON1_MASK);
 159         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 160         robot.waitForIdle();
 161         synchronized(lock) {
 162             lock.notify();
 163         }
 164     }
 165 
 166     public void itemStateChanged(ItemEvent e) {
 167         Sysout.println("ItemEvent received.  Test passes");
 168         passed = true;
 169     }
 170 
 171     public void windowOpened(WindowEvent e) {
 172         Sysout.println("windowActivated()");
 173         Thread testThread = new Thread(this);
 174         testThread.start();
 175     }
 176     public void windowActivated(WindowEvent e) {
 177     }
 178     public void windowDeactivated(WindowEvent e) {}
 179     public void windowClosed(WindowEvent e) {}
 180     public void windowClosing(WindowEvent e) {}
 181     public void windowIconified(WindowEvent e) {}
 182     public void windowDeiconified(WindowEvent e) {}
 183 
 184 }// class SelectCurrentItemTest
 185 
 186 
 187 /****************************************************
 188  Standard Test Machinery
 189  DO NOT modify anything below -- it's a standard
 190   chunk of code whose purpose is to make user
 191   interaction uniform, and thereby make it simpler
 192   to read and understand someone else's test.
 193  ****************************************************/
 194 
 195 /**
 196  This is part of the standard test machinery.
 197  It creates a dialog (with the instructions), and is the interface
 198   for sending text messages to the user.
 199  To print the instructions, send an array of strings to Sysout.createDialog
 200   WithInstructions method.  Put one line of instructions per array entry.
 201  To display a message for the tester to see, simply call Sysout.println
 202   with the string to be displayed.
 203  This mimics System.out.println but works within the test harness as well
 204   as standalone.
 205  */
 206 
 207 class Sysout
 208 {
 209     private static TestDialog dialog;
 210 
 211     public static void createDialogWithInstructions( String[] instructions )
 212     {
 213         dialog = new TestDialog( new Frame(), "Instructions" );
 214         dialog.printInstructions( instructions );
 215         dialog.setVisible(true);
 216         println( "Any messages for the tester will display here." );
 217     }
 218 
 219     public static void createDialog( )
 220     {
 221         dialog = new TestDialog( new Frame(), "Instructions" );
 222         String[] defInstr = { "Instructions will appear here. ", "" } ;
 223         dialog.printInstructions( defInstr );
 224         dialog.setLocation(0, 400);
 225         dialog.setVisible(true);
 226         println( "Any messages for the tester will display here." );
 227     }
 228 
 229 
 230     public static void printInstructions( String[] instructions )
 231     {
 232         dialog.printInstructions( instructions );
 233     }
 234 
 235 
 236     public static void println( String messageIn )
 237     {
 238         dialog.displayMessage( messageIn );
 239     }
 240 
 241 }// Sysout  class
 242 
 243 /**
 244   This is part of the standard test machinery.  It provides a place for the
 245    test instructions to be displayed, and a place for interactive messages
 246    to the user to be displayed.
 247   To have the test instructions displayed, see Sysout.
 248   To have a message to the user be displayed, see Sysout.
 249   Do not call anything in this dialog directly.
 250   */
 251 class TestDialog extends Dialog
 252 {
 253 
 254     TextArea instructionsText;
 255     TextArea messageText;
 256     int maxStringLength = 80;
 257 
 258     //DO NOT call this directly, go through Sysout
 259     public TestDialog( Frame frame, String name )
 260     {
 261         super( frame, name );
 262         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 263         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 264         add( "North", instructionsText );
 265 
 266         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 267         add("Center", messageText);
 268 
 269         pack();
 270 
 271         show();
 272     }// TestDialog()
 273 
 274     //DO NOT call this directly, go through Sysout
 275     public void printInstructions( String[] instructions )
 276     {
 277         //Clear out any current instructions
 278         instructionsText.setText( "" );
 279 
 280         //Go down array of instruction strings
 281 
 282         String printStr, remainingStr;
 283         for( int i=0; i < instructions.length; i++ )
 284         {
 285             //chop up each into pieces maxSringLength long
 286             remainingStr = instructions[ i ];
 287             while( remainingStr.length() > 0 )
 288             {
 289                 //if longer than max then chop off first max chars to print
 290                 if( remainingStr.length() >= maxStringLength )
 291                 {
 292                     //Try to chop on a word boundary
 293                     int posOfSpace = remainingStr.
 294                         lastIndexOf( ' ', maxStringLength - 1 );
 295 
 296                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 297 
 298                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 299                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 300                 }
 301                 //else just print
 302                 else
 303                 {
 304                     printStr = remainingStr;
 305                     remainingStr = "";
 306                 }
 307 
 308                 instructionsText.append( printStr + "\n" );
 309 
 310             }// while
 311 
 312         }// for
 313 
 314     }//printInstructions()
 315 
 316     //DO NOT call this directly, go through Sysout
 317     public void displayMessage( String messageIn )
 318     {
 319         messageText.append( messageIn + "\n" );
 320         System.out.println(messageIn);
 321     }
 322 
 323 }// TestDialog  class
   1 /*
   2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  64 
  65 public class SelectCurrentItemTest extends Applet implements ItemListener,
  66  WindowListener, Runnable
  67 {
  68     //Declare things used in the test, like buttons and labels here
  69     Frame frame;
  70     Choice theChoice;
  71     Robot robot;
  72 
  73     Object lock = new Object();
  74     boolean passed = false;
  75 
  76     public void init()
  77     {
  78         //Create instructions for the user here, as well as set up
  79         // the environment -- set the layout manager, add buttons,
  80         // etc.
  81 
  82         this.setLayout (new BorderLayout ());
  83 








  84         frame = new Frame("SelectCurrentItemTest");
  85         theChoice = new Choice();
  86         for (int i = 0; i < 10; i++) {
  87             theChoice.add(new String("Choice Item " + i));
  88         }
  89         theChoice.addItemListener(this);
  90         frame.add(theChoice);
  91         frame.addWindowListener(this);
  92 
  93         try {
  94             robot = new Robot();
  95             robot.setAutoDelay(500);
  96         }
  97         catch (AWTException e) {
  98             throw new RuntimeException("Unable to create Robot.  Test fails.");
  99         }
 100 
 101     }//End  init()
 102 
 103     public void start ()
 104     {
 105         //Get things going.  Request focus, set size, et cetera
 106         setSize (200,200);
 107         setVisible(true);
 108         validate();
 109 
 110         //What would normally go into main() will probably go here.
 111         //Use System.out.println for diagnostic messages that you want
 112         //to read after the test is done.
 113         //Use System.out.println for messages you want the tester to read.
 114 
 115         frame.setLocation(1,20);
 116         robot.mouseMove(10, 30);
 117         frame.pack();
 118         frame.setVisible(true);
 119         synchronized(lock) {
 120         try {
 121         lock.wait(120000);
 122         }
 123         catch(InterruptedException e) {}
 124         }
 125         robot.waitForIdle();
 126         if (!passed) {
 127             throw new RuntimeException("TEST FAILED!");
 128         }
 129 
 130         // wait to make sure ItemEvent has been processed
 131 
 132 //        try {Thread.sleep(10000);} catch (InterruptedException e){}
 133     }// start()


 139         // get bounds of Choice
 140         Dimension size = theChoice.getSize();
 141         robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);
 142 
 143         robot.setAutoDelay(250);
 144         robot.mousePress(InputEvent.BUTTON1_MASK);
 145         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 146 
 147         robot.setAutoDelay(1000);
 148         robot.mouseMove(loc.x + size.width / 2, loc.y + size.height + size.height / 2);
 149         robot.setAutoDelay(250);
 150         robot.mousePress(InputEvent.BUTTON1_MASK);
 151         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 152         robot.waitForIdle();
 153         synchronized(lock) {
 154             lock.notify();
 155         }
 156     }
 157 
 158     public void itemStateChanged(ItemEvent e) {
 159         System.out.println("ItemEvent received.  Test passes");
 160         passed = true;
 161     }
 162 
 163     public void windowOpened(WindowEvent e) {
 164         System.out.println("windowActivated()");
 165         Thread testThread = new Thread(this);
 166         testThread.start();
 167     }
 168     public void windowActivated(WindowEvent e) {
 169     }
 170     public void windowDeactivated(WindowEvent e) {}
 171     public void windowClosed(WindowEvent e) {}
 172     public void windowClosing(WindowEvent e) {}
 173     public void windowIconified(WindowEvent e) {}
 174     public void windowDeiconified(WindowEvent e) {}
 175 
 176 }// class SelectCurrentItemTest











































































































































< prev index next >