1 import java.awt.*; 2 import java.awt.event.*; 3 import java.awt.geom.Point2D; 4 import java.awt.image.BufferedImage; 5 import java.lang.reflect.Field; 6 import java.lang.reflect.Method; 7 import java.security.AccessController; 8 import java.security.PrivilegedActionException; 9 import java.security.PrivilegedExceptionAction; 10 11 /* 12 * @summary This is a helper class to find the location of a system tray icon, 13 * and skip some OS specific cases in tests. 14 * @library ../../../../../lib/testlibrary 15 * @build ExtendedRobot SystemTrayIconHelper 16 */ 17 public class SystemTrayIconHelper { 18 19 static Frame frame; 20 21 /** 22 * Call this method if the tray icon need to be followed in an automated manner 23 * This method will be called by automated testcases 24 */ 25 static Point getTrayIconLocation(TrayIcon icon) throws Exception { 26 if (icon == null) { 27 return null; 28 } 29 30 //This is added just in case the tray's native menu is visible. 31 //It has to be hidden if visible. For that, we are showing a Frame 32 //and clicking on it - the assumption is, the menu will 33 //be closed if another window is clicked 34 ExtendedRobot robot = new ExtendedRobot(); 35 try { 36 EventQueue.invokeAndWait(() -> { 37 frame = new Frame(); 38 frame.setSize(100, 100); 39 frame.setVisible(true); 40 }); 41 robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth() / 2, 42 frame.getLocationOnScreen().y + frame.getHeight() / 2); 43 robot.waitForIdle(); 44 robot.click(); 45 EventQueue.invokeAndWait(frame::dispose); 46 } catch (Exception e) { 47 return null; 48 } 49 50 if (System.getProperty("os.name").startsWith("Win")) { 51 try { 52 // sun.awt.windows.WTrayIconPeer 53 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 54 Dimension iconSize = icon.getSize(); 55 56 int width = (int) iconSize.getWidth(); 57 int height = (int) iconSize.getHeight(); 58 59 // Some previously created icons may not be removed 60 // from tray until mouse move on it. So we glide 61 // through the whole tray bar. 62 robot.glide((int) screenSize.getWidth(), (int) (screenSize.getHeight()-15), 0, (int) (screenSize.getHeight() - 15), 1, 2); 63 64 BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize)); 65 66 for (int x = (int) (screenSize.getWidth()-width); x > 0; x--) { 67 for (int y = (int) (screenSize.getHeight()-height); y > (screenSize.getHeight()-50); y--) { 68 if (imagesEquals(((BufferedImage)icon.getImage()).getSubimage(0, 0, width, height), screen.getSubimage(x, y, width, height))) { 69 return new Point(x+5, y+5); 70 } 71 } 72 } 73 } catch (Exception e) { 74 e.printStackTrace(); 75 return null; 76 } 77 } else if (System.getProperty("os.name").startsWith("Mac")) { 78 Point2D point2d; 79 try { 80 // sun.lwawt.macosx.CTrayIcon 81 Field f_peer = getField( java.awt.TrayIcon.class, "peer"); 82 83 Object peer = f_peer.get(icon); 84 Method m_getModel = peer.getClass().getDeclaredMethod( 85 "getModel"); 86 m_getModel.setAccessible(true); 87 long model = (Long) (m_getModel.invoke(peer, new Object[]{})); 88 Method m_getLocation = peer.getClass().getDeclaredMethod( 89 "nativeGetIconLocation", new Class[]{Long.TYPE}); 90 m_getLocation.setAccessible(true); 91 point2d = (Point2D)m_getLocation.invoke(peer, new Object[]{model}); 92 Point po = new Point((int)(point2d.getX()), (int)(point2d.getY())); 93 po.translate(10, -5); 94 return po; 95 }catch(Exception e) { 96 e.printStackTrace(); 97 return null; 98 } 99 } else { 100 try { 101 // sun.awt.X11.XTrayIconPeer 102 Field f_peer = getField(java.awt.TrayIcon.class, "peer"); 103 104 Object peer = f_peer.get(icon); 105 Method m_getLOS = peer.getClass().getDeclaredMethod( 106 "getLocationOnScreen", new Class[]{}); 107 m_getLOS.setAccessible(true); 108 Point point = (Point)m_getLOS.invoke(peer, new Object[]{}); 109 point.translate(5, 5); 110 return point; 111 } catch (Exception e) { 112 e.printStackTrace(); 113 return null; 114 } 115 } 116 return null; 117 } 118 119 static Field getField(final Class clz, final String fieldName) { 120 Field res = null; 121 try { 122 res = (Field)AccessController.doPrivileged((PrivilegedExceptionAction) () -> { 123 Field f = clz.getDeclaredField(fieldName); 124 f.setAccessible(true); 125 return f; 126 }); 127 } catch (PrivilegedActionException ex) { 128 ex.printStackTrace(); 129 } 130 return res; 131 } 132 133 static boolean imagesEquals(BufferedImage img1, BufferedImage img2) { 134 for (int x = 0; x < img1.getWidth(); x++) { 135 for (int y = 0; y < img1.getHeight(); y++) { 136 if (img1.getRGB(x, y) != img2.getRGB(x, y)) 137 return false; 138 } 139 } 140 return true; 141 } 142 143 static void doubleClick(Robot robot) { 144 if (System.getProperty("os.name").startsWith("Mac")) { 145 robot.mousePress(InputEvent.BUTTON3_MASK); 146 robot.delay(50); 147 robot.mouseRelease(InputEvent.BUTTON3_MASK); 148 } else { 149 robot.mousePress(InputEvent.BUTTON1_MASK); 150 robot.delay(50); 151 robot.mouseRelease(InputEvent.BUTTON1_MASK); 152 robot.delay(50); 153 robot.mousePress(InputEvent.BUTTON1_MASK); 154 robot.delay(50); 155 robot.mouseRelease(InputEvent.BUTTON1_MASK); 156 } 157 } 158 159 // Method for skipping some OS specific cases 160 static boolean skip(int button) { 161 if (System.getProperty("os.name").toLowerCase().startsWith("win")){ 162 if (button == InputEvent.BUTTON1_MASK){ 163 // See JDK-6827035 164 return true; 165 } 166 } else if (System.getProperty("os.name").toLowerCase().contains("os x")){ 167 // See JDK-7153700 168 return true; 169 } 170 return false; 171 } 172 }