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