|
1 /* |
|
2 * Copyright 2009 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 6774258 |
|
27 @summary api/java_awt/Component/index.html#PaintUpdate fails randomly |
|
28 @author dmitry.cherepanov@...: area=awt.painting |
|
29 @run main NoUpdateUponShow |
|
30 */ |
|
31 |
|
32 /** |
|
33 * NoUpdateUponShow.java |
|
34 * |
|
35 * summary: System-level painting operations shouldn't make call to update() |
|
36 */ |
|
37 |
|
38 import java.awt.*; |
|
39 import sun.awt.SunToolkit; |
|
40 |
|
41 public class NoUpdateUponShow |
|
42 { |
|
43 |
|
44 static volatile boolean wasUpdate = false; |
|
45 |
|
46 private static void init() |
|
47 { |
|
48 //*** Create instructions for the user here *** |
|
49 |
|
50 String[] instructions = |
|
51 { |
|
52 "This is an AUTOMATIC test, simply wait until it is done.", |
|
53 "The result (passed or failed) will be shown in the", |
|
54 "message window below." |
|
55 }; |
|
56 Sysout.createDialog( ); |
|
57 Sysout.printInstructions( instructions ); |
|
58 |
|
59 |
|
60 // Create the frame and the button |
|
61 Frame f = new Frame(); |
|
62 f.setBounds(100, 100, 200, 200); |
|
63 f.setLayout(new FlowLayout()); |
|
64 f.add(new Button() { |
|
65 @Override |
|
66 public void update(Graphics g) { |
|
67 wasUpdate = true; |
|
68 super.update(g); |
|
69 } |
|
70 }); |
|
71 f.setVisible(true); |
|
72 |
|
73 ((SunToolkit)Toolkit.getDefaultToolkit()).realSync(); |
|
74 |
|
75 if (wasUpdate) { |
|
76 fail(" Unexpected update. "); |
|
77 } else { |
|
78 pass(); |
|
79 } |
|
80 }//End init() |
|
81 |
|
82 /***************************************************** |
|
83 * Standard Test Machinery Section |
|
84 * DO NOT modify anything in this section -- it's a |
|
85 * standard chunk of code which has all of the |
|
86 * synchronisation necessary for the test harness. |
|
87 * By keeping it the same in all tests, it is easier |
|
88 * to read and understand someone else's test, as |
|
89 * well as insuring that all tests behave correctly |
|
90 * with the test harness. |
|
91 * There is a section following this for test- |
|
92 * classes |
|
93 ******************************************************/ |
|
94 private static boolean theTestPassed = false; |
|
95 private static boolean testGeneratedInterrupt = false; |
|
96 private static String failureMessage = ""; |
|
97 |
|
98 private static Thread mainThread = null; |
|
99 |
|
100 private static int sleepTime = 300000; |
|
101 |
|
102 // Not sure about what happens if multiple of this test are |
|
103 // instantiated in the same VM. Being static (and using |
|
104 // static vars), it aint gonna work. Not worrying about |
|
105 // it for now. |
|
106 public static void main( String args[] ) throws InterruptedException |
|
107 { |
|
108 mainThread = Thread.currentThread(); |
|
109 try |
|
110 { |
|
111 init(); |
|
112 } |
|
113 catch( TestPassedException e ) |
|
114 { |
|
115 //The test passed, so just return from main and harness will |
|
116 // interepret this return as a pass |
|
117 return; |
|
118 } |
|
119 //At this point, neither test pass nor test fail has been |
|
120 // called -- either would have thrown an exception and ended the |
|
121 // test, so we know we have multiple threads. |
|
122 |
|
123 //Test involves other threads, so sleep and wait for them to |
|
124 // called pass() or fail() |
|
125 try |
|
126 { |
|
127 Thread.sleep( sleepTime ); |
|
128 //Timed out, so fail the test |
|
129 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); |
|
130 } |
|
131 catch (InterruptedException e) |
|
132 { |
|
133 //The test harness may have interrupted the test. If so, rethrow the exception |
|
134 // so that the harness gets it and deals with it. |
|
135 if( ! testGeneratedInterrupt ) throw e; |
|
136 |
|
137 //reset flag in case hit this code more than once for some reason (just safety) |
|
138 testGeneratedInterrupt = false; |
|
139 |
|
140 if ( theTestPassed == false ) |
|
141 { |
|
142 throw new RuntimeException( failureMessage ); |
|
143 } |
|
144 } |
|
145 |
|
146 }//main |
|
147 |
|
148 public static synchronized void setTimeoutTo( int seconds ) |
|
149 { |
|
150 sleepTime = seconds * 1000; |
|
151 } |
|
152 |
|
153 public static synchronized void pass() |
|
154 { |
|
155 Sysout.println( "The test passed." ); |
|
156 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); |
|
157 //first check if this is executing in main thread |
|
158 if ( mainThread == Thread.currentThread() ) |
|
159 { |
|
160 //Still in the main thread, so set the flag just for kicks, |
|
161 // and throw a test passed exception which will be caught |
|
162 // and end the test. |
|
163 theTestPassed = true; |
|
164 throw new TestPassedException(); |
|
165 } |
|
166 theTestPassed = true; |
|
167 testGeneratedInterrupt = true; |
|
168 mainThread.interrupt(); |
|
169 }//pass() |
|
170 |
|
171 public static synchronized void fail() |
|
172 { |
|
173 //test writer didn't specify why test failed, so give generic |
|
174 fail( "it just plain failed! :-)" ); |
|
175 } |
|
176 |
|
177 public static synchronized void fail( String whyFailed ) |
|
178 { |
|
179 Sysout.println( "The test failed: " + whyFailed ); |
|
180 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); |
|
181 //check if this called from main thread |
|
182 if ( mainThread == Thread.currentThread() ) |
|
183 { |
|
184 //If main thread, fail now 'cause not sleeping |
|
185 throw new RuntimeException( whyFailed ); |
|
186 } |
|
187 theTestPassed = false; |
|
188 testGeneratedInterrupt = true; |
|
189 failureMessage = whyFailed; |
|
190 mainThread.interrupt(); |
|
191 }//fail() |
|
192 |
|
193 }// class ValidBounds |
|
194 |
|
195 //This exception is used to exit from any level of call nesting |
|
196 // when it's determined that the test has passed, and immediately |
|
197 // end the test. |
|
198 class TestPassedException extends RuntimeException |
|
199 { |
|
200 } |
|
201 |
|
202 //*********** End Standard Test Machinery Section ********** |
|
203 |
|
204 |
|
205 //************ Begin classes defined for the test **************** |
|
206 |
|
207 // if want to make listeners, here is the recommended place for them, then instantiate |
|
208 // them in init() |
|
209 |
|
210 /* Example of a class which may be written as part of a test |
|
211 class NewClass implements anInterface |
|
212 { |
|
213 static int newVar = 0; |
|
214 |
|
215 public void eventDispatched(AWTEvent e) |
|
216 { |
|
217 //Counting events to see if we get enough |
|
218 eventCount++; |
|
219 |
|
220 if( eventCount == 20 ) |
|
221 { |
|
222 //got enough events, so pass |
|
223 |
|
224 ValidBounds.pass(); |
|
225 } |
|
226 else if( tries == 20 ) |
|
227 { |
|
228 //tried too many times without getting enough events so fail |
|
229 |
|
230 ValidBounds.fail(); |
|
231 } |
|
232 |
|
233 }// eventDispatched() |
|
234 |
|
235 }// NewClass class |
|
236 |
|
237 */ |
|
238 |
|
239 |
|
240 //************** End classes defined for the test ******************* |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 /**************************************************** |
|
246 Standard Test Machinery |
|
247 DO NOT modify anything below -- it's a standard |
|
248 chunk of code whose purpose is to make user |
|
249 interaction uniform, and thereby make it simpler |
|
250 to read and understand someone else's test. |
|
251 ****************************************************/ |
|
252 |
|
253 /** |
|
254 This is part of the standard test machinery. |
|
255 It creates a dialog (with the instructions), and is the interface |
|
256 for sending text messages to the user. |
|
257 To print the instructions, send an array of strings to Sysout.createDialog |
|
258 WithInstructions method. Put one line of instructions per array entry. |
|
259 To display a message for the tester to see, simply call Sysout.println |
|
260 with the string to be displayed. |
|
261 This mimics System.out.println but works within the test harness as well |
|
262 as standalone. |
|
263 */ |
|
264 |
|
265 class Sysout |
|
266 { |
|
267 private static TestDialog dialog; |
|
268 |
|
269 public static void createDialogWithInstructions( String[] instructions ) |
|
270 { |
|
271 dialog = new TestDialog( new Frame(), "Instructions" ); |
|
272 dialog.printInstructions( instructions ); |
|
273 dialog.setVisible(true); |
|
274 println( "Any messages for the tester will display here." ); |
|
275 } |
|
276 |
|
277 public static void createDialog( ) |
|
278 { |
|
279 dialog = new TestDialog( new Frame(), "Instructions" ); |
|
280 String[] defInstr = { "Instructions will appear here. ", "" } ; |
|
281 dialog.printInstructions( defInstr ); |
|
282 dialog.setVisible(true); |
|
283 println( "Any messages for the tester will display here." ); |
|
284 } |
|
285 |
|
286 |
|
287 public static void printInstructions( String[] instructions ) |
|
288 { |
|
289 dialog.printInstructions( instructions ); |
|
290 } |
|
291 |
|
292 |
|
293 public static void println( String messageIn ) |
|
294 { |
|
295 dialog.displayMessage( messageIn ); |
|
296 System.out.println(messageIn); |
|
297 } |
|
298 |
|
299 }// Sysout class |
|
300 |
|
301 /** |
|
302 This is part of the standard test machinery. It provides a place for the |
|
303 test instructions to be displayed, and a place for interactive messages |
|
304 to the user to be displayed. |
|
305 To have the test instructions displayed, see Sysout. |
|
306 To have a message to the user be displayed, see Sysout. |
|
307 Do not call anything in this dialog directly. |
|
308 */ |
|
309 class TestDialog extends Dialog |
|
310 { |
|
311 |
|
312 TextArea instructionsText; |
|
313 TextArea messageText; |
|
314 int maxStringLength = 80; |
|
315 |
|
316 //DO NOT call this directly, go through Sysout |
|
317 public TestDialog( Frame frame, String name ) |
|
318 { |
|
319 super( frame, name ); |
|
320 int scrollBoth = TextArea.SCROLLBARS_BOTH; |
|
321 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); |
|
322 add( "North", instructionsText ); |
|
323 |
|
324 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); |
|
325 add("Center", messageText); |
|
326 |
|
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 }// TestDialog class |