1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
* Copyright (c) 2019, 2020, 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.
*/
/*
* @test
* @bug 8224475 8238985
* @summary Verify that JTextPane renders images properly for HTML text
* @run main/manual TestJTextPaneHTMLRendering
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Image;
import java.net.URL;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.EditorKit;
import javax.swing.SwingUtilities;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class TestJTextPaneHTMLRendering {
private static JFrame mainFrame = new JFrame();
private static Dictionary<URL, Image> cache;
private static JTextPane textPane;
private static URL urlArrow;
private static volatile boolean testResult = false;
private static volatile CountDownLatch countDownLatch;
private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n" +
"Verify that the JTextPane is filled with blue arrow images.\n" +
"There should be 200 images (10 rows of 20 images each).\n" +
"This test will run for 10 iterations and the current iteration\n" +
"is being displayed at top of JTextPane. JTextpane will be\n" +
"repainted each time and should have same output\n"+
"If yes, Press Pass, Otherwise, Press Fail.\n";
public static void main(String args[]) throws Exception {
urlArrow = new URL("http:\\arrow.png");
countDownLatch = new CountDownLatch(1);
SwingUtilities.invokeLater(TestJTextPaneHTMLRendering::createUI);
countDownLatch.await(15, TimeUnit.MINUTES);
SwingUtilities.invokeLater(mainFrame::dispose);
if (!testResult) {
throw new RuntimeException("Test failed!");
}
}
private static void createUI() {
JPanel mainControlPanel = new JPanel(new BorderLayout(20, 20));
JPanel resultButtonPanel = new JPanel(new GridBagLayout());
createTestUI(mainControlPanel);
JTextArea instructionTextArea = new JTextArea();
instructionTextArea.setText(INSTRUCTIONS);
instructionTextArea.setEditable(false);
instructionTextArea.setBackground(Color.white);
mainControlPanel.add(instructionTextArea, BorderLayout.NORTH);
JButton passButton = new JButton("Pass");
passButton.setActionCommand("Pass");
passButton.addActionListener((ActionEvent e) -> {
testResult = true;
countDownLatch.countDown();
});
JButton failButton = new JButton("Fail");
failButton.setActionCommand("Fail");
failButton.addActionListener(e -> {
countDownLatch.countDown();
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
resultButtonPanel.add(passButton, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
resultButtonPanel.add(failButton, gbc);
mainControlPanel.add(resultButtonPanel, BorderLayout.SOUTH);
mainFrame.add(mainControlPanel);
mainFrame.pack();
mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
mainFrame.dispose();
countDownLatch.countDown();
}
});
mainFrame.setVisible(true);
}
static void createTestUI(JPanel panel) {
textPane = new JTextPane();
panel.add(textPane, BorderLayout.CENTER);
final EditorKit l_kit = textPane.getEditorKitForContentType("text/html");
textPane.setEditable(false);
textPane.setEditorKit(l_kit);
cache = (Dictionary<URL, Image>)textPane.getDocument().getProperty("imageCache");
if (cache==null) {
cache=new Hashtable<URL, Image>();
textPane.getDocument().putProperty("imageCache",cache);
}
URL arrowLocationUrl = TestJTextPaneHTMLRendering.class.getResource("arrow.png");
ImageIcon imageIcon = new ImageIcon(arrowLocationUrl);
Image image = imageIcon.getImage();
Image scaledImage = image.getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH);
cache.put(urlArrow, scaledImage);
new Thread(TestJTextPaneHTMLRendering::runTest).start();
}
static void runTest() {
for (int i=0; i < 10; i++)
{
StringBuffer sb = new StringBuffer();
sb.append("<html><body bgcolor=\"#BBBBBB\"><center>Iteration " + (i+1) + " -> " + "<br>");
for (int j=1;j<201;j++)
{
sb.append("<img src=\"" + urlArrow + "\">");
if (j%20 == 0) sb.append("<br>");
}
textPane.setText(sb.toString());
textPane.validate();
textPane.repaint();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) { System.err.println(e); }
}
}
}
|