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 * @bug 8147648 8163160
27 * @summary [hidpi] multiresolution image: wrong resolution variant is used as
28 * icon in the Unity panel
29 * @run main/manual/othervm -Dsun.java2d.uiScale=2 IconTest
30 */
31import java.awt.Color;
32import java.awt.Graphics;
33import java.awt.GridBagConstraints;
34import java.awt.GridBagLayout;
35import java.awt.event.ActionEvent;
36import java.awt.event.ActionListener;
37import java.awt.event.WindowAdapter;
38import java.awt.event.WindowEvent;
39import java.awt.image.BaseMultiResolutionImage;
40import java.awt.image.BufferedImage;
41import java.util.concurrent.CountDownLatch;
42import javax.swing.ImageIcon;
43import javax.swing.JButton;
44import javax.swing.JFrame;
45import javax.swing.JLabel;
46import javax.swing.JPanel;
47import javax.swing.SwingUtilities;
48
49public class IconTest {
50
51    private final static int SZ = 8;
52    private static GridBagLayout layout;
53    private static JPanel mainControlPanel;
54    private static JPanel resultButtonPanel;
55    private static JLabel instructionText;
56    private static JButton passButton;
57    private static JButton failButton;
58    private static JButton testButton;
59    private static JFrame f;
60    private static CountDownLatch latch;
61
62    private static BufferedImage generateImage(int scale, Color c) {
63        int x = SZ * scale;
64        BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
65        Graphics g = img.getGraphics();
66        try {
67            if (g != null) {
68                g.setColor(c);
69                g.fillRect(0, 0, x, x);
70                g.setColor(Color.GREEN);
71                g.drawRect(0, 0, x-1, x-1);
72            }
73        } finally {
74            g.dispose();
75        }
76        return img;
77    }
78
79
80    private static void createUI() throws Exception {
81        SwingUtilities.invokeAndWait(new Runnable() {
82            public void run() {
83                f = new JFrame("TrayIcon Test");
84
85                final BaseMultiResolutionImage IMG = new BaseMultiResolutionImage(
86                        new BufferedImage[]{generateImage(1, Color.RED), generateImage(2, Color.BLUE)});
87                layout = new GridBagLayout();
88                mainControlPanel = new JPanel(layout);
89                resultButtonPanel = new JPanel(layout);
90                f.setIconImage(IMG);
91                GridBagConstraints gbc = new GridBagConstraints();
92                String instructions
93                        = "<html>INSTRUCTIONS:<br>"
94                        + "Check if test button icon and unity icon are both "
95                        + "blue with green border.<br><br>"
96                        + "If Icon color is blue press pass"
97                        + " else press fail.<br><br></html>";
98
99                instructionText = new JLabel();
100                instructionText.setText(instructions);
101
102                gbc.gridx = 0;
103                gbc.gridy = 0;
104                gbc.fill = GridBagConstraints.HORIZONTAL;
105                mainControlPanel.add(instructionText, gbc);
106                testButton = new JButton("Test");
107                testButton.setActionCommand("Test");
108                mainControlPanel.add(testButton, gbc);
109
110                testButton.setIcon(new ImageIcon(IMG));
111                gbc.gridx = 0;
112                gbc.gridy = 0;
113                resultButtonPanel.add(testButton, gbc);
114
115                passButton = new JButton("Pass");
116                passButton.setActionCommand("Pass");
117                passButton.addActionListener((ActionEvent e) -> {
118                    latch.countDown();
119                    f.dispose();
120                });
121                failButton = new JButton("Fail");
122                failButton.setActionCommand("Fail");
123                failButton.addActionListener(new ActionListener() {
124                    @Override
125                    public void actionPerformed(ActionEvent e) {
126                        latch.countDown();
127                        f.dispose();
128                        throw new RuntimeException("Test Failed");
129                    }
130                });
131                gbc.gridx = 1;
132                gbc.gridy = 0;
133                resultButtonPanel.add(passButton, gbc);
134                gbc.gridx = 2;
135                gbc.gridy = 0;
136                resultButtonPanel.add(failButton, gbc);
137
138                gbc.gridx = 0;
139                gbc.gridy = 1;
140                mainControlPanel.add(resultButtonPanel, gbc);
141
142                f.add(mainControlPanel);
143                f.setSize(400, 200);
144                f.setLocationRelativeTo(null);
145                f.setVisible(true);
146
147                f.addWindowListener(new WindowAdapter() {
148                    @Override
149                    public void windowClosing(WindowEvent e) {
150                        latch.countDown();
151                        f.dispose();
152                    }
153                });
154            }
155        });
156    }
157
158    public static void main(String[] args) throws Exception {
159        latch = new CountDownLatch(1);
160        createUI();
161        latch.await();
162    }
163}
164
165