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 * @key headful
27 * @bug 8150258
28 * @author a.stepanov
29 * @summary Check that correct resolution variants are chosen for menu icons
30 *          when multiresolution image is used for their construction.
31 *
32 * @library ../../../../lib/testlibrary/
33 * @build ExtendedRobot
34 * @run main/othervm -Dsun.java2d.uiScale=1 MenuMultiresolutionIconTest
35 * @run main/othervm -Dsun.java2d.uiScale=2 MenuMultiresolutionIconTest
36 */
37
38
39import java.awt.*;
40import java.awt.event.*;
41import java.awt.image.*;
42import javax.swing.*;
43
44public class MenuMultiresolutionIconTest extends JPanel {
45
46    private final static int DELAY = 1000;
47    private final static int SZ = 50;
48    private final static String SCALE = "sun.java2d.uiScale";
49    private final static Color C1X = Color.RED, C2X = Color.BLUE;
50    private final ExtendedRobot r;
51
52    private static BufferedImage generateImage(int scale, Color c) {
53
54        int x = SZ * scale;
55        BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
56        Graphics g = img.getGraphics();
57        g.setColor(c);
58        g.fillRect(0, 0, x, x);
59        return img;
60    }
61
62    private static BaseMultiResolutionImage createIcon() {
63
64        return new BaseMultiResolutionImage(new BufferedImage[] {
65            generateImage(1, C1X), generateImage(2, C2X)});
66    }
67
68    private JFrame     frame;
69    private JPopupMenu popup;
70    private JMenuItem  popupItem;
71    private JMenu      menu;
72
73    public MenuMultiresolutionIconTest() throws Exception {
74
75        r = new ExtendedRobot();
76        SwingUtilities.invokeAndWait(this::createUI);
77    }
78
79    private void createUI() {
80
81        ImageIcon ii = new ImageIcon(createIcon());
82
83        popup = new JPopupMenu();
84        popupItem = new JMenuItem("test", ii);
85        popup.add(popupItem);
86        popupItem.setHorizontalTextPosition(JMenuItem.RIGHT);
87        addMouseListener(new MousePopupListener());
88
89        frame = new JFrame();
90        JMenuBar menuBar = new JMenuBar();
91        menu = new JMenu("test");
92        menuBar.add(menu);
93        menu.add(new JMenuItem("test", ii));
94        menu.add(new JRadioButtonMenuItem("test", ii, true));
95        menu.add(new JCheckBoxMenuItem("test", ii, true));
96
97        frame.setJMenuBar(menuBar);
98        frame.setContentPane(this);
99        frame.setSize(300, 300);
100        frame.setVisible(true);
101    }
102
103    private class MousePopupListener extends MouseAdapter {
104
105        @Override
106        public void mousePressed(MouseEvent e)  { showPopup(e); }
107        @Override
108        public void mouseClicked(MouseEvent e)  { showPopup(e); }
109        @Override
110        public void mouseReleased(MouseEvent e) { showPopup(e); }
111
112        private void showPopup(MouseEvent e) {
113            if (e.isPopupTrigger()) {
114                popup.show(MenuMultiresolutionIconTest.this, e.getX(), e.getY());
115            }
116        }
117    }
118
119    private boolean eqColors(Color c1, Color c2) {
120
121        int tol = 15;
122        return (
123            Math.abs(c2.getRed()   - c1.getRed()  ) < tol &&
124            Math.abs(c2.getGreen() - c1.getGreen()) < tol &&
125            Math.abs(c2.getBlue()  - c1.getBlue() ) < tol);
126    }
127
128    private void checkIconColor(Point p, String what) {
129
130        String scale = System.getProperty(SCALE);
131        Color expected = "2".equals(scale) ? C2X : C1X;
132        Color c = r.getPixelColor(p.x + SZ / 2, p.y + SZ / 2);
133        if (!eqColors(c, expected)) {
134            frame.dispose();
135            throw new RuntimeException("invalid " + what + "menu item icon " +
136                "color, expected: " + expected + ", got: " + c);
137        }
138        System.out.println(what + "item icon check passed");
139    }
140
141    private void doTest() {
142
143        r.waitForIdle(2 * DELAY);
144
145        Point p = getLocationOnScreen();
146        r.mouseMove(p.x + getWidth() / 4, p.y + getHeight() / 4);
147        r.waitForIdle(DELAY);
148        r.click(InputEvent.BUTTON3_DOWN_MASK);
149        r.waitForIdle(DELAY);
150        p = popupItem.getLocationOnScreen();
151        checkIconColor(p, "popup ");
152        r.waitForIdle(DELAY);
153
154        p = menu.getLocationOnScreen();
155        r.mouseMove(p.x + menu.getWidth() / 2, p.y + menu.getHeight() / 2);
156        r.waitForIdle(DELAY);
157        r.click();
158        p = menu.getItem(0).getLocationOnScreen();
159        checkIconColor(p, "");
160        r.waitForIdle(DELAY);
161
162        p = menu.getItem(1).getLocationOnScreen();
163        checkIconColor(p, "radiobutton ");
164        r.waitForIdle(DELAY);
165
166        p = menu.getItem(2).getLocationOnScreen();
167        checkIconColor(p, "checkbox ");
168        r.waitForIdle(DELAY);
169
170        frame.dispose();
171    }
172
173    public static void main(String s[]) throws Exception {
174
175        (new MenuMultiresolutionIconTest()).doTest();
176    }
177}
178