6097N/A/*
6097N/A * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
6097N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6097N/A *
6097N/A * This code is free software; you can redistribute it and/or modify it
6097N/A * under the terms of the GNU General Public License version 2 only, as
6097N/A * published by the Free Software Foundation.
6097N/A *
6097N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6097N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6097N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6097N/A * version 2 for more details (a copy is included in the LICENSE file that
6097N/A * accompanied this code).
6097N/A *
6097N/A * You should have received a copy of the GNU General Public License version
6097N/A * 2 along with this work; if not, write to the Free Software Foundation,
6097N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6097N/A *
6097N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6097N/A * or visit www.oracle.com if you need additional information or have any
6097N/A * questions.
6097N/A */
6097N/A
6097N/A/*
6097N/A * @test
6097N/A * @bug 6385277
6097N/A * @summary Tests that override redirect window gets activated on click.
6097N/A * @author anton.tarasov@sun.com: area=awt.focus
6097N/A * @library ../../regtesthelpers
6097N/A * @build Util
6097N/A * @run main SimpleWindowActivationTest
6097N/A */
6097N/Aimport java.awt.*;
6097N/Aimport java.awt.event.*;
6097N/Aimport java.util.concurrent.Callable;
6097N/Aimport javax.swing.SwingUtilities;
6097N/Aimport sun.awt.SunToolkit;
6097N/Aimport test.java.awt.regtesthelpers.Util;
6097N/A
6097N/Apublic class SimpleWindowActivationTest {
6097N/A
6097N/A private static Frame frame;
6097N/A private static Window window;
6097N/A private static Button fbutton;
6097N/A private static Button wbutton;
6097N/A private static Label label;
6097N/A private static Robot robot;
6097N/A private static SunToolkit toolkit;
6097N/A
6097N/A public static void main(String[] args) throws Exception {
6097N/A
6097N/A if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
6097N/A System.out.println("No testing on Motif. Test passed.");
6097N/A return;
6097N/A }
6097N/A
6097N/A toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
6097N/A robot = new Robot();
6097N/A robot.setAutoDelay(50);
6097N/A
6097N/A Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
6097N/A
6097N/A public void eventDispatched(AWTEvent e) {
6097N/A System.out.println(e);
6097N/A }
6097N/A }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
6097N/A
6097N/A createAndShowWindow();
6097N/A toolkit.realSync();
6097N/A
6097N/A createAndShowFrame();
6097N/A toolkit.realSync();
6097N/A
6097N/A // click on Frame
6097N/A clickOn(getClickPoint(frame));
6097N/A
6097N/A if (!frame.isFocused()) {
6097N/A throw new RuntimeException("Error: a frame couldn't be focused by click.");
6097N/A }
6097N/A
6097N/A //click on Label in Window
6097N/A clickOn(getClickPoint(label));
6097N/A
6097N/A if (!window.isFocused()) {
6097N/A throw new RuntimeException("Test failed: the window couldn't be activated by click!");
6097N/A }
6097N/A
6097N/A // bring focus back to the frame
6097N/A clickOn(getClickPoint(fbutton));
6097N/A
6097N/A if (!frame.isFocused()) {
6097N/A throw new RuntimeException("Error: a frame couldn't be focused by click.");
6097N/A }
6097N/A
6097N/A // Test 2. Verifies that clicking on a component of unfocusable Window
6097N/A // won't activate it.
6097N/A
6097N/A window.setFocusableWindowState(false);
6097N/A toolkit.realSync();
6097N/A
6097N/A
6097N/A clickOn(getClickPoint(label));
6097N/A
6097N/A if (window.isFocused()) {
6097N/A throw new RuntimeException("Test failed: unfocusable window got activated by click!");
6097N/A }
6097N/A System.out.println("Test passed.");
6097N/A
6097N/A }
6097N/A
6097N/A private static void createAndShowWindow() {
6097N/A
6097N/A frame = new Frame("Test Frame");
6097N/A window = new Window(frame);
6097N/A wbutton = new Button("wbutton");
6097N/A label = new Label("label");
6097N/A
6097N/A window.setBounds(800, 200, 300, 100);
6097N/A window.setLayout(new FlowLayout());
6097N/A window.add(wbutton);
6097N/A window.add(label);
6097N/A window.setVisible(true);
6097N/A
6097N/A }
6097N/A
6097N/A private static void createAndShowFrame() {
6097N/A fbutton = new Button("fbutton");
6097N/A
6097N/A frame.setBounds(800, 0, 300, 100);
6097N/A frame.setLayout(new FlowLayout());
6097N/A frame.add(fbutton);
6097N/A frame.setVisible(true);
6097N/A
6097N/A }
6097N/A
6097N/A static void clickOn(Point point) {
6097N/A
6097N/A robot.mouseMove(point.x, point.y);
6097N/A
6097N/A robot.mousePress(InputEvent.BUTTON1_MASK);
6097N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
6097N/A
6097N/A toolkit.realSync();
6097N/A }
6097N/A
6097N/A static Point getClickPoint(Component c) {
6097N/A Point p = c.getLocationOnScreen();
6097N/A Dimension d = c.getSize();
6097N/A return new Point(p.x + (int) (d.getWidth() / 2), p.y + (int) (d.getHeight() / 2));
6097N/A }
6097N/A
6097N/A static Point getClickPoint(Frame frame) {
6097N/A Point p = frame.getLocationOnScreen();
6097N/A Dimension d = frame.getSize();
6097N/A return new Point(p.x + (int) (d.getWidth() / 2), p.y + (frame.getInsets().top / 2));
6097N/A }
6097N/A}