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 import java.awt.*;
  25 import java.awt.event.KeyEvent;
  26 
  27 import static jdk.testlibrary.Asserts.*;
  28 
  29 
  30 /*
  31  * @test
  32  * @key headful
  33  * @bug 8047367
  34  * @summary Check whether a Dialog set with null modality type
  35  *          behaves like a modeless dialog
  36  *
  37  * @library ../helpers ../../../../lib/testlibrary/
  38  * @build ExtendedRobot
  39  * @build Flag
  40  * @build TestDialog
  41  * @build TestFrame
  42  * @build TestWindow
  43  * @run main NullModalityDialogTest
  44  */
  45 
  46 
  47 public class NullModalityDialogTest {
  48 
  49     class CustomDialog extends TestDialog {
  50         public CustomDialog(Frame f) {
  51             super(f);
  52         }
  53         @Override
  54         public void doOpenAction() {
  55             if (frame != null) {
  56                 frame.setVisible(true);
  57             }
  58             if (window != null) {
  59                 window.setVisible(true);
  60             }
  61         }
  62     }
  63 
  64     class CustomFrame extends TestFrame {
  65         @Override
  66         public void doOpenAction() {
  67             if (dialog != null) {
  68                 dialog.setVisible(true);
  69             }
  70         }
  71     }
  72 
  73     private TestFrame  parent;
  74     private TestDialog dialog;
  75     private TestFrame  frame;
  76     private TestWindow window;
  77 
  78     private static final int delay = 1000;
  79 
  80     private final ExtendedRobot robot;
  81 
  82     NullModalityDialogTest() throws Exception {
  83 
  84         robot = new ExtendedRobot();
  85         EventQueue.invokeLater(this::createGUI);
  86     }
  87 
  88     private void createGUI() {
  89 
  90         parent = new CustomFrame();
  91         parent.setTitle("Parent");
  92         parent.setLocation(50, 50);
  93 
  94         dialog = new CustomDialog(parent);
  95         dialog.setTitle("Dialog");
  96         dialog.setModalityType((Dialog.ModalityType) null);
  97         dialog.setLocation(250, 50);
  98 
  99         frame = new TestFrame();
 100         frame.setTitle("Frame");
 101         frame.setLocation(50, 250);
 102 
 103         window = new TestWindow(frame);
 104         window.setLocation(250, 250);
 105 
 106         parent.setVisible(true);
 107     }
 108 
 109     private void closeAll() {
 110         if (parent != null) { parent.dispose(); }
 111         if (dialog != null) { dialog.dispose(); }
 112         if (frame  != null) {  frame.dispose(); }
 113         if (window != null) { window.dispose(); }
 114     }
 115 
 116     public void doTest() throws Exception {
 117 
 118         robot.waitForIdle(delay);
 119 
 120         parent.clickOpenButton(robot);
 121         robot.waitForIdle(delay);
 122 
 123         dialog.activated.waitForFlagTriggered();
 124         assertTrue(dialog.activated.flag(), "Dialog did not trigger " +
 125                 "Window Activated event when it became visible");
 126 
 127         dialog.closeGained.waitForFlagTriggered();
 128         assertTrue(dialog.closeGained.flag(), "the 1st button did not gain focus " +
 129             "when the Dialog became visible");
 130 
 131         assertTrue(dialog.closeButton.hasFocus(), "the 1st button in the Dialog " +
 132             "gained focus but lost it afterwards");
 133 
 134         dialog.openGained.reset();
 135 
 136         robot.type(KeyEvent.VK_TAB);
 137 
 138         dialog.openGained.waitForFlagTriggered();
 139         assertTrue(dialog.openGained.flag(),
 140             "Tab navigation did not happen properly on Dialog. Open button " +
 141             "did not gain focus on tab press when parent frame is visible");
 142 
 143         dialog.clickOpenButton(robot);
 144         robot.waitForIdle(delay);
 145 
 146         frame.activated.waitForFlagTriggered();
 147         assertTrue(frame.activated.flag(), "Frame did not trigger activated when " +
 148             "made visible. Dialog and its parent frame are visible");
 149 
 150         frame.checkUnblockedFrame(robot, "Frame is the parent of a visible Dialog.");
 151         window.checkUnblockedWindow(robot, "Frame and its child Dialog are visible.");
 152 
 153         robot.waitForIdle(delay);
 154 
 155         EventQueue.invokeAndWait(this::closeAll);
 156     }
 157 
 158     public static void main(String[] args) throws Exception {
 159         NullModalityDialogTest test = new NullModalityDialogTest();
 160         test.doTest();
 161     }
 162 }