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
202N/A @bug 4823903
202N/A @summary Tests actual focused window retaining.
202N/A @author Anton.Tarasov: area=awt.focus
202N/A @library ../../regtesthelpers
202N/A @build Util
202N/A @run main ActualFocusedWindowRetaining
200N/A*/
200N/A
200N/Aimport java.awt.*;
200N/Aimport java.awt.event.*;
200N/Aimport java.lang.reflect.*;
200N/Aimport java.applet.*;
202N/Aimport test.java.awt.regtesthelpers.Util;
200N/A
200N/Apublic class ActualFocusedWindowRetaining extends Applet {
200N/A public static Frame frame = new Frame("Other Frame");
200N/A public static Frame owner = new Frame("Test Frame");
200N/A public static Button otherButton1 = new Button("Other Button 1");
200N/A public static Button otherButton2 = new Button("Other Button 2");
200N/A public static Button otherButton3 = new Button("Other Button 3");
200N/A public static Button testButton1 = new Button("Test Button 1");
200N/A public static Button testButton2 = new Button("Test Button 2");
200N/A public static Button testButton3 = new Button("Test Button 3");
200N/A public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200);
200N/A public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300);
200N/A public static int step;
202N/A public static Robot robot = Util.createRobot();
200N/A
200N/A public static void main(String[] args) {
200N/A ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining();
200N/A a.init();
200N/A a.start();
200N/A }
200N/A
202N/A public void start () {
200N/A Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
200N/A public void eventDispatched(AWTEvent e) {
200N/A Object src = e.getSource();
200N/A Class cls = src.getClass();
200N/A
200N/A if (cls == TestWindow.class) {
202N/A System.out.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">");
200N/A } else if (cls == Frame.class) {
202N/A System.out.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">");
200N/A } else if (cls == Button.class) {
202N/A System.out.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">");
200N/A } else {
202N/A System.out.println(e.paramString() + " on <Non-testing component>");
200N/A }
200N/A }
200N/A }, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
200N/A
202N/A setSize (500, 200);
200N/A setVisible(true);
200N/A validate();
200N/A
200N/A frame.setSize(new Dimension(400, 100));
200N/A frame.setLocation(800, 400);
200N/A frame.setVisible(true);
200N/A frame.toFront();
200N/A
200N/A owner.setLayout(new FlowLayout());
200N/A owner.add(testButton1);
200N/A owner.add(otherButton1);
200N/A owner.pack();
200N/A owner.setLocation(800, 100);
200N/A owner.setSize(new Dimension(400, 100));
200N/A owner.setVisible(true);
200N/A owner.toFront();
202N/A Util.waitTillShown(owner);
200N/A
200N/A window1.setVisible(true);
200N/A window2.setVisible(true);
200N/A window1.toFront();
200N/A window2.toFront();
200N/A // Wait longer...
202N/A Util.waitTillShown(window1);
202N/A Util.waitTillShown(window2);
200N/A
200N/A test();
200N/A
200N/A frame.dispose();
200N/A owner.dispose();
200N/A }
200N/A
200N/A public void test() {
200N/A Button[] butArr = new Button[] {testButton3, testButton2, testButton1};
200N/A Window[] winArr = new Window[] {window2, window1, owner};
200N/A
200N/A step = 1;
200N/A for (int i = 0; i < 3; i++) {
202N/A clickInSeriesCheckFocus(null, butArr[i], frame);
202N/A clickOwnerCheckFocus(winArr[i], butArr[i]);
200N/A step++;
200N/A }
200N/A
200N/A step = 4;
202N/A clickInSeriesCheckFocus(testButton3, testButton1, frame);
202N/A clickOwnerCheckFocus(owner, testButton1);
200N/A
200N/A step = 5;
202N/A clickInSeriesCheckFocus(testButton3, testButton2, frame);
202N/A clickOwnerCheckFocus(window1, testButton2);
200N/A
200N/A step = 6;
202N/A clickInSeriesCheckFocus(testButton1, testButton2, frame);
202N/A clickOwnerCheckFocus(window1, testButton2);
200N/A
200N/A step = 7;
202N/A clickInSeriesCheckFocus(testButton1, testButton2, frame);
200N/A window1.setVisible(false);
202N/A Util.waitForIdle(robot);
202N/A clickOwnerCheckFocus(owner, testButton1);
200N/A
200N/A step = 8;
200N/A window1.setVisible(true);
202N/A Util.waitTillShown(window1);
202N/A clickInSeriesCheckFocus(null, testButton2, frame);
202N/A clickOwnerCheckFocus(window1, testButton2);
200N/A }
200N/A
200N/A boolean checkFocusOwner(Component comp) {
200N/A return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
200N/A }
200N/A
200N/A boolean checkFocusedWindow(Window win) {
200N/A return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow());
200N/A }
200N/A
202N/A void clickOwnerCheckFocus(Window focusedWindow, Component focusedComp) {
202N/A Util.clickOnTitle(owner, robot);
202N/A robot.delay(500);
202N/A
202N/A if (!checkFocusedWindow(focusedWindow)) {
202N/A stopTest("Test failed: actual focused window didn't get a focus");
202N/A }
202N/A if (!checkFocusOwner(focusedComp)) {
202N/A stopTest("Test failed: actual focus owner didn't get a focus");
202N/A }
202N/A }
202N/A
202N/A void clickInSeriesCheckFocus(Component comp1, Component comp2, Frame frame) {
202N/A if (comp1 != null) {
202N/A clickOnCheckFocusOwner(comp1);
202N/A }
202N/A if (comp2 != null) {
202N/A clickOnCheckFocusOwner(comp2);
202N/A }
202N/A clickOnCheckFocusedWindow(frame);
200N/A }
200N/A
200N/A void clickOnCheckFocusOwner(Component c) {
202N/A Util.clickOnComp(c, robot);
202N/A robot.delay(500);
202N/A
200N/A if (!checkFocusOwner(c)) {
200N/A stopTest("Error: can't bring a focus on Component by clicking on it");
200N/A }
200N/A }
200N/A
200N/A void clickOnCheckFocusedWindow(Frame f) {
202N/A Util.clickOnTitle(f, robot);
202N/A robot.delay(500);
202N/A
200N/A if (!checkFocusedWindow(f)) {
200N/A stopTest("Error: can't bring a focus on Frame by clicking on it");
200N/A }
200N/A }
200N/A
200N/A void stopTest(String msg) {
200N/A throw new RuntimeException(new String("Step " + step + ": " + msg));
200N/A }
200N/A}
200N/A
200N/Aclass TestWindow extends Window {
200N/A TestWindow(Frame owner, Button otherButton, Button testButton, int x, int y) {
200N/A super(owner);
200N/A
200N/A setLayout(new FlowLayout());
200N/A setLocation(x, y);
200N/A add(testButton);
200N/A add(otherButton);
200N/A pack();
200N/A setBackground(Color.green);
200N/A }
200N/A}