--- old/src/java.desktop/unix/classes/sun/awt/X11/XTrayIconPeer.java 2016-06-09 14:21:15.492967951 +0530 +++ new/src/java.desktop/unix/classes/sun/awt/X11/XTrayIconPeer.java 2016-06-09 14:21:15.112777951 +0530 @@ -34,6 +34,7 @@ import java.security.PrivilegedAction; import java.lang.reflect.InvocationTargetException; import sun.util.logging.PlatformLogger; +import java.awt.geom.AffineTransform; public class XTrayIconPeer implements TrayIconPeer, InfoWindow.Balloon.LiveArguments, @@ -600,6 +601,11 @@ try { gr.setColor(getBackground()); gr.fillRect(0, 0, curW, curH); + AffineTransform tx = GraphicsEnvironment. + getLocalGraphicsEnvironment(). + getDefaultScreenDevice(). + getDefaultConfiguration().getDefaultTransform(); + gr.setTransform(tx); gr.drawImage(image, 0, 0, curW, curH, observer); gr.dispose(); --- old/src/java.desktop/windows/classes/sun/awt/windows/WTrayIconPeer.java 2016-06-09 14:21:16.425433952 +0530 +++ new/src/java.desktop/windows/classes/sun/awt/windows/WTrayIconPeer.java 2016-06-09 14:21:16.081261952 +0530 @@ -28,10 +28,16 @@ import java.awt.Graphics2D; import java.awt.AWTEvent; import java.awt.Frame; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; import java.awt.PopupMenu; import java.awt.Point; import java.awt.TrayIcon; import java.awt.Image; +import java.awt.Insets; +import java.awt.Toolkit; +import java.awt.geom.AffineTransform; import java.awt.peer.TrayIconPeer; import java.awt.image.*; @@ -130,7 +136,26 @@ if (gr != null) { try { gr.setPaintMode(); - + GraphicsEnvironment ge = GraphicsEnvironment. + getLocalGraphicsEnvironment(); + Toolkit toolkit = Toolkit.getDefaultToolkit(); + GraphicsDevice[] gds = ge.getScreenDevices(); + GraphicsDevice device = ge.getDefaultScreenDevice(); + Insets in = new Insets(0, 0, 0, 0); + for (GraphicsDevice gd : gds) { + GraphicsConfiguration gc = gd.getDefaultConfiguration(); + Insets insets = toolkit.getScreenInsets(gc); + if (insets.bottom > in.bottom + || insets.top > in.top + || insets.left > in.left + || insets.right > in.right) { + in = insets; + device = gd; + } + } + AffineTransform tx = device.getDefaultConfiguration(). + getDefaultTransform(); + gr.setTransform(tx); gr.drawImage(image, 0, 0, (autosize ? TRAY_ICON_WIDTH : image.getWidth(observer)), (autosize ? TRAY_ICON_HEIGHT : image.getHeight(observer)), observer); --- old/test/java/awt/image/multiresolution/MultiResolutionTrayIconTest/MultiResolutionTrayIconTest.java 2016-06-09 14:21:17.317879951 +0530 +++ new/test/java/awt/image/multiresolution/MultiResolutionTrayIconTest/MultiResolutionTrayIconTest.java 2016-06-09 14:21:16.997719951 +0530 @@ -22,96 +22,234 @@ */ -/* - @test - @bug 8150176 8151773 - @summary Check if correct resolution variant is used for tray icon. - @author a.stepanov - @run applet/manual=yesno MultiResolutionTrayIconTest.html -*/ - - -import java.applet.Applet; -import java.awt.*; -import java.awt.event.*; -import java.awt.image.*; - - -public class MultiResolutionTrayIconTest extends Applet { +/** + * @test + * @bug 8150176 8151773 + * @summary Check if correct resolution variant is used for tray icon. + * @run main/manual MultiResolutionTrayIconTest + */ +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.GridBagLayout; +import java.awt.GridBagConstraints; +import java.awt.SystemTray; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.image.BaseMultiResolutionImage; +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.swing.JFrame; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; +import java.io.InputStream; + +public class MultiResolutionTrayIconTest { + + private static SystemTray tray; + private static TrayIcon icon; + private static GridBagLayout layout; + private static JPanel mainControlPanel; + private static JPanel resultButtonPanel; + private static JLabel instructionText; + private static JButton passButton; + private static JButton failButton; + private static JButton startButton; + private static JFrame mainFrame; + + public static void main(String[] args) throws Exception { + String osName = System.getProperty("os.name"); + if (osName.contains("Win")) { + createUI(); + } else if (args.length == 0) { + createChildProcess(); + } else { + createUI(); + } + } - private SystemTray tray; - private TrayIcon icon, iconMRI; + static void createChildProcess() { + String javaPath = System.getProperty("java.home"); + String classPathDir = System.getProperty("java.class.path"); + Map env = new HashMap(); + env.put("GDK_SCALE", "2"); + int exitValue = doExec(env, javaPath + File.separator + "bin" + File.separator + + "java", "-cp", + classPathDir, "MultiResolutionTrayIconTest", "1"); + if (exitValue != 0) { + throw new RuntimeException("Test Failed"); + } + } - public void init() { this.setLayout(new BorderLayout()); } + static int doExec(Map envToSet, String... cmds) { + Process p = null; + ProcessBuilder pb = new ProcessBuilder(cmds); + Map env = pb.environment(); + if (envToSet != null) { + env.putAll(envToSet); + } + BufferedReader rdr = null; + InputStream errorStream = null; + try { + List outputList = new ArrayList<>(); + pb.redirectErrorStream(true); + p = pb.start(); + rdr = new BufferedReader(new InputStreamReader(p.getInputStream())); + String in = rdr.readLine(); + while (in != null) { + outputList.add(in); + in = rdr.readLine(); + throw new RuntimeException("Test Failed"); + } + + p.waitFor(); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + + try { + if (rdr != null) { + rdr.close(); + } + } catch (IOException ex) { + } - public void start() { + p.destroy(); + } + return p.exitValue(); + } - boolean trayIsSupported = SystemTray.isSupported(); - Button b = new Button("Start"); - if (trayIsSupported) { + public static void createUI() throws Exception { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + mainFrame = new JFrame("TrayIcon Test"); + boolean trayIsSupported = SystemTray.isSupported(); + tray = SystemTray.getSystemTray(); + Dimension d = tray.getTrayIconSize(); + icon = new TrayIcon(createIcon(d.width, d.height)); + icon.setImageAutoSize(true); + layout = new GridBagLayout(); + mainControlPanel = new JPanel(layout); + resultButtonPanel = new JPanel(layout); + + GridBagConstraints gbc = new GridBagConstraints(); + String instructions + = "INSTRUCTIONS:
" + + "Press start button to add icon to system tray.

" + + "If Icon color is green test" + + " passes else failed.

"; + + instructionText = new JLabel(); + instructionText.setText(instructions); + + gbc.gridx = 0; + gbc.gridy = 0; + gbc.fill = GridBagConstraints.HORIZONTAL; + mainControlPanel.add(instructionText, gbc); + startButton = new JButton("Start"); + startButton.setActionCommand("Start"); + if (trayIsSupported) { + + startButton.addActionListener((ActionEvent e) -> { + doTest(); + }); + } else { + startButton.setEnabled(false); + System.out.println("system tray is not supported"); + } + gbc.gridx = 0; + gbc.gridy = 0; + resultButtonPanel.add(startButton, gbc); + + passButton = new JButton("Pass"); + passButton.setActionCommand("Pass"); + passButton.addActionListener((ActionEvent e) -> { + removeIcon(); + mainFrame.dispose(); + }); + failButton = new JButton("Fail"); + failButton.setActionCommand("Fail"); + failButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + removeIcon(); + mainFrame.dispose(); + throw new RuntimeException("Test Failed"); + } + }); + gbc.gridx = 1; + gbc.gridy = 0; + resultButtonPanel.add(passButton, gbc); + gbc.gridx = 2; + gbc.gridy = 0; + resultButtonPanel.add(failButton, gbc); + + gbc.gridx = 0; + gbc.gridy = 1; + mainControlPanel.add(resultButtonPanel, gbc); + + mainFrame.add(mainControlPanel); + mainFrame.setSize(400, 200); + mainFrame.setLocationRelativeTo(null); + mainFrame.setVisible(true); + + mainFrame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + removeIcon(); + mainFrame.dispose(); + } + }); + } + }); - prepareIcons(); - b.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { doTest(); } - }); - } else { - b.setLabel("not supported"); - b.setEnabled(false); - System.out.println("system tray is not supported"); - } - add(b, BorderLayout.CENTER); + } - validate(); - setVisible(true); + private static BaseMultiResolutionImage createIcon(int w, int h) { + return new BaseMultiResolutionImage( + new BufferedImage[]{generateImage(w, h, 1, Color.RED), + generateImage(w, h, 2, Color.GREEN)}); } - private BufferedImage generateImage(int w, int h, Color c) { + private static BufferedImage generateImage(int w, int h, int scale, Color c) { - BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); + int x = w * scale, y = h * scale; + BufferedImage img = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(c); - g.fillRect(0, 0, w, h); + g.fillRect(0, 0, x, y); g.setColor(Color.WHITE); - int r = (Math.min(w, h) >= 8) ? 3 : 1; - g.fillRect(r, r, w - 2 * r, h - 2 * r); + g.fillRect(x / 3, y / 3, x / 3, y / 3); return img; } - private void prepareIcons() { + private static void doTest() { - tray = SystemTray.getSystemTray(); - Dimension d = tray.getTrayIconSize(); - int w = d.width, h = d.height; - - BufferedImage img = generateImage(w, h, Color.BLUE); - // use wrong icon size for "nok" - BufferedImage nok = generateImage(w / 2 + 2, h / 2 + 2, Color.RED); - BaseMultiResolutionImage mri = - new BaseMultiResolutionImage(new BufferedImage[] {nok, img}); - icon = new TrayIcon(img); - icon.setImageAutoSize(true); // just in case - iconMRI = new TrayIcon(mri); - iconMRI.setImageAutoSize(true); - } - - private void doTest() { - - if (tray.getTrayIcons().length > 0) { return; } // icons were added already + if (tray.getTrayIcons().length > 0) { + return; + } try { tray.add(icon); - tray.add(iconMRI); } catch (Exception e) { throw new RuntimeException(e); } } - public void stop() { - - // check for null, just in case + private static void removeIcon() { if (tray != null) { tray.remove(icon); - tray.remove(iconMRI); } } }