Path: blob/master/test/jdk/javax/print/ServiceUIPropBtnTest.java
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 824674225@summary Verifies ServiceUI.printDialog does not support properties dialog26@run main/manual ServiceUIPropBtnTest27*/2829import java.awt.BorderLayout;30import java.awt.FlowLayout;31import java.awt.event.WindowAdapter;32import java.awt.event.WindowEvent;33import java.util.concurrent.CountDownLatch;34import java.util.concurrent.TimeUnit;35import javax.swing.JButton;36import javax.swing.JDialog;37import javax.swing.JLabel;38import javax.swing.JPanel;39import javax.swing.JTextArea;40import javax.swing.SwingUtilities;41import javax.swing.Timer;42import javax.swing.WindowConstants;43import javax.print.DocFlavor;44import javax.print.PrintService;45import javax.print.PrintServiceLookup;46import javax.print.ServiceUI;47import javax.print.attribute.HashPrintRequestAttributeSet;4849public class ServiceUIPropBtnTest {50private static final CountDownLatch testEndedSignal = new CountDownLatch(1);51private static final int testTimeout = 120000;52private static volatile String testFailureMsg;53private static volatile boolean testPassed;54private static volatile boolean testFinished;5556public static void main(String[] args) throws Exception {57SwingUtilities.invokeLater(() -> createAndShowTestDialog());5859try {60if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) {61throw new RuntimeException(String.format(62"Test timeout '%d ms' elapsed.", testTimeout));63}64if (!testPassed) {65String failureMsg = testFailureMsg;66if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) {67throw new RuntimeException(failureMsg);68} else {69throw new RuntimeException("Test failed.");70}71}72} catch (InterruptedException ie) {73throw new RuntimeException(ie);74} finally {75testFinished = true;76}77}7879private static void doTest() throws Exception {80PrintService service = PrintServiceLookup.lookupDefaultPrintService();81PrintService[] services =82PrintServiceLookup.lookupPrintServices(null, null);83HashPrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();84ServiceUI.printDialog(null, 200, 200, services, service,85DocFlavor.SERVICE_FORMATTED.PAGEABLE, attrs);86}8788private static void pass() {89testPassed = true;90testEndedSignal.countDown();91}9293private static void fail(String failureMsg) {94testFailureMsg = failureMsg;95testPassed = false;96testEndedSignal.countDown();97}9899private static String convertMillisToTimeStr(int millis) {100if (millis < 0) {101return "00:00:00";102}103int hours = millis / 3600000;104int minutes = (millis - hours * 3600000) / 60000;105int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000;106return String.format("%02d:%02d:%02d", hours, minutes, seconds);107}108109private static void createAndShowTestDialog() {110String description =111" 1. Click on \"Start Test\" button.\r\n" +112" 2. A print dialog will be shown. \r\n" +113" 3. It includes \"Properties\" button for showing properties dialog.\r\n" +114" 4. Click on \"Cancel\" button.\r\n" +115"\r\n" +116" If the \"Properties\" button is enabled and \r\n"+117" clicking on it does not show any dialog, click on \"FAIL\" button,\r\n" +118" otherwise if the button is disabled, click on \"PASS\" button.";119120final JDialog dialog = new JDialog();121dialog.setTitle("PropertiesButtonPrintDialog");122dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);123dialog.addWindowListener(new WindowAdapter() {124@Override125public void windowClosing(WindowEvent e) {126dialog.dispose();127fail("Main dialog was closed.");128}129});130131final JLabel testTimeoutLabel = new JLabel(String.format(132"Test timeout: %s", convertMillisToTimeStr(testTimeout)));133final long startTime = System.currentTimeMillis();134final Timer timer = new Timer(0, null);135timer.setDelay(1000);136timer.addActionListener((e) -> {137int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime);138if ((leftTime < 0) || testFinished) {139timer.stop();140dialog.dispose();141}142testTimeoutLabel.setText(String.format(143"Test timeout: %s", convertMillisToTimeStr(leftTime)));144});145timer.start();146147JTextArea textArea = new JTextArea(description);148textArea.setEditable(false);149150final JButton testButton = new JButton("Start Test");151final JButton passButton = new JButton("PASS");152final JButton failButton = new JButton("FAIL");153testButton.addActionListener((e) -> {154testButton.setEnabled(false);155new Thread(() -> {156try {157doTest();158159SwingUtilities.invokeLater(() -> {160passButton.setEnabled(true);161failButton.setEnabled(true);162});163} catch (Throwable t) {164t.printStackTrace();165dialog.dispose();166fail("Exception occurred in a thread executing the test.");167}168}).start();169});170passButton.setEnabled(false);171passButton.addActionListener((e) -> {172dialog.dispose();173pass();174});175failButton.setEnabled(false);176failButton.addActionListener((e) -> {177dialog.dispose();178fail("Properties dialog is not disabled for ServiceUI.printDialog");179});180181JPanel mainPanel = new JPanel(new BorderLayout());182JPanel labelPanel = new JPanel(new FlowLayout());183labelPanel.add(testTimeoutLabel);184mainPanel.add(labelPanel, BorderLayout.NORTH);185mainPanel.add(textArea, BorderLayout.CENTER);186JPanel buttonPanel = new JPanel(new FlowLayout());187buttonPanel.add(testButton);188buttonPanel.add(passButton);189buttonPanel.add(failButton);190mainPanel.add(buttonPanel, BorderLayout.SOUTH);191dialog.add(mainPanel);192193dialog.pack();194dialog.setVisible(true);195}196}197198199