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.test.lib.Asserts.*;
  27 
  28 // FDF: Frame - Dialog - Frame
  29 
  30 public class OnTopFDFTest {
  31 
  32     private volatile CustomDialog dialog;
  33     private volatile TestFrame leftFrame, rightFrame;
  34     private volatile Dialog hiddenDialog;
  35     private volatile Frame  hiddenFrame;
  36 
  37     private static final int delay = 500;
  38     private final ExtendedRobot robot;
  39 
  40     public enum DialogOwner {HIDDEN_DIALOG, NULL_DIALOG, HIDDEN_FRAME, NULL_FRAME, FRAME};
  41 
  42     private DialogOwner owner;
  43     boolean setModal;
  44 
  45     Dialog.ModalityType modalityType;
  46 
  47     private OnTopFDFTest(Dialog.ModalityType modType,
  48                          boolean             modal,
  49                          DialogOwner         o) throws Exception {
  50 
  51         modalityType = modType;
  52         setModal = modal;
  53         owner = o;
  54         robot = new ExtendedRobot();
  55         EventQueue.invokeLater(this::createGUI);
  56     }
  57 
  58     public OnTopFDFTest(Dialog.ModalityType modalityType,
  59                         DialogOwner         o) throws Exception {
  60         this(modalityType, false, o);
  61     }
  62 
  63     public OnTopFDFTest(DialogOwner o) throws Exception {
  64         this(null, true, o);
  65     }
  66 
  67     private void createGUI() {
  68 
  69         leftFrame = new TestFrame();
  70         leftFrame.setSize(200, 100);
  71         leftFrame.setLocation(50, 50);
  72         leftFrame.setVisible(true);
  73 
  74         switch (owner) {
  75             case HIDDEN_DIALOG:
  76                 hiddenDialog = new Dialog((Frame) null);
  77                 dialog = new CustomDialog(hiddenDialog);
  78                 break;
  79             case NULL_DIALOG:
  80                 dialog = new CustomDialog((Dialog) null);
  81                 break;
  82             case HIDDEN_FRAME:
  83                 hiddenFrame = new Frame();
  84                 dialog = new CustomDialog(hiddenFrame);
  85                 break;
  86             case NULL_FRAME:
  87                 dialog = new CustomDialog((Frame) null);
  88                 break;
  89             case FRAME:
  90                 dialog = new CustomDialog(leftFrame);
  91                 break;
  92         }
  93 
  94         if (setModal) {
  95             dialog.setModal(true);
  96             modalityType = dialog.getModalityType();
  97         } else if (modalityType != null) {
  98             dialog.setModalityType(modalityType);
  99         }
 100 
 101         dialog.setSize(200, 100);
 102         dialog.setLocation(200, 50);
 103 
 104         rightFrame = new TestFrame();
 105         rightFrame.setSize(200, 100);
 106         rightFrame.setLocation(350, 50);
 107 
 108         dialog.setVisible(true);
 109     }
 110 
 111     private void BlockingTest() throws Exception {
 112 
 113         dialog.checkUnblockedDialog(robot, "Checking if " + modalityType +
 114             " dialog appears on top of blocked Frame.");
 115         robot.waitForIdle(delay);
 116 
 117         rightFrame.closeClicked.waitForFlagTriggered(5);
 118         assertFalse(rightFrame.closeClicked.flag(),
 119             "Frame is on top of " + modalityType + " Dialog.");
 120         robot.waitForIdle(delay);
 121 
 122         leftFrame.transferFocusToBlockedFrame(robot,
 123             modalityType + " dialog blocks the Frame.", leftFrame.closeButton);
 124         robot.waitForIdle(delay);
 125 
 126         dialog.clickCloseButton(robot);
 127         robot.waitForIdle(delay);
 128     }
 129 
 130     private void Test() throws Exception {
 131 
 132         rightFrame.clickCloseButton(robot);
 133         robot.waitForIdle(delay);
 134 
 135         rightFrame.closeClicked.reset();
 136         dialog.transferFocusToDialog(
 137             robot, "Frame partially hides the dialog.", dialog.openButton);
 138         robot.waitForIdle(delay);
 139 
 140         dialog.checkUnblockedDialog(
 141             robot, "This is " + modalityType + " dialog.");
 142         robot.waitForIdle(delay);
 143 
 144         rightFrame.closeClicked.waitForFlagTriggered(5);
 145         assertFalse(rightFrame.closeClicked.flag(), "Clicking on a " +
 146             modalityType + " dialog did not bring it to the top. " +
 147             "A frame is on top of the dialog.");
 148         robot.waitForIdle(delay);
 149 
 150         dialog.closeClicked.reset();
 151 
 152         if (owner == DialogOwner.FRAME) {
 153 
 154             if (modalityType == Dialog.ModalityType.MODELESS) {
 155                 leftFrame.transferFocusToFrame(robot, "modeless dialog " +
 156                     "partially hides the Frame.", leftFrame.closeButton);
 157             } else {
 158                 leftFrame.transferFocusToBlockedFrame(robot, "a document modal " +
 159                     "dialog partially hides the Frame.", leftFrame.closeButton);
 160             }
 161 
 162         } else {
 163 
 164             leftFrame.transferFocusToFrame(robot,
 165                 "A dialog partially hides the Frame.", leftFrame.closeButton);
 166             robot.waitForIdle(delay);
 167 
 168             leftFrame.checkUnblockedFrame(robot,
 169                 modalityType + " dialog present should not block this Frame.");
 170             robot.waitForIdle(delay);
 171 
 172             dialog.closeClicked.waitForFlagTriggered(5);
 173             assertFalse(dialog.closeClicked.flag(), "Clicking on a frame did not " +
 174                 "bring it to the top. The document modal dialog is staying on top.");
 175         }
 176 
 177         robot.waitForIdle(delay);
 178     }
 179 
 180 
 181     public void doTest() throws Exception {
 182 
 183         try {
 184 
 185             robot.waitForIdle(delay);
 186 
 187             dialog.activated.waitForFlagTriggered();
 188             assertTrue(dialog.activated.flag(), "Dialog still not visible.");
 189 
 190             dialog.clickOpenButton(robot);
 191             robot.waitForIdle(delay);
 192 
 193             switch (modalityType) {
 194                 case DOCUMENT_MODAL:
 195                 case MODELESS:
 196                     Test();
 197                     break;
 198                 case APPLICATION_MODAL:
 199                 case TOOLKIT_MODAL:
 200                     BlockingTest();
 201                     break;
 202             }
 203 
 204         } finally {
 205             EventQueue.invokeAndWait(this::closeAll);
 206         }
 207     }
 208 
 209     private void closeAll() {
 210         if (dialog != null) { dialog.dispose(); }
 211         if (leftFrame  != null) {  leftFrame.dispose(); }
 212         if (rightFrame != null) { rightFrame.dispose(); }
 213         if (hiddenDialog != null) { hiddenDialog.dispose(); }
 214         if (hiddenFrame  != null) {  hiddenFrame.dispose(); }
 215     }
 216 
 217 
 218     class CustomDialog extends TestDialog {
 219 
 220         public CustomDialog(Dialog d) { super(d); }
 221         public CustomDialog(Frame  f) { super(f); }
 222 
 223         @Override
 224         public void doOpenAction() {
 225             if (rightFrame != null) {
 226                 rightFrame.setVisible(true);
 227             }
 228         }
 229     }
 230 }