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 import java.awt.*;
  26 import static jdk.testlibrary.Asserts.*;
  27 
  28 
  29 // FDW: Frame -> Dialog -> Window
  30 
  31 public class BlockingFDWTest {
  32 
  33     private TestFrame  frame;
  34     private TestDialog dialog;
  35     private TestWindow window;
  36 
  37     private Dialog hiddenDialog;
  38     private Frame  hiddenFrame;
  39 
  40     private static final int delay = 500;
  41     private final ExtendedRobot robot;
  42 
  43     public enum DialogOwner {HIDDEN_DIALOG, NULL_DIALOG, HIDDEN_FRAME, NULL_FRAME};
  44 
  45     public BlockingFDWTest(Dialog.ModalityType modalityType,
  46                            DialogOwner         owner) throws Exception {
  47 
  48         robot = new ExtendedRobot();
  49         EventQueue.invokeLater(() -> { createGUI(modalityType, owner); });
  50     }
  51 
  52     private void createGUI(Dialog.ModalityType modalityType,
  53                            DialogOwner         owner) {
  54 
  55         frame = new CustomFrame();
  56         frame.setLocation(50, 50);
  57 
  58         switch (owner) {
  59             case HIDDEN_DIALOG:
  60                 hiddenDialog = new Dialog((Frame) null);
  61                 dialog = new CustomDialog(hiddenDialog);
  62                 break;
  63             case NULL_DIALOG:
  64                 dialog = new CustomDialog((Dialog) null);
  65                 break;
  66             case HIDDEN_FRAME:
  67                 hiddenFrame = new Frame();
  68                 dialog = new CustomDialog(hiddenFrame);
  69                 break;
  70             case NULL_FRAME:
  71                 dialog = new CustomDialog((Frame) null);
  72                 break;
  73         }
  74 
  75         assertFalse(dialog == null, "error: null dialog");
  76 
  77         dialog.setLocation(250, 50);
  78         if (modalityType != null) {
  79             dialog.setModalityType(modalityType);
  80         }
  81 
  82         window = new TestWindow(frame);
  83         window.setLocation(450, 50);
  84 
  85         frame.setVisible(true);
  86     }
  87 
  88     public void doTest() throws Exception {
  89 
  90         try {
  91 
  92             robot.waitForIdle(delay);
  93 
  94             frame.clickOpenButton(robot);
  95             robot.waitForIdle(delay);
  96 
  97             dialog.activated.waitForFlagTriggered();
  98             assertTrue(dialog.activated.flag(), "Dialog did not trigger " +
  99                 "Window Activated event when it became visible");
 100 
 101             dialog.closeGained.waitForFlagTriggered();
 102             assertTrue(dialog.closeGained.flag(), "the 1st Dialog button " +
 103                 "did not gain focus when it became visible");
 104 
 105             assertTrue(dialog.closeButton.hasFocus(), "the 1st Dialog button " +
 106                 "gained the focus but lost it afterwards");
 107 
 108             frame.checkUnblockedFrame(robot, "A " + dialog.getModalityType() + " dialog is visible.");
 109 
 110             dialog.checkUnblockedDialog(robot, "A Frame is visible.");
 111 
 112             dialog.openClicked.reset();
 113             dialog.clickOpenButton(robot);
 114             robot.waitForIdle(delay);
 115 
 116             window.checkUnblockedWindow(robot,
 117                 "A Frame and a " + dialog.getModalityType() + " Dialog are visible.");
 118             robot.waitForIdle(delay);
 119 
 120         } finally {
 121             EventQueue.invokeAndWait(this::closeAll);
 122         }
 123     }
 124 
 125     private void closeAll() {
 126         if (frame  != null) {  frame.dispose(); }
 127         if (dialog != null) { dialog.dispose(); }
 128         if (window != null) { window.dispose(); }
 129         if (hiddenDialog != null) { hiddenDialog.dispose(); }
 130         if (hiddenFrame  != null) {  hiddenFrame.dispose(); }
 131     }
 132 
 133 
 134     class CustomFrame extends TestFrame {
 135 
 136         @Override
 137         public void doOpenAction() {
 138             if (dialog != null) { dialog.setVisible(true); }
 139         }
 140     }
 141 
 142     class CustomDialog extends TestDialog {
 143 
 144         public CustomDialog(Dialog dialog) {
 145             super(dialog);
 146         }
 147 
 148         public CustomDialog(Frame frame) {
 149             super(frame);
 150         }
 151 
 152         @Override
 153         public void doOpenAction() {
 154             if (window != null) { window.setVisible(true); }
 155         }
 156 
 157         @Override
 158         public void doCloseAction() {
 159             this.dispose();
 160         }
 161     }
 162 }