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