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 * @test 25 * @bug 7064425 6948907 26 * @summary Verifies if owner Frame is associated with print dialog of PrinterJob 27 * @run main/manual TestPrintDlgFrameAssociation 28 */ 29 import java.awt.BorderLayout; 30 import java.awt.Button; 31 import java.awt.FlowLayout; 32 import java.awt.Frame; 33 import java.awt.Label; 34 import java.awt.Panel; 35 import java.awt.event.WindowAdapter; 36 import java.awt.event.WindowEvent; 37 import java.awt.print.PrinterJob; 38 import javax.print.attribute.HashPrintRequestAttributeSet; 39 import javax.print.attribute.PrintRequestAttributeSet; 40 import javax.swing.JButton; 41 import javax.swing.JDialog; 42 import javax.swing.JPanel; 43 import javax.swing.JTextArea; 44 import javax.swing.SwingUtilities; 45 46 public class TestPrintDlgFrameAssociation { 47 private static Thread mainThread; 48 private static boolean testPassed; 49 private static boolean testGeneratedInterrupt; 50 private static Button print; 51 private static Label dialogName; 52 private static Frame frame; 53 private static boolean start; 54 private static Thread t; 55 56 public static void main(String args[]) throws Exception { 57 SwingUtilities.invokeAndWait(() -> { 58 doTest(TestPrintDlgFrameAssociation::frameTest); 59 }); 60 mainThread = Thread.currentThread(); 61 try { 62 Thread.sleep(60000); 63 } catch (InterruptedException e) { 64 if (!testPassed && testGeneratedInterrupt) { 65 throw new RuntimeException("Print dialog not disposed." 66 + " Print dialog is not associated with owner Frame`"); 67 } 68 } 69 if (!testGeneratedInterrupt) { 70 throw new RuntimeException("user has not executed the test"); 71 } 72 } 73 74 private static void frameTest() { 75 Panel panel =new Panel(); 76 77 print = new Button("PrintDialog"); 78 print.setActionCommand("PrintDialog"); 79 print.addActionListener((e) -> { 80 PrinterJob job = PrinterJob.getPrinterJob(); 81 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 82 t.start(); 83 start = true; 84 job.printDialog(aset); 85 }); 86 87 panel.add(print); 88 89 frame = new Frame("Test Frame"); 90 frame.setLayout (new BorderLayout ()); 91 frame.add(panel,"South"); 92 frame.pack(); 93 frame.setVisible(true); 94 95 t = new Thread (() -> { 96 if (start) { 97 try { 98 Thread.sleep(5000); 99 } catch (InterruptedException ex) {} 100 frame.dispose(); 101 } 102 }); 103 } 104 105 106 public static synchronized void pass() { 107 testPassed = true; 108 testGeneratedInterrupt = true; 109 mainThread.interrupt(); 110 } 111 112 public static synchronized void fail() { 113 testPassed = false; 114 testGeneratedInterrupt = true; 115 mainThread.interrupt(); 116 } 117 118 private static void doTest(Runnable action) { 119 String description 120 = " A Frame with PrintDialog Button is shown. Press PrintDialog.\n" 121 + " A print dialog will be shown. Do not press any button.\n" 122 + " After 5 secs the frame along with this print dialog will be disposed.\n" 123 + " If the print dialog is not disposed, press FAIL else press PASS"; 124 125 final JDialog dialog = new JDialog(); 126 dialog.setTitle("printSelectionTest"); 127 JTextArea textArea = new JTextArea(description); 128 textArea.setEditable(false); 129 final JButton testButton = new JButton("Start Test"); 130 final JButton passButton = new JButton("PASS"); 131 passButton.setEnabled(false); 132 passButton.addActionListener((e) -> { 133 dialog.dispose(); 134 pass(); 135 }); 136 final JButton failButton = new JButton("FAIL"); 137 failButton.setEnabled(false); 138 failButton.addActionListener((e) -> { 139 dialog.dispose(); 140 fail(); 141 }); 142 testButton.addActionListener((e) -> { 143 testButton.setEnabled(false); 144 action.run(); 145 passButton.setEnabled(true); 146 failButton.setEnabled(true); 147 }); 148 JPanel mainPanel = new JPanel(new BorderLayout()); 149 mainPanel.add(textArea, BorderLayout.CENTER); 150 JPanel buttonPanel = new JPanel(new FlowLayout()); 151 buttonPanel.add(testButton); 152 buttonPanel.add(passButton); 153 buttonPanel.add(failButton); 154 mainPanel.add(buttonPanel, BorderLayout.SOUTH); 155 dialog.add(mainPanel); 156 dialog.pack(); 157 dialog.setVisible(true); 158 dialog.addWindowListener(new WindowAdapter() { 159 @Override 160 public void windowClosing(WindowEvent e) { 161 System.out.println("main dialog closing"); 162 testGeneratedInterrupt = false; 163 mainThread.interrupt(); 164 } 165 }); 166 167 } 168 }