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