1 /*
   2  * Copyright (c) 2007, 2014, 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 import java.awt.*;
  25 import java.awt.event.*;
  26 
  27 import static jdk.testlibrary.Asserts.*;
  28 
  29 
  30 
  31 public class TestWindow extends Window implements ActionListener,
  32     FocusListener, WindowFocusListener, WindowListener {
  33 
  34     public Button closeButton, openButton, dummyButton;
  35 
  36     public Flag closeClicked, openClicked, dummyClicked;
  37     public Flag closeGained,  openGained,  dummyGained;
  38     public Flag closeLost,    openLost,    dummyLost;
  39     public Flag focusGained, focusLost;
  40     public Flag activated;
  41 
  42     public static int delay = 500;
  43     public static int keyDelay = 100;
  44 
  45     public TestWindow(Frame owner) {
  46         super(owner);
  47         initializeGUI();
  48     }
  49 
  50     public TestWindow(Window window) {
  51         super(window);
  52         initializeGUI();
  53     }
  54 
  55     public void resetStatus() {
  56         activated.reset();
  57         focusGained.reset();
  58         closeGained.reset();
  59         openGained.reset();
  60         closeClicked.reset();
  61         openClicked.reset();
  62     }
  63 
  64     private void initFlags() {
  65         closeClicked = new Flag();
  66         openClicked  = new Flag();
  67         dummyClicked = new Flag();
  68         closeGained  = new Flag();
  69         openGained   = new Flag();
  70         dummyGained  = new Flag();
  71         closeLost    = new Flag();
  72         openLost     = new Flag();
  73         dummyLost    = new Flag();
  74         focusGained  = new Flag();
  75         focusLost    = new Flag();
  76         activated    = new Flag();
  77     }
  78 
  79     private void initializeGUI() {
  80 
  81         initFlags();
  82 
  83         this.addWindowFocusListener(this);
  84         this.addWindowListener(this);
  85 
  86         this.setLayout(new GridLayout(3, 1));
  87 
  88         Panel topPanel;
  89         topPanel = new Panel();
  90         topPanel.setFocusable(false);
  91         this.add(topPanel);
  92 
  93         Panel p = new Panel();
  94         p.setLayout(new GridLayout(1, 3));
  95 
  96         closeButton = new Button("Close");
  97         closeButton.addActionListener(this);
  98         closeButton.addFocusListener(this);
  99 
 100         openButton = new Button("Open");
 101         openButton.addActionListener(this);
 102         openButton.addFocusListener(this);
 103 
 104         dummyButton = new Button("Dummy");
 105         dummyButton.addActionListener(this);
 106         dummyButton.addFocusListener(this);
 107 
 108         p.add(closeButton);
 109         p.add(openButton);
 110         p.add(dummyButton);
 111 
 112         this.add(p);
 113 
 114         Panel bottomPanel = new Panel();
 115         bottomPanel.setFocusable(false);
 116         this.add(bottomPanel);
 117 
 118         setSize(150, 150);
 119     }
 120 
 121     public void doOpenAction()  {}
 122     public void doCloseAction() {}
 123     public void doDummyAction() {}
 124 
 125     @Override
 126     public void actionPerformed(ActionEvent event) {
 127         if (closeButton.equals(event.getSource())) {
 128             closeClicked.flagTriggered();
 129             doCloseAction();
 130         } else if (openButton.equals(event.getSource())) {
 131             openClicked.flagTriggered();
 132             doOpenAction();
 133         } else if (dummyButton.equals(event.getSource())) {
 134             dummyClicked.flagTriggered();
 135             doDummyAction();
 136         }
 137     }
 138 
 139     @Override
 140     public void focusGained(FocusEvent event) {
 141         if (closeButton.equals(event.getSource())) {
 142             closeGained.flagTriggered();
 143         } else if (openButton.equals(event.getSource())) {
 144             openGained.flagTriggered();
 145         } else if (dummyButton.equals(event.getSource())) {
 146             dummyGained.flagTriggered();
 147         }
 148     }
 149 
 150     @Override
 151     public void focusLost(FocusEvent event) {
 152         if (closeButton.equals(event.getSource())) {
 153             closeLost.flagTriggered();
 154         } else if (openButton.equals(event.getSource())) {
 155             openLost.flagTriggered();
 156         } else if (dummyButton.equals(event.getSource())) {
 157             dummyLost.flagTriggered();
 158         }
 159     }
 160 
 161     @Override
 162     public void windowGainedFocus(WindowEvent event) {
 163         focusGained.flagTriggered();
 164     }
 165 
 166     @Override
 167     public void windowLostFocus(WindowEvent event) {
 168         focusLost.flagTriggered();
 169     }
 170 
 171     @Override
 172     public void windowActivated(WindowEvent e) {
 173         activated.flagTriggered();
 174     }
 175 
 176     @Override
 177     public void windowClosed(WindowEvent e) {}
 178 
 179     @Override
 180     public void windowClosing(WindowEvent e) {
 181         System.err.println("User closed window!");
 182         System.exit(1);
 183     }
 184 
 185     @Override
 186     public void windowDeactivated(WindowEvent e) {}
 187 
 188     @Override
 189     public void windowDeiconified(WindowEvent e) {}
 190 
 191     @Override
 192     public void windowIconified(WindowEvent e) {}
 193 
 194     @Override
 195     public void windowOpened(WindowEvent e) {}
 196 
 197     public void clickButton(Button b, ExtendedRobot robot) {
 198         try {
 199             Flag.waitTillShown(b);
 200         } catch (InterruptedException e) {}
 201 
 202         if ((closeButton.equals(b) || openButton.equals(b) ||
 203             dummyButton.equals(b)) && robot != null) {
 204             robot.mouseMove((int) b.getLocationOnScreen().x + b.getSize().width / 2,
 205                             (int) b.getLocationOnScreen().y + b.getSize().height / 2);
 206             robot.delay(delay);
 207             robot.click();
 208             robot.delay(delay);
 209         }
 210     }
 211 
 212     public void clickOpenButton(ExtendedRobot robot) throws Exception {
 213         openClicked.reset();
 214         clickButton(openButton, robot);
 215 
 216         openClicked.waitForFlagTriggered();
 217         assertTrue(openClicked.flag(),
 218             "clicking the window Open button did not trigger an action");
 219     }
 220 
 221     public void clickCloseButton(ExtendedRobot robot) throws Exception {
 222         closeClicked.reset();
 223         clickButton(closeButton, robot);
 224 
 225         closeClicked.waitForFlagTriggered();
 226         assertTrue(closeClicked.flag(),
 227             "clicking the window Close button did not trigger an action");
 228     }
 229 
 230     public void clickDummyButton(ExtendedRobot robot) throws Exception {
 231         dummyClicked.reset();
 232         clickButton(dummyButton, robot);
 233 
 234         dummyClicked.waitForFlagTriggered();
 235         assertTrue(dummyClicked.flag(),
 236             "clicking the window Dummy button did not trigger an action");
 237     }
 238 
 239     public void checkBlockedWindow(ExtendedRobot robot,
 240                                    String message) throws Exception {
 241         dummyGained.reset();
 242         dummyClicked.reset();
 243         focusGained.reset();
 244 
 245         clickButton(dummyButton, robot);
 246 
 247         robot.waitForIdle(delay);
 248 
 249         assertFalse(dummyClicked.flag(),
 250             "DummyButton on blocked Window triggered action when clicked. " + message);
 251 
 252         assertFalse(dummyGained.flag(),
 253             "DummyButton on blocked Window gained focus when clicked. " + message);
 254 
 255         assertFalse(focusGained.flag(),
 256             "A blocked window gained focus when component clicked. " + message);
 257     }
 258 
 259     public void checkUnblockedWindowWithBlockedParent(
 260             ExtendedRobot robot, String message) throws Exception {
 261 
 262         dummyGained.reset();
 263         dummyClicked.reset();
 264         clickButton(dummyButton, robot);
 265 
 266         dummyClicked.waitForFlagTriggered();
 267 
 268         assertTrue(dummyClicked.flag(),
 269             "DummyButton on Window did not trigger action when clicked. " + message);
 270 
 271         assertFalse(dummyGained.flag(),
 272             "DummyButton on Window gained focus " +
 273             "when its parent is non-focusable. "  + message);
 274     }
 275 
 276     public void checkUnblockedWindow(ExtendedRobot robot,
 277                                      String message) throws Exception {
 278         dummyGained.reset();
 279         dummyClicked.reset();
 280         clickButton(dummyButton, robot);
 281 
 282         dummyGained.waitForFlagTriggered();
 283         assertTrue(dummyGained.flag(),
 284             "DummyButton on Window did not gain focus on clicking. " + message);
 285 
 286         assertTrue(dummyClicked.flag(), 
 287             "DummyButton on Window did not trigger action on clicking. " + message);
 288 
 289         closeGained.reset();
 290         robot.type(KeyEvent.VK_TAB);
 291 
 292         closeGained.waitForFlagTriggered();
 293         assertTrue(closeGained.flag(),
 294             "Tab navigation did not happen properly on Window. First " +
 295             "button did not gain focus on tab press. " + message);
 296     }
 297 
 298     public void checkCloseButtonFocusGained() {
 299         checkCloseButtonFocusGained(Flag.ATTEMPTS);
 300     }
 301 
 302     public void checkCloseButtonFocusGained(int attempts) {
 303         try {
 304             closeGained.waitForFlagTriggered(attempts);
 305         } catch (InterruptedException e) {}
 306         assertTrue(closeGained.flag(),
 307             "window Close button did not gain focus");
 308     }
 309 
 310     public void checkOpenButtonFocusGained() {
 311         checkOpenButtonFocusGained(Flag.ATTEMPTS);
 312     }
 313 
 314     public void checkOpenButtonFocusGained(int attempts) {
 315         try {
 316             openGained.waitForFlagTriggered(attempts);
 317         } catch (InterruptedException e) {}
 318         assertTrue(openGained.flag(),
 319             "window Open button did not gain focus");
 320     }
 321 
 322     public void checkOpenButtonFocusLost() {
 323         checkOpenButtonFocusLost(Flag.ATTEMPTS);
 324     }
 325 
 326     public void checkOpenButtonFocusLost(int attempts) {
 327         try {
 328             openLost.waitForFlagTriggered(attempts);
 329         } catch (InterruptedException e) {}
 330         assertTrue(openLost.flag(),
 331             "window Open button did not lose focus");
 332     }
 333 }