blob: 512bb65f5231fffaa699930017333489a98c9a5a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 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 4811096
27 @summary Tests whether a LW menu correctly overlaps a HW button
28 @author anthony.petrov@...: area=awt.mixing
29 @library ../regtesthelpers
30 @build Util
31 @run main LWPopupMenu
32*/
33
34
35/**
36 * LWPopupMenu.java
37 *
38 * summary: Tests whether a LW menu correctly overlaps a HW button
39 */
40
41import java.awt.*;
42import java.awt.event.*;
43import javax.swing.*;
44import test.java.awt.regtesthelpers.Util;
45
46
47
48public class LWPopupMenu
49{
50
51 //*** test-writer defined static variables go here ***
52
53 static volatile boolean failed = true;
54
55 private static void init()
56 {
57 //*** Create instructions for the user here ***
58
59 String[] instructions =
60 {
61 "This is an AUTOMATIC test, simply wait until it is done.",
62 "The result (passed or failed) will be shown in the",
63 "message window below."
64 };
65 Sysout.createDialog( );
66 Sysout.printInstructions( instructions );
67
68 JFrame f = new JFrame("LW menu test");
69
70 JMenuBar menubar = new JMenuBar();
71 f.setJMenuBar(menubar);
72
73 // Create lightweight-enabled menu
74 JMenu lmenu = new JMenu("Lite Menu");
75 lmenu.add("Salad");
76 lmenu.add( new AbstractAction("Fruit Plate") {
77 public void actionPerformed(ActionEvent e) {
78 failed = false;
79 }
80 });
81 lmenu.add("Water");
82 menubar.add(lmenu);
83
84 // Create Heavyweight AWT Button
85 Button heavy = new Button(" Heavyweight Button ");
86
87 // Add heavy button to box
88 Box box = Box.createVerticalBox();
89 box.add(Box.createVerticalStrut(20));
90 box.add(heavy);
91 box.add(Box.createVerticalStrut(20));
92
93 f.getContentPane().add("Center", box);
94
95 f.pack();
96 f.show();
97
98 Robot robot = Util.createRobot();
99 robot.setAutoDelay(20);
100
101 Util.waitForIdle(robot);
102
103 // Activate the menu
104 Point lLoc = lmenu.getLocationOnScreen();
105 robot.mouseMove(lLoc.x + 5, lLoc.y + 5);
106
107 robot.mousePress(InputEvent.BUTTON1_MASK);
108 robot.mouseRelease(InputEvent.BUTTON1_MASK);
109 Util.waitForIdle(robot);
110
111
112 // Click on the "Fruit Plate" menu item.
113 // It's assumed that the menu item is located
114 // above the heavyweight button.
115 Point bLoc = heavy.getLocationOnScreen();
116 robot.mouseMove(bLoc.x + 10, bLoc.y + 5);
117
118 robot.mousePress(InputEvent.BUTTON1_MASK);
119 robot.mouseRelease(InputEvent.BUTTON1_MASK);
120 Util.waitForIdle(robot);
121
122 if (failed) {
123 LWPopupMenu.fail("The LW menu item did not received the click.");
124 } else {
125 LWPopupMenu.pass();
126 }
127 }//End init()
128
129
130
131 /*****************************************************
132 * Standard Test Machinery Section
133 * DO NOT modify anything in this section -- it's a
134 * standard chunk of code which has all of the
135 * synchronisation necessary for the test harness.
136 * By keeping it the same in all tests, it is easier
137 * to read and understand someone else's test, as
138 * well as insuring that all tests behave correctly
139 * with the test harness.
140 * There is a section following this for test-
141 * classes
142 ******************************************************/
143 private static boolean theTestPassed = false;
144 private static boolean testGeneratedInterrupt = false;
145 private static String failureMessage = "";
146
147 private static Thread mainThread = null;
148
149 private static int sleepTime = 300000;
150
151 // Not sure about what happens if multiple of this test are
152 // instantiated in the same VM. Being static (and using
153 // static vars), it aint gonna work. Not worrying about
154 // it for now.
155 public static void main( String args[] ) throws InterruptedException
156 {
157 mainThread = Thread.currentThread();
158 try
159 {
160 init();
161 }
162 catch( TestPassedException e )
163 {
164 //The test passed, so just return from main and harness will
165 // interepret this return as a pass
166 return;
167 }
168 //At this point, neither test pass nor test fail has been
169 // called -- either would have thrown an exception and ended the
170 // test, so we know we have multiple threads.
171
172 //Test involves other threads, so sleep and wait for them to
173 // called pass() or fail()
174 try
175 {
176 Thread.sleep( sleepTime );
177 //Timed out, so fail the test
178 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
179 }
180 catch (InterruptedException e)
181 {
182 //The test harness may have interrupted the test. If so, rethrow the exception
183 // so that the harness gets it and deals with it.
184 if( ! testGeneratedInterrupt ) throw e;
185
186 //reset flag in case hit this code more than once for some reason (just safety)
187 testGeneratedInterrupt = false;
188
189 if ( theTestPassed == false )
190 {
191 throw new RuntimeException( failureMessage );
192 }
193 }
194
195 }//main
196
197 public static synchronized void setTimeoutTo( int seconds )
198 {
199 sleepTime = seconds * 1000;
200 }
201
202 public static synchronized void pass()
203 {
204 Sysout.println( "The test passed." );
205 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
206 //first check if this is executing in main thread
207 if ( mainThread == Thread.currentThread() )
208 {
209 //Still in the main thread, so set the flag just for kicks,
210 // and throw a test passed exception which will be caught
211 // and end the test.
212 theTestPassed = true;
213 throw new TestPassedException();
214 }
215 theTestPassed = true;
216 testGeneratedInterrupt = true;
217 mainThread.interrupt();
218 }//pass()
219
220 public static synchronized void fail()
221 {
222 //test writer didn't specify why test failed, so give generic
223 fail( "it just plain failed! :-)" );
224 }
225
226 public static synchronized void fail( String whyFailed )
227 {
228 Sysout.println( "The test failed: " + whyFailed );
229 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
230 //check if this called from main thread
231 if ( mainThread == Thread.currentThread() )
232 {
233 //If main thread, fail now 'cause not sleeping
234 throw new RuntimeException( whyFailed );
235 }
236 theTestPassed = false;
237 testGeneratedInterrupt = true;
238 failureMessage = whyFailed;
239 mainThread.interrupt();
240 }//fail()
241
242}// class LWPopupMenu
243
244//This exception is used to exit from any level of call nesting
245// when it's determined that the test has passed, and immediately
246// end the test.
247class TestPassedException extends RuntimeException
248{
249}
250
251//*********** End Standard Test Machinery Section **********
252
253
254//************ Begin classes defined for the test ****************
255
256// if want to make listeners, here is the recommended place for them, then instantiate
257// them in init()
258
259/* Example of a class which may be written as part of a test
260class NewClass implements anInterface
261 {
262 static int newVar = 0;
263
264 public void eventDispatched(AWTEvent e)
265 {
266 //Counting events to see if we get enough
267 eventCount++;
268
269 if( eventCount == 20 )
270 {
271 //got enough events, so pass
272
273 LWPopupMenu.pass();
274 }
275 else if( tries == 20 )
276 {
277 //tried too many times without getting enough events so fail
278
279 LWPopupMenu.fail();
280 }
281
282 }// eventDispatched()
283
284 }// NewClass class
285
286*/
287
288
289//************** End classes defined for the test *******************
290
291
292
293
294/****************************************************
295 Standard Test Machinery
296 DO NOT modify anything below -- it's a standard
297 chunk of code whose purpose is to make user
298 interaction uniform, and thereby make it simpler
299 to read and understand someone else's test.
300 ****************************************************/
301
302/**
303 This is part of the standard test machinery.
304 It creates a dialog (with the instructions), and is the interface
305 for sending text messages to the user.
306 To print the instructions, send an array of strings to Sysout.createDialog
307 WithInstructions method. Put one line of instructions per array entry.
308 To display a message for the tester to see, simply call Sysout.println
309 with the string to be displayed.
310 This mimics System.out.println but works within the test harness as well
311 as standalone.
312 */
313
314class Sysout
315{
316 private static TestDialog dialog;
317
318 public static void createDialogWithInstructions( String[] instructions )
319 {
320 dialog = new TestDialog( new Frame(), "Instructions" );
321 dialog.printInstructions( instructions );
322 dialog.setVisible(true);
323 println( "Any messages for the tester will display here." );
324 }
325
326 public static void createDialog( )
327 {
328 dialog = new TestDialog( new Frame(), "Instructions" );
329 String[] defInstr = { "Instructions will appear here. ", "" } ;
330 dialog.printInstructions( defInstr );
331 dialog.setVisible(true);
332 println( "Any messages for the tester will display here." );
333 }
334
335
336 public static void printInstructions( String[] instructions )
337 {
338 dialog.printInstructions( instructions );
339 }
340
341
342 public static void println( String messageIn )
343 {
344 dialog.displayMessage( messageIn );
345 System.out.println(messageIn);
346 }
347
348}// Sysout class
349
350/**
351 This is part of the standard test machinery. It provides a place for the
352 test instructions to be displayed, and a place for interactive messages
353 to the user to be displayed.
354 To have the test instructions displayed, see Sysout.
355 To have a message to the user be displayed, see Sysout.
356 Do not call anything in this dialog directly.
357 */
358class TestDialog extends Dialog
359{
360
361 TextArea instructionsText;
362 TextArea messageText;
363 int maxStringLength = 80;
364
365 //DO NOT call this directly, go through Sysout
366 public TestDialog( Frame frame, String name )
367 {
368 super( frame, name );
369 int scrollBoth = TextArea.SCROLLBARS_BOTH;
370 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
371 add( "North", instructionsText );
372
373 messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
374 add("Center", messageText);
375
376 pack();
377
378 setVisible(true);
379 }// TestDialog()
380
381 //DO NOT call this directly, go through Sysout
382 public void printInstructions( String[] instructions )
383 {
384 //Clear out any current instructions
385 instructionsText.setText( "" );
386
387 //Go down array of instruction strings
388
389 String printStr, remainingStr;
390 for( int i=0; i < instructions.length; i++ )
391 {
392 //chop up each into pieces maxSringLength long
393 remainingStr = instructions[ i ];
394 while( remainingStr.length() > 0 )
395 {
396 //if longer than max then chop off first max chars to print
397 if( remainingStr.length() >= maxStringLength )
398 {
399 //Try to chop on a word boundary
400 int posOfSpace = remainingStr.
401 lastIndexOf( ' ', maxStringLength - 1 );
402
403 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
404
405 printStr = remainingStr.substring( 0, posOfSpace + 1 );
406 remainingStr = remainingStr.substring( posOfSpace + 1 );
407 }
408 //else just print
409 else
410 {
411 printStr = remainingStr;
412 remainingStr = "";
413 }
414
415 instructionsText.append( printStr + "\n" );
416
417 }// while
418
419 }// for
420
421 }//printInstructions()
422
423 //DO NOT call this directly, go through Sysout
424 public void displayMessage( String messageIn )
425 {
426 messageText.append( messageIn + "\n" );
427 System.out.println(messageIn);
428 }
429
430}// TestDialog class