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