Path: blob/master/test/jdk/java/awt/Focus/NonFocusableWindowTest/NonfocusableOwnerTest.java
41152 views
/*1* Copyright (c) 2008, 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 618235927@summary Tests that Window having non-focusable owner can't be a focus owner.28@library ../../regtesthelpers29@build Util30@run main NonfocusableOwnerTest31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637public class NonfocusableOwnerTest {38Robot robot = Util.createRobot();39Frame frame;40Dialog dialog;41Window window1;42Window window2;43Button button = new Button("button");4445public static void main(String[] args) {46NonfocusableOwnerTest test = new NonfocusableOwnerTest();47test.start();48}4950public void start() {51Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {52public void eventDispatched(AWTEvent e) {53System.out.println(e.toString());54}55}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);5657frame = new Frame("Frame");58frame.setName("Frame-owner");59frame.setBounds(100, 0, 100, 100);60dialog = new Dialog(frame, "Dialog");61dialog.setName("Dialog-owner");62dialog.setBounds(100, 0, 100, 100);6364window1 = new Window(frame);65window1.setName("1st child");66window1.setBounds(100, 300, 100, 100);67window2 = new Window(window1);68window2.setName("2nd child");69window2.setBounds(100, 500, 100, 100);7071test1(frame, window1);72test2(frame, window1, window2);73test3(frame, window1, window2);7475window1 = new Window(dialog);76window1.setBounds(100, 300, 100, 100);77window1.setName("1st child");78window2 = new Window(window1);79window2.setName("2nd child");80window2.setBounds(100, 500, 100, 100);8182test1(dialog, window1);83test2(dialog, window1, window2);84test3(dialog, window1, window2);8586System.out.println("Test passed.");87}8889void test1(Window owner, Window child) {90System.out.println("* * * STAGE 1 * * *\nWindow owner: " + owner);9192owner.setFocusableWindowState(false);93owner.setVisible(true);9495child.add(button);96child.setVisible(true);9798Util.waitTillShown(child);99100Util.clickOnComp(button, robot);101if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {102throw new RuntimeException("Test Failed.");103}104child.dispose();105owner.dispose();106}107108void test2(Window owner, Window child1, Window child2) {109System.out.println("* * * STAGE 2 * * *\nWindow nowner: " + owner);110111owner.setFocusableWindowState(false);112owner.setVisible(true);113114child1.setFocusableWindowState(true);115child1.setVisible(true);116117child2.add(button);118child2.setVisible(true);119120Util.waitTillShown(child2);121122Util.clickOnComp(button, robot);123if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {124throw new RuntimeException("Test failed.");125}126child2.dispose();127child1.dispose();128owner.dispose();129}130131void test3(Window owner, Window child1, Window child2) {132System.out.println("* * * STAGE 3 * * *\nWidow owner: " + owner);133134owner.setFocusableWindowState(true);135owner.setVisible(true);136137child1.setFocusableWindowState(false);138child1.setVisible(true);139140child2.setFocusableWindowState(true);141child2.add(button);142child2.setVisible(true);143144Util.waitTillShown(child2);145146Util.clickOnComp(button, robot);147System.err.println("focus owner: " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());148if (button != KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {149throw new RuntimeException("Test failed.");150}151child1.dispose();152child2.dispose();153owner.dispose();154}155}156157158