1 /*
   2  * Copyright (c) 2007, 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 6365992 6379599 8137137
  27  * @summary REG: Showing and disposing a native print dialog makes the main
  28  *  frame inactive, Win32
  29  * @run main/manual RestoreActiveWindowTest
  30  */
  31 
  32 import java.awt.Frame;
  33 import java.awt.Button;
  34 import java.awt.GridBagLayout;
  35 import java.awt.Panel;
  36 import java.awt.TextArea;
  37 import java.awt.GridLayout;
  38 import java.awt.GridBagConstraints;
  39 import java.awt.Color;
  40 import java.awt.event.ActionEvent;
  41 import java.awt.event.ActionListener;
  42 import java.awt.print.PrinterJob;
  43 import java.awt.print.PageFormat;
  44 
  45 public class RestoreActiveWindowTest implements ActionListener {
  46 
  47     private static Frame mainFrame;
  48     private static Button printDialogButton;
  49     private static Button pageDialogButton;
  50     private static Frame instructionFrame;
  51     private static GridBagLayout layout;
  52     private static Panel mainControlPanel;
  53     private static Panel resultButtonPanel;
  54     private static TextArea instructionTextArea;
  55     private static Button passButton;
  56     private static Button failButton;
  57     private static Thread mainThread = null;
  58     private static boolean testPassed = false;
  59     private static boolean isInterrupted = false;
  60     private static final int testTimeOut = 300000;
  61 
  62     public void createAndShowGUI() {
  63         mainFrame = new Frame();
  64         mainFrame.setSize(200, 200);
  65         mainFrame.setLocationRelativeTo(null);
  66         mainFrame.setLayout(new GridLayout(2, 1));
  67 
  68         printDialogButton = new Button("show a native print dialog");
  69         pageDialogButton = new Button("show a native page dialog");
  70         printDialogButton.addActionListener(new ActionListener() {
  71             @Override
  72             public void actionPerformed(ActionEvent ae) {
  73                 PrinterJob.getPrinterJob().printDialog();
  74             }
  75         });
  76         pageDialogButton.addActionListener(new ActionListener() {
  77             @Override
  78             public void actionPerformed(ActionEvent ae) {
  79                 PrinterJob.getPrinterJob().pageDialog(new PageFormat());
  80             }
  81         });
  82 
  83         mainFrame.add(printDialogButton);
  84         mainFrame.add(pageDialogButton);
  85         mainFrame.setVisible(true);
  86     }
  87 
  88     private void createInstructionUI() {
  89         instructionFrame = new Frame("Native Print Dialog and Page Dialog");
  90         layout = new GridBagLayout();
  91         mainControlPanel = new Panel(layout);
  92         resultButtonPanel = new Panel(layout);
  93 
  94         GridBagConstraints gbc = new GridBagConstraints();
  95         String instructions
  96                 = "\nINSTRUCTIONS:\n"
  97                 + "\n   1. Click on the 'show a native print dialog' button. A "
  98                 + "native print dialog will come up."
  99                 + "\n   2. Click on the 'Cancel' button on Mac OS X or "
 100                 + "'close'(X) on other paltforms. Dialog will be closed."
 101                 + "\n   3. After the dialog is closed another window should "
 102                 + "become the active window."
 103                 + "\n   4. If there no any active window then the test has "
 104                 + "failed. Click on 'Fail' Button."
 105                 + "\n   5. Click on the 'show a native page dialog' button. A "
 106                 + "native page dialog will come up."
 107                 + "\n   6. Click on the 'Cancel' button on Mac OS X or "
 108                 + "'close'(X) on other paltforms. Dialog will be closed."
 109                 + "\n   7. After the dialog is closed another window should "
 110                 + "become the active window."
 111                 + "\n   8. If there no any active window then the test has "
 112                 + "failed. Click on 'Fail' Button."
 113                 + "\n   9. Test Passed. Click on 'Pass' Button.";
 114 
 115         instructionTextArea = new TextArea(13, 80);
 116         instructionTextArea.setText(instructions);
 117         instructionTextArea.setEnabled(false);
 118         instructionTextArea.setBackground(Color.white);
 119 
 120         gbc.gridx = 0;
 121         gbc.gridy = 0;
 122         gbc.weightx = 0.5;
 123         gbc.fill = GridBagConstraints.HORIZONTAL;
 124         mainControlPanel.add(instructionTextArea, gbc);
 125 
 126         passButton = new Button("Pass");
 127         passButton.setName("Pass");
 128         passButton.addActionListener((ActionListener) this);
 129 
 130         failButton = new Button("Fail");
 131         failButton.setName("Fail");
 132         failButton.addActionListener((ActionListener) this);
 133 
 134         gbc.gridx = 0;
 135         gbc.gridy = 0;
 136         resultButtonPanel.add(passButton, gbc);
 137         gbc.gridx = 1;
 138         gbc.gridy = 0;
 139         resultButtonPanel.add(failButton, gbc);
 140         gbc.gridx = 0;
 141         gbc.gridy = 1;
 142         mainControlPanel.add(resultButtonPanel, gbc);
 143 
 144         instructionFrame.add(mainControlPanel);
 145         instructionFrame.pack();
 146         instructionFrame.setVisible(true);
 147     }
 148 
 149     @Override
 150     public void actionPerformed(ActionEvent ae) {
 151         if (ae.getSource() instanceof Button) {
 152             Button btn = (Button) ae.getSource();
 153             switch (btn.getName()) {
 154                 case "Pass":
 155                     testPassed = true;
 156                     isInterrupted = true;
 157                     mainThread.interrupt();
 158                     break;
 159                 case "Fail":
 160                     testPassed = false;
 161                     isInterrupted = true;
 162                     mainThread.interrupt();
 163                     break;
 164             }
 165         }
 166     }
 167 
 168     private static void cleanUp() {
 169         mainFrame.dispose();
 170         instructionFrame.dispose();
 171     }
 172 
 173     public static void main(String args[]) {
 174         RestoreActiveWindowTest printDialogs = new RestoreActiveWindowTest();
 175         printDialogs.createInstructionUI();
 176         printDialogs.createAndShowGUI();
 177 
 178         mainThread = Thread.currentThread();
 179         try {
 180             mainThread.sleep(testTimeOut);
 181         } catch (InterruptedException ex) {
 182             if (!testPassed) {
 183                 throw new RuntimeException("Updating TrayIcon popup menu"
 184                         + " items FAILED");
 185             }
 186         } finally {
 187             cleanUp();
 188         }
 189 
 190         if (!isInterrupted) {
 191             throw new RuntimeException("Test Timed out after "
 192                     + testTimeOut / 1000 + " seconds");
 193         }
 194     }
 195 }