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 static jdk.testlibrary.Asserts.*;
  27 
  28 
  29 /*
  30  * @test
  31  * @bug 8049617
  32  * @summary Test if a document modality works as expected:
  33  *          whether all the windows lying down the document root
  34  *          (Frame) get blocked.
  35  *
  36  * @library ../helpers ../../../../lib/testlibrary/
  37  * @build ExtendedRobot
  38  * @build Flag
  39  * @build TestDialog
  40  * @build TestFrame
  41  * @build TestWindow
  42  * @run main BlockingDocModalTest
  43  */
  44 
  45 
  46 public class BlockingDocModalTest {
  47 
  48     private static final int delay = 500;
  49     private final ExtendedRobot robot;
  50 
  51     private TestDialog dialog, childDialog;
  52     private TestFrame  frame;
  53     private TestWindow window;
  54 
  55 
  56     public BlockingDocModalTest() throws Exception {
  57 
  58         robot = new ExtendedRobot();
  59         EventQueue.invokeLater(this::createGUI);
  60     }
  61 
  62     private void createGUI() {
  63 
  64         frame = new CustomFrame();
  65         frame.setLocation(50, 50);
  66         frame.setVisible(true);
  67 
  68         dialog = new TestDialog(frame);
  69         dialog.setLocation(250, 250);
  70         dialog.setVisible(true);
  71 
  72         childDialog = new CustomDialog(dialog);
  73         childDialog.setLocation(250, 50);
  74         childDialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  75 
  76         window = new TestWindow(frame);
  77         window.setLocation(50, 250);
  78     }
  79 
  80     public void doTest() throws Exception {
  81 
  82         try {
  83 
  84             robot.waitForIdle(delay);
  85 
  86             frame.clickOpenButton(robot);
  87             robot.waitForIdle(delay);
  88 
  89             childDialog.activated.waitForFlagTriggered();
  90             assertTrue(childDialog.activated.flag(), "Dialog did not trigger " +
  91                 "Window Activated event when it became visible");
  92 
  93             childDialog.closeGained.waitForFlagTriggered();
  94             assertTrue(childDialog.closeGained.flag(), "the 1st button did not " +
  95                 "gain focus when the Dialog became visible");
  96 
  97             assertTrue(childDialog.closeButton.hasFocus(), "the 1st dialog button " +
  98                 "gained focus but lost it afterwards");
  99 
 100             frame.checkBlockedFrame(robot, "A document modal Dialog from " +
 101                 "this Frame's child hierarchy should block this frame");
 102 
 103             childDialog.checkUnblockedDialog(robot,
 104                 "This is a document modal childDialog.");
 105 
 106             childDialog.clickOpenButton(robot);
 107             robot.waitForIdle(delay);
 108 
 109             window.checkBlockedWindow(robot,
 110                 "A document modal dialog having a parent belonging " +
 111                 "to this Window's document hierarchy is displayed.");
 112 
 113             dialog.checkBlockedDialog(robot,
 114                 "A document modal child dialog should block this Dialog.");
 115 
 116             robot.waitForIdle(delay);
 117 
 118         } finally {
 119             EventQueue.invokeAndWait(this::closeAll);
 120         }
 121     }
 122 
 123     private void closeAll() {
 124         if (dialog != null) { dialog.dispose(); }
 125         if ( frame != null) {  frame.dispose(); }
 126         if (window != null) { window.dispose(); }
 127         if (childDialog != null) { childDialog.dispose(); }
 128     }
 129 
 130 
 131     class CustomFrame extends TestFrame {
 132 
 133         @Override
 134         public void doOpenAction() {
 135             if (childDialog != null) { childDialog.setVisible(true); }
 136         }
 137     }
 138 
 139     class CustomDialog extends TestDialog {
 140 
 141         public CustomDialog(Dialog d) { super(d); }
 142 
 143         @Override
 144         public void doOpenAction() {
 145             if (window != null) { window.setVisible(true); }
 146         }
 147     }
 148 
 149 
 150     public static void main(String[] args) throws Exception {
 151         (new BlockingDocModalTest()).doTest();
 152     }
 153 }