2766N/A/*
2766N/A * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
2766N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2766N/A *
2766N/A * This code is free software; you can redistribute it and/or modify it
2766N/A * under the terms of the GNU General Public License version 2 only, as
2766N/A * published by the Free Software Foundation. Oracle designates this
2766N/A * particular file as subject to the "Classpath" exception as provided
2766N/A * by Oracle in the LICENSE file that accompanied this code.
2766N/A *
2766N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2766N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2766N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2766N/A * version 2 for more details (a copy is included in the LICENSE file that
2766N/A * accompanied this code).
2766N/A *
2766N/A * You should have received a copy of the GNU General Public License version
2766N/A * 2 along with this work; if not, write to the Free Software Foundation,
2766N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2766N/A *
2766N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2766N/A * or visit www.oracle.com if you need additional information or have any
2766N/A * questions.
2766N/A */
2766N/A
2766N/A/*
2766N/A @test FocusOwnerFrameOnClick.java %W% %E%
2766N/A @bug 6886678
2766N/A @summary Tests that clicking an owner frame switches focus from its owned window.
2766N/A @author Anton Tarasov: area=awt.focus
2766N/A @library ../../regtesthelpers
2766N/A @build Util
2766N/A @run main FocusOwnerFrameOnClick
2766N/A*/
2766N/A
2766N/Aimport java.awt.*;
2766N/Aimport java.awt.event.*;
2766N/Aimport java.applet.Applet;
2766N/Aimport java.util.concurrent.atomic.AtomicBoolean;
2766N/Aimport java.lang.reflect.InvocationTargetException;
2766N/Aimport test.java.awt.regtesthelpers.Util;
2766N/A
2766N/Apublic class FocusOwnerFrameOnClick extends Applet {
2766N/A Robot robot;
2766N/A Frame frame = new Frame("Frame");
2766N/A Window window = new Window(frame);
2766N/A Button fButton = new Button("fButton");
2766N/A Button wButton = new Button("wButton");
2766N/A
2766N/A AtomicBoolean focused = new AtomicBoolean(false);
2766N/A
2766N/A public static void main(String[] args) {
2766N/A FocusOwnerFrameOnClick app = new FocusOwnerFrameOnClick();
2766N/A app.init();
2766N/A app.start();
2766N/A }
2766N/A
2766N/A public void init() {
2766N/A robot = Util.createRobot();
2766N/A
2766N/A frame.setLayout(new FlowLayout());
2766N/A frame.setSize(200, 200);
2766N/A frame.add(fButton);
2766N/A
2766N/A window.setLocation(300, 0);
2766N/A window.add(wButton);
2766N/A window.pack();
2766N/A }
2766N/A
2766N/A public void start() {
2766N/A frame.setVisible(true);
2766N/A Util.waitForIdle(robot);
2766N/A
2766N/A window.setVisible(true);
2766N/A Util.waitForIdle(robot);
2766N/A
2766N/A if (!wButton.hasFocus()) {
2766N/A if (!Util.trackFocusGained(wButton, new Runnable() {
2766N/A public void run() {
2766N/A Util.clickOnComp(wButton, robot);
2766N/A }
2766N/A }, 2000, false))
2766N/A {
2766N/A throw new TestErrorException("wButton didn't gain focus on showing");
2766N/A }
2766N/A }
2766N/A
2766N/A Runnable clickAction = new Runnable() {
2766N/A public void run() {
2766N/A Point loc = fButton.getLocationOnScreen();
2766N/A Dimension dim = fButton.getSize();
2766N/A
2766N/A robot.mouseMove(loc.x, loc.y + dim.height + 20);
2766N/A robot.delay(50);
2766N/A robot.mousePress(InputEvent.BUTTON1_MASK);
2766N/A robot.delay(50);
2766N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
2766N/A }
2766N/A };
2766N/A
2766N/A if (!Util.trackWindowGainedFocus(frame, clickAction, 2000, true)) {
2766N/A throw new TestFailedException("The frame wasn't focused on click");
2766N/A }
2766N/A
2766N/A System.out.println("Test passed.");
2766N/A }
2766N/A}
2766N/A
2766N/A/**
2766N/A * Thrown when the behavior being verified is found wrong.
2766N/A */
2766N/Aclass TestFailedException extends RuntimeException {
2766N/A TestFailedException(String msg) {
2766N/A super("Test failed: " + msg);
2766N/A }
2766N/A}
2766N/A
2766N/A/**
2766N/A * Thrown when an error not related to the behavior being verified is encountered.
2766N/A */
2766N/Aclass TestErrorException extends RuntimeException {
2766N/A TestErrorException(String msg) {
2766N/A super("Unexpected error: " + msg);
2766N/A }
2766N/A}