-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8292948: JEditorPane ignores font-size styles in external linked css-…
…file Backport-of: 1caba0f13c42121c9e1c6648715ec7c31349b537
- Loading branch information
1 parent
541a284
commit ffa8a10
Showing
4 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
test/jdk/javax/swing/text/html/StyleSheet/TestExternalCSSFontSize.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.base { | ||
font: 16px sans-serif; | ||
font-size: 16; | ||
} | ||
|
||
.bigger { | ||
font-weight: bold; | ||
font-size: 150%; | ||
} | ||
|
||
.smaller { | ||
font-style: italic; | ||
font-size: 75%; | ||
} |
21 changes: 21 additions & 0 deletions
21
test/jdk/javax/swing/text/html/StyleSheet/TestExternalCSSFontSize.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<title>JDK-8292948: JEditorPane ignores font-size styles in external linked css-file</title> | ||
<link rel="stylesheet" href="TestExternalCSSFontSize.css" type="text/css"> | ||
</head> | ||
<body> | ||
|
||
<div class="base"> | ||
|
||
<p class="bigger">Bigger text (24)</p> | ||
|
||
<p>Normal size text (16)</p> | ||
|
||
<p class="smaller">Smaller text (12)</p> | ||
|
||
</div> | ||
|
||
</body> | ||
</html> |
171 changes: 171 additions & 0 deletions
171
test/jdk/javax/swing/text/html/StyleSheet/TestExternalCSSFontSize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import java.awt.Graphics; | ||
import java.awt.image.BufferedImage; | ||
import javax.imageio.ImageIO; | ||
import javax.swing.JEditorPane; | ||
import javax.swing.SwingUtilities; | ||
import javax.swing.text.GlyphView; | ||
import javax.swing.text.View; | ||
|
||
/* | ||
* @test | ||
* @bug 8292948 | ||
* @summary Tests font-size declarations from an external style sheet. | ||
* @run main TestExternalCSSFontSize | ||
*/ | ||
public class TestExternalCSSFontSize { | ||
|
||
private static final int[] expectedFontSizes = { 24, 16, 12 }; | ||
|
||
private volatile JEditorPane editor; | ||
|
||
private volatile CountDownLatch loadLatch; | ||
|
||
TestExternalCSSFontSize() {} | ||
|
||
void setUp() { | ||
String fileName = getClass().getName().replace('.', '/') + ".html"; | ||
URL htmlFile = getClass().getClassLoader().getResource(fileName); | ||
if (htmlFile == null) { | ||
throw new IllegalStateException("Resource not found: " + fileName); | ||
} | ||
|
||
loadLatch = new CountDownLatch(1); | ||
editor = new JEditorPane(); | ||
editor.setContentType("text/html"); | ||
editor.addPropertyChangeListener("page", evt -> { | ||
System.out.append("Loaded: ").println(evt.getNewValue()); | ||
loadLatch.countDown(); | ||
}); | ||
try { | ||
editor.setPage(htmlFile); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
void verify() { | ||
editor.setSize(editor.getPreferredSize()); // Do lay out text | ||
|
||
scanFontSizes(editor.getUI().getRootView(editor), 0); | ||
} | ||
|
||
private int scanFontSizes(View view, int branchIndex) { | ||
int currentIndex = branchIndex; | ||
for (int i = 0; i < view.getViewCount(); i++) { | ||
View child = view.getView(i); | ||
if (child instanceof GlyphView) { | ||
if (child.getElement() | ||
.getAttributes().getAttribute("CR") == Boolean.TRUE) { | ||
continue; | ||
} | ||
assertFontSize((GlyphView) child, currentIndex++); | ||
} else { | ||
currentIndex = scanFontSizes(child, currentIndex); | ||
} | ||
} | ||
return currentIndex; | ||
} | ||
|
||
private void assertFontSize(GlyphView child, int index) { | ||
printSource(child); | ||
if (index >= expectedFontSizes.length) { | ||
throw new AssertionError("Unexpected text run #" | ||
+ index + " (>= " + expectedFontSizes.length + ")"); | ||
} | ||
|
||
int actualFontSize = child.getFont().getSize(); | ||
if (actualFontSize != expectedFontSizes[index]) { | ||
throw new AssertionError("Font size expected [" | ||
+ expectedFontSizes[index] + "] but found [" + actualFontSize +"]"); | ||
} | ||
} | ||
|
||
private void printSource(View textRun) { | ||
try { | ||
editor.getEditorKit().write(System.out, | ||
editor.getDocument(), textRun.getStartOffset(), | ||
textRun.getEndOffset() - textRun.getStartOffset()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
void run() throws Throwable { | ||
SwingUtilities.invokeAndWait(this::setUp); | ||
if (loadLatch.await(5, TimeUnit.SECONDS)) { | ||
SwingUtilities.invokeAndWait(this::verify); | ||
} else { | ||
throw new IllegalStateException("Page loading timed out"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Throwable { | ||
TestExternalCSSFontSize test = new TestExternalCSSFontSize(); | ||
boolean success = false; | ||
try { | ||
test.run(); | ||
success = true; | ||
} finally { | ||
if (!success || hasOpt(args, "-capture")) { | ||
if (test.editor == null) { | ||
System.err.println("Can't save image (test.editor is null)"); | ||
} else { | ||
String suffix = success ? "-success" : "-failure"; | ||
SwingUtilities.invokeAndWait(() -> test.captureImage(suffix)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static boolean hasOpt(String[] args, String opt) { | ||
return Arrays.asList(args).contains(opt); | ||
} | ||
|
||
private void captureImage(String suffix) { | ||
try { | ||
BufferedImage capture = | ||
new BufferedImage(editor.getWidth(), editor.getHeight(), | ||
BufferedImage.TYPE_INT_ARGB); | ||
Graphics g = capture.getGraphics(); | ||
editor.paint(g); | ||
g.dispose(); | ||
|
||
ImageIO.write(capture, "png", | ||
new File(getClass().getSimpleName() + suffix + ".png")); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
ffa8a10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Issues