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
24import java.awt.Color;
25import java.awt.FlowLayout;
26import java.awt.GridBagConstraints;
27import java.awt.GridBagLayout;
28import java.awt.event.ActionEvent;
29import java.awt.event.ActionListener;
30import java.awt.event.WindowAdapter;
31import java.awt.event.WindowEvent;
32import java.util.concurrent.CountDownLatch;
33import java.util.concurrent.TimeUnit;
34import javax.swing.BoxLayout;
35import javax.swing.JButton;
36import javax.swing.JCheckBox;
37import javax.swing.JComboBox;
38import javax.swing.JFrame;
39import javax.swing.JPanel;
40import javax.swing.JRadioButton;
41import javax.swing.JScrollPane;
42import javax.swing.JTextArea;
43import javax.swing.ScrollPaneConstants;
44import javax.swing.SwingUtilities;
45
46/*
47 * @test
48 * @bug 8165485
49 * @summary Bad rendering of Swing UI controls with Motif L&F on HiDPI display
50 * @run main/manual/othervm -Dsun.java2d.uiScale=2
51 * -Dswing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel MotifHiDPIIconsTest
52 */
53public class MotifHiDPIIconsTest {
54
55    private static volatile boolean testResult = false;
56    private static volatile CountDownLatch countDownLatch;
57    private static final String INSTRUCTIONS = "INSTRUCTIONS:\n"
58            + "Check that the icons are painted smoothly on Swing UI controls:\n"
59            + "  - JRadioButton\n"
60            + "  - JCheckBox\n"
61            + "  - JComboBox\n"
62            + "  - JScrollPane (vertical and horizontal scroll bars)\n"
63            + "\n"
64            + "If so, press PASS, else press FAIL.\n";
65
66    public static void main(String args[]) throws Exception {
67        countDownLatch = new CountDownLatch(1);
68
69        SwingUtilities.invokeLater(MotifHiDPIIconsTest::createUI);
70        countDownLatch.await(15, TimeUnit.MINUTES);
71
72        if (!testResult) {
73            throw new RuntimeException("Test fails!");
74        }
75    }
76
77    private static void createUI() {
78
79        final JFrame mainFrame = new JFrame("Motif L&F icons test");
80        GridBagLayout layout = new GridBagLayout();
81        JPanel mainControlPanel = new JPanel(layout);
82        JPanel resultButtonPanel = new JPanel(layout);
83
84        GridBagConstraints gbc = new GridBagConstraints();
85
86
87        JPanel testPanel = createJPanel();
88
89        gbc.gridx = 0;
90        gbc.gridy = 0;
91        gbc.fill = GridBagConstraints.HORIZONTAL;
92        mainControlPanel.add(testPanel, gbc);
93
94        JTextArea instructionTextArea = new JTextArea();
95        instructionTextArea.setText(INSTRUCTIONS);
96        instructionTextArea.setEditable(false);
97        instructionTextArea.setBackground(Color.white);
98
99        gbc.gridx = 0;
100        gbc.gridy = 1;
101        gbc.fill = GridBagConstraints.HORIZONTAL;
102        mainControlPanel.add(instructionTextArea, gbc);
103
104        JButton passButton = new JButton("Pass");
105        passButton.setActionCommand("Pass");
106        passButton.addActionListener((ActionEvent e) -> {
107            testResult = true;
108            mainFrame.dispose();
109            countDownLatch.countDown();
110
111        });
112
113        JButton failButton = new JButton("Fail");
114        failButton.setActionCommand("Fail");
115        failButton.addActionListener(new ActionListener() {
116            @Override
117            public void actionPerformed(ActionEvent e) {
118                mainFrame.dispose();
119                countDownLatch.countDown();
120            }
121        });
122
123        gbc.gridx = 0;
124        gbc.gridy = 0;
125        resultButtonPanel.add(passButton, gbc);
126
127        gbc.gridx = 1;
128        gbc.gridy = 0;
129        resultButtonPanel.add(failButton, gbc);
130
131        gbc.gridx = 0;
132        gbc.gridy = 2;
133        mainControlPanel.add(resultButtonPanel, gbc);
134
135        mainFrame.add(mainControlPanel);
136        mainFrame.pack();
137
138        mainFrame.addWindowListener(new WindowAdapter() {
139
140            @Override
141            public void windowClosing(WindowEvent e) {
142                mainFrame.dispose();
143                countDownLatch.countDown();
144            }
145        });
146        mainFrame.setVisible(true);
147    }
148
149    private static JPanel createJPanel() {
150        JPanel panel = new JPanel();
151        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
152
153        JPanel iconPanel = new JPanel(new FlowLayout());
154        JRadioButton radioButton = new JRadioButton();
155        radioButton.setSelected(false);
156        iconPanel.add(radioButton);
157        radioButton = new JRadioButton();
158        radioButton.setSelected(true);
159        iconPanel.add(radioButton);
160        panel.add(iconPanel);
161
162        iconPanel = new JPanel(new FlowLayout());
163        JCheckBox checkBox = new JCheckBox();
164        checkBox.setSelected(false);
165        iconPanel.add(checkBox);
166        checkBox = new JCheckBox();
167        checkBox.setSelected(true);
168        iconPanel.add(checkBox);
169        panel.add(iconPanel);
170
171        iconPanel = new JPanel(new FlowLayout());
172        JComboBox<String> comboBox = new JComboBox(new String[]{"111", "222"});
173        iconPanel.add(comboBox);
174        panel.add(iconPanel);
175
176        iconPanel = new JPanel(new FlowLayout());
177        JTextArea textArea = new JTextArea(3, 7);
178        textArea.setText("AAA");
179        JScrollPane scrollPane = new JScrollPane(textArea);
180        scrollPane.setHorizontalScrollBarPolicy(
181                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
182        scrollPane.setVerticalScrollBarPolicy(
183                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
184        iconPanel.add(scrollPane);
185        panel.add(iconPanel);
186
187        return panel;
188    }
189}
190