Path: blob/master/test/jdk/java/awt/Focus/ActualFocusedWindowTest/ActualFocusedWindowRetaining.java
41152 views
/*1* Copyright (c) 2008, 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 482390327@summary Tests actual focused window retaining.28@library ../../regtesthelpers29@build Util30@run main ActualFocusedWindowRetaining31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637public class ActualFocusedWindowRetaining {38public static Frame frame = new Frame("Other Frame");39public static Frame owner = new Frame("Test Frame");40public static Button otherButton1 = new Button("Other Button 1");41public static Button otherButton2 = new Button("Other Button 2");42public static Button otherButton3 = new Button("Other Button 3");43public static Button testButton1 = new Button("Test Button 1");44public static Button testButton2 = new Button("Test Button 2");45public static Button testButton3 = new Button("Test Button 3");46public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200);47public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300);48public static int step;49public static Robot robot = Util.createRobot();5051public static void main(String[] args) {52ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining();53a.start();54}5556public void start () {57Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {58public void eventDispatched(AWTEvent e) {59Object src = e.getSource();60Class cls = src.getClass();6162if (cls == TestWindow.class) {63System.out.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">");64} else if (cls == Frame.class) {65System.out.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">");66} else if (cls == Button.class) {67System.out.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">");68} else {69System.out.println(e.paramString() + " on <Non-testing component>");70}71}72}, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);7374frame.setSize(new Dimension(400, 100));75frame.setLocation(800, 400);76frame.setVisible(true);77frame.toFront();7879owner.setLayout(new FlowLayout());80owner.add(testButton1);81owner.add(otherButton1);82owner.pack();83owner.setLocation(800, 100);84owner.setSize(new Dimension(400, 100));85owner.setVisible(true);86owner.toFront();87Util.waitTillShown(owner);8889window1.setVisible(true);90window2.setVisible(true);91window1.toFront();92window2.toFront();93// Wait longer...94Util.waitTillShown(window1);95Util.waitTillShown(window2);9697test();9899frame.dispose();100owner.dispose();101}102103public void test() {104Button[] butArr = new Button[] {testButton3, testButton2, testButton1};105Window[] winArr = new Window[] {window2, window1, owner};106107step = 1;108for (int i = 0; i < 3; i++) {109clickInSeriesCheckFocus(null, butArr[i], frame);110clickOwnerCheckFocus(winArr[i], butArr[i]);111step++;112}113114step = 4;115clickInSeriesCheckFocus(testButton3, testButton1, frame);116clickOwnerCheckFocus(owner, testButton1);117118step = 5;119clickInSeriesCheckFocus(testButton3, testButton2, frame);120clickOwnerCheckFocus(window1, testButton2);121122step = 6;123clickInSeriesCheckFocus(testButton1, testButton2, frame);124clickOwnerCheckFocus(window1, testButton2);125126step = 7;127clickInSeriesCheckFocus(testButton1, testButton2, frame);128window1.setVisible(false);129Util.waitForIdle(robot);130clickOwnerCheckFocus(owner, testButton1);131132step = 8;133window1.setVisible(true);134Util.waitTillShown(window1);135clickInSeriesCheckFocus(null, testButton2, frame);136clickOwnerCheckFocus(window1, testButton2);137}138139boolean checkFocusOwner(Component comp) {140return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());141}142143boolean checkFocusedWindow(Window win) {144return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow());145}146147void clickOwnerCheckFocus(Window focusedWindow, Component focusedComp) {148Util.clickOnTitle(owner, robot);149robot.delay(500);150151if (!checkFocusedWindow(focusedWindow)) {152stopTest("Test failed: actual focused window didn't get a focus");153}154if (!checkFocusOwner(focusedComp)) {155stopTest("Test failed: actual focus owner didn't get a focus");156}157}158159void clickInSeriesCheckFocus(Component comp1, Component comp2, Frame frame) {160if (comp1 != null) {161clickOnCheckFocusOwner(comp1);162}163if (comp2 != null) {164clickOnCheckFocusOwner(comp2);165}166clickOnCheckFocusedWindow(frame);167}168169void clickOnCheckFocusOwner(Component c) {170Util.clickOnComp(c, robot);171robot.delay(500);172173if (!checkFocusOwner(c)) {174stopTest("Error: can't bring a focus on Component by clicking on it");175}176}177178void clickOnCheckFocusedWindow(Frame f) {179Util.clickOnTitle(f, robot);180robot.delay(500);181182if (!checkFocusedWindow(f)) {183stopTest("Error: can't bring a focus on Frame by clicking on it");184}185}186187void stopTest(String msg) {188throw new RuntimeException(new String("Step " + step + ": " + msg));189}190}191192class TestWindow extends Window {193TestWindow(Frame owner, Button otherButton, Button testButton, int x, int y) {194super(owner);195196setLayout(new FlowLayout());197setLocation(x, y);198add(testButton);199add(otherButton);200pack();201setBackground(Color.green);202}203}204205206