1 /* 2 @test 3 @bug 6345002 4 @summary grab problems with EmbeddedFrame 5 @author Oleg.Semenov@sun.com area=EmbeddedFrame 6 @modules java.desktop/java.awt.peer 7 java.desktop/sun.awt 8 @run main EmbeddedFrameGrabTest 9 */ 10 11 /** 12 * EmbeddedFrameGrabTest.java 13 * 14 * summary: grab problems with EmbeddedFrame 15 */ 16 17 import java.awt.Frame; 18 import java.awt.peer.FramePeer; 19 import javax.swing.JComboBox; 20 import java.awt.Panel; 21 import java.awt.BorderLayout; 22 import java.awt.Robot; 23 import java.awt.event.InputEvent; 24 import java.awt.Rectangle; 25 import java.awt.TextArea; 26 import java.awt.Dialog; 27 28 import java.lang.reflect.Constructor; 29 import java.lang.reflect.Field; 30 31 import sun.awt.AWTAccessor; 32 33 public class EmbeddedFrameGrabTest 34 { 35 private static void init() 36 { 37 //*** Create instructions for the user here *** 38 39 String[] instructions = 40 { 41 "This is an AUTOMATIC test, simply wait until it is done.", 42 "The result (passed or failed) will be shown in the", 43 "message window below." 44 }; 45 Sysout.createDialog( ); 46 Sysout.printInstructions( instructions ); 47 48 if (!System.getProperty("os.name").startsWith("Windows")) { 49 System.out.println("This is Windows only test."); 50 EmbeddedFrameGrabTest.pass(); 51 return; 52 } 53 54 try { 55 final Frame frame = new Frame("AWT Frame"); 56 frame.pack(); 57 frame.setSize(200,200); 58 FramePeer frame_peer = AWTAccessor.getComponentAccessor().getPeer(frame); 59 Sysout.println("frame's peer = " + frame_peer); 60 Class comp_peer_class = 61 Class.forName("sun.awt.windows.WComponentPeer"); 62 Sysout.println("comp peer class = " + comp_peer_class); 63 Field hwnd_field = comp_peer_class.getDeclaredField("hwnd"); 64 hwnd_field.setAccessible(true); 65 Sysout.println("hwnd_field =" + hwnd_field); 66 long hwnd = hwnd_field.getLong(frame_peer); 67 Sysout.println("hwnd = " + hwnd); 68 69 Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame"); 70 Constructor constructor = clazz.getConstructor (new Class [] {long.class}); 71 final Frame embedded_frame = 72 (Frame) constructor.newInstance (new Object[] {new Long(hwnd)});; 73 Sysout.println("embedded_frame = " + embedded_frame); 74 final JComboBox combo = new JComboBox(new String[] { 75 "Item 1", "Item 2" 76 } ); 77 combo.setSelectedIndex(1); 78 final Panel p = new Panel(); 79 p.setLayout(new BorderLayout()); 80 embedded_frame.add(p,BorderLayout.CENTER); 81 embedded_frame.validate(); 82 p.add(combo); 83 p.validate(); 84 frame.setVisible(true); 85 Robot robot = new Robot(); 86 robot.delay(2000); 87 Rectangle clos = new Rectangle(combo.getLocationOnScreen(),combo.getSize()); 88 robot.mouseMove(clos.x+clos.width/2,clos.y+clos.height/2); 89 robot.mousePress(InputEvent.BUTTON1_MASK); 90 robot.mouseRelease(InputEvent.BUTTON1_MASK); 91 robot.delay(1000); 92 if (!combo.isPopupVisible()) { 93 throw new RuntimeException("Combobox popup is not visible!"); 168 if( ! testGeneratedInterrupt ) throw e; 169 170 //reset flag in case hit this code more than once for some reason (just safety) 171 testGeneratedInterrupt = false; 172 173 if ( theTestPassed == false ) 174 { 175 throw new RuntimeException( failureMessage ); 176 } 177 } 178 179 }//main 180 181 public static synchronized void setTimeoutTo( int seconds ) 182 { 183 sleepTime = seconds * 1000; 184 } 185 186 public static synchronized void pass() 187 { 188 Sysout.println( "The test passed." ); 189 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 190 //first check if this is executing in main thread 191 if ( mainThread == Thread.currentThread() ) 192 { 193 //Still in the main thread, so set the flag just for kicks, 194 // and throw a test passed exception which will be caught 195 // and end the test. 196 theTestPassed = true; 197 throw new TestPassedException(); 198 } 199 theTestPassed = true; 200 testGeneratedInterrupt = true; 201 mainThread.interrupt(); 202 }//pass() 203 204 public static synchronized void fail() 205 { 206 //test writer didn't specify why test failed, so give generic 207 fail( "it just plain failed! :-)" ); 208 } 209 210 public static synchronized void fail( String whyFailed ) 211 { 212 Sysout.println( "The test failed: " + whyFailed ); 213 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 214 //check if this called from main thread 215 if ( mainThread == Thread.currentThread() ) 216 { 217 //If main thread, fail now 'cause not sleeping 218 throw new RuntimeException( whyFailed ); 219 } 220 theTestPassed = false; 221 testGeneratedInterrupt = true; 222 failureMessage = whyFailed; 223 mainThread.interrupt(); 224 }//fail() 225 226 }// class EmbeddedFrameGrabTest 227 228 //This exception is used to exit from any level of call nesting 229 // when it's determined that the test has passed, and immediately 230 // end the test. 231 class TestPassedException extends RuntimeException 232 { 233 } 251 eventCount++; 252 253 if( eventCount == 20 ) 254 { 255 //got enough events, so pass 256 257 EmbeddedFrameGrabTest.pass(); 258 } 259 else if( tries == 20 ) 260 { 261 //tried too many times without getting enough events so fail 262 263 EmbeddedFrameGrabTest.fail(); 264 } 265 266 }// eventDispatched() 267 268 }// NewClass class 269 270 */ 271 272 273 //************** End classes defined for the test ******************* 274 275 276 277 278 /**************************************************** 279 Standard Test Machinery 280 DO NOT modify anything below -- it's a standard 281 chunk of code whose purpose is to make user 282 interaction uniform, and thereby make it simpler 283 to read and understand someone else's test. 284 ****************************************************/ 285 286 /** 287 This is part of the standard test machinery. 288 It creates a dialog (with the instructions), and is the interface 289 for sending text messages to the user. 290 To print the instructions, send an array of strings to Sysout.createDialog 291 WithInstructions method. Put one line of instructions per array entry. 292 To display a message for the tester to see, simply call Sysout.println 293 with the string to be displayed. 294 This mimics System.out.println but works within the test harness as well 295 as standalone. 296 */ 297 298 class Sysout 299 { 300 private static TestDialog dialog; 301 302 public static void createDialogWithInstructions( String[] instructions ) 303 { 304 dialog = new TestDialog( new Frame(), "Instructions" ); 305 dialog.printInstructions( instructions ); 306 dialog.setVisible(true); 307 println( "Any messages for the tester will display here." ); 308 } 309 310 public static void createDialog( ) 311 { 312 dialog = new TestDialog( new Frame(), "Instructions" ); 313 String[] defInstr = { "Instructions will appear here. ", "" } ; 314 dialog.printInstructions( defInstr ); 315 dialog.setVisible(true); 316 println( "Any messages for the tester will display here." ); 317 } 318 319 320 public static void printInstructions( String[] instructions ) 321 { 322 dialog.printInstructions( instructions ); 323 } 324 325 326 public static void println( String messageIn ) 327 { 328 dialog.displayMessage( messageIn ); 329 System.out.println(messageIn); 330 } 331 332 }// Sysout class 333 334 /** 335 This is part of the standard test machinery. It provides a place for the 336 test instructions to be displayed, and a place for interactive messages 337 to the user to be displayed. 338 To have the test instructions displayed, see Sysout. 339 To have a message to the user be displayed, see Sysout. 340 Do not call anything in this dialog directly. 341 */ 342 class TestDialog extends Dialog 343 { 344 345 TextArea instructionsText; 346 TextArea messageText; 347 int maxStringLength = 80; 348 349 //DO NOT call this directly, go through Sysout 350 public TestDialog( Frame frame, String name ) 351 { 352 super( frame, name ); 353 int scrollBoth = TextArea.SCROLLBARS_BOTH; 354 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 355 add( "North", instructionsText ); 356 357 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 358 add("Center", messageText); 359 360 pack(); 361 362 setVisible(true); 363 }// TestDialog() 364 365 //DO NOT call this directly, go through Sysout 366 public void printInstructions( String[] instructions ) 367 { 368 //Clear out any current instructions 369 instructionsText.setText( "" ); 370 371 //Go down array of instruction strings 372 373 String printStr, remainingStr; 374 for( int i=0; i < instructions.length; i++ ) 375 { 376 //chop up each into pieces maxSringLength long 377 remainingStr = instructions[ i ]; 378 while( remainingStr.length() > 0 ) 379 { 380 //if longer than max then chop off first max chars to print 381 if( remainingStr.length() >= maxStringLength ) 382 { 383 //Try to chop on a word boundary 384 int posOfSpace = remainingStr. 385 lastIndexOf( ' ', maxStringLength - 1 ); 386 387 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 388 389 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 390 remainingStr = remainingStr.substring( posOfSpace + 1 ); 391 } 392 //else just print 393 else 394 { 395 printStr = remainingStr; 396 remainingStr = ""; 397 } 398 399 instructionsText.append( printStr + "\n" ); 400 401 }// while 402 403 }// for 404 405 }//printInstructions() 406 407 //DO NOT call this directly, go through Sysout 408 public void displayMessage( String messageIn ) 409 { 410 messageText.append( messageIn + "\n" ); 411 System.out.println(messageIn); 412 } 413 414 }// TestDialog class | 1 /* 2 * Copyright (c) 2013, 2015, 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 @test 26 @bug 6345002 27 @summary grab problems with EmbeddedFrame 28 @requires (os.family == "windows") 29 @modules java.desktop/java.awt.peer 30 @modules java.desktop/sun.awt 31 @modules java.desktop/sun.awt.windows 32 @author Oleg.Semenov@sun.com area=EmbeddedFrame 33 @run main EmbeddedFrameGrabTest 34 */ 35 36 /** 37 * EmbeddedFrameGrabTest.java 38 * 39 * summary: grab problems with EmbeddedFrame 40 */ 41 42 import java.awt.Frame; 43 import java.awt.peer.FramePeer; 44 import javax.swing.JComboBox; 45 import java.awt.Panel; 46 import java.awt.BorderLayout; 47 import java.awt.Robot; 48 import java.awt.event.InputEvent; 49 import java.awt.Rectangle; 50 import java.awt.TextArea; 51 import java.awt.Dialog; 52 53 import java.lang.reflect.Constructor; 54 import java.lang.reflect.Field; 55 56 import sun.awt.AWTAccessor; 57 58 public class EmbeddedFrameGrabTest 59 { 60 private static void init() 61 { 62 if (!System.getProperty("os.name").startsWith("Windows")) { 63 System.out.println("This is Windows only test."); 64 EmbeddedFrameGrabTest.pass(); 65 return; 66 } 67 68 try { 69 final Frame frame = new Frame("AWT Frame"); 70 frame.pack(); 71 frame.setSize(200,200); 72 FramePeer frame_peer = AWTAccessor.getComponentAccessor().getPeer(frame); 73 System.out.println("frame's peer = " + frame_peer); 74 Class comp_peer_class = 75 Class.forName("sun.awt.windows.WComponentPeer"); 76 System.out.println("comp peer class = " + comp_peer_class); 77 Field hwnd_field = comp_peer_class.getDeclaredField("hwnd"); 78 hwnd_field.setAccessible(true); 79 System.out.println("hwnd_field =" + hwnd_field); 80 long hwnd = hwnd_field.getLong(frame_peer); 81 System.out.println("hwnd = " + hwnd); 82 83 Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame"); 84 Constructor constructor = clazz.getConstructor (new Class [] {long.class}); 85 final Frame embedded_frame = 86 (Frame) constructor.newInstance (new Object[] {new Long(hwnd)});; 87 System.out.println("embedded_frame = " + embedded_frame); 88 final JComboBox combo = new JComboBox(new String[] { 89 "Item 1", "Item 2" 90 } ); 91 combo.setSelectedIndex(1); 92 final Panel p = new Panel(); 93 p.setLayout(new BorderLayout()); 94 embedded_frame.add(p,BorderLayout.CENTER); 95 embedded_frame.validate(); 96 p.add(combo); 97 p.validate(); 98 frame.setVisible(true); 99 Robot robot = new Robot(); 100 robot.delay(2000); 101 Rectangle clos = new Rectangle(combo.getLocationOnScreen(),combo.getSize()); 102 robot.mouseMove(clos.x+clos.width/2,clos.y+clos.height/2); 103 robot.mousePress(InputEvent.BUTTON1_MASK); 104 robot.mouseRelease(InputEvent.BUTTON1_MASK); 105 robot.delay(1000); 106 if (!combo.isPopupVisible()) { 107 throw new RuntimeException("Combobox popup is not visible!"); 182 if( ! testGeneratedInterrupt ) throw e; 183 184 //reset flag in case hit this code more than once for some reason (just safety) 185 testGeneratedInterrupt = false; 186 187 if ( theTestPassed == false ) 188 { 189 throw new RuntimeException( failureMessage ); 190 } 191 } 192 193 }//main 194 195 public static synchronized void setTimeoutTo( int seconds ) 196 { 197 sleepTime = seconds * 1000; 198 } 199 200 public static synchronized void pass() 201 { 202 System.out.println( "The test passed." ); 203 System.out.println( "The test is over, hit Ctl-C to stop Java VM" ); 204 //first check if this is executing in main thread 205 if ( mainThread == Thread.currentThread() ) 206 { 207 //Still in the main thread, so set the flag just for kicks, 208 // and throw a test passed exception which will be caught 209 // and end the test. 210 theTestPassed = true; 211 throw new TestPassedException(); 212 } 213 theTestPassed = true; 214 testGeneratedInterrupt = true; 215 mainThread.interrupt(); 216 }//pass() 217 218 public static synchronized void fail() 219 { 220 //test writer didn't specify why test failed, so give generic 221 fail( "it just plain failed! :-)" ); 222 } 223 224 public static synchronized void fail( String whyFailed ) 225 { 226 System.out.println( "The test failed: " + whyFailed ); 227 System.out.println( "The test is over, hit Ctl-C to stop Java VM" ); 228 //check if this called from main thread 229 if ( mainThread == Thread.currentThread() ) 230 { 231 //If main thread, fail now 'cause not sleeping 232 throw new RuntimeException( whyFailed ); 233 } 234 theTestPassed = false; 235 testGeneratedInterrupt = true; 236 failureMessage = whyFailed; 237 mainThread.interrupt(); 238 }//fail() 239 240 }// class EmbeddedFrameGrabTest 241 242 //This exception is used to exit from any level of call nesting 243 // when it's determined that the test has passed, and immediately 244 // end the test. 245 class TestPassedException extends RuntimeException 246 { 247 } 265 eventCount++; 266 267 if( eventCount == 20 ) 268 { 269 //got enough events, so pass 270 271 EmbeddedFrameGrabTest.pass(); 272 } 273 else if( tries == 20 ) 274 { 275 //tried too many times without getting enough events so fail 276 277 EmbeddedFrameGrabTest.fail(); 278 } 279 280 }// eventDispatched() 281 282 }// NewClass class 283 284 */ |