Path: blob/master/test/jdk/javax/swing/JTextPane/TestJTextPaneHTMLRendering.java
41152 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8224475 823898526* @summary Verify that JTextPane renders images properly for HTML text27* @run main/manual TestJTextPaneHTMLRendering28*/2930import java.awt.BorderLayout;31import java.awt.Color;32import java.awt.GridBagConstraints;33import java.awt.GridBagLayout;34import java.awt.event.ActionEvent;35import java.awt.event.WindowAdapter;36import java.awt.event.WindowEvent;37import java.awt.Image;38import java.net.URL;39import java.util.Dictionary;40import java.util.Hashtable;41import javax.swing.JButton;42import javax.swing.JTextArea;43import javax.swing.ImageIcon;44import javax.swing.JFrame;45import javax.swing.JPanel;46import javax.swing.JTextPane;47import javax.swing.text.EditorKit;48import javax.swing.SwingUtilities;49import java.util.concurrent.CountDownLatch;50import java.util.concurrent.TimeUnit;515253public class TestJTextPaneHTMLRendering {54private static JFrame mainFrame = new JFrame();55private static Dictionary<URL, Image> cache;56private static JTextPane textPane;57private static URL urlArrow;5859private static volatile boolean testResult = false;60private static volatile CountDownLatch countDownLatch;61private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n" +62"Verify that the JTextPane is filled with blue arrow images.\n" +63"There should be 200 images (10 rows of 20 images each).\n" +64"This test will run for 10 iterations and the current iteration\n" +65"is being displayed at top of JTextPane. JTextpane will be\n" +66"repainted each time and should have same output\n"+67"If yes, Press Pass, Otherwise, Press Fail.\n";6869public static void main(String args[]) throws Exception {70urlArrow = new URL("http:\\arrow.png");71countDownLatch = new CountDownLatch(1);7273SwingUtilities.invokeLater(TestJTextPaneHTMLRendering::createUI);74countDownLatch.await(15, TimeUnit.MINUTES);75SwingUtilities.invokeLater(mainFrame::dispose);7677if (!testResult) {78throw new RuntimeException("Test failed!");79}80}8182private static void createUI() {83JPanel mainControlPanel = new JPanel(new BorderLayout(20, 20));84JPanel resultButtonPanel = new JPanel(new GridBagLayout());8586createTestUI(mainControlPanel);8788JTextArea instructionTextArea = new JTextArea();89instructionTextArea.setText(INSTRUCTIONS);90instructionTextArea.setEditable(false);91instructionTextArea.setBackground(Color.white);92mainControlPanel.add(instructionTextArea, BorderLayout.NORTH);9394JButton passButton = new JButton("Pass");95passButton.setActionCommand("Pass");96passButton.addActionListener((ActionEvent e) -> {97testResult = true;98countDownLatch.countDown();99100});101102JButton failButton = new JButton("Fail");103failButton.setActionCommand("Fail");104failButton.addActionListener(e -> {105countDownLatch.countDown();106});107108GridBagConstraints gbc = new GridBagConstraints();109gbc.gridx = 0;110gbc.gridy = 0;111112resultButtonPanel.add(passButton, gbc);113114gbc.gridx = 1;115gbc.gridy = 0;116resultButtonPanel.add(failButton, gbc);117118mainControlPanel.add(resultButtonPanel, BorderLayout.SOUTH);119120mainFrame.add(mainControlPanel);121mainFrame.pack();122123mainFrame.addWindowListener(new WindowAdapter() {124@Override125public void windowClosing(WindowEvent e) {126mainFrame.dispose();127countDownLatch.countDown();128}129});130mainFrame.setVisible(true);131}132133static void createTestUI(JPanel panel) {134textPane = new JTextPane();135panel.add(textPane, BorderLayout.CENTER);136137final EditorKit l_kit = textPane.getEditorKitForContentType("text/html");138textPane.setEditable(false);139textPane.setEditorKit(l_kit);140cache = (Dictionary<URL, Image>)textPane.getDocument().getProperty("imageCache");141if (cache==null) {142cache=new Hashtable<URL, Image>();143textPane.getDocument().putProperty("imageCache",cache);144}145146URL arrowLocationUrl = TestJTextPaneHTMLRendering.class.getResource("arrow.png");147ImageIcon imageIcon = new ImageIcon(arrowLocationUrl);148Image image = imageIcon.getImage();149Image scaledImage = image.getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH);150cache.put(urlArrow, scaledImage);151new Thread(TestJTextPaneHTMLRendering::runTest).start();152}153154static void runTest() {155for (int i=0; i < 10; i++)156{157StringBuffer sb = new StringBuffer();158sb.append("<html><body bgcolor=\"#BBBBBB\"><center>Iteration " + (i+1) + " -> " + "<br>");159for (int j=1;j<201;j++)160{161sb.append("<img src=\"" + urlArrow + "\">");162if (j%20 == 0) sb.append("<br>");163}164textPane.setText(sb.toString());165textPane.validate();166textPane.repaint();167try {168Thread.currentThread().sleep(1000);169} catch (InterruptedException e) { System.err.println(e); }170}171}172}173174175