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 /* @test
25  * @key headful
26  * @bug 8212904
27  * @summary Verifies JTextArea line wrapping using UI scale
28  * @run main JTextAreaWordWrapTest
29  */
30 import java.awt.BorderLayout;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33 import java.io.IOException;
34 import java.net.URISyntaxException;
35 import javax.swing.JFrame;
36 import javax.swing.JScrollPane;
37 import javax.swing.JTextArea;
38 import javax.swing.ScrollPaneConstants;
39 import javax.swing.SwingUtilities;
40 
41 public class JTextAreaWordWrapTest {
42 
43     static JFrame frame;
44     static JFrame frame1;
45     static JTextArea textArea;
46     static JTextArea textArea1;
47 
doWrapOnTest()48     public static void doWrapOnTest() {
49 
50         frame = new JFrame();
51         frame.setSize( 720, 300 );
52         frame.setLayout( new BorderLayout() );
53 
54         textArea = new JTextArea();
55         textArea.setLineWrap( true );
56         textArea.setWrapStyleWord( true );
57 
58         StringBuffer sb = new StringBuffer();
59         for (int i = 0; i < 100; i++) {
60             sb.append( "zz zzz zzzz zz zz zz zzz xzzzz zzzzzzzzzzzzzzzzx yyyyyyy tttttttttt sssss hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n" );
61         }
62         textArea.setText( sb.toString() );
63         JScrollPane pane = new JScrollPane( textArea,
64                                             ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
65                                             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
66         frame.add( pane, BorderLayout.CENTER );
67         frame.setVisible( true );
68 
69     }
70 
doWrapOffTest()71     public static void doWrapOffTest() {
72         frame1 = new JFrame();
73         frame1.setSize( 720, 300 );
74         frame1.setLayout( new BorderLayout() );
75 
76         textArea1 = new JTextArea();
77 
78         StringBuffer sb1 = new StringBuffer();
79         for (int i = 0; i < 100; i++) {
80             sb1.append( "zz zzz zzzz zz zz zz zzz xzzzz zzzzzzzzzzzzzzzzx yyyyyyy tttttttttt sssss hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n" );
81         }
82         textArea1.setText( sb1.toString() );
83         JScrollPane pane1 = new JScrollPane( textArea1,
84                                              ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
85                                              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
86         frame1.add( pane1, BorderLayout.CENTER );
87         frame1.setLocationRelativeTo(null);
88         frame1.setVisible( true );
89     }
90 
main( String[] args )91     public static void main( String[] args ) throws Exception {
92         System.setProperty( "sun.java2d.uiScale", "1.25" );
93         try {
94             SwingUtilities.invokeAndWait(() -> doWrapOnTest());
95             Thread.sleep(500);
96             SwingUtilities.invokeAndWait(() -> doWrapOffTest());
97             Thread.sleep(500);
98 
99             SwingUtilities.invokeAndWait(() -> {
100 
101                 int wraponHeight = textArea.getHeight();
102                 System.out.println("wraponheight " + wraponHeight);
103                 int wrapoffHeight = textArea1.getHeight();
104                 System.out.println("wrapoffheight " + wrapoffHeight);
105 
106                 if (wraponHeight == wrapoffHeight) {
107                     throw new RuntimeException("JTextArea line wrapping incorrect when using UI scale");
108                 }
109             });
110 
111         } finally {
112             SwingUtilities.invokeAndWait(() -> frame.dispose());
113             SwingUtilities.invokeAndWait(() -> frame1.dispose());
114         }
115     }
116 }
117