< prev index next >

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

Print this page


   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;


  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");


 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 
   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;


  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                 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");


 175     }
 176 
 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 
 189     private static void removeIcon() {
 190         if (tray != null) {
 191             tray.remove(icon);
 192         }
 193     }
 194 }

< prev index next >