1 /*
2  * Copyright (c) 2016, 2020, 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 /**
26  * @test
27  * @key headful
28  * @bug 8150176 8151773 8150176 8241791
29  * @summary Check if correct resolution variant is used for tray icon.
30  * @run main/manual/othervm -Dsun.java2d.uiScale=2 MultiResolutionTrayIconTest
31  */
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.Graphics;
35 import java.awt.GridBagLayout;
36 import java.awt.GridBagConstraints;
37 import java.awt.SystemTray;
38 import java.awt.TrayIcon;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.WindowAdapter;
42 import java.awt.event.WindowEvent;
43 import java.awt.image.BaseMultiResolutionImage;
44 import java.awt.image.BufferedImage;
45 import javax.swing.JFrame;
46 import javax.swing.JButton;
47 import javax.swing.JLabel;
48 import javax.swing.JPanel;
49 import javax.swing.SwingUtilities;
50 import java.util.concurrent.CountDownLatch;
51 import java.util.concurrent.TimeUnit;
52 
53 public class MultiResolutionTrayIconTest {
54     private static SystemTray tray;
55     private static TrayIcon icon;
56     private static GridBagLayout layout;
57     private static JPanel mainControlPanel;
58     private static JPanel resultButtonPanel;
59     private static JLabel instructionText;
60     private static JButton passButton;
61     private static JButton failButton;
62     private static JButton startButton;
63     private static JFrame mainFrame;
64     private static CountDownLatch latch;
65 
main(String[] args)66     public static void main(String[] args) throws Exception {
67         latch = new CountDownLatch(1);
68         createUI();
69         latch.await(200, TimeUnit.SECONDS);
70     }
71 
createUI()72     public static void createUI() throws Exception {
73         SwingUtilities.invokeAndWait(new Runnable() {
74             public void run() {
75                 mainFrame = new JFrame("TrayIcon Test");
76                 if (!SystemTray.isSupported()) {
77                     System.out.println("system tray is not supported");
78                     latch.countDown();
79                     return;
80                 }
81                 tray = SystemTray.getSystemTray();
82                 Dimension d = tray.getTrayIconSize();
83                 icon = new TrayIcon(createIcon(d.width, d.height));
84                 icon.setImageAutoSize(true);
85                 layout = new GridBagLayout();
86                 mainControlPanel = new JPanel(layout);
87                 resultButtonPanel = new JPanel(layout);
88 
89                 GridBagConstraints gbc = new GridBagConstraints();
90                 String instructions
91                         = "<html>INSTRUCTIONS:<br>"
92                         + "Press start button to add icon to system tray.<br><br>"
93                         + "If Icon color is green test"
94                         + " passes else failed.<br><br></html>";
95 
96                 instructionText = new JLabel();
97                 instructionText.setText(instructions);
98 
99                 gbc.gridx = 0;
100                 gbc.gridy = 0;
101                 gbc.fill = GridBagConstraints.HORIZONTAL;
102                 mainControlPanel.add(instructionText, gbc);
103                 startButton = new JButton("Start");
104                 startButton.setActionCommand("Start");
105                 startButton.addActionListener((ActionEvent e) -> {
106                     doTest();
107                 });
108                 gbc.gridx = 0;
109                 gbc.gridy = 0;
110                 resultButtonPanel.add(startButton, gbc);
111 
112                 passButton = new JButton("Pass");
113                 passButton.setActionCommand("Pass");
114                 passButton.addActionListener((ActionEvent e) -> {
115                     latch.countDown();
116                     removeIcon();
117                     mainFrame.dispose();
118                 });
119                 failButton = new JButton("Fail");
120                 failButton.setActionCommand("Fail");
121                 failButton.addActionListener(new ActionListener() {
122                     @Override
123                     public void actionPerformed(ActionEvent e) {
124                         removeIcon();
125                         latch.countDown();
126                         mainFrame.dispose();
127                         throw new RuntimeException("Test Failed");
128                     }
129                 });
130                 gbc.gridx = 1;
131                 gbc.gridy = 0;
132                 resultButtonPanel.add(passButton, gbc);
133                 gbc.gridx = 2;
134                 gbc.gridy = 0;
135                 resultButtonPanel.add(failButton, gbc);
136 
137                 gbc.gridx = 0;
138                 gbc.gridy = 1;
139                 mainControlPanel.add(resultButtonPanel, gbc);
140 
141                 mainFrame.add(mainControlPanel);
142                 mainFrame.setSize(400, 200);
143                 mainFrame.setLocationRelativeTo(null);
144                 mainFrame.setVisible(true);
145 
146                 mainFrame.addWindowListener(new WindowAdapter() {
147                     @Override
148                     public void windowClosing(WindowEvent e) {
149                         removeIcon();
150                         latch.countDown();
151                         mainFrame.dispose();
152                     }
153                 });
154             }
155         });
156 
157     }
158 
createIcon(int w, int h)159     private static BaseMultiResolutionImage createIcon(int w, int h) {
160         return new BaseMultiResolutionImage(
161                 new BufferedImage[]{generateImage(w, h, 1, Color.RED),
162                     generateImage(w, h, 2, Color.GREEN)});
163     }
164 
generateImage(int w, int h, int scale, Color c)165     private static BufferedImage generateImage(int w, int h, int scale, Color c) {
166 
167         int x = w * scale, y = h * scale;
168         BufferedImage img = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
169         Graphics g = img.getGraphics();
170         g.setColor(c);
171         g.fillRect(0, 0, x, y);
172         g.setColor(Color.WHITE);
173         g.fillRect(x / 3, y / 3, x / 3, y / 3);
174         return img;
175     }
176 
doTest()177     private static void doTest() {
178 
179         if (tray.getTrayIcons().length > 0) {
180             return;
181         }
182         try {
183             tray.add(icon);
184         } catch (Exception e) {
185             throw new RuntimeException(e);
186         }
187     }
188 
removeIcon()189     private static void removeIcon() {
190         if (tray != null) {
191             tray.remove(icon);
192         }
193     }
194 }
195