1 /* 2 * Copyright (c) 2020, 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 8246742 26 @summary Verifies ServiceUI.printDialog does not support properties dialog 27 @run main/manual ServiceUIPropBtnTest 28 */ 29 30 import java.awt.BorderLayout; 31 import java.awt.FlowLayout; 32 import java.awt.event.WindowAdapter; 33 import java.awt.event.WindowEvent; 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.TimeUnit; 36 import javax.swing.JButton; 37 import javax.swing.JDialog; 38 import javax.swing.JLabel; 39 import javax.swing.JPanel; 40 import javax.swing.JTextArea; 41 import javax.swing.SwingUtilities; 42 import javax.swing.Timer; 43 import javax.swing.WindowConstants; 44 import javax.print.DocFlavor; 45 import javax.print.PrintService; 46 import javax.print.PrintServiceLookup; 47 import javax.print.ServiceUI; 48 import javax.print.attribute.HashPrintRequestAttributeSet; 49 50 public class ServiceUIPropBtnTest { 51 private static final CountDownLatch testEndedSignal = new CountDownLatch(1); 52 private static final int testTimeout = 120000; 53 private static volatile String testFailureMsg; 54 private static volatile boolean testPassed; 55 private static volatile boolean testFinished; 56 57 public static void main(String[] args) throws Exception { 58 SwingUtilities.invokeLater(() -> createAndShowTestDialog()); 59 60 try { 61 if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) { 62 throw new RuntimeException(String.format( 63 "Test timeout '%d ms' elapsed.", testTimeout)); 64 } 65 if (!testPassed) { 66 String failureMsg = testFailureMsg; 67 if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) { 68 throw new RuntimeException(failureMsg); 69 } else { 70 throw new RuntimeException("Test failed."); 71 } 72 } 73 } catch (InterruptedException ie) { 74 throw new RuntimeException(ie); 75 } finally { 76 testFinished = true; 77 } 78 } 79 80 private static void doTest() throws Exception { 81 PrintService service = PrintServiceLookup.lookupDefaultPrintService(); 82 PrintService[] services = 83 PrintServiceLookup.lookupPrintServices(null, null); 84 HashPrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); 85 ServiceUI.printDialog(null, 200, 200, services, service, 86 DocFlavor.SERVICE_FORMATTED.PAGEABLE, attrs); 87 } 88 89 private static void pass() { 90 testPassed = true; 91 testEndedSignal.countDown(); 92 } 93 94 private static void fail(String failureMsg) { 95 testFailureMsg = failureMsg; 96 testPassed = false; 97 testEndedSignal.countDown(); 98 } 99 100 private static String convertMillisToTimeStr(int millis) { 101 if (millis < 0) { 102 return "00:00:00"; 103 } 104 int hours = millis / 3600000; 105 int minutes = (millis - hours * 3600000) / 60000; 106 int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000; 107 return String.format("%02d:%02d:%02d", hours, minutes, seconds); 108 } 109 110 private static void createAndShowTestDialog() { 111 String description = 112 " 1. Click on \"Start Test\" button.\r\n" + 113 " 2. A print dialog will be shown. \r\n" + 114 " 3. It includes \"Properties\" button for showing properties dialog.\r\n" + 115 " 4. Click on \"Cancel\" button.\r\n" + 116 "\r\n" + 117 " If the \"Properties\" button is enabled and \r\n"+ 118 " clicking on it does not show any dialog, click on \"FAIL\" button,\r\n" + 119 " otherwise if the button is disabled, click on \"PASS\" button."; 120 121 final JDialog dialog = new JDialog(); 122 dialog.setTitle("PropertiesButtonPrintDialog"); 123 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 124 dialog.addWindowListener(new WindowAdapter() { 125 @Override 126 public void windowClosing(WindowEvent e) { 127 dialog.dispose(); 128 fail("Main dialog was closed."); 129 } 130 }); 131 132 final JLabel testTimeoutLabel = new JLabel(String.format( 133 "Test timeout: %s", convertMillisToTimeStr(testTimeout))); 134 final long startTime = System.currentTimeMillis(); 135 final Timer timer = new Timer(0, null); 136 timer.setDelay(1000); 137 timer.addActionListener((e) -> { 138 int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime); 139 if ((leftTime < 0) || testFinished) { 140 timer.stop(); 141 dialog.dispose(); 142 } 143 testTimeoutLabel.setText(String.format( 144 "Test timeout: %s", convertMillisToTimeStr(leftTime))); 145 }); 146 timer.start(); 147 148 JTextArea textArea = new JTextArea(description); 149 textArea.setEditable(false); 150 151 final JButton testButton = new JButton("Start Test"); 152 final JButton passButton = new JButton("PASS"); 153 final JButton failButton = new JButton("FAIL"); 154 testButton.addActionListener((e) -> { 155 testButton.setEnabled(false); 156 new Thread(() -> { 157 try { 158 doTest(); 159 160 SwingUtilities.invokeLater(() -> { 161 passButton.setEnabled(true); 162 failButton.setEnabled(true); 163 }); 164 } catch (Throwable t) { 165 t.printStackTrace(); 166 dialog.dispose(); 167 fail("Exception occurred in a thread executing the test."); 168 } 169 }).start(); 170 }); 171 passButton.setEnabled(false); 172 passButton.addActionListener((e) -> { 173 dialog.dispose(); 174 pass(); 175 }); 176 failButton.setEnabled(false); 177 failButton.addActionListener((e) -> { 178 dialog.dispose(); 179 fail("Properties dialog is not disabled for ServiceUI.printDialog"); 180 }); 181 182 JPanel mainPanel = new JPanel(new BorderLayout()); 183 JPanel labelPanel = new JPanel(new FlowLayout()); 184 labelPanel.add(testTimeoutLabel); 185 mainPanel.add(labelPanel, BorderLayout.NORTH); 186 mainPanel.add(textArea, BorderLayout.CENTER); 187 JPanel buttonPanel = new JPanel(new FlowLayout()); 188 buttonPanel.add(testButton); 189 buttonPanel.add(passButton); 190 buttonPanel.add(failButton); 191 mainPanel.add(buttonPanel, BorderLayout.SOUTH); 192 dialog.add(mainPanel); 193 194 dialog.pack(); 195 dialog.setVisible(true); 196 } 197 198 }