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!");
 108             }
 109             robot.mouseMove(clos.x+clos.width/2,clos.y+clos.height+3);
 110             robot.mousePress(InputEvent.BUTTON1_MASK);
 111             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 112             robot.delay(1000);
 113             if (combo.getSelectedIndex() != 0) {
 114                 throw new RuntimeException("Combobox selection has not changed!");
 115             }
 116             embedded_frame.remove(p);
 117             embedded_frame.dispose();
 118             frame.dispose();
 119         } catch (Throwable thr) {
 120             thr.printStackTrace();
 121             EmbeddedFrameGrabTest.fail("TEST FAILED: " + thr);
 122         }
 123         EmbeddedFrameGrabTest.pass();
 124 
 125     }//End  init()
 126 
 127 
 128 
 129     /*****************************************************
 130      * Standard Test Machinery Section
 131      * DO NOT modify anything in this section -- it's a
 132      * standard chunk of code which has all of the
 133      * synchronisation necessary for the test harness.
 134      * By keeping it the same in all tests, it is easier
 135      * to read and understand someone else's test, as
 136      * well as insuring that all tests behave correctly
 137      * with the test harness.
 138      * There is a section following this for test-
 139      * classes
 140      ******************************************************/
 141     private static boolean theTestPassed = false;
 142     private static boolean testGeneratedInterrupt = false;
 143     private static String failureMessage = "";
 144 
 145     private static Thread mainThread = null;
 146 
 147     private static int sleepTime = 300000;
 148 
 149     // Not sure about what happens if multiple of this test are
 150     //  instantiated in the same VM.  Being static (and using
 151     //  static vars), it aint gonna work.  Not worrying about
 152     //  it for now.
 153     public static void main( String args[] ) throws InterruptedException
 154     {
 155         mainThread = Thread.currentThread();
 156         try
 157         {
 158             init();
 159         }
 160         catch( TestPassedException e )
 161         {
 162             //The test passed, so just return from main and harness will
 163             // interepret this return as a pass
 164             return;
 165         }
 166         //At this point, neither test pass nor test fail has been
 167         // called -- either would have thrown an exception and ended the
 168         // test, so we know we have multiple threads.
 169 
 170         //Test involves other threads, so sleep and wait for them to
 171         // called pass() or fail()
 172         try
 173         {
 174             Thread.sleep( sleepTime );
 175             //Timed out, so fail the test
 176             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 177         }
 178         catch (InterruptedException e)
 179         {
 180             //The test harness may have interrupted the test.  If so, rethrow the exception
 181             // so that the harness gets it and deals with it.
 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 }
 248 
 249 //*********** End Standard Test Machinery Section **********
 250 
 251 
 252 //************ Begin classes defined for the test ****************
 253 
 254 // if want to make listeners, here is the recommended place for them, then instantiate
 255 //  them in init()
 256 
 257 /* Example of a class which may be written as part of a test
 258 class NewClass implements anInterface
 259  {
 260    static int newVar = 0;
 261 
 262    public void eventDispatched(AWTEvent e)
 263     {
 264       //Counting events to see if we get enough
 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 */