Path: blob/master/test/jdk/java/awt/Focus/FocusOwnerFrameOnClick/FocusOwnerFrameOnClick.java
41152 views
/*1* Copyright (c) 1995, 2018, 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 688667827@summary Tests that clicking an owner frame switches focus from its owned window.28@library ../../regtesthelpers29@build Util30@run main FocusOwnerFrameOnClick31*/3233import java.awt.*;34import java.awt.event.*;35import java.util.concurrent.atomic.AtomicBoolean;36import test.java.awt.regtesthelpers.Util;3738public class FocusOwnerFrameOnClick {39Robot robot;40Frame frame = new Frame("Frame");41Window window = new Window(frame);42Button fButton = new Button("fButton");43Button wButton = new Button("wButton");4445AtomicBoolean focused = new AtomicBoolean(false);4647public static void main(String[] args) {48FocusOwnerFrameOnClick app = new FocusOwnerFrameOnClick();49app.init();50app.start();51}5253public void init() {54robot = Util.createRobot();5556frame.setLayout(new FlowLayout());57frame.setSize(200, 200);58frame.add(fButton);5960window.setLocation(300, 0);61window.add(wButton);62window.pack();63}6465public void start() {66frame.setVisible(true);67Util.waitForIdle(robot);6869window.setVisible(true);70Util.waitForIdle(robot);7172if (!wButton.hasFocus()) {73if (!Util.trackFocusGained(wButton, new Runnable() {74public void run() {75Util.clickOnComp(wButton, robot);76}77}, 2000, false))78{79throw new TestErrorException("wButton didn't gain focus on showing");80}81}8283Runnable clickAction = new Runnable() {84public void run() {85Point loc = fButton.getLocationOnScreen();86Dimension dim = fButton.getSize();8788robot.mouseMove(loc.x, loc.y + dim.height + 20);89robot.delay(50);90robot.mousePress(InputEvent.BUTTON1_MASK);91robot.delay(50);92robot.mouseRelease(InputEvent.BUTTON1_MASK);93}94};9596if (!Util.trackWindowGainedFocus(frame, clickAction, 2000, true)) {97throw new TestFailedException("The frame wasn't focused on click");98}99100System.out.println("Test passed.");101}102}103104/**105* Thrown when the behavior being verified is found wrong.106*/107class TestFailedException extends RuntimeException {108TestFailedException(String msg) {109super("Test failed: " + msg);110}111}112113/**114* Thrown when an error not related to the behavior being verified is encountered.115*/116class TestErrorException extends RuntimeException {117TestErrorException(String msg) {118super("Unexpected error: " + msg);119}120}121122123