< prev index next >

test/java/awt/TrayIcon/ModalityTest/ModalityTest.java

Print this page




  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 import java.awt.*;
  25 import java.awt.event.*;
  26 import java.awt.image.BufferedImage;
  27 
  28 /*
  29  * @test
  30  * @summary Check for MouseEvents with all mouse buttons
  31  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  32  * @library ../../../../lib/testlibrary ../
  33  * @build ExtendedRobot SystemTrayIconHelper
  34  * @run main ModalityTest
  35  */
  36 public class ModalityTest {
  37 

  38     TrayIcon icon;
  39     ExtendedRobot robot;
  40     Dialog d;
  41 
  42     boolean actionPerformed = false;
  43 
  44     private boolean dialogVisible = false;
  45     private Object dialogLock = new Object();
  46 
  47     Object actionLock = new Object();
  48     Object pressLock = new Object();
  49     Object releaseLock = new Object();
  50     Object clickLock = new Object();
  51     Object moveLock = new Object();
  52     static final int clickDelay = 50;
  53 
  54     boolean mousePressed = false;
  55     boolean mouseReleased = false;
  56     boolean mouseClicked = false;
  57     boolean mouseMoved = false;


  63     };
  64 
  65     String[] buttonNames = {
  66         "BUTTON1",
  67         "BUTTON2",
  68         "BUTTON3"
  69     };
  70 
  71     public static void main(String[] args) throws Exception {
  72         if (! SystemTray.isSupported()) {
  73             System.out.println("SystemTray not supported on the platform under test. " +
  74                                "Marking the test passed");
  75         } else {
  76             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
  77                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  78                         "in Windows 7, which is behavior by default.\n" +
  79                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  80                         "\"Always show all icons and notifications on the taskbar\" true " +
  81                         "to avoid this problem. Or change behavior only for Java SE tray " +
  82                         "icon and rerun test.");
  83 
  84             new ModalityTest().doTest();
  85         }
  86     }
  87 
  88     public ModalityTest() throws Exception {
  89         robot = new ExtendedRobot();
  90         EventQueue.invokeLater(this::initializeGUI);
  91     }
  92 
  93     void initializeGUI() {
  94         SystemTray tray = SystemTray.getSystemTray();
  95         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
  96         icon.addActionListener(event -> {
  97             actionPerformed = true;
  98             synchronized (actionLock) {
  99                 try {
 100                     actionLock.notifyAll();
 101                 } catch (Exception e) {
 102                 }
 103             }


 208         d.setVisible(true);
 209     }
 210 
 211     void doTest() throws Exception {
 212 
 213         if (! dialogVisible) {
 214             synchronized (dialogLock) {
 215                 try {
 216                     dialogLock.wait(3000);
 217                 } catch (Exception e) {
 218                 }
 219             }
 220         }
 221 
 222         if (! dialogVisible)
 223             throw new RuntimeException("ERROR: TIMEOUT: The thread in EDT not yet complete");
 224 
 225         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 226         if (iconPosition == null)
 227             throw new RuntimeException("Unable to find the icon location!");






 228 
 229         if (! d.isVisible())
 230             throw new RuntimeException("FAIL: The modal dialog is not yet visible");
 231 
 232         robot.mouseMove(iconPosition.x, iconPosition.y);
 233         robot.waitForIdle(2000);
 234 

 235         SystemTrayIconHelper.doubleClick(robot);
 236 
 237         if (! actionPerformed) {
 238             synchronized (actionLock) {
 239                 try {
 240                     actionLock.wait(3000);
 241                 } catch (Exception e) {
 242                 }
 243             }
 244         }
 245         if (! actionPerformed)
 246             throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");

 247 
 248         for (int i = 0; i < buttonTypes.length; i++) {
 249             mousePressed = false;





 250             robot.mousePress(buttonTypes[i]);

 251 
 252             if (! mousePressed) {
 253                 synchronized (pressLock) {
 254                     try {
 255                         pressLock.wait(3000);
 256                     } catch (Exception e) {
 257                     }
 258                 }
 259             }
 260             if (! mousePressed)
 261                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 262                     throw new RuntimeException("FAIL: mousePressed not triggered when " +
 263                             buttonNames[i] + " pressed");
 264 
 265             mouseReleased = false;
 266             mouseClicked = false;





 267             robot.mouseRelease(buttonTypes[i]);

 268 
 269             if (! mouseReleased) {
 270                 synchronized (releaseLock) {
 271                     try {
 272                         releaseLock.wait(3000);
 273                     } catch (Exception e) {
 274                     }
 275                 }
 276             }
 277             if (! mouseReleased)
 278                 throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 279                         buttonNames[i] + " released");
 280 
 281             if (! mouseClicked) {
 282                 synchronized (clickLock) {
 283                     try {
 284                         clickLock.wait(3000);
 285                     } catch (Exception e) {
 286                     }
 287                 }
 288             }
 289             if (! mouseClicked)
 290                 throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 291                         buttonNames[i] + " pressed & released");
 292         }
 293 
 294         mouseMoved = false;
 295         robot.mouseMove(iconPosition.x, iconPosition.y);
 296         robot.glide(iconPosition.x + 100, iconPosition.y);
 297 
 298         if (! mouseMoved)
 299             if (! SystemTrayIconHelper.skip(0) )
 300                 throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");

 301     }
 302 }


  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 import java.awt.*;
  25 import java.awt.event.*;
  26 import java.awt.image.BufferedImage;
  27 
  28 /*
  29  * @test
  30  * @summary Check for MouseEvents with all mouse buttons
  31  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  32  * @library ../../../../lib/testlibrary ../
  33  * @build ExtendedRobot SystemTrayIconHelper
  34  * @run main ModalityTest
  35  */
  36 public class ModalityTest {
  37 
  38     private static boolean isOEL7;
  39     TrayIcon icon;
  40     ExtendedRobot robot;
  41     Dialog d;
  42 
  43     boolean actionPerformed = false;
  44 
  45     private boolean dialogVisible = false;
  46     private Object dialogLock = new Object();
  47 
  48     Object actionLock = new Object();
  49     Object pressLock = new Object();
  50     Object releaseLock = new Object();
  51     Object clickLock = new Object();
  52     Object moveLock = new Object();
  53     static final int clickDelay = 50;
  54 
  55     boolean mousePressed = false;
  56     boolean mouseReleased = false;
  57     boolean mouseClicked = false;
  58     boolean mouseMoved = false;


  64     };
  65 
  66     String[] buttonNames = {
  67         "BUTTON1",
  68         "BUTTON2",
  69         "BUTTON3"
  70     };
  71 
  72     public static void main(String[] args) throws Exception {
  73         if (! SystemTray.isSupported()) {
  74             System.out.println("SystemTray not supported on the platform under test. " +
  75                                "Marking the test passed");
  76         } else {
  77             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
  78                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  79                         "in Windows 7, which is behavior by default.\n" +
  80                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  81                         "\"Always show all icons and notifications on the taskbar\" true " +
  82                         "to avoid this problem. Or change behavior only for Java SE tray " +
  83                         "icon and rerun test.");
  84             isOEL7 = SystemTrayIconHelper.isOel7();
  85             new ModalityTest().doTest();
  86         }
  87     }
  88 
  89     public ModalityTest() throws Exception {
  90         robot = new ExtendedRobot();
  91         EventQueue.invokeLater(this::initializeGUI);
  92     }
  93 
  94     void initializeGUI() {
  95         SystemTray tray = SystemTray.getSystemTray();
  96         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
  97         icon.addActionListener(event -> {
  98             actionPerformed = true;
  99             synchronized (actionLock) {
 100                 try {
 101                     actionLock.notifyAll();
 102                 } catch (Exception e) {
 103                 }
 104             }


 209         d.setVisible(true);
 210     }
 211 
 212     void doTest() throws Exception {
 213 
 214         if (! dialogVisible) {
 215             synchronized (dialogLock) {
 216                 try {
 217                     dialogLock.wait(3000);
 218                 } catch (Exception e) {
 219                 }
 220             }
 221         }
 222 
 223         if (! dialogVisible)
 224             throw new RuntimeException("ERROR: TIMEOUT: The thread in EDT not yet complete");
 225 
 226         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 227         if (iconPosition == null)
 228             throw new RuntimeException("Unable to find the icon location!");
 229         if (isOEL7) {
 230             // close tray
 231             robot.mouseMove(100,100);
 232             robot.click(InputEvent.BUTTON1_MASK);
 233             robot.waitForIdle(2000);
 234         }
 235 
 236         if (! d.isVisible())
 237             throw new RuntimeException("FAIL: The modal dialog is not yet visible");
 238 
 239         robot.mouseMove(iconPosition.x, iconPosition.y);
 240         robot.waitForIdle(2000);
 241 
 242         if(!isOEL7) {
 243             SystemTrayIconHelper.doubleClick(robot);
 244 
 245             if (!actionPerformed) {
 246                 synchronized (actionLock) {
 247                     try {
 248                         actionLock.wait(3000);
 249                     } catch (Exception e) {
 250                     }
 251                 }
 252             }
 253             if (!actionPerformed)
 254                 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
 255         }
 256 
 257         for (int i = 0; i < buttonTypes.length; i++) {
 258             mousePressed = false;
 259             if(isOEL7) {
 260                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 261                 robot.mouseMove(iconPosition.x, iconPosition.y);
 262                 robot.click(buttonTypes[i]);
 263             } else {
 264                 robot.mousePress(buttonTypes[i]);
 265             }
 266 
 267             if (! mousePressed) {
 268                 synchronized (pressLock) {
 269                     try {
 270                         pressLock.wait(6000);
 271                     } catch (Exception e) {
 272                     }
 273                 }
 274             }
 275             if (! mousePressed)
 276                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 277                     throw new RuntimeException("FAIL: mousePressed not triggered when " +
 278                             buttonNames[i] + " pressed");
 279 
 280             mouseReleased = false;
 281             mouseClicked = false;
 282             if(isOEL7) {
 283                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 284                 robot.mouseMove(iconPosition.x, iconPosition.y);
 285                 robot.click(buttonTypes[i]);
 286             } else {
 287                 robot.mouseRelease(buttonTypes[i]);
 288             }
 289 
 290             if (! mouseReleased) {
 291                 synchronized (releaseLock) {
 292                     try {
 293                         releaseLock.wait(6000);
 294                     } catch (Exception e) {
 295                     }
 296                 }
 297             }
 298             if (! mouseReleased)
 299                 throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 300                         buttonNames[i] + " released");
 301 
 302             if (! mouseClicked) {
 303                 synchronized (clickLock) {
 304                     try {
 305                         clickLock.wait(6000);
 306                     } catch (Exception e) {
 307                     }
 308                 }
 309             }
 310             if (! mouseClicked)
 311                 throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 312                         buttonNames[i] + " pressed & released");
 313         }
 314         if (!isOEL7) {
 315             mouseMoved = false;
 316             robot.mouseMove(iconPosition.x, iconPosition.y);
 317             robot.glide(iconPosition.x + 100, iconPosition.y);
 318 
 319             if (!mouseMoved)
 320                 if (!SystemTrayIconHelper.skip(0))
 321                     throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
 322         }
 323     }
 324 }
< prev index next >