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 boolean eqColors(Color c1, Color c2) {
 119 
 120         int tol = 15;
 121         return (
 122             Math.abs(c2.getRed()   - c1.getRed()  ) < tol &&
 123             Math.abs(c2.getGreen() - c1.getGreen()) < tol &&
 124             Math.abs(c2.getBlue()  - c1.getBlue() ) < tol);
 125     }
 126 
 127     private void checkIconColor(Point p, String what) {
 128         
 129         String scale = System.getProperty(SCALE);
 130         Color expected = "2".equals(scale) ? C2X : C1X;
 131         Color c = r.getPixelColor(p.x + SZ / 2, p.y + SZ / 2);
 132         if (!eqColors(c, expected)) {
 133             frame.dispose();
 134             throw new RuntimeException("invalid " + what + "menu item icon " +
 135                 "color, expected: " + expected + ", got: " + c);
 136         }
 137         System.out.println(what + "item icon check passed");
 138     }
 139 
 140     private void doTest() {
 141 
 142         r.waitForIdle(2 * DELAY);
 143 
 144         Point p = getLocationOnScreen();
 145         r.mouseMove(p.x + getWidth() / 4, p.y + getHeight() / 4);
 146         r.waitForIdle(DELAY);
 147         r.click(InputEvent.BUTTON3_DOWN_MASK);
 148         r.waitForIdle(DELAY);
 149         p = popupItem.getLocationOnScreen();
 150         checkIconColor(p, "popup ");
 151         r.waitForIdle(DELAY);
 152 
 153         p = menu.getLocationOnScreen();
 154         r.mouseMove(p.x + menu.getWidth() / 2, p.y + menu.getHeight() / 2);
 155         r.waitForIdle(DELAY);
 156         r.click();
 157         p = menu.getItem(0).getLocationOnScreen();
 158         checkIconColor(p, "");
 159         r.waitForIdle(DELAY);
 160 
 161         p = menu.getItem(1).getLocationOnScreen();
 162         checkIconColor(p, "radiobutton ");
 163         r.waitForIdle(DELAY);
 164 
 165         p = menu.getItem(2).getLocationOnScreen();
 166         checkIconColor(p, "checkbox ");
 167         r.waitForIdle(DELAY);
 168 
 169         frame.dispose();
 170     }
 171 
 172     public static void main(String s[]) throws Exception {
 173 


 174         (new MenuMultiresolutionIconTest()).doTest();
 175     }

 176 }
--- EOF ---