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