Path: blob/master/test/jdk/java/awt/Focus/ShowFrameCheckForegroundTest/ShowFrameCheckForegroundTest.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 649297027@summary Tests that showing a toplvel in a not foreground Java process activates it.28@library ../../regtesthelpers29@build Util30@run main ShowFrameCheckForegroundTest31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637public class ShowFrameCheckForegroundTest {38Robot robot;39Frame nofocusFrame = new Frame("Non-focusable");40Frame frame = new Frame("Frame");41Dialog dialog1 = new Dialog(nofocusFrame, "Owned Dialog", false);42Dialog dialog2 = new Dialog((Frame)null, "Owned Dialog", false);43Window testToplevel = null;44Button testButton = new Button("button");45Button showButton = new Button("show");46Runnable action = new Runnable() {47public void run() {48robot.keyPress(KeyEvent.VK_SPACE);49robot.delay(50);50robot.keyRelease(KeyEvent.VK_SPACE);51}52};535455public static void main(String[] args) {56ShowFrameCheckForegroundTest app = new ShowFrameCheckForegroundTest();57app.init();58app.start();59}6061public void init() {62robot = Util.createRobot();63}6465public void start() {66showButton.addActionListener(new ActionListener() {67public void actionPerformed(ActionEvent e) {68Util.showWindowWait(testToplevel);69}70});71nofocusFrame.add(showButton);72nofocusFrame.pack();73nofocusFrame.setFocusableWindowState(false);74nofocusFrame.setLocation(200, 200);75nofocusFrame.setVisible(true);76Util.waitForIdle(robot);7778robot.delay(3000);7980// 1. Show the toplvel without clicking into the non-focusable frame.81test(frame, 1);82test(dialog1, 1);83test(dialog2, 1);8485// 2. Showing the toplvel via clicking into the non-focusable frame.86test(frame, 2);87test(dialog1, 2);88test(dialog2, 2);8990System.out.println("Test passed.");91}9293private void test(Window toplevel, int stage) {94toplevel.add(testButton);95toplevel.pack();96toplevel.setLocation(400, 200);9798switch (stage) {99case 1:100Util.showWindowWait(toplevel);101break;102case 2:103testToplevel = toplevel;104Util.showWindowWait(nofocusFrame);105Util.waitForIdle(robot);106Util.clickOnComp(showButton, robot);107break;108}109Util.waitForIdle(robot);110111if (!Util.trackActionPerformed(testButton, action, 2000, false)) {112throw new TestFailedException("Stage " + stage + ". The toplevel " + toplevel + " wasn't made foreground on showing");113}114System.out.println("Stage " + stage + ". Toplevel " + toplevel + " - passed");115toplevel.dispose();116}117}118119class TestFailedException extends RuntimeException {120TestFailedException(String msg) {121super("Test failed: " + msg);122}123}124125126