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