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 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.setBounds(650, 10, 200, 230); 65 mainFrame.setLayout(new GridLayout(2, 1)); 66 67 printDialogButton = new Button("show a native print dialog"); 68 pageDialogButton = new Button("show a native page dialog"); 69 printDialogButton.addActionListener(new ActionListener() { 70 @Override 71 public void actionPerformed(ActionEvent ae) { 72 PrinterJob.getPrinterJob().printDialog(); 73 } 74 }); 75 pageDialogButton.addActionListener(new ActionListener() { 76 @Override 77 public void actionPerformed(ActionEvent ae) { 78 PrinterJob.getPrinterJob().pageDialog(new PageFormat()); 79 } 80 }); 81 82 mainFrame.add(printDialogButton); 83 mainFrame.add(pageDialogButton); 84 mainFrame.setVisible(true); 85 } 86 87 private void createInstructionUI() { 88 instructionFrame = new Frame("Native Print Dialog and Page Dialog"); 89 instructionFrame.setBounds(0, 0, 650, 230); 90 layout = new GridBagLayout(); 91 mainControlPanel = new Panel(layout); 92 resultButtonPanel = new Panel(layout); 93 94 GridBagConstraints gbc = new GridBagConstraints(); 95 String instructions 96 = "INSTRUCTIONS:" 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(); 116 instructionTextArea.setBounds(10, 10, 650, 230); 117 instructionTextArea.setText(instructions); 118 instructionTextArea.setEnabled(false); 119 instructionTextArea.setBackground(Color.white); 120 121 gbc.gridx = 0; 122 gbc.gridy = 0; 123 gbc.weightx = 0.5; 124 gbc.fill = GridBagConstraints.HORIZONTAL; 125 mainControlPanel.add(instructionTextArea, gbc); 126 127 passButton = new Button("Pass"); 128 passButton.setName("Pass"); 129 passButton.addActionListener((ActionListener) this); 130 131 failButton = new Button("Fail"); 132 failButton.setName("Fail"); 133 failButton.addActionListener((ActionListener) this); 134 135 gbc.gridx = 0; 136 gbc.gridy = 0; 137 resultButtonPanel.add(passButton, gbc); 138 gbc.gridx = 1; 139 gbc.gridy = 0; 140 resultButtonPanel.add(failButton, gbc); 141 gbc.gridx = 0; 142 gbc.gridy = 1; 143 mainControlPanel.add(resultButtonPanel, gbc); 144 145 instructionFrame.add(mainControlPanel); 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 }