1 /* 2 test 1.2 98/08/05 3 @bug 4449139 4 @summary test MouseWheelEvent generation by Scrollbar component 5 @author : area=Scrollbar 6 @run applet ScrollbarMouseWheelTest.html 7 */ 8 9 import java.applet.Applet; 10 import java.awt.*; 11 import java.awt.event.*; 12 13 public class ScrollbarMouseWheelTest extends Applet implements 14 MouseWheelListener, WindowListener 15 { 16 final static String tk = Toolkit.getDefaultToolkit().getClass().getName(); 17 final static int REPS = 5; 18 // There is a bug on Windows: 4616935. 19 // Wheel events comes to every component in the hierarchy so we should 20 // check a platform. 21 // There are two scrollbars within one Panel and both accept 5 clicks, so 22 // Panel would accept 5*2 clicks on Windows. 23 final static int PANEL_REPS = tk.equals("sun.awt.windows.WToolkit")? REPS * 2: REPS; 24 25 static boolean testDone = false; 26 27 Frame frame; 28 Scrollbar sb1; 29 Scrollbar sb2; 30 Panel pnl; 31 class Sema { 32 boolean flag; 33 boolean getVal() { return flag;} 34 void setVal(boolean b) { flag = b;} 35 } 36 Sema sema = new Sema(); 37 38 Robot robot; 39 40 int sb1upevents, sb2upevents, pnlupevents; 41 int sb1downevents, sb2downevents, pnldownevents; 42 43 public void init() 44 { 45 //Create instructions for the user here, as well as set up 46 // the environment -- set the layout manager, add buttons, 47 // etc. 48 49 this.setLayout (new BorderLayout ()); 50 51 String[] instructions = 52 { 53 "This is an AUTOMATIC test", 54 "simply wait until it is done" 55 }; 56 Sysout.createDialog( ); 57 Sysout.printInstructions( instructions ); 58 59 }//End init() 60 61 public void start () 62 { 63 64 //Get things going. Request focus, set size, et cetera 65 setSize (200,200); 66 show(); 67 68 //What would normally go into main() will probably go here. 69 //Use System.out.println for diagnostic messages that you want 70 //to read after the test is done. 71 //Use Sysout.println for messages you want the tester to read. 72 73 // Move mouse to upper-right area 74 try { 75 robot = new Robot(); 76 } catch (AWTException e) { 77 System.out.println("Problem creating Robot. FAIL."); 78 throw new RuntimeException("Problem creating Robot. FAIL."); 79 80 } 81 82 robot.setAutoDelay(500); 83 robot.setAutoWaitForIdle(true); 84 robot.mouseMove(150, 200); 85 86 // Show test Frame 87 Frame frame = new Frame("ScrollbarMouseWheelTest"); 88 frame.addWindowListener(this); 89 pnl = new Panel(); 90 pnl.setLayout(new GridLayout(1,2)); 91 pnl.addMouseWheelListener(this); 92 sb1 = new Scrollbar(); 93 sb1.addMouseWheelListener(this); 94 pnl.add(sb1); 95 sb2 = new Scrollbar(); 96 pnl.add(sb2); 97 frame.add(pnl); 98 frame.setSize(200, 400); 99 frame.setVisible(true); 100 frame.toFront(); 101 102 // When Frame is active, start testing (handled in windowActivated()) 103 while (true) { 104 synchronized (sema) { 105 if (sema.getVal()) { 106 break; 107 } 108 } 109 } 110 doTest(); 111 112 Sysout.println("Test done."); 113 if (sb1upevents == REPS && 114 sb2upevents == 0 && 115 pnlupevents == PANEL_REPS && 116 sb1downevents == REPS && 117 sb2downevents == 0 && 118 pnldownevents == PANEL_REPS) { 119 Sysout.println("PASSED."); 120 } else { 121 System.out.println("Test Failed:" + 122 "\n\tsb1upevents =" + sb1upevents + 123 "\n\tsb2upevents = " + sb2upevents + 124 "\n\tpnlupevents = " + pnlupevents + 125 "\n\tsb1downevents =" + sb1downevents + 126 "\n\tsb2downevents = " + sb2downevents + 127 "\n\tpnldownevents = " + pnldownevents); 128 throw new RuntimeException("Test FAILED."); 129 } 130 }// start() 131 132 public void doTest() { 133 if (!testDone) { 134 // up on sb1 135 testComp(sb1, true); 136 // down on sb1 137 testComp(sb1, false); 138 // up on sb2 139 testComp(sb2, true); 140 // down on sb2 141 testComp(sb2, false); 142 } 143 else { 144 Sysout.println("Skipping test - already done"); 145 } 146 } 147 148 public void testComp(Component comp, boolean up) { 149 int loop; 150 Rectangle bounds; 151 152 bounds = comp.getBounds(); 153 robot.mouseMove(bounds.x + bounds.width / 2, 154 bounds.y + bounds.height / 2); 155 for (loop = 0; loop < REPS; loop++) { 156 Sysout.println("Robot.mouseWheel() on " + comp.getName()); 157 robot.mouseWheel(up ? -1 : 1); 158 } 159 } 160 161 public void mouseWheelMoved(MouseWheelEvent mwe) { 162 Component src = mwe.getComponent(); 163 Sysout.println("mouseWheelMoved() on " + src.getName()); 164 if (mwe.getWheelRotation() == -1) { 165 if (src == sb1) { 166 sb1upevents++; 167 } else if (src == sb2) { 168 sb2upevents++; 169 } else if (src == pnl) { 170 pnlupevents++; 171 } else { 172 System.out.println("weird source component"); 173 } 174 } else if (mwe.getWheelRotation() == 1) { 175 if (src == sb1) { 176 sb1downevents++; 177 } else if (src == sb2) { 178 sb2downevents++; 179 } else if (src == pnl) { 180 pnldownevents++; 181 } else { 182 System.out.println("weird source component"); 183 } 184 } else { 185 System.out.println("weird wheel rotation"); 186 } 187 } 188 189 public void windowActivated(WindowEvent we) { 190 synchronized (sema) { 191 sema.setVal(true); 192 } 193 } 194 195 public void windowClosed(WindowEvent we) {} 196 public void windowClosing(WindowEvent we) {} 197 public void windowDeactivated(WindowEvent we) {} 198 public void windowDeiconified(WindowEvent we) {} 199 public void windowIconified(WindowEvent we) {} 200 public void windowOpened(WindowEvent we) {} 201 202 203 204 }// class ScrollbarMouseWheelTest 205 206 207 /**************************************************** 208 Standard Test Machinery 209 DO NOT modify anything below -- it's a standard 210 chunk of code whose purpose is to make user 211 interaction uniform, and thereby make it simpler 212 to read and understand someone else's test. 213 ****************************************************/ 214 215 /** 216 This is part of the standard test machinery. 217 It creates a dialog (with the instructions), and is the interface 218 for sending text messages to the user. 219 To print the instructions, send an array of strings to Sysout.createDialog 220 WithInstructions method. Put one line of instructions per array entry. 221 To display a message for the tester to see, simply call Sysout.println 222 with the string to be displayed. 223 This mimics System.out.println but works within the test harness as well 224 as standalone. 225 */ 226 227 class Sysout 228 { 229 private static TestDialog dialog; 230 231 public static void createDialogWithInstructions( String[] instructions ) 232 { 233 dialog = new TestDialog( new Frame(), "Instructions" ); 234 dialog.printInstructions( instructions ); 235 dialog.show(); 236 } 237 238 public static void createDialog( ) 239 { 240 dialog = new TestDialog( new Frame(), "Instructions" ); 241 String[] defInstr = { "Instructions will appear here. ", "" } ; 242 dialog.printInstructions( defInstr ); 243 dialog.show(); 244 } 245 246 247 public static void printInstructions( String[] instructions ) 248 { 249 dialog.printInstructions( instructions ); 250 } 251 252 253 public static void println( String messageIn ) 254 { 255 dialog.displayMessage( messageIn ); 256 } 257 258 }// Sysout class 259 260 /** 261 This is part of the standard test machinery. It provides a place for the 262 test instructions to be displayed, and a place for interactive messages 263 to the user to be displayed. 264 To have the test instructions displayed, see Sysout. 265 To have a message to the user be displayed, see Sysout. 266 Do not call anything in this dialog directly. 267 */ 268 class TestDialog extends Dialog 269 { 270 271 TextArea instructionsText; 272 TextArea messageText; 273 int maxStringLength = 80; 274 275 //DO NOT call this directly, go through Sysout 276 public TestDialog( Frame frame, String name ) 277 { 278 super( frame, name ); 279 int scrollBoth = TextArea.SCROLLBARS_BOTH; 280 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 281 add( "North", instructionsText ); 282 283 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 284 add("South", messageText); 285 286 pack(); 287 setLocation(300,0); 288 289 show(); 290 }// TestDialog() 291 292 //DO NOT call this directly, go through Sysout 293 public void printInstructions( String[] instructions ) 294 { 295 //Clear out any current instructions 296 instructionsText.setText( "" ); 297 298 //Go down array of instruction strings 299 300 String printStr, remainingStr; 301 for( int i=0; i < instructions.length; i++ ) 302 { 303 //chop up each into pieces maxSringLength long 304 remainingStr = instructions[ i ]; 305 while( remainingStr.length() > 0 ) 306 { 307 //if longer than max then chop off first max chars to print 308 if( remainingStr.length() >= maxStringLength ) 309 { 310 //Try to chop on a word boundary 311 int posOfSpace = remainingStr. 312 lastIndexOf( ' ', maxStringLength - 1 ); 313 314 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 315 316 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 317 remainingStr = remainingStr.substring( posOfSpace + 1 ); 318 } 319 //else just print 320 else 321 { 322 printStr = remainingStr; 323 remainingStr = ""; 324 } 325 326 instructionsText.append( printStr + "\n" ); 327 328 }// while 329 330 }// for 331 332 }//printInstructions() 333 334 //DO NOT call this directly, go through Sysout 335 public void displayMessage( String messageIn ) 336 { 337 messageText.append( messageIn + "\n" ); 338 } 339 340 }// TestDialog class