|
| 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 8226464 |
| 26 | + @summary Verifies TitledBorder label is not cutoff in hidpi for SynthL&F. |
| 27 | + @run main/manual TitledBorderLabel |
| 28 | + */ |
| 29 | + |
| 30 | +import java.awt.BorderLayout; |
| 31 | +import java.awt.Dimension; |
| 32 | +import java.awt.FlowLayout; |
| 33 | +import java.awt.Graphics; |
| 34 | +import java.awt.Graphics2D; |
| 35 | +import java.awt.Font; |
| 36 | +import java.awt.event.WindowAdapter; |
| 37 | +import java.awt.event.WindowEvent; |
| 38 | +import java.util.concurrent.CountDownLatch; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | +import javax.swing.BorderFactory; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.JButton; |
| 43 | +import javax.swing.JDialog; |
| 44 | +import javax.swing.JLabel; |
| 45 | +import javax.swing.JPanel; |
| 46 | +import javax.swing.JScrollPane; |
| 47 | +import javax.swing.JTextArea; |
| 48 | +import javax.swing.ScrollPaneConstants; |
| 49 | +import javax.swing.SwingUtilities; |
| 50 | +import javax.swing.SwingConstants; |
| 51 | +import javax.swing.WindowConstants; |
| 52 | +import javax.swing.Timer; |
| 53 | +import javax.swing.UIManager; |
| 54 | + |
| 55 | +public class TitledBorderLabel { |
| 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 JPanel panel; |
| 62 | + private static JFrame frame; |
| 63 | + |
| 64 | + public static void main(String[] args) throws Exception { |
| 65 | + System.setProperty( "sun.java2d.uiScale", "1.5" ); |
| 66 | + SwingUtilities.invokeLater(() -> createAndShowTestDialog()); |
| 67 | + |
| 68 | + try { |
| 69 | + if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) { |
| 70 | + throw new RuntimeException(String.format( |
| 71 | + "Test timeout '%d ms' elapsed.", testTimeout)); |
| 72 | + } |
| 73 | + if (!testPassed) { |
| 74 | + String failureMsg = testFailureMsg; |
| 75 | + if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) { |
| 76 | + throw new RuntimeException(failureMsg); |
| 77 | + } else { |
| 78 | + throw new RuntimeException("Test failed."); |
| 79 | + } |
| 80 | + } |
| 81 | + } catch (InterruptedException ie) { |
| 82 | + throw new RuntimeException(ie); |
| 83 | + } finally { |
| 84 | + testFinished = true; |
| 85 | + SwingUtilities.invokeAndWait(() -> frame.dispose()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static void doTest() throws Exception { |
| 90 | + UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel"); |
| 91 | + frame = new JFrame("TitledBorderLabelTester"); |
| 92 | + JPanel panel = new JPanel(); |
| 93 | + panel.setBorder(BorderFactory.createTitledBorder("CERTIFICATE CERTIFICATE CERTIFICATE CERTIFICATE")); |
| 94 | + frame.getContentPane().add(panel); |
| 95 | + frame.setPreferredSize(new Dimension(500, 150)); |
| 96 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 97 | + frame.pack(); |
| 98 | + frame.setLocationRelativeTo(null); |
| 99 | + frame.setVisible(true); |
| 100 | + } |
| 101 | + |
| 102 | + private static void pass() { |
| 103 | + testPassed = true; |
| 104 | + testEndedSignal.countDown(); |
| 105 | + } |
| 106 | + |
| 107 | + private static void fail(String failureMsg) { |
| 108 | + testFailureMsg = failureMsg; |
| 109 | + testPassed = false; |
| 110 | + testEndedSignal.countDown(); |
| 111 | + } |
| 112 | + |
| 113 | + private static String convertMillisToTimeStr(int millis) { |
| 114 | + if (millis < 0) { |
| 115 | + return "00:00:00"; |
| 116 | + } |
| 117 | + int hours = millis / 3600000; |
| 118 | + int minutes = (millis - hours * 3600000) / 60000; |
| 119 | + int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000; |
| 120 | + return String.format("%02d:%02d:%02d", hours, minutes, seconds); |
| 121 | + } |
| 122 | + |
| 123 | + private static void createAndShowTestDialog() { |
| 124 | + String description = |
| 125 | + " 1. Click on \"Start Test\" button.\r\n" + |
| 126 | + " 2. A TitledBorder with text \"CERTIFICATE CERTIFICATE CERTIFICATE CERTIFICATE\" is shown. \r\n" + |
| 127 | + " 3. Please verify if the text is shown fully and not truncated " + |
| 128 | + "\r\n" + |
| 129 | + " If the titledborder text is displayed fully,\r\n" + |
| 130 | + " click on \"PASS\" button, otherwise click on \"FAIL\" button."; |
| 131 | + |
| 132 | + final JDialog dialog = new JDialog(); |
| 133 | + dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 134 | + dialog.addWindowListener(new WindowAdapter() { |
| 135 | + @Override |
| 136 | + public void windowClosing(WindowEvent e) { |
| 137 | + dialog.dispose(); |
| 138 | + fail("Main dialog was closed."); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + final JLabel testTimeoutLabel = new JLabel(String.format( |
| 143 | + "Test timeout: %s", convertMillisToTimeStr(testTimeout))); |
| 144 | + final long startTime = System.currentTimeMillis(); |
| 145 | + final Timer timer = new Timer(0, null); |
| 146 | + timer.setDelay(1000); |
| 147 | + timer.addActionListener((e) -> { |
| 148 | + int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime); |
| 149 | + if ((leftTime < 0) || testFinished) { |
| 150 | + timer.stop(); |
| 151 | + dialog.dispose(); |
| 152 | + } |
| 153 | + testTimeoutLabel.setText(String.format( |
| 154 | + "Test timeout: %s", convertMillisToTimeStr(leftTime))); |
| 155 | + }); |
| 156 | + timer.start(); |
| 157 | + |
| 158 | + JTextArea textArea = new JTextArea(description); |
| 159 | + textArea.setEditable(false); |
| 160 | + |
| 161 | + final JButton testButton = new JButton("Start Test"); |
| 162 | + final JButton passButton = new JButton("PASS"); |
| 163 | + final JButton failButton = new JButton("FAIL"); |
| 164 | + testButton.addActionListener((e) -> { |
| 165 | + testButton.setEnabled(false); |
| 166 | + new Thread(() -> { |
| 167 | + try { |
| 168 | + doTest(); |
| 169 | + |
| 170 | + SwingUtilities.invokeLater(() -> { |
| 171 | + passButton.setEnabled(true); |
| 172 | + failButton.setEnabled(true); |
| 173 | + }); |
| 174 | + } catch (Throwable t) { |
| 175 | + t.printStackTrace(); |
| 176 | + dialog.dispose(); |
| 177 | + fail("Exception occurred in a thread executing the test."); |
| 178 | + } |
| 179 | + }).start(); |
| 180 | + }); |
| 181 | + passButton.setEnabled(false); |
| 182 | + passButton.addActionListener((e) -> { |
| 183 | + dialog.dispose(); |
| 184 | + pass(); |
| 185 | + }); |
| 186 | + failButton.setEnabled(false); |
| 187 | + failButton.addActionListener((e) -> { |
| 188 | + dialog.dispose(); |
| 189 | + fail("TitledBorder label is cut off"); |
| 190 | + }); |
| 191 | + |
| 192 | + JPanel mainPanel = new JPanel(new BorderLayout()); |
| 193 | + JPanel labelPanel = new JPanel(new FlowLayout()); |
| 194 | + labelPanel.add(testTimeoutLabel); |
| 195 | + mainPanel.add(labelPanel, BorderLayout.NORTH); |
| 196 | + mainPanel.add(textArea, BorderLayout.CENTER); |
| 197 | + JPanel buttonPanel = new JPanel(new FlowLayout()); |
| 198 | + buttonPanel.add(testButton); |
| 199 | + buttonPanel.add(passButton); |
| 200 | + buttonPanel.add(failButton); |
| 201 | + mainPanel.add(buttonPanel, BorderLayout.SOUTH); |
| 202 | + dialog.add(mainPanel); |
| 203 | + |
| 204 | + dialog.pack(); |
| 205 | + dialog.setVisible(true); |
| 206 | + } |
| 207 | + |
| 208 | +} |
| 209 | + |
0 commit comments