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