1 /*
   2  * Copyright (c) 2007, 2015, 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 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 /java/awt/patchlib
  33  * @library ../../../../lib/testlibrary ../
  34  * @build ExtendedRobot SystemTrayIconHelper
  35  * @build java.desktop/java.awt.Helper
  36  * @run main ModalityTest
  37  */
  38 public class ModalityTest {
  39 
  40     private static boolean isOEL7;
  41     TrayIcon icon;
  42     ExtendedRobot robot;
  43     Dialog d;
  44 
  45     boolean actionPerformed = false;
  46 
  47     private boolean dialogVisible = false;
  48     private Object dialogLock = new Object();
  49 
  50     Object actionLock = new Object();
  51     Object pressLock = new Object();
  52     Object releaseLock = new Object();
  53     Object clickLock = new Object();
  54     Object moveLock = new Object();
  55     static final int clickDelay = 50;
  56 
  57     boolean mousePressed = false;
  58     boolean mouseReleased = false;
  59     boolean mouseClicked = false;
  60     boolean mouseMoved = false;
  61 
  62     int[] buttonTypes = {
  63         InputEvent.BUTTON1_MASK,
  64         InputEvent.BUTTON2_MASK,
  65         InputEvent.BUTTON3_MASK
  66     };
  67 
  68     String[] buttonNames = {
  69         "BUTTON1",
  70         "BUTTON2",
  71         "BUTTON3"
  72     };
  73 
  74     public static void main(String[] args) throws Exception {
  75         if (! SystemTray.isSupported()) {
  76             System.out.println("SystemTray not supported on the platform under test. " +
  77                                "Marking the test passed");
  78         } else {
  79             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
  80                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  81                         "in Windows 7, which is behavior by default.\n" +
  82                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  83                         "\"Always show all icons and notifications on the taskbar\" true " +
  84                         "to avoid this problem. Or change behavior only for Java SE tray " +
  85                         "icon and rerun test.");
  86             isOEL7 = SystemTrayIconHelper.isOel7();
  87             new ModalityTest().doTest();
  88         }
  89     }
  90 
  91     public ModalityTest() throws Exception {
  92         robot = new ExtendedRobot();
  93         EventQueue.invokeLater(this::initializeGUI);
  94     }
  95 
  96     void initializeGUI() {
  97         SystemTray tray = SystemTray.getSystemTray();
  98         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
  99         icon.addActionListener(event -> {
 100             actionPerformed = true;
 101             synchronized (actionLock) {
 102                 try {
 103                     actionLock.notifyAll();
 104                 } catch (Exception e) {
 105                 }
 106             }
 107         });
 108         icon.addMouseListener(new MouseAdapter() {
 109             public void mousePressed(MouseEvent event) {
 110                 mousePressed = true;
 111                 Point p = event.getPoint();
 112                 if (p.x != event.getX() || p.y != event.getY())
 113                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
 114                             "not return the same value as getX/getY " +
 115                             "for mousePressed");
 116 
 117                 if (! icon.equals(event.getSource()))
 118                     throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
 119                             "did not return TrayIcon object");
 120 
 121                 synchronized (pressLock) {
 122                     try {
 123                         pressLock.notifyAll();
 124                     } catch (Exception e) {
 125                     }
 126                 }
 127             }
 128 
 129             public void mouseReleased(MouseEvent event) {
 130                 mouseReleased = true;
 131                 Point p = event.getPoint();
 132                 if (p.x != event.getX() || p.y != event.getY())
 133                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
 134                             "not return the same value as getX/getY " +
 135                             "for mouseReleased");
 136 
 137                 if (! icon.equals(event.getSource()))
 138                     throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
 139                             "did not return TrayIcon object");
 140 
 141                 synchronized (releaseLock) {
 142                     try {
 143                         releaseLock.notifyAll();
 144                     } catch (Exception e) {
 145                     }
 146                 }
 147             }
 148 
 149             public void mouseClicked(MouseEvent event) {
 150                 mouseClicked = true;
 151                 Point p = event.getPoint();
 152                 if (p.x != event.getX() || p.y != event.getY())
 153                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
 154                             "not return the same value as getX/getY " +
 155                             "for mouseClicked");
 156 
 157                 if (! icon.equals(event.getSource()))
 158                     throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
 159                             "did not return TrayIcon object");
 160 
 161                 synchronized (clickLock) {
 162                     try {
 163                         clickLock.notifyAll();
 164                     } catch (Exception e) {
 165                     }
 166                 }
 167             }
 168         });
 169 
 170         icon.addMouseMotionListener(new MouseMotionAdapter() {
 171             public void mouseMoved(MouseEvent event) {
 172                 mouseMoved = true;
 173                 Point p = event.getPoint();
 174                 if (p.x != event.getX() || p.y != event.getY())
 175                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
 176                             "not return the same value as getX/getY " +
 177                             "for mouseMoved");
 178 
 179                 if (!icon.equals(event.getSource()))
 180                     throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " +
 181                             "did not return TrayIcon object");
 182 
 183                 synchronized (moveLock) {
 184                     try {
 185                         moveLock.notifyAll();
 186                     } catch (Exception e) {
 187                     }
 188                 }
 189             }
 190         });
 191 
 192         try {
 193             tray.add(icon);
 194         } catch (AWTException e) {
 195             throw new RuntimeException(e);
 196         }
 197 
 198         d = new Dialog((Frame) null, "Modal Dialog");
 199         d.setLocation(200, 200);
 200         d.setSize(100, 100);
 201         d.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
 202 
 203         dialogVisible = true;
 204         synchronized (dialogLock) {
 205             try {
 206                 dialogLock.notifyAll();
 207             } catch (Exception e) {
 208             }
 209         }
 210 
 211         d.setVisible(true);
 212     }
 213 
 214     void doTest() throws Exception {
 215 
 216         if (! dialogVisible) {
 217             synchronized (dialogLock) {
 218                 try {
 219                     dialogLock.wait(3000);
 220                 } catch (Exception e) {
 221                 }
 222             }
 223         }
 224 
 225         if (! dialogVisible)
 226             throw new RuntimeException("ERROR: TIMEOUT: The thread in EDT not yet complete");
 227 
 228         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 229         if (iconPosition == null)
 230             throw new RuntimeException("Unable to find the icon location!");
 231         if (isOEL7) {
 232             // close tray
 233             robot.mouseMove(100,100);
 234             robot.click(InputEvent.BUTTON1_MASK);
 235             robot.waitForIdle(2000);
 236         }
 237 
 238         if (! d.isVisible())
 239             throw new RuntimeException("FAIL: The modal dialog is not yet visible");
 240 
 241         robot.mouseMove(iconPosition.x, iconPosition.y);
 242         robot.waitForIdle(2000);
 243 
 244         if(!isOEL7) {
 245             SystemTrayIconHelper.doubleClick(robot);
 246 
 247             if (!actionPerformed) {
 248                 synchronized (actionLock) {
 249                     try {
 250                         actionLock.wait(3000);
 251                     } catch (Exception e) {
 252                     }
 253                 }
 254             }
 255             if (!actionPerformed)
 256                 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
 257         }
 258 
 259         for (int i = 0; i < buttonTypes.length; i++) {
 260             mousePressed = false;
 261             if(isOEL7) {
 262                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 263                 robot.mouseMove(iconPosition.x, iconPosition.y);
 264                 robot.click(buttonTypes[i]);
 265             } else {
 266                 robot.mousePress(buttonTypes[i]);
 267             }
 268 
 269             if (! mousePressed) {
 270                 synchronized (pressLock) {
 271                     try {
 272                         pressLock.wait(6000);
 273                     } catch (Exception e) {
 274                     }
 275                 }
 276             }
 277             if (! mousePressed)
 278                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 279                     throw new RuntimeException("FAIL: mousePressed not triggered when " +
 280                             buttonNames[i] + " pressed");
 281 
 282             mouseReleased = false;
 283             mouseClicked = false;
 284             if(isOEL7) {
 285                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 286                 robot.mouseMove(iconPosition.x, iconPosition.y);
 287                 robot.click(buttonTypes[i]);
 288             } else {
 289                 robot.mouseRelease(buttonTypes[i]);
 290             }
 291 
 292             if (! mouseReleased) {
 293                 synchronized (releaseLock) {
 294                     try {
 295                         releaseLock.wait(6000);
 296                     } catch (Exception e) {
 297                     }
 298                 }
 299             }
 300             if (! mouseReleased)
 301                 throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 302                         buttonNames[i] + " released");
 303 
 304             if (! mouseClicked) {
 305                 synchronized (clickLock) {
 306                     try {
 307                         clickLock.wait(6000);
 308                     } catch (Exception e) {
 309                     }
 310                 }
 311             }
 312             if (! mouseClicked)
 313                 throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 314                         buttonNames[i] + " pressed & released");
 315         }
 316         if (!isOEL7) {
 317             mouseMoved = false;
 318             robot.mouseMove(iconPosition.x, iconPosition.y);
 319             robot.glide(iconPosition.x + 100, iconPosition.y);
 320 
 321             if (!mouseMoved)
 322                 if (!SystemTrayIconHelper.skip(0))
 323                     throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
 324         }
 325     }
 326 }