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