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 // DDF: Dialog - Dialog - Frame
  29 
  30 public class OnTopDDFTest {
  31 
  32     private volatile TestDialog dialog, leftDialog;
  33     private volatile TestFrame rightFrame;
  34     private volatile Frame hiddenFrame;
  35 
  36     private static final int delay = 500;
  37     private final ExtendedRobot robot;
  38 
  39     boolean setModal;
  40 
  41     Dialog.ModalityType modalityType;
  42 
  43     private OnTopDDFTest(Dialog.ModalityType modType,
  44                          boolean             modal) throws Exception {
  45         modalityType = modType;
  46         setModal = modal;
  47 
  48         robot = new ExtendedRobot();
  49         EventQueue.invokeLater(this::createGUI);
  50     }
  51 
  52     public OnTopDDFTest(Dialog.ModalityType modalityType) throws Exception {
  53         this(modalityType, false);
  54     }
  55 
  56     public OnTopDDFTest() throws Exception {
  57         this(null, true);
  58     }
  59 
  60     private void createGUI() {
  61 
  62         hiddenFrame = new Frame();
  63         leftDialog = new TestDialog(hiddenFrame);
  64         leftDialog.setSize(200, 100);
  65         leftDialog.setLocation(50, 50);
  66         leftDialog.setVisible(true);
  67 
  68         dialog = new CustomDialog(leftDialog);
  69         if (setModal) {
  70             dialog.setModal(true);
  71             modalityType = dialog.getModalityType();
  72         } else if (modalityType != null) {
  73             dialog.setModalityType(modalityType);
  74         }
  75 
  76         dialog.setSize(200, 100);
  77         dialog.setLocation(200, 50);
  78 
  79         rightFrame = new TestFrame();
  80         rightFrame.setSize(200, 100);
  81         rightFrame.setLocation(350, 50);
  82 
  83         dialog.setVisible(true);
  84     }
  85 
  86     public void doTest() throws Exception {
  87 
  88         try {
  89 
  90             robot.waitForIdle(delay);
  91 
  92             dialog.activated.waitForFlagTriggered();
  93             assertTrue(dialog.activated.flag(), "Dialog still not visible.");
  94 
  95             dialog.clickOpenButton(robot);
  96             robot.waitForIdle(delay);
  97 
  98             if ((modalityType == Dialog.ModalityType.MODELESS) ||
  99                 (modalityType == Dialog.ModalityType.DOCUMENT_MODAL)) {
 100 
 101                 rightFrame.clickCloseButton(robot);
 102                 robot.waitForIdle(delay);
 103 
 104                 rightFrame.closeClicked.reset();
 105                 dialog.transferFocusToDialog(robot, "A Frame partially hides the " +
 106                     modalityType + " Dialog.", dialog.openButton);
 107                 robot.waitForIdle(delay);
 108 
 109                 dialog.checkUnblockedDialog(robot,
 110                     "This is " + modalityType + " dialog and no other Dialogs blocks it.");
 111                 robot.waitForIdle(delay);
 112 
 113                 rightFrame.closeClicked.waitForFlagTriggered(5);
 114                 assertFalse(rightFrame.closeClicked.flag(), "Clicking on " + modalityType +
 115                     "dialog did not bring it to the top. A frame on top of Dialog.");
 116                 robot.waitForIdle(delay);
 117 
 118                 dialog.closeClicked.reset();
 119                 if (modalityType == Dialog.ModalityType.MODELESS) {
 120                     leftDialog.transferFocusToDialog(robot, "This dialog is not " +
 121                         "blocked by any other dialogs.", leftDialog.closeButton);
 122                 } else {
 123                     leftDialog.transferFocusToBlockedDialog(robot, "This dialog is not " +
 124                         "blocked by any other dialogs.", leftDialog.closeButton);
 125                 }
 126             } else {
 127                 dialog.checkUnblockedDialog(robot, "Checking if modal dialog " +
 128                     "appears on top of blocked Frame.");
 129                 robot.waitForIdle(delay);
 130 
 131                 rightFrame.closeClicked.waitForFlagTriggered(5);
 132                 assertFalse(rightFrame.closeClicked.flag(),
 133                     "Frame on top of an application modal Dialog.");
 134                 robot.waitForIdle(delay);
 135 
 136                 leftDialog.transferFocusToBlockedDialog(robot,
 137                     "An application modal dialog blocks the Dialog.", leftDialog.closeButton);
 138             }
 139 
 140             robot.waitForIdle(delay);
 141 
 142             dialog.clickCloseButton(robot);
 143             robot.waitForIdle(delay);
 144 
 145         } finally {
 146             EventQueue.invokeAndWait(this::closeAll);
 147         }
 148     }
 149 
 150     private void closeAll() {
 151         if (dialog != null) { dialog.dispose(); }
 152         if (leftDialog != null) { leftDialog.dispose(); }
 153         if (rightFrame != null) { rightFrame.dispose(); }
 154         if (hiddenFrame != null) { hiddenFrame.dispose(); }
 155     }
 156 
 157 
 158     class CustomDialog extends TestDialog {
 159 
 160         public CustomDialog(Dialog d) { super(d); }
 161 
 162         @Override
 163         public void doOpenAction() {
 164             if (rightFrame != null) {
 165                 rightFrame.setVisible(true);
 166             }
 167         }
 168     }
 169 }