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