1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 import java.awt.*; 26 import java.awt.event.InputEvent; 27 import javax.swing.*; 28 import java.io.*; 29 30 /** 31 * AWT/Swing overlapping test for Panel and JPanel behavior during resizing. 32 * <p>See <a href="https://bugs.openjdk.java.net/browse/JDK-6786219">JDK-6786219</a> for details 33 */ 34 /* 35 @test 36 @bug 6786219 37 @summary Issues when resizing the frame after mixing of heavy weight & light weight components 38 @author sergey.grinev@oracle.com: area=awt.mixing 39 @build FrameBorderCounter 40 @run main MixingPanelsResizing 41 */ 42 public class MixingPanelsResizing { 43 44 static volatile boolean failed = false; 45 46 private static JFrame frame; 47 private static JButton jbutton; 48 private static Button awtButton; 49 private static JButton jbutton2; 50 private static Button awtButton2; 51 private static final Color jbColor = Color.RED; 52 private static final Color awtColor = Color.ORANGE; 53 private static final Color jb2Color = Color.BLUE; 54 private static final Color awt2Color = Color.CYAN; 55 private static final int ROBOT_DELAY = 500; 56 57 private static Point lLoc; 58 private static int borderShift; 59 60 private static int frameBorderCounter() { 61 String JAVA_HOME = System.getProperty("java.home"); 62 try { 63 Process p = Runtime.getRuntime().exec(JAVA_HOME + "/bin/java FrameBorderCounter"); 64 try { 65 p.waitFor(); 66 } catch (InterruptedException e) { 67 e.printStackTrace(); 68 throw new RuntimeException(e); 69 } 70 if (p.exitValue() != 0) { 71 throw new RuntimeException("FrameBorderCounter exited with not null code!\n" + readInputStream(p.getErrorStream())); 72 } 73 return Integer.parseInt(readInputStream(p.getInputStream()).trim()); 74 } catch (IOException e) { 75 e.printStackTrace(); 76 throw new RuntimeException(e); 77 } 78 } 79 80 private static String readInputStream(InputStream is) throws IOException { 81 byte[] buffer = new byte[4096]; 82 int len = 0; 83 StringBuilder sb = new StringBuilder(); 84 try (InputStreamReader isr = new InputStreamReader(is)) { 85 while ((len = is.read(buffer)) > 0) { 86 sb.append(new String(buffer, 0, len)); 87 } 88 } 89 return sb.toString(); 90 } 91 92 private static void init() throws Exception { 93 //*** Create instructions for the user here *** 94 95 borderShift = frameBorderCounter(); 96 borderShift = Math.abs(borderShift) == 1 ? borderShift : (borderShift / 2); 97 String[] instructions = { 98 "This is an AUTOMATIC test, simply wait until it is done.", 99 "The result (passed or failed) will be shown in the", 100 "message window below." 101 }; 102 SwingUtilities.invokeAndWait(new Runnable() { 103 public void run() { 104 Sysout.createDialog(); 105 Sysout.printInstructions(instructions); 106 107 // prepare controls 108 109 frame = new JFrame(); 110 111 Panel awtPanel = new Panel(); 112 awtPanel.setBackground(Color.GREEN); 113 awtButton = new Button("AWTButton"); 114 awtPanel.add(awtButton); 115 awtButton.setForeground(awtColor); 116 awtButton.setBackground(awtColor); 117 jbutton = new JButton("SwingButton"); 118 awtPanel.add(jbutton); 119 jbutton.setForeground(jbColor); 120 jbutton.setBackground(jbColor); 121 122 JPanel jPanel = new JPanel(); 123 jbutton2 = new JButton("SwingButton2"); 124 jPanel.add(jbutton2); 125 jbutton2.setForeground(jb2Color); 126 jbutton2.setBackground(jb2Color); 127 awtButton2 = new Button("AWT Button2"); 128 jPanel.add(awtButton2); 129 awtButton2.setForeground(awt2Color); 130 awtButton2.setBackground(awt2Color); 131 jPanel.setBackground(Color.YELLOW); 132 133 frame.add(awtPanel, BorderLayout.SOUTH); 134 frame.add(jPanel, BorderLayout.NORTH); 135 136 frame.pack(); 137 frame.setVisible(true); 138 } 139 }); 140 141 ///////////////////////// 142 143 final Robot robot = Util.createRobot(); 144 robot.setAutoDelay(ROBOT_DELAY); 145 146 Util.waitForIdle(robot); 147 148 SwingUtilities.invokeAndWait(new Runnable() { 149 public void run() { 150 lLoc = frame.getLocationOnScreen(); 151 lLoc.translate(frame.getWidth() + borderShift, frame.getHeight() + borderShift); 152 } 153 }); 154 155 //grow 156 robot.mouseMove(lLoc.x, lLoc.y); 157 robot.mousePress(InputEvent.BUTTON1_MASK); 158 159 Runnable test = new Runnable() { 160 161 public void run() { 162 Point btnLoc = jbutton.getLocationOnScreen(); 163 Color c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5); 164 if (!c.equals(jbColor)) { 165 fail("JButton was not redrawn properly on AWT Panel during move"); 166 } 167 168 btnLoc = awtButton.getLocationOnScreen(); 169 c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5); 170 if (!c.equals(awtColor)) { 171 fail("AWT Button was not redrawn properly on AWT Panel during move"); 172 } 173 174 btnLoc = jbutton2.getLocationOnScreen(); 175 c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5); 176 if (!c.equals(jb2Color)) { 177 fail("JButton was not redrawn properly on JPanel during move"); 178 } 179 180 btnLoc = awtButton2.getLocationOnScreen(); 181 c = robot.getPixelColor(btnLoc.x + 5, btnLoc.y + 5); 182 if (!c.equals(awt2Color)) { 183 fail("ATW Button was not redrawn properly on JPanel during move"); 184 } 185 } 186 }; 187 188 for (int i = 0; i < 30; i++) { 189 test.run(); 190 robot.mouseMove(lLoc.x + 20 * i, lLoc.y + 10 * i); 191 } 192 robot.mouseRelease(InputEvent.BUTTON1_MASK); 193 194 //back 195 System.out.println("fast back"); 196 robot.mousePress(InputEvent.BUTTON1_MASK); 197 for (int i = 5; i >= 0; i--) { 198 test.run(); 199 robot.mouseMove(lLoc.x + 120 * i, lLoc.y + 60 * i); 200 } 201 robot.mouseRelease(InputEvent.BUTTON1_MASK); 202 203 pass(); 204 }//End init() 205 /***************************************************** 206 * Standard Test Machinery Section 207 * DO NOT modify anything in this section -- it's a 208 * standard chunk of code which has all of the 209 * synchronisation necessary for the test harness. 210 * By keeping it the same in all tests, it is easier 211 * to read and understand someone else's test, as 212 * well as insuring that all tests behave correctly 213 * with the test harness. 214 * There is a section following this for test- 215 * classes 216 ******************************************************/ 217 private static boolean theTestPassed = false; 218 private static boolean testGeneratedInterrupt = false; 219 private static String failureMessage = ""; 220 private static Thread mainThread = null; 221 private static int sleepTime = 300000; 222 223 // Not sure about what happens if multiple of this test are 224 // instantiated in the same VM. Being static (and using 225 // static vars), it aint gonna work. Not worrying about 226 // it for now. 227 public static void main(String args[]) throws Exception { 228 if (!Toolkit.getDefaultToolkit().isDynamicLayoutActive()) { 229 System.out.println("Dynamic layout is not active. Test passes."); 230 return; 231 } 232 mainThread = Thread.currentThread(); 233 try { 234 init(); 235 } catch (TestPassedException e) { 236 //The test passed, so just return from main and harness will 237 // interepret this return as a pass 238 return; 239 } 240 //At this point, neither test pass nor test fail has been 241 // called -- either would have thrown an exception and ended the 242 // test, so we know we have multiple threads. 243 244 //Test involves other threads, so sleep and wait for them to 245 // called pass() or fail() 246 try { 247 Thread.sleep(sleepTime); 248 //Timed out, so fail the test 249 throw new RuntimeException("Timed out after " + sleepTime / 1000 + " seconds"); 250 } catch (InterruptedException e) { 251 //The test harness may have interrupted the test. If so, rethrow the exception 252 // so that the harness gets it and deals with it. 253 if (!testGeneratedInterrupt) { 254 throw e; 255 } 256 257 //reset flag in case hit this code more than once for some reason (just safety) 258 testGeneratedInterrupt = false; 259 260 if (theTestPassed == false) { 261 throw new RuntimeException(failureMessage); 262 } 263 } 264 265 }//main 266 267 public static synchronized void setTimeoutTo(int seconds) { 268 sleepTime = seconds * 1000; 269 } 270 271 public static synchronized void pass() { 272 Sysout.println("The test passed."); 273 Sysout.println("The test is over, hit Ctl-C to stop Java VM"); 274 //first check if this is executing in main thread 275 if (mainThread == Thread.currentThread()) { 276 //Still in the main thread, so set the flag just for kicks, 277 // and throw a test passed exception which will be caught 278 // and end the test. 279 theTestPassed = true; 280 throw new TestPassedException(); 281 } 282 theTestPassed = true; 283 testGeneratedInterrupt = true; 284 mainThread.interrupt(); 285 }//pass() 286 287 public static synchronized void fail() { 288 //test writer didn't specify why test failed, so give generic 289 fail("it just plain failed! :-)"); 290 } 291 292 public static synchronized void fail(String whyFailed) { 293 Sysout.println("The test failed: " + whyFailed); 294 Sysout.println("The test is over, hit Ctl-C to stop Java VM"); 295 //check if this called from main thread 296 if (mainThread == Thread.currentThread()) { 297 //If main thread, fail now 'cause not sleeping 298 throw new RuntimeException(whyFailed); 299 } 300 theTestPassed = false; 301 testGeneratedInterrupt = true; 302 failureMessage = whyFailed; 303 mainThread.interrupt(); 304 }//fail() 305 }// class JButtonInGlassPane 306 class TestPassedException extends RuntimeException { 307 } 308 309 //*********** End Standard Test Machinery Section ********** 310 //************ Begin classes defined for the test **************** 311 // if want to make listeners, here is the recommended place for them, then instantiate 312 // them in init() 313 314 /* Example of a class which may be written as part of a test 315 class NewClass implements anInterface 316 { 317 static int newVar = 0; 318 319 public void eventDispatched(AWTEvent e) 320 { 321 //Counting events to see if we get enough 322 eventCount++; 323 324 if( eventCount == 20 ) 325 { 326 //got enough events, so pass 327 328 JButtonInGlassPane.pass(); 329 } 330 else if( tries == 20 ) 331 { 332 //tried too many times without getting enough events so fail 333 334 JButtonInGlassPane.fail(); 335 } 336 337 }// eventDispatched() 338 339 }// NewClass class 340 341 */ 342 //************** End classes defined for the test ******************* 343 /**************************************************** 344 Standard Test Machinery 345 DO NOT modify anything below -- it's a standard 346 chunk of code whose purpose is to make user 347 interaction uniform, and thereby make it simpler 348 to read and understand someone else's test. 349 ****************************************************/ 350 /** 351 This is part of the standard test machinery. 352 It creates a dialog (with the instructions), and is the interface 353 for sending text messages to the user. 354 To print the instructions, send an array of strings to Sysout.createDialog 355 WithInstructions method. Put one line of instructions per array entry. 356 To display a message for the tester to see, simply call Sysout.println 357 with the string to be displayed. 358 This mimics System.out.println but works within the test harness as well 359 as standalone. 360 */ 361 class Sysout { 362 363 private static TestDialog dialog; 364 365 public static void createDialogWithInstructions(String[] instructions) { 366 dialog = new TestDialog(new Frame(), "Instructions"); 367 dialog.printInstructions(instructions); 368 dialog.setVisible(true); 369 println("Any messages for the tester will display here."); 370 } 371 372 public static void createDialog() { 373 dialog = new TestDialog(new Frame(), "Instructions"); 374 String[] defInstr = {"Instructions will appear here. ", ""}; 375 dialog.printInstructions(defInstr); 376 dialog.setVisible(true); 377 println("Any messages for the tester will display here."); 378 } 379 380 public static void printInstructions(String[] instructions) { 381 dialog.printInstructions(instructions); 382 } 383 384 public static void println(String messageIn) { 385 dialog.displayMessage(messageIn); 386 System.out.println(messageIn); 387 } 388 }// Sysout class 389 390 /** 391 This is part of the standard test machinery. It provides a place for the 392 test instructions to be displayed, and a place for interactive messages 393 to the user to be displayed. 394 To have the test instructions displayed, see Sysout. 395 To have a message to the user be displayed, see Sysout. 396 Do not call anything in this dialog directly. 397 */ 398 class TestDialog extends Dialog { 399 400 TextArea instructionsText; 401 TextArea messageText; 402 int maxStringLength = 80; 403 404 //DO NOT call this directly, go through Sysout 405 public TestDialog(Frame frame, String name) { 406 super(frame, name); 407 int scrollBoth = TextArea.SCROLLBARS_BOTH; 408 instructionsText = new TextArea("", 15, maxStringLength, scrollBoth); 409 add("North", instructionsText); 410 411 messageText = new TextArea("", 5, maxStringLength, scrollBoth); 412 add("Center", messageText); 413 414 pack(); 415 416 setVisible(true); 417 }// TestDialog() 418 419 //DO NOT call this directly, go through Sysout 420 public void printInstructions(String[] instructions) { 421 //Clear out any current instructions 422 instructionsText.setText(""); 423 424 //Go down array of instruction strings 425 426 String printStr, remainingStr; 427 for (int i = 0; i < instructions.length; i++) { 428 //chop up each into pieces maxSringLength long 429 remainingStr = instructions[i]; 430 while (remainingStr.length() > 0) { 431 //if longer than max then chop off first max chars to print 432 if (remainingStr.length() >= maxStringLength) { 433 //Try to chop on a word boundary 434 int posOfSpace = remainingStr.lastIndexOf(' ', maxStringLength - 1); 435 436 if (posOfSpace <= 0) { 437 posOfSpace = maxStringLength - 1; 438 } 439 440 printStr = remainingStr.substring(0, posOfSpace + 1); 441 remainingStr = remainingStr.substring(posOfSpace + 1); 442 } //else just print 443 else { 444 printStr = remainingStr; 445 remainingStr = ""; 446 } 447 448 instructionsText.append(printStr + "\n"); 449 450 }// while 451 452 }// for 453 454 }//printInstructions() 455 456 //DO NOT call this directly, go through Sysout 457 public void displayMessage(String messageIn) { 458 messageText.append(messageIn + "\n"); 459 System.out.println(messageIn); 460 } 461 }// TestDialog class 462