|
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 |
|
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 final int WIDTH = 200; |
|
44 private static final int HEIGHT = 200; |
|
45 private static JFrame f; |
|
46 |
|
47 private static JEditorPane editorPane1; |
|
48 private static JEditorPane editorPane2; |
|
49 private static JEditorPane editorPane3; |
|
50 private static JEditorPane editorPane4; |
|
51 |
|
52 private static void test(Robot r, JEditorPane editorPane) throws Exception { |
|
53 |
|
54 SwingUtilities.invokeAndWait(() -> { |
|
55 f = new JFrame(); |
|
56 editorPane.setEditable(false); |
|
57 f.add(editorPane); |
|
58 f.setSize(220,240); |
|
59 f.setLocationRelativeTo(null); |
|
60 |
|
61 f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
|
62 f.setVisible(true); |
|
63 }); |
|
64 |
|
65 r.waitForIdle(); |
|
66 r.delay(500); |
|
67 |
|
68 SwingUtilities.invokeAndWait(() -> { |
|
69 Insets insets = editorPane.getInsets(); |
|
70 Point loc = editorPane.getLocationOnScreen(); |
|
71 |
|
72 final Color blue = Color.BLUE; |
|
73 final int offset = 10; |
|
74 |
|
75 Color center = r.getPixelColor(loc.x + insets.left + WIDTH / 2, |
|
76 loc.y + insets.top + HEIGHT / 2); |
|
77 Color left = r.getPixelColor(loc.x + insets.left + offset, |
|
78 loc.y + insets.top + HEIGHT / 2); |
|
79 Color right = r.getPixelColor(loc.x + insets.left + WIDTH - offset, |
|
80 loc.y + insets.top + HEIGHT / 2); |
|
81 Color bottom = r.getPixelColor(loc.x + insets.left + WIDTH / 2, |
|
82 loc.y + insets.top + HEIGHT - offset); |
|
83 Color top = r.getPixelColor(loc.x + insets.left + WIDTH / 2, |
|
84 loc.y + insets.top + offset); |
|
85 |
|
86 f.dispose(); |
|
87 |
|
88 System.out.println("center color: " + center); |
|
89 System.out.println("left color: " + left); |
|
90 System.out.println("right color: " + right); |
|
91 System.out.println("bottom color: " + bottom); |
|
92 System.out.println("top color: " + top); |
|
93 System.out.println(); |
|
94 |
|
95 if (!(blue.equals(center) && blue.equals(left) && blue.equals(right) && |
|
96 blue.equals(top) && blue.equals(bottom))) { |
|
97 throw new RuntimeException("Test failed: Image not scaled correctly"); |
|
98 } |
|
99 }); |
|
100 |
|
101 r.waitForIdle(); |
|
102 } |
|
103 |
|
104 public static void main(String[] args) throws Exception { |
|
105 |
|
106 final String ABSOLUTE_FILE_PATH = ImageViewTest.class.getResource("circle.png").getPath(); |
|
107 |
|
108 System.out.println(ABSOLUTE_FILE_PATH); |
|
109 |
|
110 Robot r = new Robot(); |
|
111 |
|
112 SwingUtilities.invokeAndWait(() -> { |
|
113 editorPane1 = new JEditorPane("text/html", |
|
114 "<img height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\""); |
|
115 |
|
116 editorPane2 = new JEditorPane("text/html", |
|
117 "<img width=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\""); |
|
118 |
|
119 editorPane3 = new JEditorPane("text/html", |
|
120 "<img width=\"200\" height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\""); |
|
121 |
|
122 editorPane4 = new JEditorPane("text/html", |
|
123 "<img src=\"file:///" + ABSOLUTE_FILE_PATH + "\""); |
|
124 |
|
125 }); |
|
126 |
|
127 r.waitForIdle(); |
|
128 |
|
129 System.out.println("Test with only height set to 200"); |
|
130 test(r, editorPane1); |
|
131 |
|
132 System.out.println("Test with only width set to 200"); |
|
133 test(r, editorPane2); |
|
134 |
|
135 System.out.println("Test with none of them set"); |
|
136 test(r, editorPane3); |
|
137 |
|
138 System.out.println("Test with both of them set to 200"); |
|
139 test(r, editorPane4); |
|
140 |
|
141 System.out.println("Test Passed."); |
|
142 } |
|
143 } |