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