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