Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/FocusOwnerFrameOnClick/FocusOwnerFrameOnClick.java
47867 views
/*1* Copyright (c) 1995, 2013, 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@test FocusOwnerFrameOnClick.java %W% %E%25@bug 688667826@summary Tests that clicking an owner frame switches focus from its owned window.27@author Anton Tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main FocusOwnerFrameOnClick31*/3233import java.awt.*;34import java.awt.event.*;35import java.applet.Applet;36import java.util.concurrent.atomic.AtomicBoolean;37import java.lang.reflect.InvocationTargetException;38import test.java.awt.regtesthelpers.Util;3940public class FocusOwnerFrameOnClick extends Applet {41Robot robot;42Frame frame = new Frame("Frame");43Window window = new Window(frame);44Button fButton = new Button("fButton");45Button wButton = new Button("wButton");4647AtomicBoolean focused = new AtomicBoolean(false);4849public static void main(String[] args) {50FocusOwnerFrameOnClick app = new FocusOwnerFrameOnClick();51app.init();52app.start();53}5455public void init() {56robot = Util.createRobot();5758frame.setLayout(new FlowLayout());59frame.setSize(200, 200);60frame.add(fButton);6162window.setLocation(300, 0);63window.add(wButton);64window.pack();65}6667public void start() {68frame.setVisible(true);69Util.waitForIdle(robot);7071window.setVisible(true);72Util.waitForIdle(robot);7374if (!wButton.hasFocus()) {75if (!Util.trackFocusGained(wButton, new Runnable() {76public void run() {77Util.clickOnComp(wButton, robot);78}79}, 2000, false))80{81throw new TestErrorException("wButton didn't gain focus on showing");82}83}8485Runnable clickAction = new Runnable() {86public void run() {87Point loc = fButton.getLocationOnScreen();88Dimension dim = fButton.getSize();8990robot.mouseMove(loc.x, loc.y + dim.height + 20);91robot.delay(50);92robot.mousePress(InputEvent.BUTTON1_MASK);93robot.delay(50);94robot.mouseRelease(InputEvent.BUTTON1_MASK);95}96};9798if (!Util.trackWindowGainedFocus(frame, clickAction, 2000, true)) {99throw new TestFailedException("The frame wasn't focused on click");100}101102System.out.println("Test passed.");103}104}105106/**107* Thrown when the behavior being verified is found wrong.108*/109class TestFailedException extends RuntimeException {110TestFailedException(String msg) {111super("Test failed: " + msg);112}113}114115/**116* Thrown when an error not related to the behavior being verified is encountered.117*/118class TestErrorException extends RuntimeException {119TestErrorException(String msg) {120super("Unexpected error: " + msg);121}122}123124125