blob: 709378c744e712824e84b94c5430789e49bec8c1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 @test
26 @bug 6479820
27 @summary Spurious entry/exit mouse events
28 @author Andrei Dmitriev: area=awt.event
29 @run main/manual SpuriousExitEnter
30*/
31
32/**
33 * SpuriousExitEnter.java
34 *
35 * summary: exit and enter mouse event generated when resizing the window
36 */
37
38import java.awt.*;
39import java.awt.event.*;
40
41public class SpuriousExitEnter {
42 static Frame frame = new Frame("SpuriousExitEnter");
43
44 private static void init() {
45 String[] instructions =
46 {
47 "You see a plain Frame.",
48 "1) Press and hold the left mouse button over its right-bottom corner.",
49 "2) Resize the frame several times by dragging a mouse fast.",
50 "3) Let Frame be as small as possible, continue dragging mouse over the frame.",
51 "4) Let Frame be as large as possible.",
52 "5) If Enter or Exit events are output in the area below while resizing ",
53 " then the test failed. Otherwise, passed."
54 };
55
56 Sysout.createDialog( );
57 Sysout.printInstructions( instructions );
58 Sysout.enableNumbering(true);
59
60 frame.addMouseListener(new MouseAdapter(){
61 public void mouseEntered(MouseEvent e){
62 Sysout.println("Entered : " + e);
63 }
64 public void mouseExited(MouseEvent e){
65 Sysout.println("Exited : " + e);
66 }
67 });
68
69 frame.setSize(600, 200);
70 frame.setVisible(true);
71 }//End init()
72
73
74 /*****************************************************
75 * Standard Test Machinery Section
76 * DO NOT modify anything in this section -- it's a
77 * standard chunk of code which has all of the
78 * synchronisation necessary for the test harness.
79 * By keeping it the same in all tests, it is easier
80 * to read and understand someone else's test, as
81 * well as insuring that all tests behave correctly
82 * with the test harness.
83 * There is a section following this for test-defined
84 * classes
85 ******************************************************/
86 private static boolean theTestPassed = false;
87 private static boolean testGeneratedInterrupt = false;
88 private static String failureMessage = "";
89
90 private static Thread mainThread = null;
91
92 private static int sleepTime = 300000;
93
94 public static void main( String args[] ) throws InterruptedException
95 {
96 mainThread = Thread.currentThread();
97
98 try
99 {
100 init();
101 }
102 catch( TestPassedException e )
103 {
104 //The test passed, so just return from main and harness will
105 // interepret this return as a pass
106 return;
107 }
108 //At this point, neither test passed nor test failed has been
109 // called -- either would have thrown an exception and ended the
110 // test, so we know we have multiple threads.
111
112 //Test involves other threads, so sleep and wait for them to
113 // called pass() or fail()
114 try
115 {
116 Thread.sleep( sleepTime );
117 //Timed out, so fail the test
118 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
119 }
120 catch (InterruptedException e)
121 {
122 if( ! testGeneratedInterrupt ) throw e;
123
124 //reset flag in case hit this code more than once for some reason (just safety)
125 testGeneratedInterrupt = false;
126 if ( theTestPassed == false )
127 {
128 throw new RuntimeException( failureMessage );
129 }
130 }
131
132 }//main
133
134 public static synchronized void setTimeoutTo( int seconds )
135 {
136 sleepTime = seconds * 1000;
137 }
138
139 public static synchronized void pass()
140 {
141 Sysout.println( "The test passed." );
142 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
143 //first check if this is executing in main thread
144 if ( mainThread == Thread.currentThread() )
145 {
146 //Still in the main thread, so set the flag just for kicks,
147 // and throw a test passed exception which will be caught
148 // and end the test.
149 theTestPassed = true;
150 throw new TestPassedException();
151 }
152 //pass was called from a different thread, so set the flag and interrupt
153 // the main thead.
154 theTestPassed = true;
155 testGeneratedInterrupt = true;
156 if (mainThread != null){
157 mainThread.interrupt();
158 }
159 }//pass()
160
161 public static synchronized void fail()
162 {
163 //test writer didn't specify why test failed, so give generic
164 fail( "it just plain failed! :-)" );
165 }
166
167 public static synchronized void fail( String whyFailed )
168 {
169 Sysout.println( "The test failed: " + whyFailed );
170 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
171 //check if this called from main thread
172 if ( mainThread == Thread.currentThread() )
173 {
174 //If main thread, fail now 'cause not sleeping
175 throw new RuntimeException( whyFailed );
176 }
177 theTestPassed = false;
178 testGeneratedInterrupt = true;
179 failureMessage = whyFailed;
180 mainThread.interrupt();
181 }//fail()
182
183}// class SpuriousExitEnter
184
185//This exception is used to exit from any level of call nesting
186// when it's determined that the test has passed, and immediately
187// end the test.
188
189class TestPassedException extends RuntimeException
190{
191}
192
193//*********** End Standard Test Machinery Section **********
194
195
196//************ Begin classes defined for the test ****************
197
198// make listeners in a class defined here, and instantiate them in init()
199
200/* Example of a class which may be written as part of a test
201class NewClass implements anInterface
202 {
203 static int newVar = 0;
204
205 public void eventDispatched(AWTEvent e)
206 {
207 //Counting events to see if we get enough
208 eventCount++;
209
210 if( eventCount == 20 )
211 {
212 //got enough events, so pass
213
214 ManualMainTest.pass();
215 }
216 else if( tries == 20 )
217 {
218 //tried too many times without getting enough events so fail
219
220 ManualMainTest.fail();
221 }
222
223 }// eventDispatched()
224
225 }// NewClass class
226
227*/
228
229
230//************** End classes defined for the test *******************
231
232
233
234
235/****************************************************
236 Standard Test Machinery
237 DO NOT modify anything below -- it's a standard
238 chunk of code whose purpose is to make user
239 interaction uniform, and thereby make it simpler
240 to read and understand someone else's test.
241 ****************************************************/
242
243/**
244 This is part of the standard test machinery.
245 It creates a dialog (with the instructions), and is the interface
246 for sending text messages to the user.
247 To print the instructions, send an array of strings to Sysout.createDialog
248 WithInstructions method. Put one line of instructions per array entry.
249 To display a message for the tester to see, simply call Sysout.println
250 with the string to be displayed.
251 This mimics System.out.println but works within the test harness as well
252 as standalone.
253 */
254
255class Sysout
256{
257 private static TestDialog dialog;
258 private static boolean numbering = false;
259 private static int messageNumber = 0;
260
261 public static void createDialogWithInstructions( String[] instructions )
262 {
263 dialog = new TestDialog( new Frame(), "Instructions" );
264 dialog.printInstructions( instructions );
265 dialog.setVisible(true);
266 println( "Any messages for the tester will display here." );
267 }
268
269 public static void createDialog( )
270 {
271 dialog = new TestDialog( new Frame(), "Instructions" );
272 String[] defInstr = { "Instructions will appear here. ", "" } ;
273 dialog.printInstructions( defInstr );
274 dialog.setVisible(true);
275 println( "Any messages for the tester will display here." );
276 }
277
278 /* Enables message counting for the tester. */
279 public static void enableNumbering(boolean enable){
280 numbering = enable;
281 }
282
283 public static void printInstructions( String[] instructions )
284 {
285 dialog.printInstructions( instructions );
286 }
287
288
289 public static void println( String messageIn )
290 {
291 if (numbering) {
292 messageIn = "" + messageNumber + " " + messageIn;
293 messageNumber++;
294 }
295 dialog.displayMessage( messageIn );
296 }
297
298}// Sysout class
299
300/**
301 This is part of the standard test machinery. It provides a place for the
302 test instructions to be displayed, and a place for interactive messages
303 to the user to be displayed.
304 To have the test instructions displayed, see Sysout.
305 To have a message to the user be displayed, see Sysout.
306 Do not call anything in this dialog directly.
307 */
308class TestDialog extends Dialog implements ActionListener
309{
310
311 TextArea instructionsText;
312 TextArea messageText;
313 int maxStringLength = 80;
314 Panel buttonP = new Panel();
315 Button passB = new Button( "pass" );
316 Button failB = new Button( "fail" );
317
318 //DO NOT call this directly, go through Sysout
319 public TestDialog( Frame frame, String name )
320 {
321 super( frame, name );
322 int scrollBoth = TextArea.SCROLLBARS_BOTH;
323 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
324 add( "North", instructionsText );
325
326 messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
327 add("Center", messageText);
328
329 passB = new Button( "pass" );
330 passB.setActionCommand( "pass" );
331 passB.addActionListener( this );
332 buttonP.add( "East", passB );
333
334 failB = new Button( "fail" );
335 failB.setActionCommand( "fail" );
336 failB.addActionListener( this );
337 buttonP.add( "West", failB );
338
339 add( "South", buttonP );
340 pack();
341
342 setVisible(true);
343 }// TestDialog()
344
345 //DO NOT call this directly, go through Sysout
346 public void printInstructions( String[] instructions )
347 {
348 //Clear out any current instructions
349 instructionsText.setText( "" );
350
351 //Go down array of instruction strings
352
353 String printStr, remainingStr;
354 for( int i=0; i < instructions.length; i++ )
355 {
356 //chop up each into pieces maxSringLength long
357 remainingStr = instructions[ i ];
358 while( remainingStr.length() > 0 )
359 {
360 //if longer than max then chop off first max chars to print
361 if( remainingStr.length() >= maxStringLength )
362 {
363 //Try to chop on a word boundary
364 int posOfSpace = remainingStr.
365 lastIndexOf( ' ', maxStringLength - 1 );
366
367 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
368
369 printStr = remainingStr.substring( 0, posOfSpace + 1 );
370 remainingStr = remainingStr.substring( posOfSpace + 1 );
371 }
372 //else just print
373 else
374 {
375 printStr = remainingStr;
376 remainingStr = "";
377 }
378
379 instructionsText.append( printStr + "\n" );
380
381 }// while
382
383 }// for
384
385 }//printInstructions()
386
387 //DO NOT call this directly, go through Sysout
388 public void displayMessage( String messageIn )
389 {
390 messageText.append( messageIn + "\n" );
391 System.out.println(messageIn);
392 }
393
394 //catch presses of the passed and failed buttons.
395 //simply call the standard pass() or fail() static methods of
396 //ManualMainTest
397 public void actionPerformed( ActionEvent e )
398 {
399 if( e.getActionCommand() == "pass" )
400 {
401 SpuriousExitEnter.pass();
402 }
403 else
404 {
405 SpuriousExitEnter.fail();
406 }
407 }
408
409}// TestDialog class