1 /*
   2  * Copyright (c) 2016, 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 /*
  25  * @test
  26  * @bug 8150258
  27  * @author a.stepanov
  28  * @summary Check that correct resolution variants are chosen for menu icons
  29  *          when multiresolution image is used for their construction.
  30  *
  31  * @library ../../../../lib/testlibrary/
  32  * @build ExtendedRobot
  33  * @run main/othervm -Dsun.java2d.uiScale=1 MenuMultiresolutionIconTest
  34  * @run main/othervm -Dsun.java2d.uiScale=2 MenuMultiresolutionIconTest
  35  */
  36 
  37 
  38 import java.awt.*;
  39 import java.awt.event.*;
  40 import java.awt.image.*;
  41 import javax.swing.*;
  42 
  43 public class MenuMultiresolutionIconTest extends JPanel {
  44 
  45     private final static int DELAY = 1000;
  46     private final static int SZ = 50;
  47     private final static String SCALE = "sun.java2d.uiScale";
  48     private final static Color C1X = Color.RED, C2X = Color.BLUE;
  49     private final ExtendedRobot r;
  50 
  51     private static BufferedImage generateImage(int scale, Color c) {
  52 
  53         int x = SZ * scale;
  54         BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
  55         Graphics g = img.getGraphics();
  56         g.setColor(c);
  57         g.fillRect(0, 0, x, x);
  58         return img;
  59     }
  60 
  61     private static BaseMultiResolutionImage createIcon() {
  62 
  63         return new BaseMultiResolutionImage(new BufferedImage[] {
  64             generateImage(1, C1X), generateImage(2, C2X)});
  65     }
  66 
  67     private JFrame     frame;
  68     private JPopupMenu popup;
  69     private JMenuItem  popupItem;
  70     private JMenu      menu;
  71 
  72     public MenuMultiresolutionIconTest() throws Exception {
  73 
  74         r = new ExtendedRobot();
  75         SwingUtilities.invokeAndWait(this::createUI);
  76     }
  77 
  78     private void createUI() {
  79 
  80         ImageIcon ii = new ImageIcon(createIcon());
  81 
  82         popup = new JPopupMenu();
  83         popupItem = new JMenuItem("test", ii);
  84         popup.add(popupItem);
  85         popupItem.setHorizontalTextPosition(JMenuItem.RIGHT);
  86         addMouseListener(new MousePopupListener());
  87 
  88         frame = new JFrame();
  89         JMenuBar menuBar = new JMenuBar();
  90         menu = new JMenu("test");
  91         menuBar.add(menu);
  92         menu.add(new JMenuItem("test", ii));
  93         menu.add(new JRadioButtonMenuItem("test", ii, true));
  94         menu.add(new JCheckBoxMenuItem("test", ii, true));
  95 
  96         frame.setJMenuBar(menuBar);
  97         frame.setContentPane(this);
  98         frame.setSize(300, 300);
  99         frame.setVisible(true);
 100     }
 101 
 102     private class MousePopupListener extends MouseAdapter {
 103 
 104         @Override
 105         public void mousePressed(MouseEvent e)  { showPopup(e); }
 106         @Override
 107         public void mouseClicked(MouseEvent e)  { showPopup(e); }
 108         @Override
 109         public void mouseReleased(MouseEvent e) { showPopup(e); }
 110 
 111         private void showPopup(MouseEvent e) {
 112             if (e.isPopupTrigger()) {
 113                 popup.show(MenuMultiresolutionIconTest.this, e.getX(), e.getY());
 114             }
 115         }
 116     }
 117 
 118     private static boolean is2x() {
 119 
 120         return GraphicsEnvironment.getLocalGraphicsEnvironment().
 121             getDefaultScreenDevice().getDefaultConfiguration().
 122             getDefaultTransform().getScaleX() > 1.001;
 123     }
 124 
 125     private boolean eqColors(Color c1, Color c2) {
 126 
 127         int tol = 15;
 128         return (
 129             Math.abs(c2.getRed()   - c1.getRed()  ) < tol &&
 130             Math.abs(c2.getGreen() - c1.getGreen()) < tol &&
 131             Math.abs(c2.getBlue()  - c1.getBlue() ) < tol);
 132     }
 133 
 134     private void checkIconColor(Point p, String what) {
 135 
 136         Color expected = is2x() ? C2X : C1X;

 137         Color c = r.getPixelColor(p.x + SZ / 2, p.y + SZ / 2);
 138         if (!eqColors(c, expected)) {
 139             frame.dispose();
 140             throw new RuntimeException("invalid " + what + "menu item icon " +
 141                 "color, expected: " + expected + ", got: " + c);
 142         }
 143         System.out.println(what + "item icon check passed");
 144     }
 145 
 146     private void doTest() {
 147 
 148         r.waitForIdle(2 * DELAY);
 149 
 150         Point p = getLocationOnScreen();
 151         r.mouseMove(p.x + getWidth() / 4, p.y + getHeight() / 4);
 152         r.waitForIdle(DELAY);
 153         r.click(InputEvent.BUTTON3_DOWN_MASK);
 154         r.waitForIdle(DELAY);
 155         p = popupItem.getLocationOnScreen();
 156         checkIconColor(p, "popup ");
 157         r.waitForIdle(DELAY);
 158 
 159         p = menu.getLocationOnScreen();
 160         r.mouseMove(p.x + menu.getWidth() / 2, p.y + menu.getHeight() / 2);
 161         r.waitForIdle(DELAY);
 162         r.click();
 163         p = menu.getItem(0).getLocationOnScreen();
 164         checkIconColor(p, "");
 165         r.waitForIdle(DELAY);
 166 
 167         p = menu.getItem(1).getLocationOnScreen();
 168         checkIconColor(p, "radiobutton ");
 169         r.waitForIdle(DELAY);
 170 
 171         p = menu.getItem(2).getLocationOnScreen();
 172         checkIconColor(p, "checkbox ");
 173         r.waitForIdle(DELAY);
 174 
 175         frame.dispose();
 176     }
 177 
 178     public static void main(String s[]) throws Exception {
 179 
 180         // TODO: remove is2x() after JDK-8150844 fix
 181         if (is2x() == "2".equals(System.getProperty(SCALE))) {
 182             (new MenuMultiresolutionIconTest()).doTest();
 183         }
 184     }
 185 }
--- EOF ---