Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java
58462 views
1
/*
2
* Copyright (c) 2018, 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
* @key headful
27
* @bug 8195095 8206238 8208638
28
* @summary Tests if Images are scaled correctly in JEditorPane.
29
* @run main ImageViewTest
30
*/
31
import java.awt.Robot;
32
import java.awt.Point;
33
import java.awt.Color;
34
import java.awt.Insets;
35
36
import javax.swing.JEditorPane;
37
import javax.swing.SwingUtilities;
38
import javax.swing.JFrame;
39
import javax.swing.WindowConstants;
40
41
public class ImageViewTest {
42
43
private static JFrame f;
44
45
private static void test(Robot r, JEditorPane editorPane,
46
final int WIDTH, final int HEIGHT ) throws Exception {
47
48
SwingUtilities.invokeAndWait(() -> {
49
f = new JFrame();
50
editorPane.setEditable(false);
51
f.add(editorPane);
52
f.setSize(WIDTH + 20, HEIGHT + 40);
53
f.setLocationRelativeTo(null);
54
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
55
//This line will trigger the imageupdate, and consequently, the view
56
//will be populated with the appropriate color when the pixel color
57
//is queried by robot.
58
editorPane.getUI().getPreferredSize(editorPane);
59
f.setVisible(true);
60
});
61
62
r.waitForIdle();
63
r.delay(500);
64
65
SwingUtilities.invokeAndWait(() -> {
66
Insets insets = editorPane.getInsets();
67
Point loc = editorPane.getLocationOnScreen();
68
69
final Color blue = Color.BLUE;
70
final int offset = 10;
71
72
Color center = r.getPixelColor(loc.x + insets.left + WIDTH / 2,
73
loc.y + insets.top + HEIGHT / 2);
74
Color left = r.getPixelColor(loc.x + insets.left + offset,
75
loc.y + insets.top + HEIGHT / 2);
76
Color right = r.getPixelColor(loc.x + insets.left + WIDTH - offset,
77
loc.y + insets.top + HEIGHT / 2);
78
Color bottom = r.getPixelColor(loc.x + insets.left + WIDTH / 2,
79
loc.y + insets.top + HEIGHT - offset);
80
Color top = r.getPixelColor(loc.x + insets.left + WIDTH / 2,
81
loc.y + insets.top + offset);
82
83
f.dispose();
84
85
System.out.println("center color: " + center);
86
System.out.println("left color: " + left);
87
System.out.println("right color: " + right);
88
System.out.println("bottom color: " + bottom);
89
System.out.println("top color: " + top);
90
System.out.println();
91
92
if (!(blue.equals(center) && blue.equals(left) && blue.equals(right) &&
93
blue.equals(top) && blue.equals(bottom))) {
94
throw new RuntimeException("Test failed: Image not scaled correctly");
95
}
96
});
97
98
r.waitForIdle();
99
}
100
101
public static void main(String[] args) throws Exception {
102
103
final String ABSOLUTE_FILE_PATH = ImageViewTest.class.getResource("circle.png").getPath();
104
105
System.out.println(ABSOLUTE_FILE_PATH);
106
107
Robot r = new Robot();
108
109
final JEditorPane[] editorPanes = new JEditorPane[11];
110
111
SwingUtilities.invokeAndWait(() -> {
112
editorPanes[0] = new JEditorPane("text/html",
113
"<img height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
114
115
editorPanes[1] = new JEditorPane("text/html",
116
"<img width=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
117
118
editorPanes[2] = new JEditorPane("text/html",
119
"<img width=\"200\" height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
120
121
editorPanes[3] = new JEditorPane("text/html",
122
"<img src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
123
124
editorPanes[4] = new JEditorPane("text/html",
125
"<img width=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
126
127
editorPanes[5] = new JEditorPane("text/html",
128
"<img height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
129
130
editorPanes[6] = new JEditorPane("text/html",
131
"<img width=\"100\" height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
132
133
editorPanes[7] = new JEditorPane("text/html",
134
"<img width=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
135
136
editorPanes[8] = new JEditorPane("text/html",
137
"<img height=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
138
139
editorPanes[9] = new JEditorPane("text/html",
140
"<img width=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
141
142
editorPanes[10] = new JEditorPane("text/html",
143
"<img height=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
144
145
});
146
147
r.waitForIdle();
148
149
System.out.println("Test with only height set to 200");
150
test(r, editorPanes[0], 200, 200);
151
152
System.out.println("Test with only width set to 200");
153
test(r, editorPanes[1], 200, 200);
154
155
System.out.println("Test with both of them set");
156
test(r, editorPanes[2], 200, 200);
157
158
System.out.println("Test with none of them set to 200");
159
test(r, editorPanes[3], 200, 200);
160
161
System.out.println("Test with only width set to 100");
162
test(r, editorPanes[4], 100, 100);
163
164
System.out.println("Test with only height set to 100");
165
test(r, editorPanes[5], 100, 100);
166
167
System.out.println("Test with both width and height set to 100");
168
test(r, editorPanes[6], 100, 100);
169
170
System.out.println("Test with only width set to 50");
171
test(r, editorPanes[7], 50, 50);
172
173
System.out.println("Test with only height set to 50");
174
test(r, editorPanes[8], 50, 50);
175
176
System.out.println("Test with only width set to 300");
177
test(r, editorPanes[9], 300, 300);
178
179
System.out.println("Test with only height set to 300");
180
test(r, editorPanes[10], 300, 300);
181
182
System.out.println("Test Passed.");
183
}
184
}
185
186