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.*;
  26 import java.awt.print.*;
  27 
  28 import static jdk.testlibrary.Asserts.*;
  29 
  30 
  31 public class ExcludeFrameTest implements AWTEventListener {
  32     class CustomFrame extends TestFrame {
  33 
  34         @Override
  35         public void doOpenAction() {
  36             if (window != null) {
  37                 window.setVisible(true);
  38             }
  39         }
  40     }
  41 
  42     class CustomDialog extends TestDialog {
  43 
  44         public CustomDialog(Frame frame) {
  45             super(frame);
  46         }
  47 
  48         @Override
  49         public void doOpenAction() {
  50             switch (childDialogType) {
  51                 case PRINT_SETUP:
  52                     PrinterJob.getPrinterJob().printDialog();
  53                     break;
  54                 case PAGE_SETUP:
  55                     PrinterJob.getPrinterJob().pageDialog(new PageFormat());
  56                     break;
  57                 case FILE_DIALOG:
  58                     fileDialog = new FileDialog((Frame) null);
  59                     fileDialog.setLocation(20, 200);
  60                     fileDialog.setVisible(true);
  61                     break;
  62             }
  63         }
  64     }
  65 
  66     class CustomWindow extends TestWindow {
  67 
  68         public CustomWindow(Frame frame) {
  69             super(frame);
  70         }
  71 
  72         @Override
  73         public void doOpenAction() {
  74             if (dialog != null) {
  75                 dialog.setVisible(true);
  76             }
  77         }
  78     }
  79 
  80     private TestDialog dialog;
  81     private TestFrame  frame;
  82     private TestWindow window;
  83     private FileDialog fileDialog;
  84 
  85     private boolean windowAppeared = false;
  86     private final Object windowLock;
  87 
  88     private static final int delay = 1000;
  89     private final ExtendedRobot robot;
  90 
  91     private final Dialog.ModalExclusionType exclusionType;
  92 
  93     public enum DialogToShow {PAGE_SETUP, PRINT_SETUP, FILE_DIALOG};
  94     private final DialogToShow childDialogType;
  95     private String type;
  96 
  97     public ExcludeFrameTest(Dialog.ModalExclusionType exclType,
  98                             DialogToShow dialogType) throws Exception {
  99         exclusionType = exclType;
 100         childDialogType = dialogType;
 101 
 102         type = "File";
 103         if (dialogType == DialogToShow.PAGE_SETUP) {
 104             type = "Page setup";
 105         } else if (dialogType == DialogToShow.PRINT_SETUP) {
 106             type = "Print setup";
 107         }
 108 
 109         windowLock = new Object();
 110         robot = new ExtendedRobot();
 111         EventQueue.invokeAndWait( this::createGUI );
 112     }
 113 
 114     @Override
 115     public void eventDispatched(AWTEvent event) {
 116         if (event.getID() == WindowEvent.WINDOW_OPENED) {
 117             windowAppeared = true;
 118             synchronized (windowLock) {
 119                 windowLock.notifyAll();
 120             }
 121         }
 122     }
 123 
 124     private void createGUI() {
 125         frame = new CustomFrame();
 126         frame.setLocation(20, 20);
 127         frame.setModalExclusionType(exclusionType);
 128         dialog = new CustomDialog(frame);
 129         dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
 130         dialog.setLocation(220, 20);
 131         window = new CustomWindow(frame);
 132         window.setLocation(420, 20);
 133         Toolkit.getDefaultToolkit().addAWTEventListener(
 134                 ExcludeFrameTest.this, AWTEvent.WINDOW_EVENT_MASK);
 135         frame.setVisible(true);
 136     }
 137 
 138     private void closeAll() {
 139         if (dialog != null) { dialog.dispose(); }
 140         if (frame  != null) { frame.dispose();  }
 141         if (window != null) { window.dispose(); }
 142         if (fileDialog != null) {
 143             fileDialog.dispose();
 144         } else {
 145             robot.type(KeyEvent.VK_ESCAPE);
 146         }
 147     }
 148 
 149     public void doTest() throws Exception {
 150 
 151         robot.waitForIdle(delay);
 152 
 153         frame.clickOpenButton(robot);
 154         robot.waitForIdle(delay);
 155 
 156         window.clickOpenButton(robot);
 157         robot.waitForIdle(delay);
 158 
 159         dialog.clickOpenButton(robot);
 160         robot.waitForIdle(delay);
 161 
 162         if (! windowAppeared) {
 163             synchronized (windowLock) {
 164                 try {
 165                     windowLock.wait(10 * delay);
 166                 } catch (InterruptedException e) {}
 167             }
 168         }
 169 
 170         assertTrue(windowAppeared, type + " dialog didn't appear");
 171 
 172         frame.toFront();
 173         robot.waitForIdle(delay);
 174 
 175         String excl = "";
 176         if (exclusionType == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) {
 177             excl = "Application";
 178         } else if (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE) {
 179             excl = "Toolkit";
 180         }
 181 
 182         frame.checkUnblockedFrame(robot, excl + " modal " + type +
 183                 " dialog should not block this modal excluded Frame");
 184         dialog.checkUnblockedDialog(robot, excl + " modal " + type +
 185                 " dialog should not block this modal excluded app. modal Dialog");
 186         window.checkUnblockedWindow(robot, excl + " modal " + type +
 187                 " dialog should not block this modal excluded Window");
 188 
 189         robot.waitForIdle();
 190         closeAll();
 191     }
 192 }