|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.net.URL; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.concurrent.CountDownLatch; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +import java.awt.Graphics; |
| 33 | +import java.awt.image.BufferedImage; |
| 34 | +import javax.imageio.ImageIO; |
| 35 | +import javax.swing.JEditorPane; |
| 36 | +import javax.swing.SwingUtilities; |
| 37 | +import javax.swing.text.GlyphView; |
| 38 | +import javax.swing.text.View; |
| 39 | + |
| 40 | +/* |
| 41 | + * @test |
| 42 | + * @bug 8292948 |
| 43 | + * @summary Tests font-size declarations from an external style sheet. |
| 44 | + * @run main TestExternalCSSFontSize |
| 45 | + */ |
| 46 | +public class TestExternalCSSFontSize { |
| 47 | + |
| 48 | + private static final int[] expectedFontSizes = { 24, 16, 12 }; |
| 49 | + |
| 50 | + private volatile JEditorPane editor; |
| 51 | + |
| 52 | + private volatile CountDownLatch loadLatch; |
| 53 | + |
| 54 | + TestExternalCSSFontSize() {} |
| 55 | + |
| 56 | + void setUp() { |
| 57 | + String fileName = getClass().getName().replace('.', '/') + ".html"; |
| 58 | + URL htmlFile = getClass().getClassLoader().getResource(fileName); |
| 59 | + if (htmlFile == null) { |
| 60 | + throw new IllegalStateException("Resource not found: " + fileName); |
| 61 | + } |
| 62 | + |
| 63 | + loadLatch = new CountDownLatch(1); |
| 64 | + editor = new JEditorPane(); |
| 65 | + editor.setContentType("text/html"); |
| 66 | + editor.addPropertyChangeListener("page", evt -> { |
| 67 | + System.out.append("Loaded: ").println(evt.getNewValue()); |
| 68 | + loadLatch.countDown(); |
| 69 | + }); |
| 70 | + try { |
| 71 | + editor.setPage(htmlFile); |
| 72 | + } catch (IOException e) { |
| 73 | + throw new UncheckedIOException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + void verify() { |
| 78 | + editor.setSize(editor.getPreferredSize()); // Do lay out text |
| 79 | + |
| 80 | + scanFontSizes(editor.getUI().getRootView(editor), 0); |
| 81 | + } |
| 82 | + |
| 83 | + private int scanFontSizes(View view, int branchIndex) { |
| 84 | + int currentIndex = branchIndex; |
| 85 | + for (int i = 0; i < view.getViewCount(); i++) { |
| 86 | + View child = view.getView(i); |
| 87 | + if (child instanceof GlyphView) { |
| 88 | + if (child.getElement() |
| 89 | + .getAttributes().getAttribute("CR") == Boolean.TRUE) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + assertFontSize((GlyphView) child, currentIndex++); |
| 93 | + } else { |
| 94 | + currentIndex = scanFontSizes(child, currentIndex); |
| 95 | + } |
| 96 | + } |
| 97 | + return currentIndex; |
| 98 | + } |
| 99 | + |
| 100 | + private void assertFontSize(GlyphView child, int index) { |
| 101 | + printSource(child); |
| 102 | + if (index >= expectedFontSizes.length) { |
| 103 | + throw new AssertionError("Unexpected text run #" |
| 104 | + + index + " (>= " + expectedFontSizes.length + ")"); |
| 105 | + } |
| 106 | + |
| 107 | + int actualFontSize = child.getFont().getSize(); |
| 108 | + if (actualFontSize != expectedFontSizes[index]) { |
| 109 | + throw new AssertionError("Font size expected [" |
| 110 | + + expectedFontSizes[index] + "] but found [" + actualFontSize +"]"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void printSource(View textRun) { |
| 115 | + try { |
| 116 | + editor.getEditorKit().write(System.out, |
| 117 | + editor.getDocument(), textRun.getStartOffset(), |
| 118 | + textRun.getEndOffset() - textRun.getStartOffset()); |
| 119 | + } catch (Exception e) { |
| 120 | + e.printStackTrace(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + void run() throws Throwable { |
| 125 | + SwingUtilities.invokeAndWait(this::setUp); |
| 126 | + if (loadLatch.await(5, TimeUnit.SECONDS)) { |
| 127 | + SwingUtilities.invokeAndWait(this::verify); |
| 128 | + } else { |
| 129 | + throw new IllegalStateException("Page loading timed out"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public static void main(String[] args) throws Throwable { |
| 134 | + TestExternalCSSFontSize test = new TestExternalCSSFontSize(); |
| 135 | + boolean success = false; |
| 136 | + try { |
| 137 | + test.run(); |
| 138 | + success = true; |
| 139 | + } finally { |
| 140 | + if (!success || hasOpt(args, "-capture")) { |
| 141 | + if (test.editor == null) { |
| 142 | + System.err.println("Can't save image (test.editor is null)"); |
| 143 | + } else { |
| 144 | + String suffix = success ? "-success" : "-failure"; |
| 145 | + SwingUtilities.invokeAndWait(() -> test.captureImage(suffix)); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private static boolean hasOpt(String[] args, String opt) { |
| 152 | + return Arrays.asList(args).contains(opt); |
| 153 | + } |
| 154 | + |
| 155 | + private void captureImage(String suffix) { |
| 156 | + try { |
| 157 | + BufferedImage capture = |
| 158 | + new BufferedImage(editor.getWidth(), editor.getHeight(), |
| 159 | + BufferedImage.TYPE_INT_ARGB); |
| 160 | + Graphics g = capture.getGraphics(); |
| 161 | + editor.paint(g); |
| 162 | + g.dispose(); |
| 163 | + |
| 164 | + ImageIO.write(capture, "png", |
| 165 | + new File(getClass().getSimpleName() + suffix + ".png")); |
| 166 | + } catch (IOException e) { |
| 167 | + e.printStackTrace(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments