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