1 /* 2 * Copyright (c) 2007, 2014, 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 java.desktop/java.awt.Helper 35 * @build ExtendedRobot SystemTrayIconHelper 36 * @run main/othervm/policy=tray.policy -Djava.security.manager FunctionalityCheck 37 */ 38 39 public class FunctionalityCheck { 40 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 static final int[] buttonTypes = { 58 InputEvent.BUTTON1_MASK, 59 InputEvent.BUTTON2_MASK, 60 InputEvent.BUTTON3_MASK 61 }; 62 63 static final 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 new FunctionalityCheck().doTest(); 75 } 76 } 77 78 FunctionalityCheck() throws Exception { 79 robot = new ExtendedRobot(); 80 EventQueue.invokeAndWait(this::initializeGUI); 81 } 82 83 void initializeGUI() { 84 SystemTray tray = SystemTray.getSystemTray(); 85 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption); 86 icon.addActionListener(event -> { 87 actionPerformed = true; 88 synchronized (actionLock) { 89 try { 90 actionLock.notifyAll(); 91 } catch (Exception e) { 92 } 93 } 94 }); 95 icon.addMouseListener(new MouseAdapter() { 96 public void mousePressed(MouseEvent event) { 97 mousePressed = true; 98 Point p = event.getPoint(); 99 if (p.x != event.getX() || p.y != event.getY()) 100 throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 101 "not return the same value as getX/getY " + 102 "for mousePressed"); 103 104 if (! icon.equals(event.getSource())) 105 throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " + 106 "did not return TrayIcon object"); 107 108 synchronized (pressLock) { 109 try { 110 pressLock.notifyAll(); 111 } catch (Exception e) { 112 } 113 } 114 } 115 116 public void mouseReleased(MouseEvent event) { 117 mouseReleased = true; 118 Point p = event.getPoint(); 119 if (p.x != event.getX() || p.y != event.getY()) 120 throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 121 "not return the same value as getX/getY " + 122 "for mouseReleased"); 123 124 if (! icon.equals(event.getSource())) 125 throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " + 126 "did not return TrayIcon object"); 127 128 synchronized (releaseLock) { 129 try { 130 releaseLock.notifyAll(); 131 } catch (Exception e) { 132 } 133 } 134 } 135 136 public void mouseClicked(MouseEvent event) { 137 mouseClicked = true; 138 Point p = event.getPoint(); 139 if (p.x != event.getX() || p.y != event.getY()) 140 throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 141 "not return the same value as getX/getY " + 142 "for mouseClicked"); 143 144 if (! icon.equals(event.getSource())) 145 throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " + 146 "did not return TrayIcon object"); 147 148 synchronized (clickLock) { 149 try { 150 clickLock.notifyAll(); 151 } catch (Exception e) { 152 } 153 } 154 } 155 }); 156 157 icon.addMouseMotionListener(new MouseMotionAdapter() { 158 public void mouseMoved(MouseEvent event) { 159 mouseMoved = true; 160 Point p = event.getPoint(); 161 if (p.x != event.getX() || p.y != event.getY()) 162 throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 163 "not return the same value as getX/getY " + 164 "for mouseMoved"); 165 166 if (! icon.equals(event.getSource())) 167 throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " + 168 "did not return TrayIcon object"); 169 170 synchronized (moveLock) { 171 try { 172 moveLock.notifyAll(); 173 } catch (Exception e) { 174 } 175 } 176 } 177 }); 178 179 try { 180 tray.add(icon); 181 } catch (Exception e) { 182 throw new RuntimeException(e); 183 } 184 } 185 186 private void doTest() throws Exception { 187 188 189 190 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 191 if (iconPosition == null) 192 throw new RuntimeException("Unable to find the icon location!"); 193 194 robot.mouseMove(iconPosition.x, iconPosition.y); 195 robot.waitForIdle(2000); 196 197 SystemTrayIconHelper.doubleClick(robot); 198 199 if (! actionPerformed) { 200 synchronized (actionLock) { 201 try { 202 actionLock.wait(3000); 203 } catch (Exception e) { 204 } 205 } 206 } 207 if (! actionPerformed) 208 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked"); 209 210 for (int i = 0; i < buttonTypes.length; i++) { 211 mousePressed = false; 212 robot.mousePress(buttonTypes[i]); 213 214 if (! mousePressed) { 215 synchronized (pressLock) { 216 try { 217 pressLock.wait(3000); 218 } catch (Exception e) { 219 } 220 } 221 } 222 if (! mousePressed) 223 if (! SystemTrayIconHelper.skip(buttonTypes[i]) ) 224 throw new RuntimeException("FAIL: mousePressed not triggered when " + 225 buttonNames[i] + " pressed"); 226 227 mouseReleased = false; 228 mouseClicked = false; 229 robot.mouseRelease(buttonTypes[i]); 230 231 if (! mouseReleased) { 232 synchronized (releaseLock) { 233 try { 234 releaseLock.wait(3000); 235 } catch (Exception e) { 236 } 237 } 238 } 239 if (! mouseReleased) 240 if (! SystemTrayIconHelper.skip(buttonTypes[i]) ) 241 throw new RuntimeException("FAIL: mouseReleased not triggered when " + 242 buttonNames[i] + " released"); 243 244 if (! mouseClicked) { 245 synchronized (clickLock) { 246 try { 247 clickLock.wait(3000); 248 } catch (Exception e) { 249 } 250 } 251 } 252 if (! mouseClicked) 253 throw new RuntimeException("FAIL: mouseClicked not triggered when " + 254 buttonNames[i] + " pressed & released"); 255 } 256 257 mouseMoved = false; 258 robot.mouseMove(iconPosition.x + 100, iconPosition.y); 259 robot.glide(iconPosition.x, iconPosition.y); 260 261 if (! mouseMoved) 262 if (! SystemTrayIconHelper.skip(0) ) 263 throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon"); 264 } 265 }