|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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.awt.Component; |
| 25 | +import java.awt.Graphics; |
| 26 | +import java.awt.image.BufferedImage; |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.concurrent.atomic.AtomicReference; |
| 30 | +import java.util.function.Consumer; |
| 31 | +import javax.imageio.ImageIO; |
| 32 | +import javax.swing.JEditorPane; |
| 33 | +import javax.swing.SwingUtilities; |
| 34 | +import javax.swing.text.GlyphView; |
| 35 | +import javax.swing.text.View; |
| 36 | + |
| 37 | +/* |
| 38 | + * @test |
| 39 | + * @bug 8257664 |
| 40 | + * @summary Tests inherited font-size with parent percentage specification. |
| 41 | + * @run main TestWrongCSSFontSize |
| 42 | + */ |
| 43 | +public class TestWrongCSSFontSize { |
| 44 | + |
| 45 | + private static String text = |
| 46 | + "<html><head><style>" + |
| 47 | + "body { font-size: 14 }" + |
| 48 | + "div span { font-size: 150% }" + |
| 49 | + "span { font-size: 200% }" + |
| 50 | + "h2, .h2 { font-size: 150% }" + |
| 51 | + "</style></head><body>" + |
| 52 | + |
| 53 | + "<h2>Foo</h2>" + |
| 54 | + "<div class=h2>Bar</div>" + |
| 55 | + "<ol class=h2><li>Baz</li></ol>" + |
| 56 | + "<table class=h2><tr><td>Qux</td></tr></table>" + |
| 57 | + "<table><thead class=h2><tr><th>Qux</th></tr></thead></table>" + |
| 58 | + "<table><tr class=h2><td>Qux</td></tr></table>" + |
| 59 | + "<table><tr><td class=h2>Qux</td></tr></table>" + |
| 60 | + "<div><span>Quux</span></div>" + |
| 61 | + |
| 62 | + "</body></html>"; |
| 63 | + |
| 64 | + private static int expectedFontSize = 21; |
| 65 | + private static int expectedAssertions = 8; |
| 66 | + |
| 67 | + private JEditorPane editor; |
| 68 | + |
| 69 | + public void setUp() { |
| 70 | + editor = new JEditorPane(); |
| 71 | + editor.setContentType("text/html"); |
| 72 | + editor.setText(text); |
| 73 | + editor.setSize(editor.getPreferredSize()); // layout |
| 74 | + } |
| 75 | + |
| 76 | + public void run() { |
| 77 | + int count = forEachTextRun(editor.getUI() |
| 78 | + .getRootView(editor), this::assertFontSize); |
| 79 | + if (count != expectedAssertions) { |
| 80 | + throw new AssertionError("assertion count expected [" |
| 81 | + + expectedAssertions + "] but found [" + count + "]"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private int forEachTextRun(View view, Consumer<GlyphView> action) { |
| 86 | + int tested = 0; |
| 87 | + for (int i = 0; i < view.getViewCount(); i++) { |
| 88 | + View child = view.getView(i); |
| 89 | + if (child instanceof GlyphView) { |
| 90 | + if (child.getElement() |
| 91 | + .getAttributes().getAttribute("CR") == Boolean.TRUE) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + action.accept((GlyphView) child); |
| 95 | + tested += 1; |
| 96 | + } else { |
| 97 | + tested += forEachTextRun(child, action); |
| 98 | + } |
| 99 | + } |
| 100 | + return tested; |
| 101 | + } |
| 102 | + |
| 103 | + private void assertFontSize(GlyphView child) { |
| 104 | + printSource(child); |
| 105 | + int actualFontSize = child.getFont().getSize(); |
| 106 | + if (actualFontSize != expectedFontSize) { |
| 107 | + throw new AssertionError("font size expected [" |
| 108 | + + expectedFontSize + "] but found [" + actualFontSize +"]"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void printSource(View textRun) { |
| 113 | + try { |
| 114 | + editor.getEditorKit().write(System.out, |
| 115 | + editor.getDocument(), textRun.getStartOffset(), |
| 116 | + textRun.getEndOffset() - textRun.getStartOffset()); |
| 117 | + } catch (Exception e) { |
| 118 | + e.printStackTrace(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static void captureImage(Component comp, String path) { |
| 123 | + try { |
| 124 | + BufferedImage capture = new BufferedImage(comp.getWidth(), |
| 125 | + comp.getHeight(), BufferedImage.TYPE_INT_ARGB); |
| 126 | + Graphics g = capture.getGraphics(); |
| 127 | + comp.paint(g); |
| 128 | + g.dispose(); |
| 129 | + |
| 130 | + ImageIO.write(capture, "png", new File(path)); |
| 131 | + } catch (IOException e) { |
| 132 | + e.printStackTrace(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public static void main(String[] args) throws Throwable { |
| 137 | + TestWrongCSSFontSize test = new TestWrongCSSFontSize(); |
| 138 | + AtomicReference<Throwable> failure = new AtomicReference<>(); |
| 139 | + SwingUtilities.invokeAndWait(() -> { |
| 140 | + try { |
| 141 | + test.setUp(); |
| 142 | + test.run(); |
| 143 | + } catch (Throwable e) { |
| 144 | + failure.set(e); |
| 145 | + } finally { |
| 146 | + if (args.length == 1) { |
| 147 | + captureImage(test.editor, args[0]); |
| 148 | + } |
| 149 | + } |
| 150 | + }); |
| 151 | + if (failure.get() != null) { |
| 152 | + throw failure.get(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments