1 /* |
|
2 * Copyright (c) 2019, 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 8226513 |
|
28 * @summary JEditorPane is shown with incorrect size |
|
29 * @run main/othervm -Dsun.java2d.uiScale=1.0 JEditorPaneLayoutTest |
|
30 */ |
|
31 |
|
32 import javax.swing.JFrame; |
|
33 import javax.swing.JEditorPane; |
|
34 import javax.swing.SwingUtilities; |
|
35 import javax.swing.text.BadLocationException; |
|
36 import javax.swing.text.Document; |
|
37 import javax.swing.text.html.HTMLEditorKit; |
|
38 import java.awt.Dimension; |
|
39 import java.awt.Robot; |
|
40 |
|
41 public class JEditorPaneLayoutTest { |
|
42 |
|
43 public static final String TEXT = |
|
44 "some text some text some text <br> some text"; |
|
45 static JFrame frame; |
|
46 static JEditorPane editorPane; |
|
47 static Dimension size1; |
|
48 static Dimension size2; |
|
49 static Dimension size3; |
|
50 static Dimension size4; |
|
51 |
|
52 public static void main(String[] args) throws Exception { |
|
53 Robot robot = new Robot(); |
|
54 |
|
55 SwingUtilities.invokeAndWait(() -> { |
|
56 frame = new JFrame(); |
|
57 editorPane = new JEditorPane("text/html", TEXT); |
|
58 size1 = editorPane.getPreferredSize(); |
|
59 editorPane.setText(TEXT); |
|
60 frame.add(editorPane); |
|
61 frame.pack(); |
|
62 frame.setLocationRelativeTo(null); |
|
63 frame.setVisible(true); |
|
64 }); |
|
65 |
|
66 robot.waitForIdle(); |
|
67 robot.delay(300); |
|
68 |
|
69 SwingUtilities.invokeAndWait(() -> { |
|
70 size2 = editorPane.getSize(); |
|
71 frame.dispose(); |
|
72 |
|
73 frame = new JFrame(); |
|
74 editorPane = new JEditorPane("text/html", TEXT); |
|
75 editorPane.getPreferredSize(); |
|
76 editorPane.setText(TEXT); |
|
77 frame.add(editorPane); |
|
78 frame.setLocationRelativeTo(null); |
|
79 frame.setVisible(true); |
|
80 }); |
|
81 |
|
82 robot.waitForIdle(); |
|
83 robot.delay(300); |
|
84 |
|
85 if (!size1.equals(size2)) { |
|
86 SwingUtilities.invokeLater(frame::dispose); |
|
87 throw new RuntimeException("Wrong size " + size2 + |
|
88 " expected " + size1); |
|
89 } |
|
90 |
|
91 SwingUtilities.invokeAndWait(() -> { |
|
92 editorPane.setText(TEXT); |
|
93 frame.pack(); |
|
94 size3 = editorPane.getSize(); |
|
95 frame.dispose(); |
|
96 |
|
97 frame = new JFrame(); |
|
98 editorPane = new JEditorPane("text/html", TEXT); |
|
99 editorPane.getPreferredSize(); |
|
100 editorPane.setSize(1, 1); |
|
101 Document doc = new HTMLEditorKit().createDefaultDocument(); |
|
102 try { |
|
103 doc.insertString(0, TEXT, null); |
|
104 } catch (BadLocationException e) { |
|
105 e.printStackTrace(); |
|
106 } |
|
107 editorPane.setDocument(doc); |
|
108 editorPane.setText(TEXT); |
|
109 frame.add(editorPane); |
|
110 frame.pack(); |
|
111 frame.setLocationRelativeTo(null); |
|
112 frame.setVisible(true); |
|
113 }); |
|
114 |
|
115 robot.waitForIdle(); |
|
116 robot.delay(300); |
|
117 |
|
118 if (!size1.equals(size3)) { |
|
119 SwingUtilities.invokeLater(frame::dispose); |
|
120 throw new RuntimeException("Wrong size " + size3 + |
|
121 " expected " + size1); |
|
122 } |
|
123 |
|
124 SwingUtilities.invokeAndWait(() -> { |
|
125 size4 = editorPane.getSize(); |
|
126 frame.dispose(); |
|
127 }); |
|
128 |
|
129 robot.waitForIdle(); |
|
130 robot.delay(300); |
|
131 |
|
132 if (!size1.equals(size4)) { |
|
133 SwingUtilities.invokeLater(frame::dispose); |
|
134 throw new RuntimeException("Wrong size " + size4 + |
|
135 " expected " + size1); |
|
136 } |
|
137 } |
|
138 } |
|