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 Point point = new Point(x + 5, y + 5); 70 System.out.println("Icon location " + point); 71 return point; 72 } 73 } 74 } 75 } catch (Exception e) { 76 e.printStackTrace(); 77 return null; 78 } 79 } else if (System.getProperty("os.name").startsWith("Mac")) { 80 Point2D point2d; 81 try { 82 // sun.lwawt.macosx.CTrayIcon 83 Field f_peer = getField( java.awt.TrayIcon.class, "peer"); 84 85 Object peer = f_peer.get(icon); 86 Method m_getModel = peer.getClass().getDeclaredMethod( 87 "getModel"); 88 m_getModel.setAccessible(true); 89 long model = (Long) (m_getModel.invoke(peer, new Object[]{})); 90 Method m_getLocation = peer.getClass().getDeclaredMethod( 91 "nativeGetIconLocation", new Class[]{Long.TYPE}); 92 m_getLocation.setAccessible(true); 93 point2d = (Point2D)m_getLocation.invoke(peer, new Object[]{model}); 94 Point po = new Point((int)(point2d.getX()), (int)(point2d.getY())); 95 po.translate(10, -5); 96 System.out.println("Icon location " + po); 97 return po; 98 }catch(Exception e) { 99 e.printStackTrace(); 100 return null; 101 } 102 } else { 103 try { 104 // sun.awt.X11.XTrayIconPeer 105 Field f_peer = getField(java.awt.TrayIcon.class, "peer"); 106 107 SystemTrayIconHelper.openTrayIfNeeded(robot); 108 109 Object peer = f_peer.get(icon); 110 Method m_getLOS = peer.getClass().getDeclaredMethod( 111 "getLocationOnScreen", new Class[]{}); 112 m_getLOS.setAccessible(true); 113 Point point = (Point)m_getLOS.invoke(peer, new Object[]{}); 114 point.translate(5, 5); 115 System.out.println("Icon location " + point); 116 return point; 117 } catch (Exception e) { 118 e.printStackTrace(); 119 return null; 120 } 121 } 122 return null; 123 } 124 125 static Field getField(final Class clz, final String fieldName) { 126 Field res = null; 127 try { 128 res = (Field)AccessController.doPrivileged((PrivilegedExceptionAction) () -> { 129 Field f = clz.getDeclaredField(fieldName); 130 f.setAccessible(true); 131 return f; 132 }); 133 } catch (PrivilegedActionException ex) { 134 ex.printStackTrace(); 135 } 136 return res; 137 } 138 139 static boolean imagesEquals(BufferedImage img1, BufferedImage img2) { 140 for (int x = 0; x < img1.getWidth(); x++) { 141 for (int y = 0; y < img1.getHeight(); y++) { 142 if (img1.getRGB(x, y) != img2.getRGB(x, y)) 143 return false; 144 } 145 } 146 return true; 147 } 148 149 static void doubleClick(Robot robot) { 150 if (System.getProperty("os.name").startsWith("Mac")) { 151 robot.mousePress(InputEvent.BUTTON3_MASK); 152 robot.delay(50); 153 robot.mouseRelease(InputEvent.BUTTON3_MASK); 154 } else { 155 robot.mousePress(InputEvent.BUTTON1_MASK); 156 robot.delay(50); 157 robot.mouseRelease(InputEvent.BUTTON1_MASK); 158 robot.delay(50); 159 robot.mousePress(InputEvent.BUTTON1_MASK); 160 robot.delay(50); 161 robot.mouseRelease(InputEvent.BUTTON1_MASK); 162 } 163 } 164 165 // Method for skipping some OS specific cases 166 static boolean skip(int button) { 167 if (System.getProperty("os.name").toLowerCase().startsWith("win")){ 168 if (button == InputEvent.BUTTON1_MASK){ 169 // See JDK-6827035 170 return true; 171 } 172 } else if (System.getProperty("os.name").toLowerCase().contains("os x")){ 173 // See JDK-7153700 174 return true; 175 } 176 return false; 177 } 178 179 public static boolean openTrayIfNeeded(Robot robot) { 180 String sysv = System.getProperty("os.version"); 181 System.out.println("System version is " + sysv); 182 //Additional step to raise the system try in Gnome 3 in OEL 7 183 if(isOel7()) { 184 System.out.println("OEL 7 detected"); 185 GraphicsConfiguration gc = GraphicsEnvironment. 186 getLocalGraphicsEnvironment().getDefaultScreenDevice(). 187 getDefaultConfiguration(); 188 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 189 if(insets.bottom > 0) { 190 Dimension screenSize = Toolkit.getDefaultToolkit() 191 .getScreenSize(); 192 robot.mouseMove(screenSize.width - insets.bottom / 2, 193 screenSize.height - insets.bottom / 2); 194 robot.delay(50); 195 robot.mousePress(InputEvent.BUTTON1_MASK); 196 robot.delay(50); 197 robot.mouseRelease(InputEvent.BUTTON1_MASK); 198 robot.waitForIdle(); 199 robot.delay(1000); 200 System.out.println("Tray is opened"); 201 return true; 202 } 203 } 204 return false; 205 } 206 207 public static boolean isOel7() { 208 return System.getProperty("os.name").toLowerCase() 209 .contains("linux") && System.getProperty("os.version") 210 .toLowerCase().contains("el7"); 211 } 212 }