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