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