Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ActualFocusedWindowTest/ActualFocusedWindowBlockingTest.java
48071 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 631457526@summary Tests that previosly focused owned window doesn't steal focus when an owner's component requests focus.27@author Anton.Tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main ActualFocusedWindowBlockingTest31*/3233import java.awt.*;34import java.awt.event.*;35import java.applet.Applet;36import java.util.concurrent.atomic.AtomicBoolean;37import java.lang.reflect.InvocationTargetException;38import sun.awt.SunToolkit;39import test.java.awt.regtesthelpers.Util;4041public class ActualFocusedWindowBlockingTest extends Applet {42Robot robot = Util.createRobot();43Frame owner = new Frame("Owner Frame");44Window win = new Window(owner);45Frame frame = new Frame("Auxiliary Frame");46Button fButton = new Button("frame button") {public String toString() {return "Frame_Button";}};47Button wButton = new Button("window button") {public String toString() {return "Window_Button";}};48Button aButton = new Button("auxiliary button") {public String toString() {return "Auxiliary_Button";}};4950public static void main(String[] args) {51ActualFocusedWindowBlockingTest app = new ActualFocusedWindowBlockingTest();52app.init();53app.start();54}5556public void init() {57Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {58public void eventDispatched(AWTEvent e) {59System.out.println("--> " + e);60}61}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);6263owner.add(fButton);64win.add(wButton);65frame.add(aButton);6667owner.setName("OWNER_FRAME");68win.setName("OWNED_WINDOW");69frame.setName("AUX_FRAME");7071tuneAndShowWindows(new Window[] {owner, win, frame});72}7374public void start() {75if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {76System.out.println("No testing on Motif. Test passed.");77return;78}7980System.out.println("\nTest started:\n");8182// Test 1.8384clickOnCheckFocus(wButton);85clickOnCheckFocus(aButton);8687Util.clickOnComp(fButton, robot);88if (!testFocused(fButton)) {89throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by click");90}9192// Test 2.9394clickOnCheckFocus(wButton);95clickOnCheckFocus(aButton);9697fButton.requestFocus();98Util.waitForIdle(robot);99if (!testFocused(fButton)) {100throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by request");101}102103// Test 3.104105clickOnCheckFocus(wButton);106clickOnCheckFocus(aButton);107clickOnCheckFocus(fButton);108clickOnCheckFocus(aButton);109110Util.clickOnTitle(owner, robot);111if (!testFocused(fButton)) {112throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused as the most recent focus owner");113}114115System.out.println("Test passed.");116}117118void tuneAndShowWindows(Window[] arr) {119int y = 0;120for (Window w: arr) {121w.setLayout(new FlowLayout());122w.setBounds(100, y, 400, 150);123w.setBackground(Color.blue);124w.setVisible(true);125y += 200;126Util.waitForIdle(robot);127}128}129130void clickOnCheckFocus(Component c) {131if (c instanceof Frame) {132Util.clickOnTitle((Frame)c, robot);133} else {134Util.clickOnComp(c, robot);135}136if (!testFocused(c)) {137throw new TestErrorException(c + "couldn't get focus by click.");138}139}140141boolean testFocused(Component c) {142for (int i=0; i<10; i++) {143if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) {144return true;145}146Util.waitForIdle(robot);147}148return false;149}150151// Thrown when the behavior being verified is found wrong.152class TestFailedException extends RuntimeException {153TestFailedException(String msg) {154super("Test failed: " + msg);155}156}157158// Thrown when an error not related to the behavior being verified is encountered.159class TestErrorException extends RuntimeException {160TestErrorException(String msg) {161super("Unexpected error: " + msg);162}163}164}165166167