1/*
2 * Copyright (c) 2015, 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 8133108
28 * @summary [PIT] Container size is wrong in JEditorPane
29 * @author Semyon Sadetsky
30 */
31
32import javax.swing.*;
33import javax.swing.text.BadLocationException;
34import javax.swing.text.SimpleAttributeSet;
35import javax.swing.text.html.CSS;
36import java.awt.*;
37
38public class JTextPaneDocumentWrapping {
39
40    private static JFrame frame;
41    private static JTextPane jTextPane;
42    private static int position;
43
44    public static void main(String[] args) throws Exception{
45        SwingUtilities.invokeAndWait(new Runnable() {
46            @Override
47            public void run() {
48                frame = new JFrame();
49                frame.setUndecorated(true);
50                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
51                frame.setSize(200, 200);
52                jTextPane = new JTextPane();
53                jTextPane.setContentType("text/html");
54                jTextPane.setText(
55                        "<html><body><b id='test'>Test Test Test Test Test Test " +
56                                "Test Test Test Test Test Test Test Test Test Test " +
57                                "Test Test Test Test Test Test Test Test Test Test" +
58                                "</b></body></html>");
59                frame.getContentPane().add(jTextPane);
60                frame.setVisible(true);
61            }
62        });
63        Robot robot = new Robot();
64        robot.waitForIdle();
65        robot.delay(200);
66
67        SwingUtilities.invokeAndWait(new Runnable() {
68            @Override
69            public void run() {
70                try {
71                    position = jTextPane.modelToView(100).y;
72                    SimpleAttributeSet wrap = new SimpleAttributeSet();
73                    wrap.addAttribute(CSS.Attribute.WHITE_SPACE, "nowrap");
74                    jTextPane.getStyledDocument()
75                            .setParagraphAttributes(0, 10, wrap, true);
76                } catch (BadLocationException e) {
77                    e.printStackTrace();
78                }
79            }
80        });
81        if(position < 40) {
82            throw  new RuntimeException("Text is not wrapped " + position);
83        }
84        robot.waitForIdle();
85        robot.delay(200);
86        SwingUtilities.invokeAndWait(new Runnable() {
87            @Override
88            public void run() {
89                try {
90                    position = jTextPane.modelToView(100).y;
91                } catch (BadLocationException e) {
92                    e.printStackTrace();
93                }
94                frame.dispose();
95            }
96        });
97        if(position > 20) {
98            throw  new RuntimeException("Text is wrapped " + position);
99        }
100        System.out.println("ok");
101
102    }
103}
104