1 /* 2 * Copyright (c) 2007, 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 27 @summary REG: Showing and disposing a native print dialog makes the main frame inactive, Win32 28 @author Dmitry.Cherepanov@SUN.COM area=awt.printdialog 29 @run applet/manual=yesno RestoreActiveWindowTest.html 30 */ 31 32 import java.applet.Applet; 33 import java.awt.*; 34 import java.awt.event.*; 35 import java.awt.print.*; 36 import javax.print.attribute.*; 37 38 public class RestoreActiveWindowTest extends Applet 39 { 40 Button showBtn1 = new Button("show a native print dialog"); 41 Button showBtn2 = new Button("show a native page dialog"); 42 43 public void init() 44 { 45 showBtn1.addActionListener(new ActionListener(){ 46 public void actionPerformed(ActionEvent ae) { 47 PrinterJob.getPrinterJob().printDialog(); 48 } 49 }); 50 showBtn2.addActionListener(new ActionListener(){ 51 public void actionPerformed(ActionEvent ae){ 52 PrinterJob.getPrinterJob().pageDialog(new PageFormat()); 53 } 54 }); 55 56 add(showBtn1); 57 add(showBtn2); 58 59 String[] instructions = { 60 "1.1) Click on 'show a native print dialog'. A native print dialog will come up.", 61 "1.2) Click on the 'close'(X) button. The dialog will be closed.", 62 "1.3) After the dialog closing another window should become the active window.", 63 "1.4) If there no any active window then the test failed.", 64 "2.1) Click on 'show a native page dialog'. A native page dialog will come up.", 65 "2.2) Click on the 'close'(X) button. The dialog will be closed.", 66 "2.3) After the dialog closing another window should become the active window.", 67 "2.4) If there no any active window then the test failed.", 68 "3) Test Passed." 69 }; 70 71 Sysout.createDialogWithInstructions( instructions ); 72 73 }//End init() 74 75 public void start () 76 { 77 //Get things going. Request focus, set size, et cetera 78 setSize (200,200); 79 show(); 80 81 }// start() 82 83 //The rest of this class is the actions which perform the test... 84 85 //Use Sysout.println to communicate with the user NOT System.out!! 86 //Sysout.println ("Something Happened!"); 87 88 }// class ManualYesNoTest 89 90 /* Place other classes related to the test after this line */ 91 92 93 94 95 96 /**************************************************** 97 Standard Test Machinery 98 DO NOT modify anything below -- it's a standard 99 chunk of code whose purpose is to make user 100 interaction uniform, and thereby make it simpler 101 to read and understand someone else's test. 102 ****************************************************/ 103 104 /** 105 This is part of the standard test machinery. 106 It creates a dialog (with the instructions), and is the interface 107 for sending text messages to the user. 108 To print the instructions, send an array of strings to Sysout.createDialog 109 WithInstructions method. Put one line of instructions per array entry. 110 To display a message for the tester to see, simply call Sysout.println 111 with the string to be displayed. 112 This mimics System.out.println but works within the test harness as well 113 as standalone. 114 */ 115 116 class Sysout 117 { 118 private static TestDialog dialog; 119 120 public static void createDialogWithInstructions( String[] instructions ) 121 { 122 dialog = new TestDialog( new Frame(), "Instructions" ); 123 dialog.printInstructions( instructions ); 124 dialog.setVisible(true); 125 println( "Any messages for the tester will display here." ); 126 } 127 128 public static void createDialog( ) 129 { 130 dialog = new TestDialog( new Frame(), "Instructions" ); 131 String[] defInstr = { "Instructions will appear here. ", "" } ; 132 dialog.printInstructions( defInstr ); 133 dialog.setVisible(true); 134 println( "Any messages for the tester will display here." ); 135 } 136 137 138 public static void printInstructions( String[] instructions ) 139 { 140 dialog.printInstructions( instructions ); 141 } 142 143 144 public static void println( String messageIn ) 145 { 146 dialog.displayMessage( messageIn ); 147 } 148 149 }// Sysout class 150 151 /** 152 This is part of the standard test machinery. It provides a place for the 153 test instructions to be displayed, and a place for interactive messages 154 to the user to be displayed. 155 To have the test instructions displayed, see Sysout. 156 To have a message to the user be displayed, see Sysout. 157 Do not call anything in this dialog directly. 158 */ 159 class TestDialog extends Dialog 160 { 161 162 TextArea instructionsText; 163 TextArea messageText; 164 int maxStringLength = 80; 165 166 //DO NOT call this directly, go through Sysout 167 public TestDialog( Frame frame, String name ) 168 { 169 super( frame, name ); 170 int scrollBoth = TextArea.SCROLLBARS_BOTH; 171 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 172 add( "North", instructionsText ); 173 174 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 175 add("Center", messageText); 176 177 pack(); 178 179 setVisible(true); 180 }// TestDialog() 181 182 //DO NOT call this directly, go through Sysout 183 public void printInstructions( String[] instructions ) 184 { 185 //Clear out any current instructions 186 instructionsText.setText( "" ); 187 188 //Go down array of instruction strings 189 190 String printStr, remainingStr; 191 for( int i=0; i < instructions.length; i++ ) 192 { 193 //chop up each into pieces maxSringLength long 194 remainingStr = instructions[ i ]; 195 while( remainingStr.length() > 0 ) 196 { 197 //if longer than max then chop off first max chars to print 198 if( remainingStr.length() >= maxStringLength ) 199 { 200 //Try to chop on a word boundary 201 int posOfSpace = remainingStr. 202 lastIndexOf( ' ', maxStringLength - 1 ); 203 204 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 205 206 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 207 remainingStr = remainingStr.substring( posOfSpace + 1 ); 208 } 209 //else just print 210 else 211 { 212 printStr = remainingStr; 213 remainingStr = ""; 214 } 215 216 instructionsText.append( printStr + "\n" ); 217 218 }// while 219 220 }// for 221 222 }//printInstructions() 223 224 //DO NOT call this directly, go through Sysout 225 public void displayMessage( String messageIn ) 226 { 227 messageText.append( messageIn + "\n" ); 228 System.out.println(messageIn); 229 } 230 231 }// TestDialog class | 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 } |