test/jdk/javax/swing/SwingUtilities/TestTextPosInPrint.java
changeset 55177 eba6a83dc23b
equal deleted inserted replaced
55176:3e0a90050182 55177:eba6a83dc23b
       
     1 /*
       
     2  * Copyright (c) 2019, 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 8214702
       
    26    @summary Verifies text position for whitespaced string in printing Swing text
       
    27    @run main/manual TestTextPosInPrint
       
    28  */
       
    29 
       
    30 import java.awt.BorderLayout;
       
    31 import java.awt.FlowLayout;
       
    32 import java.awt.Graphics;
       
    33 import java.awt.Graphics2D;
       
    34 import java.awt.Font;
       
    35 import java.awt.event.WindowAdapter;
       
    36 import java.awt.event.WindowEvent;
       
    37 import java.awt.print.Printable;
       
    38 import java.awt.print.PageFormat;
       
    39 import java.awt.print.PrinterException;
       
    40 import java.awt.print.PrinterJob;
       
    41 import java.util.concurrent.CountDownLatch;
       
    42 import java.util.concurrent.TimeUnit;
       
    43 import javax.swing.JFrame;
       
    44 import javax.swing.JButton;
       
    45 import javax.swing.JDialog;
       
    46 import javax.swing.JLabel;
       
    47 import javax.swing.JPanel;
       
    48 import javax.swing.JTextArea;
       
    49 import javax.swing.SwingUtilities;
       
    50 import javax.swing.Timer;
       
    51 import javax.swing.WindowConstants;
       
    52 import javax.swing.SwingConstants;
       
    53 import javax.swing.UIManager;
       
    54 
       
    55 public class TestTextPosInPrint implements Printable {
       
    56     private static final CountDownLatch testEndedSignal = new CountDownLatch(1);
       
    57     private static final int testTimeout = 300000;
       
    58     private static volatile String testFailureMsg;
       
    59     private static volatile boolean testPassed;
       
    60     private static volatile boolean testFinished;
       
    61     private static PrinterJob job;
       
    62     private static JPanel panel;
       
    63     private static JFrame f;
       
    64 
       
    65     public static void main(String[] args) throws Exception {
       
    66         job = PrinterJob.getPrinterJob();
       
    67         if (job.getPrintService() == null) {
       
    68             System.out.println("This test requires printers to be installed. Exiting.");
       
    69             return;
       
    70         }
       
    71         SwingUtilities.invokeLater(() -> createAndShowTestDialog());
       
    72 
       
    73         try {
       
    74             if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) {
       
    75                 throw new RuntimeException(String.format(
       
    76                     "Test timeout '%d ms' elapsed.", testTimeout));
       
    77             }
       
    78             if (!testPassed) {
       
    79                 String failureMsg = testFailureMsg;
       
    80                 if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) {
       
    81                     throw new RuntimeException(failureMsg);
       
    82                 } else {
       
    83                     throw new RuntimeException("Test failed.");
       
    84                 }
       
    85             }
       
    86         } catch (InterruptedException ie) {
       
    87             throw new RuntimeException(ie);
       
    88         } finally {
       
    89             testFinished = true;
       
    90             SwingUtilities.invokeAndWait(() -> f.dispose());
       
    91         }
       
    92     }
       
    93 
       
    94     private static void doTest() throws Exception {
       
    95         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       
    96         f = new JFrame();
       
    97         f.setLocationRelativeTo(null);
       
    98         panel = new JPanel();
       
    99         panel.setLayout(new BorderLayout());
       
   100         Font font = new Font("Serif", Font.PLAIN, 12);
       
   101         JLabel l1 = new JLabel("      1. ABCDE");
       
   102         l1.setHorizontalAlignment(SwingConstants.LEFT);
       
   103         JLabel l2 = new JLabel("      2. ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
       
   104         l2.setHorizontalAlignment(SwingConstants.LEFT);
       
   105         //JLabel l3 = new JLabel("      3. ABCDE          ");
       
   106         JLabel l3 = new JLabel("      3. ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
       
   107         l3.setHorizontalAlignment(SwingConstants.LEFT);
       
   108         panel.add(BorderLayout.NORTH, l1);
       
   109         panel.add(BorderLayout.CENTER, l2);
       
   110         panel.add(BorderLayout.SOUTH, l3);
       
   111         f.getContentPane().add(BorderLayout.NORTH, panel);
       
   112         f.setSize(400, 300);
       
   113         f.setVisible(true);
       
   114 
       
   115         job.setPrintable(new TestTextPosInPrint());
       
   116         if (job.printDialog()) {
       
   117             try {
       
   118                 job.print();
       
   119             } catch (PrinterException pe) {
       
   120                 throw new RuntimeException(pe);
       
   121             }
       
   122         }
       
   123     }
       
   124 
       
   125     private static void pass() {
       
   126         testPassed = true;
       
   127         testEndedSignal.countDown();
       
   128     }
       
   129 
       
   130     private static void fail(String failureMsg) {
       
   131         testFailureMsg = failureMsg;
       
   132         testPassed = false;
       
   133         testEndedSignal.countDown();
       
   134     }
       
   135 
       
   136     private static String convertMillisToTimeStr(int millis) {
       
   137         if (millis < 0) {
       
   138             return "00:00:00";
       
   139         }
       
   140         int hours = millis / 3600000;
       
   141         int minutes = (millis - hours * 3600000) / 60000;
       
   142         int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000;
       
   143         return String.format("%02d:%02d:%02d", hours, minutes, seconds);
       
   144     }
       
   145 
       
   146     private static void createAndShowTestDialog() {
       
   147         String description =
       
   148             " 1. Click on \"Start Test\" button.\r\n" +
       
   149             " 2. Multiple strings will be displayed on console.\r\n" +
       
   150             " 3. A print dialog will be shown. Select any printer to print. " +
       
   151             "\r\n" +
       
   152             " If the printed output of the strings are same without any alignment issue, click on \"PASS\"\r\n" +
       
   153             " button, otherwise click on \"FAIL\" button.";
       
   154 
       
   155         final JDialog dialog = new JDialog();
       
   156         dialog.setTitle("SaveFileWithoutPrinter");
       
   157         dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
       
   158         dialog.addWindowListener(new WindowAdapter() {
       
   159             @Override
       
   160             public void windowClosing(WindowEvent e) {
       
   161                 dialog.dispose();
       
   162                 fail("Main dialog was closed.");
       
   163             }
       
   164         });
       
   165 
       
   166         final JLabel testTimeoutLabel = new JLabel(String.format(
       
   167             "Test timeout: %s", convertMillisToTimeStr(testTimeout)));
       
   168         final long startTime = System.currentTimeMillis();
       
   169         final Timer timer = new Timer(0, null);
       
   170         timer.setDelay(1000);
       
   171         timer.addActionListener((e) -> {
       
   172             int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime);
       
   173             if ((leftTime < 0) || testFinished) {
       
   174                 timer.stop();
       
   175                 dialog.dispose();
       
   176             }
       
   177             testTimeoutLabel.setText(String.format(
       
   178                 "Test timeout: %s", convertMillisToTimeStr(leftTime)));
       
   179         });
       
   180         timer.start();
       
   181 
       
   182         JTextArea textArea = new JTextArea(description);
       
   183         textArea.setEditable(false);
       
   184 
       
   185         final JButton testButton = new JButton("Start Test");
       
   186         final JButton passButton = new JButton("PASS");
       
   187         final JButton failButton = new JButton("FAIL");
       
   188         testButton.addActionListener((e) -> {
       
   189             testButton.setEnabled(false);
       
   190             new Thread(() -> {
       
   191                 try {
       
   192                     doTest();
       
   193 
       
   194                     SwingUtilities.invokeLater(() -> {
       
   195                         passButton.setEnabled(true);
       
   196                         failButton.setEnabled(true);
       
   197                     });
       
   198                 } catch (Throwable t) {
       
   199                     t.printStackTrace();
       
   200                     dialog.dispose();
       
   201                     fail("Exception occurred in a thread executing the test.");
       
   202                 }
       
   203             }).start();
       
   204         });
       
   205         passButton.setEnabled(false);
       
   206         passButton.addActionListener((e) -> {
       
   207             dialog.dispose();
       
   208             pass();
       
   209         });
       
   210         failButton.setEnabled(false);
       
   211         failButton.addActionListener((e) -> {
       
   212             dialog.dispose();
       
   213             fail("Printed texts are not aligned as shown in console");
       
   214         });
       
   215 
       
   216         JPanel mainPanel = new JPanel(new BorderLayout());
       
   217         JPanel labelPanel = new JPanel(new FlowLayout());
       
   218         labelPanel.add(testTimeoutLabel);
       
   219         mainPanel.add(labelPanel, BorderLayout.NORTH);
       
   220         mainPanel.add(textArea, BorderLayout.CENTER);
       
   221         JPanel buttonPanel = new JPanel(new FlowLayout());
       
   222         buttonPanel.add(testButton);
       
   223         buttonPanel.add(passButton);
       
   224         buttonPanel.add(failButton);
       
   225         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
       
   226         dialog.add(mainPanel);
       
   227 
       
   228         dialog.pack();
       
   229         dialog.setVisible(true);
       
   230     }
       
   231 
       
   232     @Override
       
   233     public int print(Graphics pg, PageFormat pf, int pageNum)
       
   234         throws PrinterException {
       
   235         if (pageNum > 0){
       
   236             return Printable.NO_SUCH_PAGE;
       
   237         }
       
   238 
       
   239         Graphics2D g2 = (Graphics2D) pg;
       
   240         g2.translate(pf.getImageableX(), pf.getImageableY());
       
   241         panel.paint(g2);
       
   242         return Printable.PAGE_EXISTS;
       
   243     }
       
   244 }