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 6345003 8171363
  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:open
  32  @author Oleg.Semenov@sun.com area=EmbeddedFrame
  33  @run main EmbeddedFrameGrabTest
  34  */
  35 /**
  36  * EmbeddedFrameGrabTest.java
  37  *
  38  * summary: grab problems with EmbeddedFrame
  39  */
  40 import java.awt.Frame;
  41 import java.awt.peer.FramePeer;
  42 import javax.swing.JComboBox;
  43 import java.awt.Panel;
  44 import java.awt.BorderLayout;
  45 import java.awt.Robot;
  46 import java.awt.event.InputEvent;
  47 import java.awt.Rectangle;
  48 import java.awt.TextArea;
  49 import java.awt.Dialog;
  50 
  51 import java.lang.reflect.Constructor;
  52 import java.lang.reflect.Field;
  53 
  54 import sun.awt.AWTAccessor;
  55 
  56 public class EmbeddedFrameGrabTest {
  57 
  58     /**
  59      * Test fails if it throws any exception.
  60      *
  61      * @throws Exception
  62      */
  63     private void init() throws Exception {
  64 
  65         if (!System.getProperty("os.name").startsWith("Windows")) {
  66             System.out.println("This is Windows only test.");
  67             return;
  68         }
  69 
  70         final Frame frame = new Frame("AWT Frame");
  71         frame.pack();
  72         frame.setSize(200, 200);
  73         FramePeer frame_peer = AWTAccessor.getComponentAccessor()
  74                                     .getPeer(frame);
  75         Class comp_peer_class
  76                 = Class.forName("sun.awt.windows.WComponentPeer");
  77         Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
  78         hwnd_field.setAccessible(true);
  79         long hwnd = hwnd_field.getLong(frame_peer);
  80 
  81         Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
  82         Constructor constructor
  83                 = clazz.getConstructor(new Class[]{long.class});
  84         final Frame embedded_frame
  85                 = (Frame) constructor.newInstance(new Object[]{
  86                     new Long(hwnd)});;
  87         final JComboBox<String> combo = new JComboBox<>(new String[]{
  88             "Item 1", "Item 2"
  89         });
  90         combo.setSelectedIndex(1);
  91         final Panel p = new Panel();
  92         p.setLayout(new BorderLayout());
  93         embedded_frame.add(p, BorderLayout.CENTER);
  94         embedded_frame.validate();
  95         p.add(combo);
  96         p.validate();
  97         frame.setVisible(true);
  98         Robot robot = new Robot();
  99         robot.delay(2000);
 100         Rectangle clos = new Rectangle(
 101                 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 
 120     }
 121 
 122     public static void main(String args[]) throws Exception {
 123         new EmbeddedFrameGrabTest().init();
 124     }
 125 
 126 }