1 /* 2 * Copyright (c) 2018, 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 8203796 26 @run main/manual DialogOwnerTest 27 @summary Test DialogOwner API 28 */ 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.awt.GraphicsConfiguration; 33 import java.awt.GridLayout; 34 import java.awt.Window; 35 import java.awt.event.ActionEvent; 36 import java.awt.print.PrinterJob; 37 import javax.print.PrintService; 38 import javax.print.PrintServiceLookup; 39 import javax.print.ServiceUI; 40 import javax.print.attribute.HashPrintRequestAttributeSet; 41 import javax.print.attribute.PrintRequestAttributeSet; 42 import javax.print.attribute.standard.DialogOwner; 43 import javax.print.attribute.standard.DialogTypeSelection; 44 import javax.swing.JButton; 45 import javax.swing.JCheckBox; 46 import javax.swing.JFrame; 47 import javax.swing.JPanel; 48 import javax.swing.JTextArea; 49 import javax.swing.SwingUtilities; 50 51 public class DialogOwnerTest extends JPanel { 52 53 static final int NONE = 0x0; 54 static final int PRINT = 0x1; 55 static final int PAGE = 0x2; 56 static final int SWING2D = 0x4; 57 static final int NATIVE2D = 0x8; 58 static final int SERVICEUI = 0x10; 59 60 static final int ONTOP = 0x20; 61 static final int OWNED = 0x40; 62 63 static PrintService[] services = 64 PrintServiceLookup.lookupPrintServices(null, null); 65 66 public static void main(String[] args) { 67 if (services.length == 0) { 68 System.out.println("No printers, exiting"); 69 return; 70 } else { 71 service = PrinterJob.getPrinterJob().getPrintService(); 72 } 73 SwingUtilities.invokeLater(() -> { 74 createUI(); 75 }); 76 while (!testFinished) { 77 try { 78 Thread.sleep(1000); 79 } catch (InterruptedException e){ 80 } 81 } 82 if (!testPassed) { 83 throw new RuntimeException("TEST FAILED."); 84 } 85 } 86 87 88 static final String otherText = 89 "This window is used to test on top behaviour\n" + 90 "For tests that are 'Owned' or 'On Top' the dialog\n" + 91 "must always stay above this window. Verify this\n " + 92 "by moving the dialog so that it partially obscures\n" + 93 "this window and then trying to raise this window."; 94 95 static final String instructions = 96 " Instructions\n" + 97 "This tests that a print dialog stays on top of either another\n" + 98 "window, or on top of all windows.\n" + 99 "For Owned tests the window titled 'Owner Window' should always \n" + 100 "stay behind the print dialog.\n" + 101 "For On Top tests all windows should stay behind the owner window.\n" + 102 "This test tracks if you have checked all the scenarios and will\n" + 103 "not allow the test to pass unless you have visited them all.\n"; 104 105 static PrintService service; 106 107 public DialogOwnerTest() { 108 super(); 109 //setLayout(new GridLayout(24, 1)); 110 } 111 112 static boolean isNative(int flags) { 113 return (flags & NATIVE2D) != 0; 114 } 115 116 static boolean isCommon(int flags) { 117 return (flags & SWING2D) != 0; 118 } 119 120 static boolean is2D(int flags) { 121 return (flags & SWING2D|NATIVE2D) != 0; 122 } 123 124 static boolean isPage(int flags) { 125 return (flags & PAGE ) != 0; 126 } 127 128 static JFrame frame; 129 static JFrame other; 130 static JButton pass; 131 static ArrayList<JPanel> panelList = new ArrayList<JPanel>(); 132 static volatile boolean testPassed, testFinished; 133 134 int testCount = 0; 135 List<String> testList = new ArrayList<String>(); 136 137 static void createUI() { 138 other = new JFrame("Owner Window"); 139 JTextArea otherTextArea = new JTextArea(otherText, 10, 40); 140 other.add(otherTextArea); 141 other.pack(); 142 other.setVisible(true); 143 other.setLocation(800, 100); 144 145 frame = new JFrame("Test Dialog Owner"); 146 frame.pack(); 147 JTextArea instructionsPanel = new JTextArea(instructions, 10, 50); 148 instructionsPanel.setEditable(false); 149 frame.add("North", instructionsPanel); 150 DialogOwnerTest test = new DialogOwnerTest(); 151 152 test.addTest("Owned Swing Print", OWNED, frame, PRINT|SWING2D); 153 test.addTest("On Top Swing Print", ONTOP, null, PRINT|SWING2D); 154 155 test.addTest("Owned Swing Page", OWNED, frame, PAGE|SWING2D); 156 test.addTest("On Top Swing Page", ONTOP, null, PAGE|SWING2D); 157 158 test.addTest("Owned javax.print", OWNED, frame, PRINT|SERVICEUI); 159 test.addTest("On Top javax.print", OWNED, null, PRINT|SERVICEUI); 160 161 test.addTest("Owned Native Print", OWNED, frame, PRINT|NATIVE2D); 162 test.addTest("On Top Native Print", OWNED, null, PRINT|NATIVE2D); 163 164 test.addTest("Owned Native Page", OWNED, frame, PAGE|NATIVE2D); 165 test.addTest("On Top Native Page", OWNED, null, PAGE|NATIVE2D); 166 167 test.setLayout(new GridLayout(panelList.size()+2, 1)); 168 169 pass = new JButton("Pass"); 170 pass.setEnabled(false); 171 pass.addActionListener((ActionEvent e) -> { 172 if (test.testList.size() > 0) { 173 return; 174 } 175 frame.dispose(); 176 other.dispose(); 177 System.out.println("User says test passed."); 178 testPassed = true; 179 testFinished = true; 180 }); 181 182 JButton fail = new JButton("Fail"); 183 fail.addActionListener((ActionEvent e) -> { 184 frame.dispose(); 185 other.dispose(); 186 System.out.println("User says test failed."); 187 testPassed = false; 188 testFinished = true; 189 }); 190 191 JPanel p = new JPanel(); 192 p.add(pass); 193 p.add(fail); 194 test.add(p); 195 196 197 for (JPanel panel : panelList) { 198 test.add(panel); 199 } 200 201 frame.add("Center", test); 202 frame.pack(); 203 frame.setLocation(0,0); 204 frame.setVisible(true); 205 } 206 207 boolean isSupported(PrintRequestAttributeSet aset, 208 int ownerFlags, Window owner, int dlgFlags) { 209 210 boolean supported = true; 211 DialogOwner ownerAttr = null; 212 if (ownerFlags != NONE) { 213 if (ownerFlags == ONTOP) { 214 ownerAttr = new DialogOwner(); 215 } else if (ownerFlags == OWNED) { 216 ownerAttr = new DialogOwner(owner); 217 } 218 aset.add(ownerAttr); 219 } 220 if (is2D(dlgFlags)) { 221 DialogTypeSelection dst = null; 222 if (isNative(dlgFlags)) { 223 dst = DialogTypeSelection.NATIVE; 224 } else if (isCommon(dlgFlags)) { 225 dst = DialogTypeSelection.COMMON; 226 } 227 if (dst != null && 228 !service.isAttributeValueSupported(dst, null, aset)) { 229 //System.out.println("This DialogType not supported"); 230 supported = false; 231 } 232 if (dst != null) { 233 aset.add(dst); 234 } 235 if (ownerAttr != null && 236 !service.isAttributeValueSupported(ownerAttr, null, aset)) { 237 //System.out.println("This DialogOwner not supported"); 238 supported = false; 239 } 240 } 241 return supported; 242 } 243 244 void addTest(String title, int ownerFlags, Window owner, int dlgFlags) { 245 246 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 247 if (!isSupported(aset, ownerFlags, owner, dlgFlags)) { 248 return; 249 } 250 251 // if we are here then this is supportable and worth testing 252 // and the attribute set is configured. 253 254 String label = title + " Dialog"; 255 JButton button = new JButton(label); 256 JCheckBox tested = new JCheckBox("Tested"); 257 tested.setEnabled(false); 258 JPanel panel = new JPanel(); 259 panel.add(tested); 260 panel.add(button); 261 panelList.add(panel); 262 //add(panel); 263 testList.add(title); 264 if (++testCount != testList.size()) { 265 throw new RuntimeException("Test titles must be unique"); 266 } 267 268 button.addActionListener((ActionEvent e) -> { 269 tested.setSelected(true); 270 testList.remove(title); 271 if (testList.isEmpty()) { 272 pass.setEnabled(true); 273 } 274 275 if (is2D(dlgFlags)) { 276 PrinterJob job = PrinterJob.getPrinterJob(); 277 if (isPage(dlgFlags)) { 278 job.pageDialog(aset); 279 } else { 280 job.printDialog(aset); 281 } 282 } else { 283 GraphicsConfiguration gc = null; 284 int x = 0, y = 0; 285 ServiceUI.printDialog(gc, x, y, services, services[0], null,aset); 286 } 287 }); 288 } 289 }