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.KeyEvent;
  26 import static jdk.test.lib.Asserts.*;
  27 
  28 // DFW: Dialog -> Frame -> Window
  29 
  30 public class BlockingDFWTest {
  31 
  32     private static final int delay = 500;
  33     private final ExtendedRobot robot;
  34 
  35     public enum Parent {DIALOG, FRAME};
  36 
  37     private ParentDialog parentDialog;
  38     private ParentFrame  parentFrame;
  39     private TestDialog dialog;
  40     private TestFrame frame;
  41     private TestWindow window;
  42 
  43 
  44     public BlockingDFWTest(Parent parentWin, Dialog.ModalityType modalityType) throws Exception {
  45 
  46         robot = new ExtendedRobot();
  47         EventQueue.invokeLater(() -> { createGUI(parentWin, modalityType); });
  48     }
  49 
  50     private void createGUI(Parent parentWin, Dialog.ModalityType modalityType) {
  51 
  52         Window p = null;
  53         switch (parentWin) {
  54             case DIALOG:
  55                 parentDialog = new ParentDialog((Dialog) null);
  56                 dialog = new CustomDialog(parentDialog);
  57                 p = parentDialog;
  58                 break;
  59             case FRAME:
  60                 parentFrame = new ParentFrame();
  61                 dialog = new CustomDialog(parentFrame);
  62                 p = parentFrame;
  63                 break;
  64         }
  65 
  66         assertFalse(p == null, "invalid parent");
  67         p.setLocation(50, 50);
  68         dialog.setLocation(250, 50);
  69         if (modalityType != null) { dialog.setModalityType(modalityType); }
  70 
  71         frame = new TestFrame();
  72         frame.setLocation(50, 250);
  73         window = new TestWindow(frame);
  74         window.setLocation(250, 250);
  75 
  76         p.setVisible(true);
  77     }
  78 
  79     public void doTest() throws Exception {
  80 
  81         try {
  82 
  83             robot.waitForIdle(delay);
  84 
  85             if (parentDialog != null) { parentDialog.clickOpenButton(robot); }
  86             else if (parentFrame != null) { parentFrame.clickOpenButton(robot); }
  87             robot.waitForIdle(delay);
  88 
  89             dialog.activated.waitForFlagTriggered();
  90             assertTrue(dialog.activated.flag(), "Dialog did not trigger " +
  91                 "Window Activated event when it became visible");
  92 
  93             dialog.closeGained.waitForFlagTriggered();
  94             assertTrue(dialog.closeGained.flag(), "the 1st button did not gain " +
  95                 "focus when the dialog became visible");
  96 
  97             assertTrue(dialog.closeButton.hasFocus(), "the 1st dialog button " +
  98                 "gained focus, but lost it afterwards");
  99 
 100             dialog.openGained.reset();
 101             robot.type(KeyEvent.VK_TAB);
 102 
 103             dialog.openGained.waitForFlagTriggered();
 104             assertTrue(dialog.openGained.flag(), "Tab navigation did not happen " +
 105                 "properly; open button did not gain focus on tab press " +
 106                 "when parent frame is visible");
 107 
 108             dialog.clickOpenButton(robot);
 109             robot.waitForIdle(delay);
 110 
 111             frame.activated.waitForFlagTriggered();
 112             assertTrue(frame.activated.flag(), "Frame did not trigger " +
 113                 "Window Activated event when made visible.");
 114 
 115             frame.checkUnblockedFrame(robot, "Frame should not be blocked.");
 116             window.checkUnblockedWindow(robot, "Window should not be blocked.");
 117             robot.waitForIdle(delay);
 118 
 119         } finally {
 120             EventQueue.invokeAndWait(this::closeAll);
 121         }
 122     }
 123 
 124     private void closeAll() {
 125         if (dialog != null) { dialog.dispose(); }
 126         if ( frame != null) {  frame.dispose(); }
 127         if (window != null) { window.dispose(); }
 128         if (parentDialog != null) { parentDialog.dispose(); }
 129         if (parentFrame  != null) {  parentFrame.dispose(); }
 130     }
 131 
 132 
 133     class ParentDialog extends TestDialog {
 134 
 135         public ParentDialog(Dialog d) { super(d); }
 136 
 137         @Override
 138         public void doOpenAction() {
 139             if (dialog != null) { dialog.setVisible(true); }
 140         }
 141     }
 142 
 143     class ParentFrame extends TestFrame {
 144 
 145         @Override
 146         public void doOpenAction() {
 147             if (dialog != null) { dialog.setVisible(true); }
 148         }
 149     }
 150 
 151     class CustomDialog extends TestDialog {
 152 
 153         public CustomDialog(Dialog d) { super(d); }
 154         public CustomDialog(Frame  f) { super(f); }
 155 
 156         @Override
 157         public void doOpenAction() {
 158             if (frame  != null) {  frame.setVisible(true); }
 159             if (window != null) { window.setVisible(true); }
 160         }
 161     }
 162 }