Path: blob/master/test/jdk/java/awt/Focus/ActualFocusedWindowTest/ActualFocusedWindowBlockingTest.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 631457527@summary Tests that previosly focused owned window doesn't steal focus when an owner's component requests focus.28@library ../../regtesthelpers29@build Util30@run main ActualFocusedWindowBlockingTest31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637public class ActualFocusedWindowBlockingTest {38Robot robot = Util.createRobot();39Frame owner = new Frame("Owner Frame");40Window win = new Window(owner);41Frame frame = new Frame("Auxiliary Frame");42Button fButton = new Button("frame button") {public String toString() {return "Frame_Button";}};43Button wButton = new Button("window button") {public String toString() {return "Window_Button";}};44Button aButton = new Button("auxiliary button") {public String toString() {return "Auxiliary_Button";}};4546public static void main(String[] args) {47ActualFocusedWindowBlockingTest app = new ActualFocusedWindowBlockingTest();48app.init();49app.start();50}5152public void init() {53Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {54public void eventDispatched(AWTEvent e) {55System.out.println("--> " + e);56}57}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);5859owner.add(fButton);60win.add(wButton);61frame.add(aButton);6263owner.setName("OWNER_FRAME");64win.setName("OWNED_WINDOW");65frame.setName("AUX_FRAME");6667tuneAndShowWindows(new Window[] {owner, win, frame});68}6970public void start() {71if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {72System.out.println("No testing on Motif. Test passed.");73return;74}7576System.out.println("\nTest started:\n");7778// Test 1.7980clickOnCheckFocus(wButton);81clickOnCheckFocus(aButton);8283Util.clickOnComp(fButton, robot);84if (!testFocused(fButton)) {85throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by click");86}8788// Test 2.8990clickOnCheckFocus(wButton);91clickOnCheckFocus(aButton);9293fButton.requestFocus();94Util.waitForIdle(robot);95if (!testFocused(fButton)) {96throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by request");97}9899// Test 3.100101clickOnCheckFocus(wButton);102clickOnCheckFocus(aButton);103clickOnCheckFocus(fButton);104clickOnCheckFocus(aButton);105106Util.clickOnTitle(owner, robot);107if (!testFocused(fButton)) {108throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused as the most recent focus owner");109}110111System.out.println("Test passed.");112}113114void tuneAndShowWindows(Window[] arr) {115int y = 0;116for (Window w: arr) {117w.setLayout(new FlowLayout());118w.setBounds(100, y, 400, 150);119w.setBackground(Color.blue);120w.setVisible(true);121y += 200;122Util.waitForIdle(robot);123}124}125126void clickOnCheckFocus(Component c) {127if (c instanceof Frame) {128Util.clickOnTitle((Frame)c, robot);129} else {130Util.clickOnComp(c, robot);131}132if (!testFocused(c)) {133throw new TestErrorException(c + "couldn't get focus by click.");134}135}136137boolean testFocused(Component c) {138for (int i=0; i<10; i++) {139if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) {140return true;141}142Util.waitForIdle(robot);143}144return false;145}146147// Thrown when the behavior being verified is found wrong.148class TestFailedException extends RuntimeException {149TestFailedException(String msg) {150super("Test failed: " + msg);151}152}153154// Thrown when an error not related to the behavior being verified is encountered.155class TestErrorException extends RuntimeException {156TestErrorException(String msg) {157super("Unexpected error: " + msg);158}159}160}161162163