Path: blob/master/test/jdk/java/awt/Focus/FocusEmbeddedFrameTest/FocusEmbeddedFrameTest.java
41152 views
/*1* Copyright (c) 2007, 2018, 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 651667527* @summary Tests that EmbeddedFrame can be focused.28* @requires (os.family == "windows")29* @modules java.desktop/java.awt.peer30* java.desktop/sun.awt31* java.desktop/sun.awt.windows32* @library /java/awt/patchlib ../../regtesthelpers33* @build java.desktop/java.awt.Helper34* @build Util UtilInternal35* @run main FocusEmbeddedFrameTest36*/3738import java.awt.*;39import java.awt.event.*;40import test.java.awt.regtesthelpers.Util;41import test.java.awt.regtesthelpers.UtilInternal;4243public class FocusEmbeddedFrameTest {44static Frame embedder = new Frame("Embedder");45static Frame ef = null;46static volatile boolean passed;4748Robot robot;4950public static void main(String[] args) {51FocusEmbeddedFrameTest app = new FocusEmbeddedFrameTest();52app.init();53app.start();54}5556public void init() {57robot = Util.createRobot();58}5960public void start() {6162if (!"sun.awt.windows.WToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {63System.out.println("The test is for WToolkit only!");64return;65}6667Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {68public void eventDispatched(AWTEvent e) {69System.err.println("--> " + e);70}71}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);7273embedder.addNotify();7475try {76ef = UtilInternal.createEmbeddedFrame(embedder);77} catch (Throwable t) {78t.printStackTrace();79throw new Error("Test error: couldn't create an EmbeddedFrame!");80}8182ef.setSize(100, 100);83ef.setBackground(Color.blue);8485embedder.addFocusListener(new FocusAdapter() {86public void focusGained(FocusEvent e) {87FocusEmbeddedFrameTest.ef.requestFocus();88}89});9091ef.addFocusListener(new FocusAdapter() {92public void focusGained(FocusEvent e) {93passed = true;94}95});9697embedder.setSize(150, 150);98embedder.setVisible(true);99100Util.waitForIdle(robot);101102if (!passed) {103throw new TestFailedException("the EmbeddedFrame didn't get focus on request!");104}105106System.out.println("Test passed.");107}108}109110class TestFailedException extends RuntimeException {111TestFailedException(String msg) {112super("Test failed: " + msg);113}114}115116117