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