< 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  * @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 
< prev index next >