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