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
24/*
25  @test
26  @key headful
27  @bug 8150176 8150844
28  @author a.stepanov
29  @summary Check if correct resolution variant is used
30           for JOptionPane dialog / internal frame icons.
31  @library ../../../../lib/testlibrary/
32  @build ExtendedRobot
33  @run main/othervm/timeout=300 -Dsun.java2d.uiScale=1 MultiResolutionJOptionPaneIconTest
34  @run main/othervm/timeout=300 -Dsun.java2d.uiScale=2 MultiResolutionJOptionPaneIconTest
35*/
36
37import java.awt.*;
38import java.awt.event.*;
39import java.awt.image.*;
40import javax.swing.*;
41
42public class MultiResolutionJOptionPaneIconTest implements ActionListener {
43
44    private final static Color C1X = Color.ORANGE, C2X = Color.CYAN;
45
46    private final boolean isInternal;
47
48    private volatile JFrame test;
49    private volatile JDialog dialog;
50    private volatile JInternalFrame frame;
51    private final JDesktopPane parentPane = new JDesktopPane();
52    private final JButton run = new JButton("run");
53
54    private final ExtendedRobot robot = new ExtendedRobot();
55
56    private static BufferedImage getSquare(int sz, Color c) {
57
58        BufferedImage img = new BufferedImage(sz, sz, BufferedImage.TYPE_INT_RGB);
59        Graphics g = img.getGraphics();
60        g.setColor(c);
61        g.fillRect(0, 0, sz, sz);
62        return img;
63    }
64
65    private static Icon getIcon() {
66
67        BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
68            new BufferedImage[]{getSquare(16, C1X), getSquare(32, C2X)});
69        return new ImageIcon(mri);
70    }
71
72    public MultiResolutionJOptionPaneIconTest(boolean internal,
73        UIManager.LookAndFeelInfo lf) throws Exception {
74
75        UIManager.setLookAndFeel(lf.getClassName());
76
77        isInternal = internal;
78        robot.setAutoDelay(50);
79        SwingUtilities.invokeAndWait(this::UI);
80    }
81
82    private void UI() {
83
84        test = new JFrame();
85        test.setLayout(new BorderLayout());
86        test.add(parentPane, BorderLayout.CENTER);
87        run.addActionListener(this);
88        test.add(run, BorderLayout.SOUTH);
89        test.setUndecorated(true);
90        test.setSize(400, 300);
91        test.setLocation(50, 50);
92        test.setVisible(true);
93    }
94
95    private void disposeAll() {
96
97        if (dialog != null) { dialog.dispose(); }
98        if (frame  != null) {  frame.dispose(); }
99        if (test   != null) {   test.dispose(); }
100    }
101
102    public void doTest() throws Exception {
103
104        robot.waitForIdle(1000);
105        clickButton(robot);
106        robot.waitForIdle(2000);
107
108        Component c = isInternal ?
109            frame.getContentPane() : dialog.getContentPane();
110
111        System.out.println("\ncheck " + (isInternal ? "internal frame" :
112            "dialog") + " icon:");
113
114        Point pt = c.getLocationOnScreen();
115        checkColors(pt.x, c.getWidth(), pt.y, c.getHeight());
116        System.out.println("ok");
117        robot.waitForIdle();
118        SwingUtilities.invokeAndWait(this::disposeAll);
119        robot.waitForIdle();
120    }
121
122    private void checkColors(int x0, int w, int y0, int h) {
123
124        boolean is2x = "2".equals(System.getProperty("sun.java2d.uiScale"));
125        Color
126            expected   = is2x ? C2X : C1X,
127            unexpected = is2x ? C1X : C2X;
128
129        for (int y = y0; y < y0 + h; y += 5) {
130            for (int x = x0; x < x0 + w; x += 5) {
131
132                Color c = robot.getPixelColor(x, y);
133                if (c.equals(unexpected)) {
134                    throw new RuntimeException(
135                        "invalid color was found, test failed");
136                } else if (c.equals(expected)) { return; }
137            }
138        }
139
140        // no icon found at all
141        throw new RuntimeException("the icon wasn't found");
142    }
143
144    private void showDialogOrFrame() {
145
146        JOptionPane pane = new JOptionPane("",
147                                           JOptionPane.DEFAULT_OPTION,
148                                           JOptionPane.INFORMATION_MESSAGE,
149                                           getIcon());
150        pane.setOptions(new Object[]{}); // no buttons
151
152        if (isInternal) {
153            frame = pane.createInternalFrame(parentPane, "");
154            frame.setLocation(0, 0);
155            frame.setVisible(true);
156        } else {
157            dialog = pane.createDialog(parentPane, "");
158            dialog.setVisible(true);
159        }
160    }
161
162    public void clickButton(ExtendedRobot robot) {
163
164        Point pt = run.getLocationOnScreen();
165        robot.mouseMove(pt.x + run.getWidth() / 2, pt.y + run.getHeight() / 2);
166        robot.waitForIdle();
167        robot.click();
168    }
169
170    @Override
171    public void actionPerformed(ActionEvent event) { showDialogOrFrame(); }
172
173
174    public static void main(String[] args) throws Exception {
175
176        for (UIManager.LookAndFeelInfo LF: UIManager.getInstalledLookAndFeels()) {
177            System.out.println("\nL&F: " + LF.getName());
178            (new MultiResolutionJOptionPaneIconTest(false, LF)).doTest();
179            (new MultiResolutionJOptionPaneIconTest(true , LF)).doTest();
180        }
181    }
182}
183