1 /*
   2  * Copyright (c) 2013, 2016, 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  * @key headful
  27  * @bug 6401700 6412803 8058950
  28  * @summary  Tests that modal dialog is shown on the screen and
  29  * iconified/restored correctly if some of its blocked windows are invisible
  30  * @requires (os.family == "linux" | os.family == "solaris")
  31  * @run main/manual InvisibleParentTest
  32  */
  33 import java.awt.Dialog;
  34 import java.awt.Frame;
  35 import java.awt.GridBagConstraints;
  36 import java.awt.GridBagLayout;
  37 import java.awt.event.ActionEvent;
  38 import java.awt.event.ActionListener;
  39 import java.util.concurrent.CountDownLatch;
  40 import java.util.concurrent.TimeUnit;
  41 import javax.swing.BorderFactory;
  42 import javax.swing.JButton;
  43 import javax.swing.JFrame;
  44 import javax.swing.JPanel;
  45 import javax.swing.JTextArea;
  46 import javax.swing.SwingUtilities;
  47 
  48 public class InvisibleParentTest {
  49 
  50     public static void main(String args[]) throws Exception {
  51         final CountDownLatch latch = new CountDownLatch(1);
  52         TestUI test = new TestUI(latch);
  53 
  54         SwingUtilities.invokeLater(new Runnable() {
  55             @Override
  56             public void run() {
  57                 try {
  58                     test.createUI();
  59                 } catch (Exception ex) {
  60                     throw new RuntimeException("Exception while creating test UI");
  61                 }
  62             }
  63         });
  64 
  65         boolean status = latch.await(5, TimeUnit.MINUTES);
  66 
  67         if (!status) {
  68             System.out.println("Test timed out.");
  69         }
  70 
  71         SwingUtilities.invokeAndWait(new Runnable() {
  72             @Override
  73             public void run() {
  74                 try {
  75                     test.disposeUI();
  76                 } catch (Exception ex) {
  77                     throw new RuntimeException("Exception while disposing test UI");
  78                 }
  79             }
  80         });
  81 
  82         if (test.testResult == false) {
  83             throw new RuntimeException("Test Failed.");
  84         }
  85     }
  86 }
  87 
  88 class TestUI {
  89 
  90     private static JFrame mainFrame;
  91     private static JPanel mainControlPanel;
  92 
  93     private static JTextArea instructionTextArea;
  94 
  95     private static JPanel resultButtonPanel;
  96     private static JButton passButton;
  97     private static JButton failButton;
  98 
  99     private static GridBagLayout layout;
 100     private final CountDownLatch latch;
 101     public boolean testResult = false;
 102 
 103     public TestUI(CountDownLatch latch) throws Exception {
 104         this.latch = latch;
 105     }
 106 
 107     public final void createUI() throws Exception {
 108 
 109         mainFrame = new JFrame("InvisibleParentTest");
 110         mainFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
 111 
 112         layout = new GridBagLayout();
 113         mainControlPanel = new JPanel(layout);
 114         resultButtonPanel = new JPanel(layout);
 115 
 116         GridBagConstraints gbc = new GridBagConstraints();
 117 
 118         // Create Test instructions
 119         String instructions
 120                 = "When the test starts two windows should appear: frame G1 and\n"
 121                 + "    dialog D1. Another one frame F1 should be minimized.\n"
 122                 + "    If the dialog is not shown (minimizied), press FAIL button.\n"
 123                 + "Then minimize frame G1 and restore F1. If the dialog D1 is not\n"
 124                 + "    restored together with F1, press FAIL, else PASS";
 125 
 126         instructionTextArea = new JTextArea();
 127         instructionTextArea.setText(instructions);
 128         instructionTextArea.setEditable(false);
 129         instructionTextArea.setBorder(BorderFactory.
 130                 createTitledBorder("Test Instructions"));
 131 
 132         gbc.gridx = 0;
 133         gbc.gridy = 0;
 134         gbc.fill = GridBagConstraints.HORIZONTAL;
 135         mainControlPanel.add(instructionTextArea, gbc);
 136 
 137         // Create resultButtonPanel with Pass, Fail buttons
 138         passButton = new JButton("Pass");
 139         passButton.setActionCommand("Pass");
 140         passButton.addActionListener((ActionEvent e) -> {
 141             System.out.println("Pass Button pressed!");
 142             testResult = true;
 143             latch.countDown();
 144 
 145         });
 146 
 147         failButton = new JButton("Fail");
 148         failButton.setActionCommand("Fail");
 149         failButton.addActionListener(new ActionListener() {
 150             @Override
 151             public void actionPerformed(ActionEvent e) {
 152                 System.out.println("Fail Button pressed!");
 153                 testResult = false;
 154                 latch.countDown();
 155             }
 156         });
 157 
 158         gbc.gridx = 0;
 159         gbc.gridy = 0;
 160         resultButtonPanel.add(passButton, gbc);
 161         gbc.gridx = 1;
 162         gbc.gridy = 0;
 163         resultButtonPanel.add(failButton, gbc);
 164 
 165         gbc.gridx = 0;
 166         gbc.gridy = 1;
 167         mainControlPanel.add(resultButtonPanel, gbc);
 168 
 169         mainFrame.add(mainControlPanel);
 170 
 171         mainFrame.pack();
 172         mainFrame.setVisible(true);
 173 
 174         // Create AWT frames and modal dialog
 175         createAWTComponents();
 176     }
 177 
 178     public void disposeUI() {
 179         mainFrame.setVisible(false);
 180         mainFrame.dispose();
 181     }
 182 
 183     private void createAWTComponents() {
 184         Frame f1 = new Frame("F1");
 185         f1.setBounds(100, 300, 100, 100);
 186         f1.setVisible(true);
 187 
 188         try {
 189             Thread.sleep(500);
 190         } catch (Exception ex) {
 191         }
 192 
 193         f1.setExtendedState(Frame.ICONIFIED);
 194 
 195         Frame g1 = new Frame("G1");
 196         g1.setBounds(150, 350, 100, 100);
 197         g1.setVisible(true);
 198 
 199         final Dialog d1 = new Dialog((Frame) null, "D1", Dialog.ModalityType.APPLICATION_MODAL);
 200         d1.setBounds(200, 400, 100, 100);
 201         new Thread(new Runnable() {
 202             public void run() {
 203                 d1.setVisible(true);
 204             }
 205         }).start();
 206     }
 207 }
 208