1 /* 2 test 1.3 02/06/25 3 @bug 4902933 4 @summary Test that selecting the current item sends an ItemEvent 5 @author bchristi : area= Choice 6 @run applet SelectCurrentItemTest.html 7 */ 8 9 // Note there is no @ in front of test above. This is so that the 10 // harness will not mistake this file as a test file. It should 11 // only see the html file as a test file. (the harness runs all 12 // valid test files, so it would run this test twice if this file 13 // were valid as well as the html file.) 14 // Also, note the area= after Your Name in the author tag. Here, you 15 // should put which functional area the test falls in. See the 16 // AWT-core home page -> test areas and/or -> AWT team for a list of 17 // areas. 18 // Note also the 'SelectCurrentItemTest.html' in the run tag. This should 19 // be changed to the name of the test. 20 21 22 /** 23 * SelectCurrentItemTest.java 24 * 25 * summary: 26 */ 27 28 import java.applet.Applet; 29 import java.awt.*; 30 import java.awt.event.*; 31 import sun.awt.SunToolkit; 32 33 //Automated tests should run as applet tests if possible because they 34 // get their environments cleaned up, including AWT threads, any 35 // test created threads, and any system resources used by the test 36 // such as file descriptors. (This is normally not a problem as 37 // main tests usually run in a separate VM, however on some platforms 38 // such as the Mac, separate VMs are not possible and non-applet 39 // tests will cause problems). Also, you don't have to worry about 40 // synchronisation stuff in Applet tests they way you do in main 41 // tests... 42 43 44 public class SelectCurrentItemTest extends Applet implements ItemListener, 45 WindowListener, Runnable 46 { 47 //Declare things used in the test, like buttons and labels here 48 Frame frame; 49 Choice theChoice; 50 Robot robot; 51 52 Object lock = new Object(); 53 boolean passed = false; 54 55 public void init() 56 { 57 //Create instructions for the user here, as well as set up 58 // the environment -- set the layout manager, add buttons, 59 // etc. 60 61 this.setLayout (new BorderLayout ()); 62 63 String[] instructions = 64 { 65 "This is an AUTOMATIC test", 66 "simply wait until it is done" 67 }; 68 Sysout.createDialog( ); 69 Sysout.printInstructions( instructions ); 70 71 frame = new Frame("SelectCurrentItemTest"); 72 theChoice = new Choice(); 73 for (int i = 0; i < 10; i++) { 74 theChoice.add(new String("Choice Item " + i)); 75 } 76 theChoice.addItemListener(this); 77 frame.add(theChoice); 78 frame.addWindowListener(this); 79 80 try { 81 robot = new Robot(); 82 robot.setAutoDelay(500); 83 } 84 catch (AWTException e) { 85 throw new RuntimeException("Unable to create Robot. Test fails."); 86 } 87 88 }//End init() 89 90 public void start () 91 { 92 //Get things going. Request focus, set size, et cetera 93 setSize (200,200); 94 setVisible(true); 95 validate(); 96 97 //What would normally go into main() will probably go here. 98 //Use System.out.println for diagnostic messages that you want 99 //to read after the test is done. 100 //Use Sysout.println for messages you want the tester to read. 101 102 frame.setLocation(1,20); 103 robot.mouseMove(10, 30); 104 frame.pack(); 105 frame.setVisible(true); 106 synchronized(lock) { 107 try { 108 lock.wait(120000); 109 } 110 catch(InterruptedException e) {} 111 } 112 robot.waitForIdle(); 113 if (!passed) { 114 throw new RuntimeException("TEST FAILED!"); 115 } 116 117 // wait to make sure ItemEvent has been processed 118 119 // try {Thread.sleep(10000);} catch (InterruptedException e){} 120 }// start() 121 122 public void run() { 123 try {Thread.sleep(1000);} catch (InterruptedException e){} 124 // get loc of Choice on screen 125 Point loc = theChoice.getLocationOnScreen(); 126 // get bounds of Choice 127 Dimension size = theChoice.getSize(); 128 robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2); 129 130 robot.setAutoDelay(250); 131 robot.mousePress(InputEvent.BUTTON1_MASK); 132 robot.mouseRelease(InputEvent.BUTTON1_MASK); 133 134 robot.setAutoDelay(1000); 135 robot.mouseMove(loc.x + size.width / 2, loc.y + size.height + size.height / 2); 136 robot.setAutoDelay(250); 137 robot.mousePress(InputEvent.BUTTON1_MASK); 138 robot.mouseRelease(InputEvent.BUTTON1_MASK); 139 ((SunToolkit)Toolkit.getDefaultToolkit()).realSync(); 140 synchronized(lock) { 141 lock.notify(); 142 } 143 } 144 145 public void itemStateChanged(ItemEvent e) { 146 Sysout.println("ItemEvent received. Test passes"); 147 passed = true; 148 } 149 150 public void windowOpened(WindowEvent e) { 151 Sysout.println("windowActivated()"); 152 Thread testThread = new Thread(this); 153 testThread.start(); 154 } 155 public void windowActivated(WindowEvent e) { 156 } 157 public void windowDeactivated(WindowEvent e) {} 158 public void windowClosed(WindowEvent e) {} 159 public void windowClosing(WindowEvent e) {} 160 public void windowIconified(WindowEvent e) {} 161 public void windowDeiconified(WindowEvent e) {} 162 163 }// class SelectCurrentItemTest 164 165 166 /**************************************************** 167 Standard Test Machinery 168 DO NOT modify anything below -- it's a standard 169 chunk of code whose purpose is to make user 170 interaction uniform, and thereby make it simpler 171 to read and understand someone else's test. 172 ****************************************************/ 173 174 /** 175 This is part of the standard test machinery. 176 It creates a dialog (with the instructions), and is the interface 177 for sending text messages to the user. 178 To print the instructions, send an array of strings to Sysout.createDialog 179 WithInstructions method. Put one line of instructions per array entry. 180 To display a message for the tester to see, simply call Sysout.println 181 with the string to be displayed. 182 This mimics System.out.println but works within the test harness as well 183 as standalone. 184 */ 185 186 class Sysout 187 { 188 private static TestDialog dialog; 189 190 public static void createDialogWithInstructions( String[] instructions ) 191 { 192 dialog = new TestDialog( new Frame(), "Instructions" ); 193 dialog.printInstructions( instructions ); 194 dialog.setVisible(true); 195 println( "Any messages for the tester will display here." ); 196 } 197 198 public static void createDialog( ) 199 { 200 dialog = new TestDialog( new Frame(), "Instructions" ); 201 String[] defInstr = { "Instructions will appear here. ", "" } ; 202 dialog.printInstructions( defInstr ); 203 dialog.setLocation(0, 400); 204 dialog.setVisible(true); 205 println( "Any messages for the tester will display here." ); 206 } 207 208 209 public static void printInstructions( String[] instructions ) 210 { 211 dialog.printInstructions( instructions ); 212 } 213 214 215 public static void println( String messageIn ) 216 { 217 dialog.displayMessage( messageIn ); 218 } 219 220 }// Sysout class 221 222 /** 223 This is part of the standard test machinery. It provides a place for the 224 test instructions to be displayed, and a place for interactive messages 225 to the user to be displayed. 226 To have the test instructions displayed, see Sysout. 227 To have a message to the user be displayed, see Sysout. 228 Do not call anything in this dialog directly. 229 */ 230 class TestDialog extends Dialog 231 { 232 233 TextArea instructionsText; 234 TextArea messageText; 235 int maxStringLength = 80; 236 237 //DO NOT call this directly, go through Sysout 238 public TestDialog( Frame frame, String name ) 239 { 240 super( frame, name ); 241 int scrollBoth = TextArea.SCROLLBARS_BOTH; 242 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 243 add( "North", instructionsText ); 244 245 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 246 add("Center", messageText); 247 248 pack(); 249 250 show(); 251 }// TestDialog() 252 253 //DO NOT call this directly, go through Sysout 254 public void printInstructions( String[] instructions ) 255 { 256 //Clear out any current instructions 257 instructionsText.setText( "" ); 258 259 //Go down array of instruction strings 260 261 String printStr, remainingStr; 262 for( int i=0; i < instructions.length; i++ ) 263 { 264 //chop up each into pieces maxSringLength long 265 remainingStr = instructions[ i ]; 266 while( remainingStr.length() > 0 ) 267 { 268 //if longer than max then chop off first max chars to print 269 if( remainingStr.length() >= maxStringLength ) 270 { 271 //Try to chop on a word boundary 272 int posOfSpace = remainingStr. 273 lastIndexOf( ' ', maxStringLength - 1 ); 274 275 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 276 277 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 278 remainingStr = remainingStr.substring( posOfSpace + 1 ); 279 } 280 //else just print 281 else 282 { 283 printStr = remainingStr; 284 remainingStr = ""; 285 } 286 287 instructionsText.append( printStr + "\n" ); 288 289 }// while 290 291 }// for 292 293 }//printInstructions() 294 295 //DO NOT call this directly, go through Sysout 296 public void displayMessage( String messageIn ) 297 { 298 messageText.append( messageIn + "\n" ); 299 System.out.println(messageIn); 300 } 301 302 }// TestDialog class