Path: blob/master/test/jdk/java/awt/Modal/InvisibleParentTest/InvisibleParentTest.java
41154 views
/*1* Copyright (c) 2013, 2020, 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* @key headful26* @bug 6401700 6412803 805895027* @summary Tests that modal dialog is shown on the screen and28* iconified/restored correctly if some of its blocked windows are invisible29* @requires os.family == "linux"30* @run main/manual InvisibleParentTest31*/32import java.awt.Dialog;33import java.awt.Frame;34import java.awt.GridBagConstraints;35import java.awt.GridBagLayout;36import java.awt.event.ActionEvent;37import java.awt.event.ActionListener;38import java.util.concurrent.CountDownLatch;39import java.util.concurrent.TimeUnit;40import javax.swing.BorderFactory;41import javax.swing.JButton;42import javax.swing.JFrame;43import javax.swing.JPanel;44import javax.swing.JTextArea;45import javax.swing.SwingUtilities;4647public class InvisibleParentTest {4849public static void main(String args[]) throws Exception {50final CountDownLatch latch = new CountDownLatch(1);51TestUI test = new TestUI(latch);5253SwingUtilities.invokeLater(new Runnable() {54@Override55public void run() {56try {57test.createUI();58} catch (Exception ex) {59throw new RuntimeException("Exception while creating test UI");60}61}62});6364boolean status = latch.await(5, TimeUnit.MINUTES);6566if (!status) {67System.out.println("Test timed out.");68}6970SwingUtilities.invokeAndWait(new Runnable() {71@Override72public void run() {73try {74test.disposeUI();75} catch (Exception ex) {76throw new RuntimeException("Exception while disposing test UI");77}78}79});8081if (test.testResult == false) {82throw new RuntimeException("Test Failed.");83}84}85}8687class TestUI {8889private static JFrame mainFrame;90private static JPanel mainControlPanel;9192private static JTextArea instructionTextArea;9394private static JPanel resultButtonPanel;95private static JButton passButton;96private static JButton failButton;9798private static GridBagLayout layout;99private final CountDownLatch latch;100public boolean testResult = false;101102public TestUI(CountDownLatch latch) throws Exception {103this.latch = latch;104}105106public final void createUI() throws Exception {107108mainFrame = new JFrame("InvisibleParentTest");109mainFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);110111layout = new GridBagLayout();112mainControlPanel = new JPanel(layout);113resultButtonPanel = new JPanel(layout);114115GridBagConstraints gbc = new GridBagConstraints();116117// Create Test instructions118String instructions119= "When the test starts two windows should appear: frame G1 and\n"120+ " dialog D1. Another one frame F1 should be minimized.\n"121+ " If the dialog is not shown (minimizied), press FAIL button.\n"122+ "Then minimize frame G1 and restore F1. If the dialog D1 is not\n"123+ " restored together with F1, press FAIL, else PASS";124125instructionTextArea = new JTextArea();126instructionTextArea.setText(instructions);127instructionTextArea.setEditable(false);128instructionTextArea.setBorder(BorderFactory.129createTitledBorder("Test Instructions"));130131gbc.gridx = 0;132gbc.gridy = 0;133gbc.fill = GridBagConstraints.HORIZONTAL;134mainControlPanel.add(instructionTextArea, gbc);135136// Create resultButtonPanel with Pass, Fail buttons137passButton = new JButton("Pass");138passButton.setActionCommand("Pass");139passButton.addActionListener((ActionEvent e) -> {140System.out.println("Pass Button pressed!");141testResult = true;142latch.countDown();143144});145146failButton = new JButton("Fail");147failButton.setActionCommand("Fail");148failButton.addActionListener(new ActionListener() {149@Override150public void actionPerformed(ActionEvent e) {151System.out.println("Fail Button pressed!");152testResult = false;153latch.countDown();154}155});156157gbc.gridx = 0;158gbc.gridy = 0;159resultButtonPanel.add(passButton, gbc);160gbc.gridx = 1;161gbc.gridy = 0;162resultButtonPanel.add(failButton, gbc);163164gbc.gridx = 0;165gbc.gridy = 1;166mainControlPanel.add(resultButtonPanel, gbc);167168mainFrame.add(mainControlPanel);169170mainFrame.pack();171mainFrame.setVisible(true);172173// Create AWT frames and modal dialog174createAWTComponents();175}176177public void disposeUI() {178mainFrame.setVisible(false);179mainFrame.dispose();180}181182private void createAWTComponents() {183Frame f1 = new Frame("F1");184f1.setBounds(100, 300, 100, 100);185f1.setVisible(true);186187try {188Thread.sleep(500);189} catch (Exception ex) {190}191192f1.setExtendedState(Frame.ICONIFIED);193194Frame g1 = new Frame("G1");195g1.setBounds(150, 350, 100, 100);196g1.setVisible(true);197198final Dialog d1 = new Dialog((Frame) null, "D1", Dialog.ModalityType.APPLICATION_MODAL);199d1.setBounds(200, 400, 100, 100);200new Thread(new Runnable() {201public void run() {202d1.setVisible(true);203}204}).start();205}206}207208209210