< prev index next >

test/java/awt/image/multiresolution/MultiResolutionTrayIconTest/MultiResolutionTrayIconTest.java

Print this page




   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   @bug 8150176 8151773
  28   @summary Check if correct resolution variant is used for tray icon.
  29   @author a.stepanov
  30   @run applet/manual=yesno MultiResolutionTrayIconTest.html
  31 */
  32 
  33 
  34 import java.applet.Applet;
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import java.awt.image.*;












































  38 












  39 
  40 public class MultiResolutionTrayIconTest extends Applet {



















  41 
  42     private SystemTray tray;
  43     private TrayIcon   icon, iconMRI;


  44 
  45     public void init() { this.setLayout(new BorderLayout()); }





  46 
  47     public void start() {



  48 




  49         boolean trayIsSupported = SystemTray.isSupported();
  50         Button b = new Button("Start");























  51         if (trayIsSupported) {
  52 
  53             prepareIcons();
  54             b.addActionListener(new ActionListener() {
  55                 @Override
  56                 public void actionPerformed(ActionEvent e) { doTest(); }
  57             });
  58         } else {
  59              b.setLabel("not supported");
  60              b.setEnabled(false);
  61              System.out.println("system tray is not supported");
  62         }
  63         add(b, BorderLayout.CENTER);














































  64 
  65         validate();
  66         setVisible(true);


  67     }
  68 
  69     private BufferedImage generateImage(int w, int h, Color c) {
  70 
  71         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

  72         Graphics g = img.getGraphics();
  73         g.setColor(c);
  74         g.fillRect(0, 0, w, h);
  75         g.setColor(Color.WHITE);
  76         int r = (Math.min(w, h) >= 8) ? 3 : 1;
  77         g.fillRect(r, r, w - 2 * r, h - 2 * r);
  78         return img;
  79     }
  80 
  81     private void prepareIcons() {
  82 
  83         tray = SystemTray.getSystemTray();
  84         Dimension d = tray.getTrayIconSize();
  85         int w = d.width, h = d.height;
  86 
  87         BufferedImage img = generateImage(w, h, Color.BLUE);
  88         // use wrong icon size for "nok"
  89         BufferedImage nok = generateImage(w / 2 + 2, h / 2 + 2, Color.RED);
  90         BaseMultiResolutionImage mri =
  91             new BaseMultiResolutionImage(new BufferedImage[] {nok, img});
  92         icon = new TrayIcon(img);
  93         icon.setImageAutoSize(true); // just in case
  94         iconMRI = new TrayIcon(mri);
  95         iconMRI.setImageAutoSize(true);
  96     }
  97 
  98     private void doTest() {
  99 
 100         if (tray.getTrayIcons().length > 0) { return; } // icons were added already
 101         try {
 102             tray.add(icon);
 103             tray.add(iconMRI);
 104         } catch (Exception e) {
 105             throw new RuntimeException(e);
 106         }
 107     }
 108 
 109     public void stop() {
 110 
 111         // check for null, just in case
 112         if (tray != null) {
 113             tray.remove(icon);
 114             tray.remove(iconMRI);
 115         }
 116     }
 117 }


   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  * @bug 8150176 8151773
  28  * @summary Check if correct resolution variant is used for tray icon.
  29  * @run main/manual MultiResolutionTrayIconTest
  30  */
  31 import java.awt.Color;
  32 import java.awt.Dimension;
  33 import java.awt.Graphics;
  34 import java.awt.GridBagLayout;
  35 import java.awt.GridBagConstraints;
  36 import java.awt.SystemTray;
  37 import java.awt.TrayIcon;
  38 import java.awt.event.ActionEvent;
  39 import java.awt.event.ActionListener;
  40 import java.awt.event.WindowAdapter;
  41 import java.awt.event.WindowEvent;
  42 import java.awt.image.BaseMultiResolutionImage;
  43 import java.awt.image.BufferedImage;
  44 import java.io.BufferedReader;
  45 import java.io.File;
  46 import java.io.IOException;
  47 import java.io.InputStreamReader;
  48 import java.util.ArrayList;
  49 import java.util.HashMap;
  50 import java.util.List;
  51 import java.util.Map;
  52 import javax.swing.JFrame;
  53 import javax.swing.JButton;
  54 import javax.swing.JLabel;
  55 import javax.swing.JPanel;
  56 import javax.swing.SwingUtilities;
  57 import java.io.InputStream;
  58 
  59 public class MultiResolutionTrayIconTest {
  60 
  61     private static SystemTray tray;
  62     private static TrayIcon icon;
  63     private static GridBagLayout layout;
  64     private static JPanel mainControlPanel;
  65     private static JPanel resultButtonPanel;
  66     private static JLabel instructionText;
  67     private static JButton passButton;
  68     private static JButton failButton;
  69     private static JButton startButton;
  70     private static JFrame mainFrame;
  71 
  72     public static void main(String[] args) throws Exception {
  73         String osName = System.getProperty("os.name");
  74         if (osName.contains("Win")) {
  75             createUI();
  76         } else if (args.length == 0) {
  77             createChildProcess();
  78         } else {
  79             createUI();
  80         }
  81     }
  82 
  83     static void createChildProcess() {
  84         String javaPath = System.getProperty("java.home");
  85         String classPathDir = System.getProperty("java.class.path");
  86         Map<String, String> env = new HashMap<String, String>();
  87         env.put("GDK_SCALE", "2");
  88         int exitValue = doExec(env, javaPath + File.separator + "bin" + File.separator
  89                 + "java", "-cp",
  90                 classPathDir, "MultiResolutionTrayIconTest", "1");
  91         if (exitValue != 0) {
  92             throw new RuntimeException("Test Failed");
  93         }
  94     }
  95 
  96     static int doExec(Map<String, String> envToSet, String... cmds) {
  97         Process p = null;
  98         ProcessBuilder pb = new ProcessBuilder(cmds);
  99         Map<String, String> env = pb.environment();
 100         if (envToSet != null) {
 101             env.putAll(envToSet);
 102         }
 103         BufferedReader rdr = null;
 104         InputStream errorStream = null;
 105         try {
 106             List<String> outputList = new ArrayList<>();
 107             pb.redirectErrorStream(true);
 108             p = pb.start();
 109             rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
 110             String in = rdr.readLine();
 111             while (in != null) {
 112                 outputList.add(in);
 113                 in = rdr.readLine();
 114                 throw new RuntimeException("Test Failed");
 115             }
 116 
 117             p.waitFor();
 118         } catch (Exception ex) {
 119             ex.printStackTrace();
 120         } finally {
 121 
 122             try {
 123                 if (rdr != null) {
 124                     rdr.close();
 125                 }
 126             } catch (IOException ex) {
 127             }
 128 
 129             p.destroy();
 130         }
 131         return p.exitValue();
 132     }
 133 
 134     public static void createUI() throws Exception {
 135         SwingUtilities.invokeAndWait(new Runnable() {
 136             public void run() {
 137                 mainFrame = new JFrame("TrayIcon Test");
 138                 boolean trayIsSupported = SystemTray.isSupported();
 139                 tray = SystemTray.getSystemTray();
 140                 Dimension d = tray.getTrayIconSize();
 141                 icon = new TrayIcon(createIcon(d.width, d.height));
 142                 icon.setImageAutoSize(true);
 143                 layout = new GridBagLayout();
 144                 mainControlPanel = new JPanel(layout);
 145                 resultButtonPanel = new JPanel(layout);
 146 
 147                 GridBagConstraints gbc = new GridBagConstraints();
 148                 String instructions
 149                         = "<html>INSTRUCTIONS:<br>"
 150                         + "Press start button to add icon to system tray.<br><br>"
 151                         + "If Icon color is green test"
 152                         + " passes else failed.<br><br></html>";
 153 
 154                 instructionText = new JLabel();
 155                 instructionText.setText(instructions);
 156 
 157                 gbc.gridx = 0;
 158                 gbc.gridy = 0;
 159                 gbc.fill = GridBagConstraints.HORIZONTAL;
 160                 mainControlPanel.add(instructionText, gbc);
 161                 startButton = new JButton("Start");
 162                 startButton.setActionCommand("Start");
 163                 if (trayIsSupported) {
 164 
 165                     startButton.addActionListener((ActionEvent e) -> {
 166                         doTest();


 167                     });
 168                 } else {
 169                     startButton.setEnabled(false);

 170                     System.out.println("system tray is not supported");
 171                 }
 172                 gbc.gridx = 0;
 173                 gbc.gridy = 0;
 174                 resultButtonPanel.add(startButton, gbc);
 175 
 176                 passButton = new JButton("Pass");
 177                 passButton.setActionCommand("Pass");
 178                 passButton.addActionListener((ActionEvent e) -> {
 179                     removeIcon();
 180                     mainFrame.dispose();
 181                 });
 182                 failButton = new JButton("Fail");
 183                 failButton.setActionCommand("Fail");
 184                 failButton.addActionListener(new ActionListener() {
 185                     @Override
 186                     public void actionPerformed(ActionEvent e) {
 187                         removeIcon();
 188                         mainFrame.dispose();
 189                         throw new RuntimeException("Test Failed");
 190                     }
 191                 });
 192                 gbc.gridx = 1;
 193                 gbc.gridy = 0;
 194                 resultButtonPanel.add(passButton, gbc);
 195                 gbc.gridx = 2;
 196                 gbc.gridy = 0;
 197                 resultButtonPanel.add(failButton, gbc);
 198 
 199                 gbc.gridx = 0;
 200                 gbc.gridy = 1;
 201                 mainControlPanel.add(resultButtonPanel, gbc);
 202 
 203                 mainFrame.add(mainControlPanel);
 204                 mainFrame.setSize(400, 200);
 205                 mainFrame.setLocationRelativeTo(null);
 206                 mainFrame.setVisible(true);
 207 
 208                 mainFrame.addWindowListener(new WindowAdapter() {
 209                     @Override
 210                     public void windowClosing(WindowEvent e) {
 211                         removeIcon();
 212                         mainFrame.dispose();
 213                     }
 214                 });
 215             }
 216         });
 217 
 218     }
 219 
 220     private static BaseMultiResolutionImage createIcon(int w, int h) {
 221         return new BaseMultiResolutionImage(
 222                 new BufferedImage[]{generateImage(w, h, 1, Color.RED),
 223                     generateImage(w, h, 2, Color.GREEN)});
 224     }
 225 
 226     private static BufferedImage generateImage(int w, int h, int scale, Color c) {
 227 
 228         int x = w * scale, y = h * scale;
 229         BufferedImage img = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
 230         Graphics g = img.getGraphics();
 231         g.setColor(c);
 232         g.fillRect(0, 0, x, y);
 233         g.setColor(Color.WHITE);
 234         g.fillRect(x / 3, y / 3, x / 3, y / 3);

 235         return img;
 236     }
 237 
 238     private static void doTest() {
 239 
 240         if (tray.getTrayIcons().length > 0) {
 241             return;











 242         }




 243         try {
 244             tray.add(icon);

 245         } catch (Exception e) {
 246             throw new RuntimeException(e);
 247         }
 248     }
 249 
 250     private static void removeIcon() {


 251         if (tray != null) {
 252             tray.remove(icon);

 253         }
 254     }
 255 }
< prev index next >