|
1 /* |
|
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 6579962 |
|
27 @summary FileDialog focus is incorrect (Linux) |
|
28 @author dmitry.cherepanov area=awt.filedialog |
|
29 @run main/manual DefaultFocusOwner |
|
30 */ |
|
31 |
|
32 /* |
|
33 * The test verifies that when the file dialog is open |
|
34 * the focus is transfered on the correct component of the dialog |
|
35 */ |
|
36 |
|
37 import java.awt.*; |
|
38 import java.awt.event.*; |
|
39 |
|
40 public class DefaultFocusOwner |
|
41 { |
|
42 |
|
43 private static void init() |
|
44 { |
|
45 //*** Create instructions for the user here *** |
|
46 |
|
47 String[] instructions = |
|
48 { |
|
49 " 1) there is a frame with a 'show dialog' button, ", |
|
50 " 2) press the butoton, the dialog will be shown, ", |
|
51 " 3) the focus will be transfered into the dialog, ", |
|
52 " 4) check that initially focus owner is the specific text field, ", |
|
53 " its label is 'File name' on Windows OS, 'Enter file name' on Linux OS, ", |
|
54 " 5) if it's true then the test passed, otherwise it failed. " |
|
55 }; |
|
56 Sysout.createDialog( ); |
|
57 Sysout.printInstructions( instructions ); |
|
58 |
|
59 Frame frame = new Frame(); |
|
60 Button button = new Button("show dialog"); |
|
61 button.addActionListener(new ActionListener(){ |
|
62 public void actionPerformed(ActionEvent ae) { |
|
63 FileDialog dialog = new FileDialog(new Frame(), "dialog"); |
|
64 dialog.setVisible(true); |
|
65 } |
|
66 }); |
|
67 frame.setBounds(200,200,200,200); |
|
68 frame.add(button); |
|
69 frame.setVisible(true); |
|
70 |
|
71 }//End init() |
|
72 |
|
73 |
|
74 |
|
75 /***************************************************** |
|
76 * Standard Test Machinery Section |
|
77 * DO NOT modify anything in this section -- it's a |
|
78 * standard chunk of code which has all of the |
|
79 * synchronisation necessary for the test harness. |
|
80 * By keeping it the same in all tests, it is easier |
|
81 * to read and understand someone else's test, as |
|
82 * well as insuring that all tests behave correctly |
|
83 * with the test harness. |
|
84 * There is a section following this for test-defined |
|
85 * classes |
|
86 ******************************************************/ |
|
87 private static boolean theTestPassed = false; |
|
88 private static boolean testGeneratedInterrupt = false; |
|
89 private static String failureMessage = ""; |
|
90 |
|
91 private static Thread mainThread = null; |
|
92 |
|
93 private static int sleepTime = 300000; |
|
94 |
|
95 public static void main( String args[] ) throws InterruptedException |
|
96 { |
|
97 mainThread = Thread.currentThread(); |
|
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 mainThread.interrupt(); |
|
157 }//pass() |
|
158 |
|
159 public static synchronized void fail() |
|
160 { |
|
161 //test writer didn't specify why test failed, so give generic |
|
162 fail( "it just plain failed! :-)" ); |
|
163 } |
|
164 |
|
165 public static synchronized void fail( String whyFailed ) |
|
166 { |
|
167 Sysout.println( "The test failed: " + whyFailed ); |
|
168 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); |
|
169 //check if this called from main thread |
|
170 if ( mainThread == Thread.currentThread() ) |
|
171 { |
|
172 //If main thread, fail now 'cause not sleeping |
|
173 throw new RuntimeException( whyFailed ); |
|
174 } |
|
175 theTestPassed = false; |
|
176 testGeneratedInterrupt = true; |
|
177 failureMessage = whyFailed; |
|
178 mainThread.interrupt(); |
|
179 }//fail() |
|
180 |
|
181 }// class ManualMainTest |
|
182 |
|
183 //This exception is used to exit from any level of call nesting |
|
184 // when it's determined that the test has passed, and immediately |
|
185 // end the test. |
|
186 class TestPassedException extends RuntimeException |
|
187 { |
|
188 } |
|
189 |
|
190 //*********** End Standard Test Machinery Section ********** |
|
191 |
|
192 |
|
193 //************ Begin classes defined for the test **************** |
|
194 |
|
195 // make listeners in a class defined here, and instantiate them in init() |
|
196 |
|
197 /* Example of a class which may be written as part of a test |
|
198 class NewClass implements anInterface |
|
199 { |
|
200 static int newVar = 0; |
|
201 |
|
202 public void eventDispatched(AWTEvent e) |
|
203 { |
|
204 //Counting events to see if we get enough |
|
205 eventCount++; |
|
206 |
|
207 if( eventCount == 20 ) |
|
208 { |
|
209 //got enough events, so pass |
|
210 |
|
211 ManualMainTest.pass(); |
|
212 } |
|
213 else if( tries == 20 ) |
|
214 { |
|
215 //tried too many times without getting enough events so fail |
|
216 |
|
217 ManualMainTest.fail(); |
|
218 } |
|
219 |
|
220 }// eventDispatched() |
|
221 |
|
222 }// NewClass class |
|
223 |
|
224 */ |
|
225 |
|
226 |
|
227 //************** End classes defined for the test ******************* |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 /**************************************************** |
|
233 Standard Test Machinery |
|
234 DO NOT modify anything below -- it's a standard |
|
235 chunk of code whose purpose is to make user |
|
236 interaction uniform, and thereby make it simpler |
|
237 to read and understand someone else's test. |
|
238 ****************************************************/ |
|
239 |
|
240 /** |
|
241 This is part of the standard test machinery. |
|
242 It creates a dialog (with the instructions), and is the interface |
|
243 for sending text messages to the user. |
|
244 To print the instructions, send an array of strings to Sysout.createDialog |
|
245 WithInstructions method. Put one line of instructions per array entry. |
|
246 To display a message for the tester to see, simply call Sysout.println |
|
247 with the string to be displayed. |
|
248 This mimics System.out.println but works within the test harness as well |
|
249 as standalone. |
|
250 */ |
|
251 |
|
252 class Sysout |
|
253 { |
|
254 private static TestDialog dialog; |
|
255 |
|
256 public static void createDialogWithInstructions( String[] instructions ) |
|
257 { |
|
258 dialog = new TestDialog( new Frame(), "Instructions" ); |
|
259 dialog.printInstructions( instructions ); |
|
260 dialog.setVisible(true); |
|
261 println( "Any messages for the tester will display here." ); |
|
262 } |
|
263 |
|
264 public static void createDialog( ) |
|
265 { |
|
266 dialog = new TestDialog( new Frame(), "Instructions" ); |
|
267 String[] defInstr = { "Instructions will appear here. ", "" } ; |
|
268 dialog.printInstructions( defInstr ); |
|
269 dialog.setVisible(true); |
|
270 println( "Any messages for the tester will display here." ); |
|
271 } |
|
272 |
|
273 |
|
274 public static void printInstructions( String[] instructions ) |
|
275 { |
|
276 dialog.printInstructions( instructions ); |
|
277 } |
|
278 |
|
279 |
|
280 public static void println( String messageIn ) |
|
281 { |
|
282 dialog.displayMessage( messageIn ); |
|
283 } |
|
284 |
|
285 }// Sysout class |
|
286 |
|
287 /** |
|
288 This is part of the standard test machinery. It provides a place for the |
|
289 test instructions to be displayed, and a place for interactive messages |
|
290 to the user to be displayed. |
|
291 To have the test instructions displayed, see Sysout. |
|
292 To have a message to the user be displayed, see Sysout. |
|
293 Do not call anything in this dialog directly. |
|
294 */ |
|
295 class TestDialog extends Dialog implements ActionListener |
|
296 { |
|
297 |
|
298 TextArea instructionsText; |
|
299 TextArea messageText; |
|
300 int maxStringLength = 80; |
|
301 Panel buttonP = new Panel(); |
|
302 Button passB = new Button( "pass" ); |
|
303 Button failB = new Button( "fail" ); |
|
304 |
|
305 //DO NOT call this directly, go through Sysout |
|
306 public TestDialog( Frame frame, String name ) |
|
307 { |
|
308 super( frame, name ); |
|
309 int scrollBoth = TextArea.SCROLLBARS_BOTH; |
|
310 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); |
|
311 add( "North", instructionsText ); |
|
312 |
|
313 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); |
|
314 add("Center", messageText); |
|
315 |
|
316 passB = new Button( "pass" ); |
|
317 passB.setActionCommand( "pass" ); |
|
318 passB.addActionListener( this ); |
|
319 buttonP.add( "East", passB ); |
|
320 |
|
321 failB = new Button( "fail" ); |
|
322 failB.setActionCommand( "fail" ); |
|
323 failB.addActionListener( this ); |
|
324 buttonP.add( "West", failB ); |
|
325 |
|
326 add( "South", buttonP ); |
|
327 pack(); |
|
328 |
|
329 setVisible(true); |
|
330 }// TestDialog() |
|
331 |
|
332 //DO NOT call this directly, go through Sysout |
|
333 public void printInstructions( String[] instructions ) |
|
334 { |
|
335 //Clear out any current instructions |
|
336 instructionsText.setText( "" ); |
|
337 |
|
338 //Go down array of instruction strings |
|
339 |
|
340 String printStr, remainingStr; |
|
341 for( int i=0; i < instructions.length; i++ ) |
|
342 { |
|
343 //chop up each into pieces maxSringLength long |
|
344 remainingStr = instructions[ i ]; |
|
345 while( remainingStr.length() > 0 ) |
|
346 { |
|
347 //if longer than max then chop off first max chars to print |
|
348 if( remainingStr.length() >= maxStringLength ) |
|
349 { |
|
350 //Try to chop on a word boundary |
|
351 int posOfSpace = remainingStr. |
|
352 lastIndexOf( ' ', maxStringLength - 1 ); |
|
353 |
|
354 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; |
|
355 |
|
356 printStr = remainingStr.substring( 0, posOfSpace + 1 ); |
|
357 remainingStr = remainingStr.substring( posOfSpace + 1 ); |
|
358 } |
|
359 //else just print |
|
360 else |
|
361 { |
|
362 printStr = remainingStr; |
|
363 remainingStr = ""; |
|
364 } |
|
365 |
|
366 instructionsText.append( printStr + "\n" ); |
|
367 |
|
368 }// while |
|
369 |
|
370 }// for |
|
371 |
|
372 }//printInstructions() |
|
373 |
|
374 //DO NOT call this directly, go through Sysout |
|
375 public void displayMessage( String messageIn ) |
|
376 { |
|
377 messageText.append( messageIn + "\n" ); |
|
378 System.out.println(messageIn); |
|
379 } |
|
380 |
|
381 //catch presses of the passed and failed buttons. |
|
382 //simply call the standard pass() or fail() static methods of |
|
383 //ManualMainTest |
|
384 public void actionPerformed( ActionEvent e ) |
|
385 { |
|
386 if( e.getActionCommand() == "pass" ) |
|
387 { |
|
388 DefaultFocusOwner.pass(); |
|
389 } |
|
390 else |
|
391 { |
|
392 DefaultFocusOwner.fail(); |
|
393 } |
|
394 } |
|
395 |
|
396 }// TestDialog class |