< prev index next >

test/java/awt/TrayIcon/ActionEventTest/ActionEventTest.java

Print this page




   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  * @test
  26  * @bug 6191390
  27  * @summary Verify that ActionEvent is received with correct modifiers set.
  28  * @library ../../../../lib/testlibrary ../


  29  * @build ExtendedRobot SystemTrayIconHelper

  30  */
  31 
  32 import java.awt.Image;
  33 import java.awt.TrayIcon;
  34 import java.awt.SystemTray;
  35 import java.awt.Robot;
  36 import java.awt.EventQueue;
  37 import java.awt.Point;
  38 import java.awt.event.ActionEvent;
  39 import java.awt.event.ActionListener;
  40 import java.awt.event.InputEvent;
  41 import java.awt.event.KeyEvent;
  42 import java.awt.image.BufferedImage;
  43 
  44 public class ActionEventTest {
  45 
  46     Image image;
  47     TrayIcon icon;
  48     Robot robot;

  49 
  50     public static void main(String[] args) throws Exception {
  51         if (!SystemTray.isSupported()) {
  52             System.out.println("SystemTray not supported on the platform." +
  53                 " Marking the test passed.");
  54         } else {
  55             if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  56                 System.err.println(
  57                     "Test can fail on Windows platform\n"+
  58                     "On Windows 7, by default icon hides behind icon pool\n" +
  59                     "Due to which test might fail\n" +
  60                     "Set \"Right mouse click\" -> " +
  61                     "\"Customize notification icons\" -> \"Always show " +
  62                     "all icons and notifications on the taskbar\" true " +
  63                     "to avoid this problem.\nOR change behavior only for " +
  64                     "Java SE tray icon and rerun test.");
  65             }
  66 
  67             ActionEventTest test = new ActionEventTest();
  68             test.doTest();
  69             test.clear();
  70         }
  71     }
  72 
  73     public ActionEventTest() throws Exception {
  74         robot = new Robot();
  75         EventQueue.invokeAndWait(this::initializeGUI);
  76     }
  77 
  78     private void initializeGUI() {
  79 
  80         icon = new TrayIcon(
  81             new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "ti");
  82         icon.addActionListener(new ActionListener() {
  83             @Override
  84             public void actionPerformed(ActionEvent ae) {

  85                 int md = ae.getModifiers();
  86                 int expectedMask = ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK
  87                         | ActionEvent.SHIFT_MASK;
  88 
  89                 if ((md & expectedMask) != expectedMask) {
  90                     clear();
  91                     throw new RuntimeException("Action Event modifiers are not"
  92                         + " set correctly.");
  93                 }
  94             }
  95         });
  96 
  97         try {
  98             SystemTray.getSystemTray().add(icon);
  99         } catch (Exception e) {
 100             throw new RuntimeException(e);
 101         }
 102     }
 103 
 104     public void clear() {



 105         SystemTray.getSystemTray().remove(icon);
 106     }
 107 
 108     void doTest() throws Exception {
 109         robot.keyPress(KeyEvent.VK_ALT);
 110         robot.keyPress(KeyEvent.VK_SHIFT);
 111         robot.keyPress(KeyEvent.VK_CONTROL);
 112 
 113         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 114         if (iconPosition == null) {
 115             throw new RuntimeException("Unable to find the icon location!");
 116         }
 117 
 118         robot.mouseMove(iconPosition.x, iconPosition.y);
 119         robot.waitForIdle();
 120 
 121         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 122         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 123         robot.delay(100);
 124         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 125         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 126         robot.delay(100);
 127         robot.waitForIdle();
 128         robot.keyRelease(KeyEvent.VK_ALT);
 129         robot.keyRelease(KeyEvent.VK_SHIFT);
 130         robot.keyRelease(KeyEvent.VK_CONTROL);
 131     }
 132 }


   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  * @test
  26  * @bug 6191390 8154328
  27  * @summary Verify that ActionEvent is received with correct modifiers set.
  28  * @library ../../../../lib/testlibrary ../
  29  * @library /java/awt/patchlib
  30  * @build java.desktop/java.awt.Helper
  31  * @build ExtendedRobot SystemTrayIconHelper
  32  * @run main ActionEventTest
  33  */
  34 
  35 import java.awt.Image;
  36 import java.awt.TrayIcon;
  37 import java.awt.SystemTray;
  38 import java.awt.Robot;
  39 import java.awt.EventQueue;
  40 import java.awt.Point;
  41 import java.awt.event.ActionEvent;
  42 import java.awt.event.ActionListener;
  43 import java.awt.event.InputEvent;
  44 import java.awt.event.KeyEvent;
  45 import java.awt.image.BufferedImage;
  46 
  47 public class ActionEventTest {
  48 
  49     Image image;
  50     TrayIcon icon;
  51     Robot robot;
  52     boolean actionPerformed;
  53 
  54     public static void main(String[] args) throws Exception {
  55         if (!SystemTray.isSupported()) {
  56             System.out.println("SystemTray not supported on the platform." +
  57                 " Marking the test passed.");
  58         } else {
  59             if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  60                 System.err.println(
  61                     "Test can fail on Windows platform\n"+
  62                     "On Windows 7, by default icon hides behind icon pool\n" +
  63                     "Due to which test might fail\n" +
  64                     "Set \"Right mouse click\" -> " +
  65                     "\"Customize notification icons\" -> \"Always show " +
  66                     "all icons and notifications on the taskbar\" true " +
  67                     "to avoid this problem.\nOR change behavior only for " +
  68                     "Java SE tray icon and rerun test.");
  69             }
  70 
  71             ActionEventTest test = new ActionEventTest();
  72             test.doTest();
  73             test.clear();
  74         }
  75     }
  76 
  77     public ActionEventTest() throws Exception {
  78         robot = new Robot();
  79         EventQueue.invokeAndWait(this::initializeGUI);
  80     }
  81 
  82     private void initializeGUI() {
  83 
  84         icon = new TrayIcon(
  85             new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "ti");
  86         icon.addActionListener(new ActionListener() {
  87             @Override
  88             public void actionPerformed(ActionEvent ae) {
  89                 actionPerformed = true;
  90                 int md = ae.getModifiers();
  91                 int expectedMask = ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK
  92                         | ActionEvent.SHIFT_MASK;
  93 
  94                 if ((md & expectedMask) != expectedMask) {
  95                     clear();
  96                     throw new RuntimeException("Action Event modifiers are not"
  97                         + " set correctly.");
  98                 }
  99             }
 100         });
 101 
 102         try {
 103             SystemTray.getSystemTray().add(icon);
 104         } catch (Exception e) {
 105             throw new RuntimeException(e);
 106         }
 107     }
 108 
 109     public void clear() {
 110         robot.keyRelease(KeyEvent.VK_ALT);
 111         robot.keyRelease(KeyEvent.VK_SHIFT);
 112         robot.keyRelease(KeyEvent.VK_CONTROL);
 113         SystemTray.getSystemTray().remove(icon);
 114     }
 115 
 116     void doTest() throws Exception {
 117         robot.keyPress(KeyEvent.VK_ALT);
 118         robot.keyPress(KeyEvent.VK_SHIFT);
 119         robot.keyPress(KeyEvent.VK_CONTROL);
 120 
 121         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 122         if (iconPosition == null) {
 123             throw new RuntimeException("Unable to find the icon location!");
 124         }
 125 
 126         robot.mouseMove(iconPosition.x, iconPosition.y);
 127         robot.waitForIdle();
 128 
 129         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 130         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 131         robot.delay(100);
 132         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 133         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

 134         robot.waitForIdle();
 135         if (!actionPerformed) {
 136             robot.delay(500);
 137         }
 138     }
 139 }
< prev index next >