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 8055003
  27  * @summary Check whether application and document modality levels for Dialog
  28  *          work properly. Also check whether the blocking dialogs are
  29  *          opening on top of the windows they block.
  30  *
  31  * @library ../helpers ../../../../lib/testlibrary/
  32  * @build ExtendedRobot
  33  * @build Flag
  34  * @build TestDialog
  35  * @build TestFrame
  36  * @run main MultipleDialogs4Test
  37  */
  38 
  39 import java.awt.*;
  40 import static jdk.testlibrary.Asserts.*;
  41 
  42 
  43 public class MultipleDialogs4Test {
  44 
  45     private volatile TopLevelFrame frame;
  46     private volatile CustomFrame   secondFrame;
  47     private volatile TestFrame     thirdFrame;
  48     private volatile TestDialog    dialog, secondDialog, docChildDialog, appChildDialog;
  49 
  50     private static final int delay = 500;
  51 
  52 
  53     private void createGUI() {
  54 
  55         frame = new TopLevelFrame();
  56         frame.setLocation(50, 50);
  57         frame.setVisible(true);
  58 
  59         dialog = new TestDialog((Dialog) null);
  60         dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
  61         dialog.setLocation(150, 50);
  62 
  63         appChildDialog = new TestDialog(dialog);
  64         appChildDialog.setLocation(150, 90);
  65 
  66         secondFrame = new CustomFrame();
  67         secondFrame.setLocation(50, 250);
  68 
  69         secondDialog = new TestDialog(secondFrame);
  70         secondDialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  71         secondDialog.setLocation(150, 250);
  72 
  73         docChildDialog = new TestDialog(secondFrame);
  74         docChildDialog.setBackground(Color.black);
  75         docChildDialog.setLocation(250, 250);
  76 
  77         thirdFrame = new TestFrame();
  78         thirdFrame.setLocation(250, 50);
  79     }
  80 
  81     public void doTest() throws Exception {
  82 
  83         try {
  84             EventQueue.invokeAndWait(this::createGUI);
  85 
  86             final int nAttempts = 3;
  87             ExtendedRobot robot = new ExtendedRobot();
  88             robot.waitForIdle(delay);
  89 
  90             frame.clickOpenButton(robot);
  91             robot.waitForIdle(delay);
  92 
  93             secondFrame.clickOpenButton(robot);
  94             robot.waitForIdle(delay);
  95 
  96             secondFrame.checkBlockedFrame(robot,
  97                 "A document modal dialog should block this Frame.");
  98 
  99             secondDialog.checkUnblockedDialog(robot, "This is a document " +
 100                 "modal dialog. No window blocks it.");
 101 
 102             frame.checkUnblockedFrame(robot,
 103                 "There are no dialogs blocking this Frame.");
 104 
 105             frame.clickCloseButton(robot);
 106             robot.waitForIdle(delay);
 107 
 108             frame.clickDummyButton(robot, nAttempts, false,
 109                 "The frame still on top even after showing a modal dialog.");
 110             robot.waitForIdle(delay);
 111 
 112             EventQueue.invokeAndWait(() -> { thirdFrame.setVisible(true); });
 113             robot.waitForIdle(delay);
 114 
 115             dialog.clickDummyButton(robot);
 116             robot.waitForIdle(delay);
 117 
 118             secondDialog.clickDummyButton(
 119                 robot, nAttempts, false, "The document modal dialog " +
 120                 "was not blocked by an application modal dialog.");
 121             robot.waitForIdle(delay);
 122 
 123             EventQueue.invokeLater(() -> { docChildDialog.setVisible(true); });
 124             robot.waitForIdle(delay);
 125 
 126             Color c = robot.getPixelColor(
 127                 (int) secondDialog.getLocationOnScreen().x + secondDialog.getSize().width  - 25,
 128                 (int) secondDialog.getLocationOnScreen().y + secondDialog.getSize().height - 25);
 129             assertFalse(c.equals(Color.black), "A dialog which should be blocked " +
 130                 "by document modal dialog overlapping it.");
 131 
 132             EventQueue.invokeLater(() -> { appChildDialog.setVisible(true); });
 133             robot.waitForIdle(delay);
 134 
 135             appChildDialog.activated.waitForFlagTriggered();
 136             assertTrue(appChildDialog.activated.flag(), "The child dialog of the " +
 137                 "application modal dialog still not visible.");
 138             robot.waitForIdle(delay);
 139 
 140             dialog.clickDummyButton(robot, nAttempts, false, "The child dialog of " +
 141                 "the application modal dialog did not overlap it.");
 142             robot.waitForIdle(delay);
 143 
 144         } finally {
 145             EventQueue.invokeAndWait(this::closeAll);
 146         }
 147     }
 148 
 149     public void closeAll() {
 150 
 151         if (frame != null) { frame.dispose(); }
 152         if (dialog != null) { dialog.dispose(); }
 153         if (appChildDialog != null) { appChildDialog.dispose(); }
 154         if (secondFrame != null) { secondFrame.dispose(); }
 155         if (secondDialog != null) { secondDialog.dispose(); }
 156         if (docChildDialog != null) { docChildDialog.dispose(); }
 157         if (thirdFrame != null) { thirdFrame.dispose(); }
 158     }
 159 
 160     class TopLevelFrame extends TestFrame {
 161 
 162         @Override
 163         public void doOpenAction() { secondFrame.setVisible(true); }
 164         @Override
 165         public void doCloseAction() { dialog.setVisible(true); }
 166     }
 167 
 168     class CustomFrame extends TestFrame {
 169 
 170         @Override
 171         public void doOpenAction() { secondDialog.setVisible(true); }
 172     }
 173 
 174     public static void main(String[] args) throws Exception {
 175         (new MultipleDialogs4Test()).doTest();
 176     }
 177 }