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