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