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  * @test
  26  * @key headful
  27  * @bug 8054358
  28  * @summary This is a simple check if a chain of dialogs having different
  29  *          modality types block each other properly.
  30  *
  31  * @library ../helpers /lib/testlibrary/
  32  * @library /test/lib
  33  * @build ExtendedRobot
  34  * @build Flag
  35  * @build TestDialog
  36  * @build TestFrame
  37  * @build TestWindow
  38  * @run main MultipleDialogs5Test
  39  */
  40 
  41 
  42 import java.awt.*;
  43 import static jdk.test.lib.Asserts.*;
  44 
  45 
  46 public class MultipleDialogs5Test {
  47 
  48     private volatile ParentFrame parent;
  49     private volatile ModalDialog appDialog, docDialog, tkDialog;
  50     private volatile CustomWindow window;
  51 
  52     private static int delay = 500;
  53 
  54     private void createGUI() {
  55 
  56         parent = new ParentFrame();
  57         parent.setLocation(50, 50);
  58         parent.setVisible(true);
  59 
  60         appDialog = new ModalDialog(parent);
  61         appDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
  62         appDialog.setLocation(250, 50);
  63 
  64         docDialog = new ModalDialog(appDialog);
  65         docDialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  66         docDialog.setLocation(450, 50);
  67 
  68         window = new CustomWindow(docDialog);
  69         window.setLocation(50, 250);
  70 
  71         tkDialog = new ModalDialog(docDialog);
  72         tkDialog.setLocation(250, 250);
  73         tkDialog.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
  74 
  75         appDialog.setWindowToOpen(docDialog);
  76         docDialog.setWindowToOpen(window);
  77     }
  78 
  79     public void doTest() throws Exception {
  80 
  81          try {
  82             EventQueue.invokeAndWait(this::createGUI);
  83 
  84             ExtendedRobot robot = new ExtendedRobot();
  85             robot.waitForIdle(delay);
  86 
  87             parent.clickOpenButton(robot);
  88             robot.waitForIdle(delay);
  89 
  90             parent.checkBlockedFrame(robot,
  91                 "This Frame is blocked by an application modal dialog.");
  92 
  93             appDialog.clickOpenButton(robot);
  94             robot.waitForIdle(delay);
  95 
  96             appDialog.checkBlockedDialog(robot,
  97                 "This Dialog is blocked by a document modal dialog.");
  98 
  99             docDialog.clickOpenButton(robot);
 100             robot.waitForIdle(delay);
 101 
 102             docDialog.checkUnblockedDialog(robot,
 103                 "This Dialog is not blocked by any modal dialog.");
 104 
 105             window.clickOpenButton(robot);
 106             robot.waitForIdle(delay);
 107 
 108             window.checkBlockedWindow(robot,
 109                 "This Window is blocked by a toolkit modal dialog.");
 110 
 111             tkDialog.clickCloseButton(robot);
 112             robot.waitForIdle(delay);
 113             assertFalse(tkDialog.isVisible(),
 114                 "The toolkit modal dialog was not disposed.");
 115 
 116             window.clickCloseButton(robot);
 117             robot.waitForIdle(delay);
 118             assertFalse(window.isVisible(),
 119                 "The window was not disposed.");
 120 
 121             docDialog.clickCloseButton(robot);
 122             robot.waitForIdle(delay);
 123             assertFalse(docDialog.isVisible(),
 124                 "The document modal dialog was not disposed.");
 125 
 126             appDialog.clickCloseButton(robot);
 127             robot.waitForIdle(delay);
 128             assertFalse(appDialog.isVisible(),
 129                 "The application modal dialog was not disposed.");
 130         } finally {
 131             EventQueue.invokeAndWait(this::closeAll); // if something wasn't closed
 132         }
 133     }
 134 
 135     private void closeAll() {
 136 
 137         if (appDialog != null) { appDialog.dispose(); }
 138         if (tkDialog  != null) {  tkDialog.dispose(); }
 139         if (docDialog != null) { docDialog.dispose(); }
 140         if (parent != null) { parent.dispose(); }
 141         if (window != null) { window.dispose(); }
 142     }
 143 
 144     private class ParentFrame extends TestFrame {
 145 
 146         @Override
 147         public void doOpenAction() {
 148             if (appDialog != null) { appDialog.setVisible(true); }
 149         }
 150     }
 151 
 152     private class CustomWindow extends TestWindow {
 153 
 154         public CustomWindow(Dialog d) { super(d); }
 155 
 156         @Override
 157         public void doOpenAction() {
 158             if (tkDialog != null) { tkDialog.setVisible(true); }
 159         }
 160 
 161         @Override
 162         public void doCloseAction() { this.dispose(); }
 163     }
 164 
 165     private class ModalDialog extends TestDialog {
 166 
 167         private Window w;
 168 
 169         public ModalDialog(Frame  f) { super(f); }
 170         public ModalDialog(Dialog d) { super(d); }
 171 
 172         public void setWindowToOpen(Window w) { this.w = w; }
 173 
 174         @Override
 175         public void doCloseAction() { this.dispose(); }
 176 
 177         @Override
 178         public void doOpenAction() {
 179             if (w != null) { w.setVisible(true); }
 180         }
 181     }
 182 
 183     public static void main(String[] args) throws Exception {
 184         (new MultipleDialogs5Test()).doTest();
 185     }
 186 }