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