1 /*
   2  * Copyright (c) 2008, 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
  26   @key headful
  27   @bug      4823903
  28   @summary  Tests actual focused window retaining.
  29   @library  ../../regtesthelpers
  30   @build    Util
  31   @run      main ActualFocusedWindowRetaining
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import test.java.awt.regtesthelpers.Util;
  37 
  38 public class ActualFocusedWindowRetaining {
  39     public static Frame frame = new Frame("Other Frame");
  40     public static Frame owner = new Frame("Test Frame");
  41     public static Button otherButton1 = new Button("Other Button 1");
  42     public static Button otherButton2 = new Button("Other Button 2");
  43     public static Button otherButton3 = new Button("Other Button 3");
  44     public static Button testButton1 = new Button("Test Button 1");
  45     public static Button testButton2 = new Button("Test Button 2");
  46     public static Button testButton3 = new Button("Test Button 3");
  47     public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200);
  48     public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300);
  49     public static int step;
  50     public static Robot robot = Util.createRobot();
  51 
  52     public static void main(String[] args) {
  53         ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining();
  54         a.start();
  55     }
  56 
  57     public void start () {
  58         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  59                 public void eventDispatched(AWTEvent e) {
  60                     Object src = e.getSource();
  61                     Class cls = src.getClass();
  62 
  63                     if (cls == TestWindow.class) {
  64                         System.out.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">");
  65                     } else if (cls == Frame.class) {
  66                         System.out.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">");
  67                     } else if (cls == Button.class) {
  68                         System.out.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">");
  69                     } else {
  70                         System.out.println(e.paramString() + " on <Non-testing component>");
  71                     }
  72                 }
  73             }, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
  74 
  75         frame.setSize(new Dimension(400, 100));
  76         frame.setLocation(800, 400);
  77         frame.setVisible(true);
  78         frame.toFront();
  79 
  80         owner.setLayout(new FlowLayout());
  81         owner.add(testButton1);
  82         owner.add(otherButton1);
  83         owner.pack();
  84         owner.setLocation(800, 100);
  85         owner.setSize(new Dimension(400, 100));
  86         owner.setVisible(true);
  87         owner.toFront();
  88         Util.waitTillShown(owner);
  89 
  90         window1.setVisible(true);
  91         window2.setVisible(true);
  92         window1.toFront();
  93         window2.toFront();
  94         // Wait longer...
  95         Util.waitTillShown(window1);
  96         Util.waitTillShown(window2);
  97 
  98         test();
  99 
 100         frame.dispose();
 101         owner.dispose();
 102     }
 103 
 104     public void test() {
 105         Button[] butArr = new Button[] {testButton3, testButton2, testButton1};
 106         Window[] winArr = new Window[] {window2, window1, owner};
 107 
 108         step = 1;
 109         for (int i = 0; i < 3; i++) {
 110             clickInSeriesCheckFocus(null, butArr[i], frame);
 111             clickOwnerCheckFocus(winArr[i], butArr[i]);
 112             step++;
 113         }
 114 
 115         step = 4;
 116         clickInSeriesCheckFocus(testButton3, testButton1, frame);
 117         clickOwnerCheckFocus(owner, testButton1);
 118 
 119         step = 5;
 120         clickInSeriesCheckFocus(testButton3, testButton2, frame);
 121         clickOwnerCheckFocus(window1, testButton2);
 122 
 123         step = 6;
 124         clickInSeriesCheckFocus(testButton1, testButton2, frame);
 125         clickOwnerCheckFocus(window1, testButton2);
 126 
 127         step = 7;
 128         clickInSeriesCheckFocus(testButton1, testButton2, frame);
 129         window1.setVisible(false);
 130         Util.waitForIdle(robot);
 131         clickOwnerCheckFocus(owner, testButton1);
 132 
 133         step = 8;
 134         window1.setVisible(true);
 135         Util.waitTillShown(window1);
 136         clickInSeriesCheckFocus(null, testButton2, frame);
 137         clickOwnerCheckFocus(window1, testButton2);
 138     }
 139 
 140     boolean checkFocusOwner(Component comp) {
 141         return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
 142     }
 143 
 144     boolean checkFocusedWindow(Window win) {
 145         return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow());
 146     }
 147 
 148     void clickOwnerCheckFocus(Window focusedWindow, Component focusedComp) {
 149         Util.clickOnTitle(owner, robot);
 150         robot.delay(500);
 151 
 152         if (!checkFocusedWindow(focusedWindow)) {
 153             stopTest("Test failed: actual focused window didn't get a focus");
 154         }
 155         if (!checkFocusOwner(focusedComp)) {
 156             stopTest("Test failed: actual focus owner didn't get a focus");
 157         }
 158     }
 159 
 160     void clickInSeriesCheckFocus(Component comp1, Component comp2, Frame frame) {
 161         if (comp1 != null) {
 162             clickOnCheckFocusOwner(comp1);
 163         }
 164         if (comp2 != null) {
 165             clickOnCheckFocusOwner(comp2);
 166         }
 167         clickOnCheckFocusedWindow(frame);
 168     }
 169 
 170     void clickOnCheckFocusOwner(Component c) {
 171         Util.clickOnComp(c, robot);
 172         robot.delay(500);
 173 
 174         if (!checkFocusOwner(c)) {
 175             stopTest("Error: can't bring a focus on Component by clicking on it");
 176         }
 177     }
 178 
 179     void clickOnCheckFocusedWindow(Frame f) {
 180         Util.clickOnTitle(f, robot);
 181         robot.delay(500);
 182 
 183         if (!checkFocusedWindow(f)) {
 184             stopTest("Error: can't bring a focus on Frame by clicking on it");
 185         }
 186     }
 187 
 188     void stopTest(String msg) {
 189         throw new RuntimeException(new String("Step " + step + ": " + msg));
 190     }
 191 }
 192 
 193 class TestWindow extends Window {
 194     TestWindow(Frame owner, Button otherButton, Button testButton, int x, int y) {
 195         super(owner);
 196 
 197         setLayout(new FlowLayout());
 198         setLocation(x, y);
 199         add(testButton);
 200         add(otherButton);
 201         pack();
 202         setBackground(Color.green);
 203     }
 204 }