1 /*
   2  * Copyright (c) 2018, 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  * @test
  26  * @bug 8208543
  27  * @requires (os.family == "mac")
  28  * @summary Support for apple.awt.documentModalSheet incomplete in Mac
  29  * @run main/manual DocumentModalSheetTest
  30  */
  31 
  32 import java.awt.Dialog;
  33 import java.awt.event.ActionEvent;
  34 import java.awt.event.ActionListener;
  35 import java.awt.FlowLayout;
  36 import javax.swing.JFrame;
  37 import javax.swing.JDialog;
  38 import javax.swing.JLabel;
  39 import javax.swing.JButton;
  40 import javax.swing.JTextArea;
  41 import javax.swing.Timer;
  42 import javax.swing.SwingUtilities;
  43 
  44 public class DocumentModalSheetTest {
  45 
  46     private static JFrame jFrame;
  47     private static JDialog jDialog;
  48     private static JFrame instructionFrame;
  49     private static Timer timer;
  50     private static final int sleepTime = 300000;
  51     private static volatile boolean testContinueFlag = true;
  52     private static final String TEST_INSTRUCTIONS =
  53     " This is a manual test\n\n" +
  54     " 1) A Modal dialog with label 'Modal Dialog as Sheet' will be displayed\n" +
  55     "   i) Press PASS if dialog appears as a sheet\n" +
  56     "   ii) Press FAIL otherwise\n" +
  57     " 2) A Modal dialog with label 'Modal Dialog as Window' will be displayed\n" +
  58     "   i) Press PASS if dialog appears as a Window\n" +
  59     "   ii) Press FAIL otherwise\n";
  60     private static String FAIL_MESSAGE = "Modal dialog displayed as a new window";
  61 
  62     private static void createAndShowInstructionFrame() throws Exception {
  63         SwingUtilities.invokeAndWait(new Runnable() {
  64             public void run() {
  65                 JButton passButton = new JButton("Pass");
  66                 passButton.setEnabled(true);
  67 
  68                 JButton failButton = new JButton("Fail");
  69                 failButton.setEnabled(true);
  70 
  71                 JTextArea instructions = new JTextArea(5, 60);
  72                 instructions.setText(TEST_INSTRUCTIONS);
  73 
  74                 instructionFrame = new JFrame("Test Instructions");
  75                 instructionFrame.add(passButton);
  76                 instructionFrame.add(failButton);
  77                 instructionFrame.add(instructions);
  78                 instructionFrame.setSize(200,200);
  79                 instructionFrame.setLayout(new FlowLayout());
  80                 instructionFrame.pack();
  81                 instructionFrame.setVisible(true);
  82 
  83                 passButton.addActionListener(ae -> {
  84                     jDialog.setVisible(false);
  85                     timer.stop();
  86                     dispose();
  87                 });
  88 
  89                 failButton.addActionListener(ae -> {
  90                     jDialog.setVisible(false);
  91                     timer.stop();
  92                     dispose() ;
  93                     testContinueFlag = false;
  94                     throw new RuntimeException(FAIL_MESSAGE);
  95                 });
  96             }
  97         });
  98     }
  99 
 100     private static void createAndShowModalDialog() throws Exception {
 101         SwingUtilities.invokeAndWait(new Runnable() {
 102             public void run() {
 103                 try {
 104                     //Display Modal Dialog as a SHEET
 105                     jFrame = new JFrame();
 106                     createAndShowModalSheet(jFrame, "Modal Dialog as Sheet");
 107                     if (testContinueFlag) {
 108                         //Display Modal Dialog as a Window
 109                         FAIL_MESSAGE = "Modal dialog displayed as a Sheet";
 110                         createAndShowModalSheet(null, "Modal Dialog as Window");
 111                         testContinueFlag = false;
 112                     }
 113                 } catch (Exception e) {
 114                     throw new RuntimeException("Modal dialog creation failed");
 115                 } finally {
 116                     if (instructionFrame != null) {
 117                         instructionFrame.dispose();
 118                     }
 119                 }
 120             }
 121         });
 122     }
 123 
 124     private static void createAndShowModalSheet(JFrame frame, String label) throws Exception {
 125         jDialog = new JDialog(frame, null, Dialog.ModalityType.DOCUMENT_MODAL);
 126         jDialog.setSize(200, 200);
 127         jDialog.getRootPane().putClientProperty("apple.awt.documentModalSheet", Boolean.TRUE);
 128         JLabel jLabel = new JLabel(label);
 129         jDialog.add(jLabel);
 130 
 131         timer = new Timer(sleepTime, new ActionListener() {
 132             public void actionPerformed(ActionEvent e) {
 133                 jDialog.setVisible(false);
 134                 dispose();
 135             }
 136         });
 137         timer.setRepeats(false);
 138         timer.start();
 139 
 140         jDialog.pack();
 141         jDialog.setVisible(true);
 142     }
 143 
 144     private static void dispose() {
 145         if (jDialog != null) {
 146             jDialog.dispose();
 147         }
 148         if (jFrame != null) {
 149             jFrame.dispose();
 150         }
 151     }
 152 
 153     public static void main(String[] args) throws Exception {
 154         createAndShowInstructionFrame();
 155         createAndShowModalDialog();
 156 
 157         if (testContinueFlag) {
 158             SwingUtilities.invokeAndWait(DocumentModalSheetTest::dispose);
 159             throw new RuntimeException("Timed out after " +
 160                                        sleepTime / 1000 + " seconds");
 161         }
 162     }
 163 }