1 /*
2  * Copyright (c) 2016, 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 import java.awt.BorderLayout;
24 import java.awt.FlowLayout;
25 import java.awt.Font;
26 import java.awt.event.WindowAdapter;
27 import java.awt.event.WindowEvent;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 import javax.swing.JButton;
31 import javax.swing.JFrame;
32 import javax.swing.JPanel;
33 import javax.swing.JTextArea;
34 import javax.swing.SwingUtilities;
35 import javax.swing.text.JTextComponent;
36 
37 /**
38  * @test
39  * @bug 8156217
40  * @summary Selected text is shifted on HiDPI display
41  * @run main/manual/othervm -Dsun.java2d.uiScale=2 TextSelectionTest
42  */
43 public class TextSelectionTest {
44 
45     private static final String INSTRUCTIONS = "This is a manual test.\n"
46             + "\n"
47             + "Select the current text from the end to the beginning.\n"
48             + "\n"
49             + "If the text is slightly shiftted from one side to another\n"
50             + "and back during selection press Fail.\n"
51             + "Otherwise, press Pass.";
52 
53     private static final CountDownLatch latch = new CountDownLatch(1);
54     private static volatile boolean passed = false;
55 
main(String[] args)56     public static void main(String[] args) throws Exception {
57         SwingUtilities.invokeAndWait(TextSelectionTest::createAndShowGUI);
58         latch.await(3, TimeUnit.MINUTES);
59         System.out.println("passed: " + passed);
60         if (!passed) {
61             throw new RuntimeException("Test fails!");
62         }
63     }
64 
createAndShowGUI()65     private static void createAndShowGUI() {
66 
67         JFrame frame = new JFrame("Follow the instructions below:");
68         frame.setSize(700, 500);
69         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
70 
71         JPanel panel = new JPanel(new BorderLayout());
72         JTextComponent textComponent = new JTextArea(INSTRUCTIONS);
73         textComponent.setEditable(false);
74         Font font = textComponent.getFont();
75         font = font.deriveFont(24.0f);
76         textComponent.setFont(font);
77         panel.add(textComponent, BorderLayout.CENTER);
78 
79         JPanel buttonsPanel = new JPanel(new FlowLayout());
80         JButton passButton = new JButton("Pass");
81         passButton.addActionListener((e) -> {
82             passed = true;
83             latch.countDown();
84             frame.dispose();
85         });
86         JButton failsButton = new JButton("Fail");
87         failsButton.addActionListener((e) -> {
88             passed = false;
89             latch.countDown();
90             frame.dispose();
91         });
92 
93         buttonsPanel.add(passButton);
94         buttonsPanel.add(failsButton);
95         panel.add(buttonsPanel, BorderLayout.SOUTH);
96 
97         frame.getContentPane().add(panel);
98 
99         frame.addWindowListener(new WindowAdapter() {
100 
101             @Override
102             public void windowClosing(WindowEvent e) {
103                 latch.countDown();
104             }
105         });
106         frame.setVisible(true);
107     }
108 }
109