< prev index next >

test/java/awt/TrayIcon/TrayIconEvents/TrayIconEventsTest.java

Print this page




  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import java.awt.event.*;
  26 import java.awt.image.BufferedImage;
  27 
  28 
  29 /*
  30  * @test
  31  * @summary Check for MouseEvents with all mouse buttons
  32  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  33  * @library ../../../../lib/testlibrary ../
  34  * @build ExtendedRobot SystemTrayIconHelper
  35  * @run main TrayIconEventsTest
  36  */
  37 
  38 public class TrayIconEventsTest {
  39 

  40     TrayIcon icon;
  41     ExtendedRobot robot;
  42 
  43     boolean actionPerformed = false;
  44     Object actionLock = new Object();
  45     Object pressLock = new Object();
  46     Object releaseLock = new Object();
  47     Object clickLock = new Object();
  48     Object moveLock = new Object();
  49 
  50     String caption = "Sample Icon";
  51     boolean mousePressed = false;
  52     boolean mouseReleased = false;
  53     boolean mouseClicked = false;
  54     boolean mouseMoved = false;
  55 
  56     int[] buttonTypes = {
  57         InputEvent.BUTTON1_MASK,
  58         InputEvent.BUTTON2_MASK,
  59         InputEvent.BUTTON3_MASK
  60     };
  61 
  62     String[] buttonNames = {
  63         "BUTTON1",
  64         "BUTTON2",
  65         "BUTTON3"
  66     };
  67 
  68     public static void main(String[] args) throws Exception {
  69         if (! SystemTray.isSupported()) {
  70             System.out.println("SystemTray not supported on the platform under test. " +
  71                                "Marking the test passed");
  72         } else {
  73             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
  74                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  75                         "in Windows 7, which is behavior by default.\n" +
  76                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  77                         "\"Always show all icons and notifications on the taskbar\" true " +
  78                         "to avoid this problem. Or change behavior only for Java SE " +
  79                         "tray icon.");



  80             new TrayIconEventsTest().doTest();
  81         }
  82     }
  83 
  84     public TrayIconEventsTest() throws Exception {
  85         robot = new ExtendedRobot();
  86         EventQueue.invokeAndWait(this::initializeGUI);
  87     }
  88 
  89     private void initializeGUI(){
  90         SystemTray tray = SystemTray.getSystemTray();
  91         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
  92         icon.addActionListener(event -> {
  93             actionPerformed = true;
  94             synchronized (actionLock) {
  95                 try {
  96 
  97                     actionLock.notifyAll();
  98                 } catch (Exception e) {
  99                 }


 178                     try {
 179                         moveLock.notifyAll();
 180                     } catch (Exception e) {
 181                     }
 182                 }
 183             }
 184         });
 185 
 186         try {
 187             tray.add(icon);
 188         } catch (AWTException e) {
 189             throw new RuntimeException(e);
 190         }
 191     }
 192 
 193     void doTest() throws Exception {
 194 
 195         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 196         if (iconPosition == null)
 197             throw new RuntimeException("Unable to find the icon location!");
 198 
 199         robot.mouseMove(iconPosition.x, iconPosition.y);


 200         robot.waitForIdle(2000);

 201 



 202         SystemTrayIconHelper.doubleClick(robot);
 203 
 204         if (! actionPerformed) {
 205             synchronized (actionLock) {
 206                 try {
 207                     actionLock.wait(10000);
 208                 } catch (Exception e) {
 209                 }
 210             }
 211         }
 212         if (! actionPerformed)
 213             throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");

 214 
 215         for (int i = 0; i < buttonTypes.length; i++) {
 216             mousePressed = false;





 217             robot.mousePress(buttonTypes[i]);

 218 
 219             if (! mousePressed) {
 220                 synchronized (pressLock) {
 221                     try {
 222                         pressLock.wait(3000);
 223                     } catch (Exception e) {
 224                     }
 225                 }
 226             }
 227             if (! mousePressed)
 228                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 229                     throw new RuntimeException("FAIL: mousePressed not triggered when " +
 230                             buttonNames[i] + " pressed");
 231 
 232             mouseReleased = false;
 233             mouseClicked = false;





 234             robot.mouseRelease(buttonTypes[i]);

 235 
 236             if (! mouseReleased) {
 237                 synchronized (releaseLock) {
 238                     try {
 239                         releaseLock.wait(3000);
 240                     } catch (Exception e) {
 241                     }
 242                 }
 243             }
 244             if (! mouseReleased)
 245                 throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 246                         buttonNames[i] + " released");
 247 
 248             if (! mouseClicked) {
 249                 synchronized (clickLock) {
 250                     try {
 251                         clickLock.wait(3000);
 252                     } catch (Exception e) {
 253                     }
 254                 }
 255             }
 256             if (! mouseClicked)
 257                 throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 258                         buttonNames[i] + " pressed & released");
 259         }
 260 

 261         mouseMoved = false;
 262         robot.mouseMove(iconPosition.x + 100, iconPosition.y);
 263         robot.glide(iconPosition.x, iconPosition.y);
 264 
 265         if (! mouseMoved)
 266             if (! SystemTrayIconHelper.skip(0) )
 267                 throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");

 268     }
 269 }


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import java.awt.event.*;
  26 import java.awt.image.BufferedImage;
  27 
  28 
  29 /*
  30  * @test
  31  * @summary Check for MouseEvents with all mouse buttons
  32  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  33  * @library ../../../../lib/testlibrary ../
  34  * @build ExtendedRobot SystemTrayIconHelper
  35  * @run main TrayIconEventsTest
  36  */
  37 
  38 public class TrayIconEventsTest {
  39 
  40     private static boolean isOEL7;
  41     TrayIcon icon;
  42     ExtendedRobot robot;
  43 
  44     boolean actionPerformed = false;
  45     Object actionLock = new Object();
  46     Object pressLock = new Object();
  47     Object releaseLock = new Object();
  48     Object clickLock = new Object();
  49     Object moveLock = new Object();
  50 
  51     String caption = "Sample Icon";
  52     boolean mousePressed = false;
  53     boolean mouseReleased = false;
  54     boolean mouseClicked = false;
  55     boolean mouseMoved = false;
  56 
  57     int[] buttonTypes = {
  58         InputEvent.BUTTON1_MASK,
  59         InputEvent.BUTTON2_MASK,
  60         InputEvent.BUTTON3_MASK
  61     };
  62 
  63     String[] buttonNames = {
  64         "BUTTON1",
  65         "BUTTON2",
  66         "BUTTON3"
  67     };
  68 
  69     public static void main(String[] args) throws Exception {
  70         if (! SystemTray.isSupported()) {
  71             System.out.println("SystemTray not supported on the platform under test. " +
  72                                "Marking the test passed");
  73         } else {
  74             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
  75                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  76                         "in Windows 7, which is behavior by default.\n" +
  77                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  78                         "\"Always show all icons and notifications on the taskbar\" true " +
  79                         "to avoid this problem. Or change behavior only for Java SE " +
  80                         "tray icon.");
  81             isOEL7 = System.getProperty("os.name").toLowerCase()
  82                     .contains("linux") && System.getProperty("os.version")
  83                     .toLowerCase().contains("el7");
  84             new TrayIconEventsTest().doTest();
  85         }
  86     }
  87 
  88     public TrayIconEventsTest() throws Exception {
  89         robot = new ExtendedRobot();
  90         EventQueue.invokeAndWait(this::initializeGUI);
  91     }
  92 
  93     private void initializeGUI(){
  94         SystemTray tray = SystemTray.getSystemTray();
  95         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
  96         icon.addActionListener(event -> {
  97             actionPerformed = true;
  98             synchronized (actionLock) {
  99                 try {
 100 
 101                     actionLock.notifyAll();
 102                 } catch (Exception e) {
 103                 }


 182                     try {
 183                         moveLock.notifyAll();
 184                     } catch (Exception e) {
 185                     }
 186                 }
 187             }
 188         });
 189 
 190         try {
 191             tray.add(icon);
 192         } catch (AWTException e) {
 193             throw new RuntimeException(e);
 194         }
 195     }
 196 
 197     void doTest() throws Exception {
 198 
 199         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 200         if (iconPosition == null)
 201             throw new RuntimeException("Unable to find the icon location!");
 202         if (isOEL7) {
 203             // close tray
 204             robot.mouseMove(100,100);
 205             robot.click(InputEvent.BUTTON1_MASK);
 206             robot.waitForIdle(2000);
 207         }
 208 
 209         robot.mouseMove(iconPosition.x, iconPosition.y);
 210         robot.waitForIdle();
 211         if(!isOEL7) {
 212             SystemTrayIconHelper.doubleClick(robot);
 213 
 214             if (!actionPerformed) {
 215                 synchronized (actionLock) {
 216                     try {
 217                         actionLock.wait(10000);
 218                     } catch (Exception e) {
 219                     }
 220                 }
 221             }
 222             if (!actionPerformed)
 223                 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
 224         }
 225 
 226         for (int i = 0; i < buttonTypes.length; i++) {
 227             mousePressed = false;
 228             if(isOEL7) {
 229                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 230                 robot.mouseMove(iconPosition.x, iconPosition.y);
 231                 robot.click(buttonTypes[i]);
 232             } else {
 233                 robot.mousePress(buttonTypes[i]);
 234             }
 235 
 236             if (! mousePressed) {
 237                 synchronized (pressLock) {
 238                     try {
 239                         pressLock.wait(6000);
 240                     } catch (Exception e) {
 241                     }
 242                 }
 243             }
 244             if (! mousePressed)
 245                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 246                     throw new RuntimeException("FAIL: mousePressed not triggered when " +
 247                             buttonNames[i] + " pressed");
 248 
 249             mouseReleased = false;
 250             mouseClicked = false;
 251             if(isOEL7) {
 252                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 253                 robot.mouseMove(iconPosition.x, iconPosition.y);
 254                 robot.click(buttonTypes[i]);
 255             } else {
 256                 robot.mouseRelease(buttonTypes[i]);
 257             }
 258 
 259             if (! mouseReleased) {
 260                 synchronized (releaseLock) {
 261                     try {
 262                         releaseLock.wait(6000);
 263                     } catch (Exception e) {
 264                     }
 265                 }
 266             }
 267             if (! mouseReleased)
 268                 throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 269                         buttonNames[i] + " released");
 270 
 271             if (! mouseClicked) {
 272                 synchronized (clickLock) {
 273                     try {
 274                         clickLock.wait(6000);
 275                     } catch (Exception e) {
 276                     }
 277                 }
 278             }
 279             if (! mouseClicked)
 280                 throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 281                         buttonNames[i] + " pressed & released");
 282         }
 283 
 284         if (!isOEL7) {
 285             mouseMoved = false;
 286             robot.mouseMove(iconPosition.x + 100, iconPosition.y);
 287             robot.glide(iconPosition.x, iconPosition.y);
 288 
 289             if (!mouseMoved)
 290                 if (!SystemTrayIconHelper.skip(0))
 291                     throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
 292         }
 293     }
 294 }
< prev index next >