jdk/test/java/awt/Focus/FocusOwnerFrameOnClick/FocusOwnerFrameOnClick.java
author serb
Tue, 12 Nov 2013 20:24:25 +0400
changeset 21596 0e3a39f29dbc
parent 6635 74a29718cc9e
child 23010 6dadb192ad81
permissions -rw-r--r--
8027696: Incorrect copyright header in the tests Reviewed-by: alanb, malenkov, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6635
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     1
/*
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     2
 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     4
 *
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 6635
diff changeset
     7
 * published by the Free Software Foundation.
6635
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     8
 *
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    13
 * accompanied this code).
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    14
 *
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    18
 *
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    21
 * questions.
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    22
 */
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    23
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    24
/*
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    25
  @test      FocusOwnerFrameOnClick.java %W% %E%
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    26
  @bug       6886678
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    27
  @summary   Tests that clicking an owner frame switches focus from its owned window.
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    28
  @author    Anton Tarasov: area=awt.focus
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    29
  @library   ../../regtesthelpers
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    30
  @build     Util
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    31
  @run       main FocusOwnerFrameOnClick
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    32
*/
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    33
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    34
import java.awt.*;
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    35
import java.awt.event.*;
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    36
import java.applet.Applet;
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    37
import java.util.concurrent.atomic.AtomicBoolean;
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    38
import java.lang.reflect.InvocationTargetException;
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    39
import test.java.awt.regtesthelpers.Util;
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    40
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    41
public class FocusOwnerFrameOnClick extends Applet {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    42
    Robot robot;
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    43
    Frame frame = new Frame("Frame");
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    44
    Window window = new Window(frame);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    45
    Button fButton = new Button("fButton");
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    46
    Button wButton = new Button("wButton");
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    47
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    48
    AtomicBoolean focused = new AtomicBoolean(false);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    49
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    50
    public static void main(String[] args) {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    51
        FocusOwnerFrameOnClick app = new FocusOwnerFrameOnClick();
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    52
        app.init();
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    53
        app.start();
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    54
    }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    55
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    56
    public void init() {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    57
        robot = Util.createRobot();
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    58
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    59
        frame.setLayout(new FlowLayout());
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    60
        frame.setSize(200, 200);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    61
        frame.add(fButton);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    62
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    63
        window.setLocation(300, 0);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    64
        window.add(wButton);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    65
        window.pack();
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    66
    }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    67
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    68
    public void start() {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    69
        frame.setVisible(true);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    70
        Util.waitForIdle(robot);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    71
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    72
        window.setVisible(true);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    73
        Util.waitForIdle(robot);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    74
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    75
        if (!wButton.hasFocus()) {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    76
            if (!Util.trackFocusGained(wButton, new Runnable() {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    77
                    public void run() {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    78
                        Util.clickOnComp(wButton, robot);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    79
                    }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    80
                }, 2000, false))
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    81
            {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    82
                throw new TestErrorException("wButton didn't gain focus on showing");
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    83
            }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    84
        }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    85
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    86
        Runnable clickAction = new Runnable() {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    87
                public void run() {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    88
                    Point loc = fButton.getLocationOnScreen();
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    89
                    Dimension dim = fButton.getSize();
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    90
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    91
                    robot.mouseMove(loc.x, loc.y + dim.height + 20);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    92
                    robot.delay(50);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    93
                    robot.mousePress(InputEvent.BUTTON1_MASK);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    94
                    robot.delay(50);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    95
                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    96
                }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    97
            };
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    98
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
    99
        if (!Util.trackWindowGainedFocus(frame, clickAction, 2000, true)) {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   100
            throw new TestFailedException("The frame wasn't focused on click");
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   101
        }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   102
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   103
        System.out.println("Test passed.");
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   104
    }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   105
}
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   106
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   107
/**
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   108
 * Thrown when the behavior being verified is found wrong.
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   109
 */
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   110
class TestFailedException extends RuntimeException {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   111
    TestFailedException(String msg) {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   112
        super("Test failed: " + msg);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   113
    }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   114
}
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   115
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   116
/**
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   117
 * Thrown when an error not related to the behavior being verified is encountered.
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   118
 */
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   119
class TestErrorException extends RuntimeException {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   120
    TestErrorException(String msg) {
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   121
        super("Unexpected error: " + msg);
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   122
    }
74a29718cc9e 6886678: Clicking on parent JFrame's client area does not switch focus from JWindow to JFrame on Windows
ant
parents:
diff changeset
   123
}