|
1 | 1 | /* |
2 | | - * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -/** |
| 24 | +/* |
25 | 25 | * @test |
26 | 26 | * @bug 6263951 |
27 | 27 | * @summary Text should be B&W, grayscale, and LCD. |
28 | | - * @run main/manual=yesno TextAAHintsTest |
| 28 | + * @requires (os.family != "mac") |
| 29 | + * @run main/manual TextAAHintsTest |
29 | 30 | */ |
30 | | -import java.awt.*; |
31 | | -import java.awt.geom.*; |
32 | | -import java.awt.image.*; |
| 31 | + |
| 32 | +import java.awt.AWTException; |
| 33 | +import java.awt.BorderLayout; |
| 34 | +import java.awt.Button; |
| 35 | +import java.awt.Color; |
| 36 | +import java.awt.Component; |
| 37 | +import java.awt.Dialog; |
| 38 | +import java.awt.Dimension; |
| 39 | +import java.awt.EventQueue; |
| 40 | +import java.awt.Frame; |
| 41 | +import java.awt.Graphics; |
| 42 | +import java.awt.Graphics2D; |
| 43 | +import java.awt.ImageCapabilities; |
| 44 | +import java.awt.Panel; |
| 45 | +import java.awt.RenderingHints; |
| 46 | +import java.awt.TextArea; |
| 47 | +import java.awt.image.BufferedImage; |
| 48 | +import java.awt.image.VolatileImage; |
| 49 | +import java.lang.reflect.InvocationTargetException; |
| 50 | +import java.util.concurrent.CountDownLatch; |
| 51 | +import java.util.concurrent.TimeUnit; |
33 | 52 |
|
34 | 53 | public class TextAAHintsTest extends Component { |
35 | 54 |
|
36 | | - String black = "This text should be solid black"; |
37 | | - String gray = "This text should be gray scale anti-aliased"; |
38 | | - String lcd = "This text should be LCD sub-pixel text (coloured)."; |
| 55 | + private static final String black = "This text should be solid black"; |
| 56 | + private static final String gray = "This text should be gray scale anti-aliased"; |
| 57 | + private static final String lcd = "This text should be LCD sub-pixel text (coloured)."; |
| 58 | + private static final CountDownLatch countDownLatch = new CountDownLatch(1); |
| 59 | + private static volatile String failureReason; |
| 60 | + private static volatile boolean testPassed = false; |
| 61 | + private static Frame frame; |
39 | 62 |
|
40 | 63 | public void paint(Graphics g) { |
41 | 64 |
|
@@ -63,28 +86,24 @@ private void drawText(Graphics g) { |
63 | 86 | RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); |
64 | 87 | g2d.drawString(black, 10, 20); |
65 | 88 |
|
66 | | - g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
67 | | - RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); |
68 | | - g2d.drawString(black, 10, 35); |
69 | | - |
70 | 89 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
71 | 90 | RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT); |
72 | | - g2d.drawString(gray, 10, 50); |
| 91 | + g2d.drawString(gray, 10, 35); |
73 | 92 |
|
74 | 93 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
75 | 94 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
76 | | - g2d.drawString(gray, 10, 65); |
| 95 | + g2d.drawString(gray, 10, 50); |
77 | 96 |
|
78 | 97 | /* For visual comparison, render grayscale with graphics AA off */ |
79 | 98 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
80 | 99 | RenderingHints.VALUE_ANTIALIAS_OFF); |
81 | | - g2d.drawString(gray, 10, 80); |
| 100 | + g2d.drawString(gray, 10, 65); |
82 | 101 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
83 | 102 | RenderingHints.VALUE_ANTIALIAS_ON); |
84 | 103 |
|
85 | 104 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
86 | 105 | RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); |
87 | | - g2d.drawString(lcd, 10, 95); |
| 106 | + g2d.drawString(lcd, 10, 80); |
88 | 107 | } |
89 | 108 |
|
90 | 109 | public void bufferedImageText(Graphics g) { |
@@ -139,11 +158,80 @@ public Dimension getPreferredSize() { |
139 | 158 | return new Dimension(500,300); |
140 | 159 | } |
141 | 160 |
|
142 | | - public static void main(String[] args) throws Exception { |
| 161 | + public static void createTestUI() { |
| 162 | + frame = new Frame("Composite and Text Test"); |
| 163 | + TextAAHintsTest textAAHintsTestObject = new TextAAHintsTest(); |
| 164 | + frame.add(textAAHintsTestObject, BorderLayout.NORTH); |
| 165 | + |
| 166 | + String instructions = """ |
| 167 | + Note: Texts are rendered with different TEXT_ANTIALIASING & |
| 168 | + VALUE_TEXT_ANTIALIAS. Text should be B&W, grayscale, and LCD. |
| 169 | + Note: The results may be visually the same. |
| 170 | + 1. Verify that first set of text are rendered correctly. |
| 171 | + 2. Second set of text are created using BufferedImage of the first text. |
| 172 | + 3. Third set of text are created using VolatileImage of the first text. |
| 173 | + """; |
| 174 | + TextArea instructionTextArea = new TextArea(instructions, 8, 50); |
| 175 | + instructionTextArea.setEditable(false); |
| 176 | + frame.add(instructionTextArea, BorderLayout.CENTER); |
| 177 | + |
| 178 | + Panel controlPanel = new Panel(); |
| 179 | + Button passButton = new Button("Pass"); |
| 180 | + passButton.addActionListener(e -> { |
| 181 | + testPassed = true; |
| 182 | + countDownLatch.countDown(); |
| 183 | + frame.dispose(); |
| 184 | + }); |
| 185 | + Button failButton = new Button("Fail"); |
| 186 | + failButton.addActionListener(e -> { |
| 187 | + getFailureReason(); |
| 188 | + testPassed = false; |
| 189 | + countDownLatch.countDown(); |
| 190 | + frame.dispose(); |
| 191 | + }); |
| 192 | + controlPanel.add(passButton); |
| 193 | + controlPanel.add(failButton); |
| 194 | + frame.add(controlPanel, BorderLayout.SOUTH); |
| 195 | + frame.pack(); |
| 196 | + frame.setLocationRelativeTo(null); |
| 197 | + frame.setVisible(true); |
| 198 | + } |
| 199 | + |
| 200 | + public static void getFailureReason() { |
| 201 | + // Show dialog to read why the testcase was failed and append the |
| 202 | + // testcase failure reason to the output |
| 203 | + final Dialog dialog = new Dialog(frame, "TestCase" + |
| 204 | + " failure reason", true); |
| 205 | + TextArea textArea = new TextArea("", 5, 60, TextArea.SCROLLBARS_BOTH); |
| 206 | + dialog.add(textArea, BorderLayout.CENTER); |
| 207 | + |
| 208 | + Button okButton = new Button("OK"); |
| 209 | + okButton.addActionListener(e1 -> { |
| 210 | + failureReason = textArea.getText(); |
| 211 | + dialog.dispose(); |
| 212 | + }); |
| 213 | + Panel ctlPanel = new Panel(); |
| 214 | + ctlPanel.add(okButton); |
| 215 | + dialog.add(ctlPanel, BorderLayout.SOUTH); |
| 216 | + dialog.setLocationRelativeTo(null); |
| 217 | + dialog.pack(); |
| 218 | + dialog.setVisible(true); |
| 219 | + } |
| 220 | + |
| 221 | + public static void main(String[] args) throws InterruptedException, InvocationTargetException { |
| 222 | + EventQueue.invokeAndWait(TextAAHintsTest::createTestUI); |
| 223 | + if (!countDownLatch.await(2, TimeUnit.MINUTES)) { |
| 224 | + EventQueue.invokeAndWait(() -> { |
| 225 | + if (frame != null) { |
| 226 | + frame.dispose(); |
| 227 | + } |
| 228 | + }); |
| 229 | + throw new RuntimeException("Timeout : No action was taken on the test."); |
| 230 | + } |
143 | 231 |
|
144 | | - Frame f = new Frame("Composite and Text Test"); |
145 | | - f.add(new TextAAHintsTest(), BorderLayout.CENTER); |
146 | | - f.pack(); |
147 | | - f.setVisible(true); |
| 232 | + if (!testPassed) { |
| 233 | + throw new RuntimeException("Test failed : Reason : " + failureReason); |
| 234 | + } |
148 | 235 | } |
149 | 236 | } |
| 237 | + |
0 commit comments