200N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
200N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
200N/A *
200N/A * This code is free software; you can redistribute it and/or modify it
200N/A * under the terms of the GNU General Public License version 2 only, as
200N/A * published by the Free Software Foundation.
200N/A *
200N/A * This code is distributed in the hope that it will be useful, but WITHOUT
200N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
200N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
200N/A * version 2 for more details (a copy is included in the LICENSE file that
200N/A * accompanied this code).
200N/A *
200N/A * You should have received a copy of the GNU General Public License version
200N/A * 2 along with this work; if not, write to the Free Software Foundation,
200N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
200N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
200N/A */
200N/A
200N/A/*
200N/A @test
200N/A @bug 6314575
200N/A @summary Tests that previosly focused owned window doesn't steal focus when an owner's component requests focus.
202N/A @author Anton.Tarasov: area=awt.focus
202N/A @library ../../regtesthelpers
202N/A @build Util
202N/A @run main ActualFocusedWindowBlockingTest
200N/A*/
200N/A
200N/Aimport java.awt.*;
200N/Aimport java.awt.event.*;
200N/Aimport java.applet.Applet;
200N/Aimport java.util.concurrent.atomic.AtomicBoolean;
200N/Aimport java.lang.reflect.InvocationTargetException;
200N/Aimport sun.awt.SunToolkit;
202N/Aimport test.java.awt.regtesthelpers.Util;
200N/A
200N/Apublic class ActualFocusedWindowBlockingTest extends Applet {
202N/A Robot robot = Util.createRobot();
200N/A Frame owner = new Frame("Owner Frame");
200N/A Window win = new Window(owner);
200N/A Frame frame = new Frame("Auxiliary Frame");
200N/A Button fButton = new Button("frame button") {public String toString() {return "Frame_Button";}};
200N/A Button wButton = new Button("window button") {public String toString() {return "Window_Button";}};
200N/A Button aButton = new Button("auxiliary button") {public String toString() {return "Auxiliary_Button";}};
200N/A
200N/A public static void main(String[] args) {
200N/A ActualFocusedWindowBlockingTest app = new ActualFocusedWindowBlockingTest();
200N/A app.init();
200N/A app.start();
200N/A }
200N/A
200N/A public void init() {
200N/A Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
200N/A public void eventDispatched(AWTEvent e) {
202N/A System.out.println("--> " + e);
200N/A }
200N/A }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
200N/A
200N/A owner.add(fButton);
200N/A win.add(wButton);
200N/A frame.add(aButton);
200N/A
200N/A owner.setName("OWNER_FRAME");
200N/A win.setName("OWNED_WINDOW");
200N/A frame.setName("AUX_FRAME");
200N/A
200N/A tuneAndShowWindows(new Window[] {owner, win, frame});
200N/A }
200N/A
200N/A public void start() {
200N/A if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
202N/A System.out.println("No testing on Motif. Test passed.");
200N/A return;
200N/A }
200N/A
202N/A System.out.println("\nTest started:\n");
200N/A
200N/A // Test 1.
200N/A
200N/A clickOnCheckFocus(wButton);
200N/A clickOnCheckFocus(aButton);
200N/A
202N/A Util.clickOnComp(fButton, robot);
200N/A if (!testFocused(fButton)) {
200N/A throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by click");
200N/A }
200N/A
200N/A // Test 2.
200N/A
200N/A clickOnCheckFocus(wButton);
200N/A clickOnCheckFocus(aButton);
200N/A
200N/A fButton.requestFocus();
202N/A Util.waitForIdle(robot);
200N/A if (!testFocused(fButton)) {
200N/A throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by request");
200N/A }
200N/A
200N/A // Test 3.
200N/A
200N/A clickOnCheckFocus(wButton);
202N/A clickOnCheckFocus(aButton);
202N/A clickOnCheckFocus(fButton);
200N/A clickOnCheckFocus(aButton);
200N/A
202N/A Util.clickOnTitle(owner, robot);
200N/A if (!testFocused(fButton)) {
200N/A throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused as the most recent focus owner");
200N/A }
200N/A
202N/A System.out.println("Test passed.");
200N/A }
200N/A
200N/A void tuneAndShowWindows(Window[] arr) {
200N/A int y = 0;
200N/A for (Window w: arr) {
200N/A w.setLayout(new FlowLayout());
200N/A w.setBounds(100, y, 400, 150);
200N/A w.setBackground(Color.blue);
200N/A w.setVisible(true);
200N/A y += 200;
202N/A Util.waitForIdle(robot);
200N/A }
200N/A }
200N/A
202N/A void clickOnCheckFocus(Component c) {
200N/A if (c instanceof Frame) {
202N/A Util.clickOnTitle((Frame)c, robot);
200N/A } else {
202N/A Util.clickOnComp(c, robot);
200N/A }
200N/A if (!testFocused(c)) {
202N/A throw new TestErrorException(c + "couldn't get focus by click.");
200N/A }
200N/A }
200N/A
200N/A boolean testFocused(Component c) {
200N/A for (int i=0; i<10; i++) {
200N/A if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) {
200N/A return true;
200N/A }
202N/A Util.waitForIdle(robot);
200N/A }
200N/A return false;
200N/A }
200N/A
202N/A // Thrown when the behavior being verified is found wrong.
202N/A class TestFailedException extends RuntimeException {
202N/A TestFailedException(String msg) {
202N/A super("Test failed: " + msg);
202N/A }
200N/A }
200N/A
202N/A // Thrown when an error not related to the behavior being verified is encountered.
202N/A class TestErrorException extends RuntimeException {
202N/A TestErrorException(String msg) {
202N/A super("Unexpected error: " + msg);
200N/A }
200N/A }
200N/A}