Path: blob/master/test/jdk/java/awt/Dialog/DocumentModalSheetTest/DocumentModalSheetTest.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 820854326* @requires (os.family == "mac")27* @summary Support for apple.awt.documentModalSheet incomplete in Mac28* @run main/manual DocumentModalSheetTest29*/3031import java.awt.Dialog;32import java.awt.event.ActionEvent;33import java.awt.event.ActionListener;34import java.awt.FlowLayout;35import javax.swing.JFrame;36import javax.swing.JDialog;37import javax.swing.JLabel;38import javax.swing.JButton;39import javax.swing.JTextArea;40import javax.swing.Timer;41import javax.swing.SwingUtilities;4243public class DocumentModalSheetTest {4445private static JFrame jFrame;46private static JDialog jDialog;47private static JFrame instructionFrame;48private static Timer timer;49private static final int sleepTime = 300000;50private static volatile boolean testContinueFlag = true;51private static final String TEST_INSTRUCTIONS =52" This is a manual test\n\n" +53" 1) A Modal dialog with label 'Modal Dialog as Sheet' will be displayed\n" +54" i) Press PASS if dialog appears as a sheet\n" +55" ii) Press FAIL otherwise\n" +56" 2) A Modal dialog with label 'Modal Dialog as Window' will be displayed\n" +57" i) Press PASS if dialog appears as a Window\n" +58" ii) Press FAIL otherwise\n";59private static String FAIL_MESSAGE = "Modal dialog displayed as a new window";6061private static void createAndShowInstructionFrame() throws Exception {62SwingUtilities.invokeAndWait(new Runnable() {63public void run() {64JButton passButton = new JButton("Pass");65passButton.setEnabled(true);6667JButton failButton = new JButton("Fail");68failButton.setEnabled(true);6970JTextArea instructions = new JTextArea(5, 60);71instructions.setText(TEST_INSTRUCTIONS);7273instructionFrame = new JFrame("Test Instructions");74instructionFrame.add(passButton);75instructionFrame.add(failButton);76instructionFrame.add(instructions);77instructionFrame.setSize(200,200);78instructionFrame.setLayout(new FlowLayout());79instructionFrame.pack();80instructionFrame.setVisible(true);8182passButton.addActionListener(ae -> {83jDialog.setVisible(false);84timer.stop();85dispose();86});8788failButton.addActionListener(ae -> {89jDialog.setVisible(false);90timer.stop();91dispose() ;92testContinueFlag = false;93throw new RuntimeException(FAIL_MESSAGE);94});95}96});97}9899private static void createAndShowModalDialog() throws Exception {100SwingUtilities.invokeAndWait(new Runnable() {101public void run() {102try {103//Display Modal Dialog as a SHEET104jFrame = new JFrame();105createAndShowModalSheet(jFrame, "Modal Dialog as Sheet");106if (testContinueFlag) {107//Display Modal Dialog as a Window108FAIL_MESSAGE = "Modal dialog displayed as a Sheet";109createAndShowModalSheet(null, "Modal Dialog as Window");110testContinueFlag = false;111}112} catch (Exception e) {113throw new RuntimeException("Modal dialog creation failed");114} finally {115if (instructionFrame != null) {116instructionFrame.dispose();117}118}119}120});121}122123private static void createAndShowModalSheet(JFrame frame, String label) throws Exception {124jDialog = new JDialog(frame, null, Dialog.ModalityType.DOCUMENT_MODAL);125jDialog.setSize(200, 200);126jDialog.getRootPane().putClientProperty("apple.awt.documentModalSheet", Boolean.TRUE);127JLabel jLabel = new JLabel(label);128jDialog.add(jLabel);129130timer = new Timer(sleepTime, new ActionListener() {131public void actionPerformed(ActionEvent e) {132jDialog.setVisible(false);133testContinueFlag = false;134dispose();135throw new RuntimeException("Timed out after " +136sleepTime / 1000 + " seconds");137}138});139timer.setRepeats(false);140timer.start();141142jDialog.pack();143jDialog.setVisible(true);144}145146private static void dispose() {147if (jDialog != null) {148jDialog.dispose();149}150if (jFrame != null) {151jFrame.dispose();152}153}154155public static void main(String[] args) throws Exception {156createAndShowInstructionFrame();157createAndShowModalDialog();158}159}160161162