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 
  25 import java.awt.*;
  26 import java.awt.event.KeyEvent;
  27 import static jdk.testlibrary.Asserts.*;
  28 
  29 import java.util.List;
  30 import java.util.ArrayList;
  31 
  32 public class BlockingWindowsDocModalTest {
  33 
  34     private ParentDialog parentDialog;
  35     private ParentFrame  parentFrame;
  36     private CustomDialog dialog;
  37     private TestDialog secondDialog, childDialog;
  38     private TestFrame  secondFrame;
  39     private TestWindow window, childWindow, secondWindow;
  40 
  41 
  42     private static final int delay = 500;
  43     private final ExtendedRobot robot;
  44 
  45     private List<Window> allWindows;
  46 
  47     public enum Parent {DIALOG, FRAME};
  48     private Parent root;
  49 
  50     public BlockingWindowsDocModalTest(Parent p) throws Exception {
  51 
  52         root = p;
  53 
  54         robot = new ExtendedRobot();
  55         EventQueue.invokeLater(this::createGUI);
  56     }
  57 
  58     private void createGUI() {
  59 
  60         allWindows = new ArrayList<>();
  61 
  62         switch (root) {
  63             case DIALOG:
  64                 parentDialog = new ParentDialog((Dialog) null);
  65                 parentDialog.setLocation(50, 50);
  66                 parentDialog.setVisible(true);
  67                 allWindows.add(parentDialog);
  68 
  69                 dialog = new CustomDialog(parentDialog);
  70                 secondDialog = new TestDialog(parentDialog);
  71                 window = new TestWindow(parentDialog);
  72                 break;
  73             case FRAME:
  74                 parentFrame = new ParentFrame();
  75                 parentFrame.setLocation(50, 50);
  76                 parentFrame.setVisible(true);
  77                 allWindows.add(parentFrame);
  78 
  79                 dialog = new CustomDialog(parentFrame);
  80                 secondDialog = new TestDialog(parentFrame);
  81                 window = new TestWindow(parentFrame);
  82                 break;
  83         }
  84 
  85         allWindows.add(dialog);
  86         allWindows.add(secondDialog);
  87         allWindows.add(window);
  88 
  89         dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  90         dialog.setLocation(250, 50);
  91         window.setLocation(450, 50);
  92         secondDialog.setLocation(450, 250);
  93 
  94         secondFrame = new TestFrame();
  95         allWindows.add(secondFrame);
  96         secondFrame.setLocation(50, 250);
  97 
  98         secondWindow = new TestWindow(secondFrame);
  99         allWindows.add(secondWindow);
 100         secondWindow.setLocation(250, 250);
 101 
 102         childDialog = new TestDialog(dialog);
 103         allWindows.add(childDialog);
 104         childDialog.setLocation(250, 450);
 105 
 106         childWindow = new TestWindow(dialog);
 107         allWindows.add(childWindow);
 108         childWindow.setLocation(50, 450);
 109     }
 110 
 111     public void doTest() throws Exception {
 112 
 113         try {
 114             robot.waitForIdle(delay);
 115 
 116             if (root == Parent.DIALOG) {
 117                 parentDialog.clickOpenButton(robot);
 118             } else { //Parent.FRAME
 119                 parentFrame.clickOpenButton(robot);
 120             }
 121             robot.waitForIdle(delay);
 122 
 123             dialog.activated.waitForFlagTriggered();
 124             assertTrue(dialog.activated.flag(), "Dialog did not trigger " +
 125                 "Window Acivated event when it became visible");
 126 
 127             dialog.closeGained.waitForFlagTriggered();
 128             assertTrue(dialog.closeGained.flag(),
 129                 "the 1st Dialog button didn't gain focus");
 130 
 131             assertTrue(dialog.closeButton.hasFocus(), "the 1st Dialog button " +
 132                 "gained focus but lost it afterwards");
 133 
 134             dialog.openGained.reset();
 135             robot.type(KeyEvent.VK_TAB);
 136 
 137             dialog.openGained.waitForFlagTriggered();
 138             assertTrue(dialog.openGained.flag(), "Tab navigation did not happen properly on Dialog; " +
 139                 "Open button did not gain focus on tab press when parent frame is visible");
 140 
 141             dialog.clickOpenButton(robot);
 142             robot.waitForIdle(delay);
 143 
 144             secondFrame.checkUnblockedFrame(robot,
 145                 "A document modal dialog and its parent are visible.");
 146             secondWindow.checkUnblockedWindow(robot,
 147                 "A Frame and a document modal Dialog are visible.");
 148 
 149             if (root == Parent.DIALOG) {
 150                 parentDialog.checkBlockedDialog(robot, "Dialog is a parent of a document modal dialog.");
 151             } else { //Parent.FRAME
 152                 parentFrame.checkBlockedFrame(robot, "Frame is a parent of a document modal dialog.");
 153             }
 154 
 155             secondDialog.checkBlockedDialog(robot,
 156                 "The parent of the Dialog is also the parent of a document modal dialog");
 157             window.checkBlockedWindow(robot,
 158                 "The parent of the Window is also the parent of a document modal dialog");
 159 
 160             childWindow.checkUnblockedWindow(robot,
 161                 "The parent of the Window is a document modal dialog");
 162             childDialog.checkUnblockedDialog(robot,
 163                 "The parent of the Dialog is a document modal dialog");
 164             robot.waitForIdle(delay);
 165 
 166         } finally {
 167             EventQueue.invokeAndWait(this::closeAll);
 168         }
 169     }
 170 
 171     private void closeAll() {
 172         for (Window w: allWindows) {
 173             if (w != null) { w.dispose(); }
 174         }
 175     }
 176 
 177     class ParentDialog extends TestDialog {
 178 
 179         public ParentDialog(Dialog d) { super(d); }
 180 
 181         @Override
 182         public void doOpenAction() {
 183             if (dialog != null) { dialog.setVisible(true); }
 184         }
 185     }
 186 
 187     class ParentFrame extends TestFrame {
 188 
 189         @Override
 190         public void doOpenAction() {
 191             if (dialog != null) { dialog.setVisible(true); }
 192         }
 193 
 194     }
 195 
 196     class CustomDialog extends TestDialog {
 197 
 198         public CustomDialog(Dialog d) { super(d); }
 199         public CustomDialog(Frame f)  { super(f); }
 200 
 201         @Override
 202         public void doOpenAction() {
 203             if (secondFrame  != null) {  secondFrame.setVisible(true); }
 204             if (secondWindow != null) { secondWindow.setVisible(true); }
 205             if (secondDialog != null) { secondDialog.setVisible(true); }
 206             if (window != null) { window.setVisible(true); }
 207             if (childWindow != null) { childWindow.setVisible(true); }
 208             if (childDialog != null) { childDialog.setVisible(true); }
 209         }
 210     }
 211 }