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