Path: blob/master/test/jdk/java/awt/EmbeddedFrame/EmbeddedFrameGrabTest/EmbeddedFrameGrabTest.java
41153 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 6345003 8171363 821199927* @summary grab problems with EmbeddedFrame28* @requires (os.family == "windows")29* @modules java.desktop/java.awt.peer30* @modules java.desktop/sun.awt31* @modules java.desktop/sun.awt.windows:open32* @author [email protected] area=EmbeddedFrame33* @run main EmbeddedFrameGrabTest34*/3536import java.awt.Frame;37import java.awt.peer.FramePeer;38import javax.swing.JComboBox;39import java.awt.Panel;40import java.awt.BorderLayout;41import java.awt.Robot;42import java.awt.event.InputEvent;43import java.awt.Rectangle;44import java.awt.TextArea;45import java.awt.Dialog;4647import java.lang.reflect.Constructor;48import java.lang.reflect.Field;4950import sun.awt.AWTAccessor;5152public class EmbeddedFrameGrabTest {5354/**55* Test fails if it throws any exception.56*57* @throws Exception58*/59private void init() throws Exception {6061if (!System.getProperty("os.name").startsWith("Windows")) {62System.out.println("This is Windows only test.");63return;64}6566final Frame frame = new Frame("AWT Frame");67frame.pack();68frame.setSize(200, 200);69frame.setLocationRelativeTo(null);70FramePeer frame_peer = AWTAccessor.getComponentAccessor()71.getPeer(frame);72Class comp_peer_class73= Class.forName("sun.awt.windows.WComponentPeer");74Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");75hwnd_field.setAccessible(true);76long hwnd = hwnd_field.getLong(frame_peer);7778Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");79Constructor constructor80= clazz.getConstructor(new Class[]{long.class});81final Frame embedded_frame82= (Frame) constructor.newInstance(new Object[]{83new Long(hwnd)});;84final JComboBox<String> combo = new JComboBox<>(new String[]{85"Item 1", "Item 2"86});87combo.setSelectedIndex(1);88final Panel p = new Panel();89p.setLayout(new BorderLayout());90embedded_frame.add(p, BorderLayout.CENTER);91embedded_frame.setBounds(0, 0, 150, 150);92embedded_frame.validate();93p.add(combo);94p.validate();95frame.setVisible(true);96Robot robot = new Robot();97robot.delay(2000);98Rectangle clos = new Rectangle(99combo.getLocationOnScreen(), combo.getSize());100robot.mouseMove(clos.x + clos.width / 2, clos.y + clos.height / 2);101robot.mousePress(InputEvent.BUTTON1_MASK);102robot.mouseRelease(InputEvent.BUTTON1_MASK);103robot.delay(1000);104if (!combo.isPopupVisible()) {105throw new RuntimeException("Combobox popup is not visible!");106}107robot.mouseMove(clos.x + clos.width / 2, clos.y + clos.height + 3);108robot.mousePress(InputEvent.BUTTON1_MASK);109robot.mouseRelease(InputEvent.BUTTON1_MASK);110robot.delay(1000);111if (combo.getSelectedIndex() != 0) {112throw new RuntimeException("Combobox selection has not changed!");113}114embedded_frame.remove(p);115embedded_frame.dispose();116frame.dispose();117118}119120public static void main(String args[]) throws Exception {121new EmbeddedFrameGrabTest().init();122}123124}125126127