Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ActualFocusedWindowTest/ActualFocusedWindowRetaining.java
48070 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@bug 482390326@summary Tests actual focused window retaining.27@author Anton.Tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main ActualFocusedWindowRetaining31*/3233import java.awt.*;34import java.awt.event.*;35import java.lang.reflect.*;36import java.applet.*;37import test.java.awt.regtesthelpers.Util;3839public class ActualFocusedWindowRetaining extends Applet {40public static Frame frame = new Frame("Other Frame");41public static Frame owner = new Frame("Test Frame");42public static Button otherButton1 = new Button("Other Button 1");43public static Button otherButton2 = new Button("Other Button 2");44public static Button otherButton3 = new Button("Other Button 3");45public static Button testButton1 = new Button("Test Button 1");46public static Button testButton2 = new Button("Test Button 2");47public static Button testButton3 = new Button("Test Button 3");48public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200);49public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300);50public static int step;51public static Robot robot = Util.createRobot();5253public static void main(String[] args) {54ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining();55a.init();56a.start();57}5859public void start () {60Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {61public void eventDispatched(AWTEvent e) {62Object src = e.getSource();63Class cls = src.getClass();6465if (cls == TestWindow.class) {66System.out.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">");67} else if (cls == Frame.class) {68System.out.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">");69} else if (cls == Button.class) {70System.out.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">");71} else {72System.out.println(e.paramString() + " on <Non-testing component>");73}74}75}, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);7677setSize (500, 200);78setVisible(true);79validate();8081frame.setSize(new Dimension(400, 100));82frame.setLocation(800, 400);83frame.setVisible(true);84frame.toFront();8586owner.setLayout(new FlowLayout());87owner.add(testButton1);88owner.add(otherButton1);89owner.pack();90owner.setLocation(800, 100);91owner.setSize(new Dimension(400, 100));92owner.setVisible(true);93owner.toFront();94Util.waitTillShown(owner);9596window1.setVisible(true);97window2.setVisible(true);98window1.toFront();99window2.toFront();100// Wait longer...101Util.waitTillShown(window1);102Util.waitTillShown(window2);103104test();105106frame.dispose();107owner.dispose();108}109110public void test() {111Button[] butArr = new Button[] {testButton3, testButton2, testButton1};112Window[] winArr = new Window[] {window2, window1, owner};113114step = 1;115for (int i = 0; i < 3; i++) {116clickInSeriesCheckFocus(null, butArr[i], frame);117clickOwnerCheckFocus(winArr[i], butArr[i]);118step++;119}120121step = 4;122clickInSeriesCheckFocus(testButton3, testButton1, frame);123clickOwnerCheckFocus(owner, testButton1);124125step = 5;126clickInSeriesCheckFocus(testButton3, testButton2, frame);127clickOwnerCheckFocus(window1, testButton2);128129step = 6;130clickInSeriesCheckFocus(testButton1, testButton2, frame);131clickOwnerCheckFocus(window1, testButton2);132133step = 7;134clickInSeriesCheckFocus(testButton1, testButton2, frame);135window1.setVisible(false);136Util.waitForIdle(robot);137clickOwnerCheckFocus(owner, testButton1);138139step = 8;140window1.setVisible(true);141Util.waitTillShown(window1);142clickInSeriesCheckFocus(null, testButton2, frame);143clickOwnerCheckFocus(window1, testButton2);144}145146boolean checkFocusOwner(Component comp) {147return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());148}149150boolean checkFocusedWindow(Window win) {151return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow());152}153154void clickOwnerCheckFocus(Window focusedWindow, Component focusedComp) {155Util.clickOnTitle(owner, robot);156robot.delay(500);157158if (!checkFocusedWindow(focusedWindow)) {159stopTest("Test failed: actual focused window didn't get a focus");160}161if (!checkFocusOwner(focusedComp)) {162stopTest("Test failed: actual focus owner didn't get a focus");163}164}165166void clickInSeriesCheckFocus(Component comp1, Component comp2, Frame frame) {167if (comp1 != null) {168clickOnCheckFocusOwner(comp1);169}170if (comp2 != null) {171clickOnCheckFocusOwner(comp2);172}173clickOnCheckFocusedWindow(frame);174}175176void clickOnCheckFocusOwner(Component c) {177Util.clickOnComp(c, robot);178robot.delay(500);179180if (!checkFocusOwner(c)) {181stopTest("Error: can't bring a focus on Component by clicking on it");182}183}184185void clickOnCheckFocusedWindow(Frame f) {186Util.clickOnTitle(f, robot);187robot.delay(500);188189if (!checkFocusedWindow(f)) {190stopTest("Error: can't bring a focus on Frame by clicking on it");191}192}193194void stopTest(String msg) {195throw new RuntimeException(new String("Step " + step + ": " + msg));196}197}198199class TestWindow extends Window {200TestWindow(Frame owner, Button otherButton, Button testButton, int x, int y) {201super(owner);202203setLayout(new FlowLayout());204setLocation(x, y);205add(testButton);206add(otherButton);207pack();208setBackground(Color.green);209}210}211212213