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 int x = SZ * scale; 53 BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB); 54 Graphics g = img.getGraphics(); 55 g.setColor(c); 56 g.fillRect(0, 0, x, x); 57 return img; 58 } 59 60 private static BaseMultiResolutionImage createIcon() { 61 return new BaseMultiResolutionImage(new BufferedImage[] { 62 generateImage(1, C1X), generateImage(2, C2X)}); 63 } 64 65 private JFrame frame; 66 private JPopupMenu popup; 67 private JMenuItem popupItem; 68 private JMenu menu; 69 70 public MenuMultiresolutionIconTest() throws Exception { 71 r = new ExtendedRobot(); 72 SwingUtilities.invokeAndWait(this::createUI); 73 } 74 75 private void createUI() { 76 ImageIcon ii = new ImageIcon(createIcon()); 77 78 popup = new JPopupMenu(); 79 popupItem = new JMenuItem("test", ii); 80 popup.add(popupItem); 81 popupItem.setHorizontalTextPosition(JMenuItem.RIGHT); 82 addMouseListener(new MousePopupListener()); 83 84 frame = new JFrame("Popup Icon Test"); 85 JMenuBar menuBar = new JMenuBar(); 86 menu = new JMenu("test"); 87 menuBar.add(menu); 88 menu.add(new JMenuItem("test", ii)); 89 menu.add(new JRadioButtonMenuItem("test", ii, true)); 90 menu.add(new JCheckBoxMenuItem("test", ii, true)); 91 92 frame.setJMenuBar(menuBar); 93 frame.setContentPane(this); 94 frame.setSize(300, 300); 95 frame.setVisible(true); 96 } 97 98 private class MousePopupListener extends MouseAdapter { 99 100 @Override 101 public void mousePressed(MouseEvent e) { showPopup(e); } 102 @Override 103 public void mouseClicked(MouseEvent e) { showPopup(e); } 104 @Override 105 public void mouseReleased(MouseEvent e) { showPopup(e); } 106 107 private void showPopup(MouseEvent e) { 108 if (e.isPopupTrigger()) { 109 popup.show(MenuMultiresolutionIconTest.this, e.getX(), e.getY()); 110 } 111 } 112 } 113 114 private void checkIconColor(Point p, String what) { 115 String scale = System.getProperty(SCALE); 116 Color expected = ((scale == null) || "1".equals(scale)) ? C1X : C2X; 117 Color c = r.getPixelColor(p.x + SZ / 2, p.y + SZ / 2); 118 if (!c.equals(expected)) { 119 throw new RuntimeException("invalid " + what + "menu item icon " + 120 "color, expected: " + expected + ", got: " + c); 121 } 122 System.out.println(what + "item icon check passed"); 123 } 124 125 private void doTest() { 126 127 r.waitForIdle(2 * DELAY); 128 129 Point p = getLocationOnScreen(); 130 r.mouseMove(p.x + getWidth() / 4, p.y + getHeight() / 4); 131 r.waitForIdle(DELAY); 132 r.click(InputEvent.BUTTON3_DOWN_MASK); 133 r.waitForIdle(DELAY); 134 p = popupItem.getLocationOnScreen(); 135 checkIconColor(p, "popup "); 136 r.waitForIdle(DELAY); 137 138 p = menu.getLocationOnScreen(); 139 r.mouseMove(p.x + menu.getWidth() / 2, p.y + menu.getHeight() / 2); 140 r.waitForIdle(DELAY); 141 r.click(); 142 p = menu.getItem(0).getLocationOnScreen(); 143 checkIconColor(p, ""); 144 r.waitForIdle(DELAY); 145 146 p = menu.getItem(1).getLocationOnScreen(); 147 checkIconColor(p, "radiobutton "); 148 r.waitForIdle(DELAY); 149 150 p = menu.getItem(2).getLocationOnScreen(); 151 checkIconColor(p, "checkbox "); 152 r.waitForIdle(DELAY); 153 154 frame.dispose(); 155 } 156 157 public static void main(String s[]) throws Exception { 158 (new MenuMultiresolutionIconTest()).doTest(); 159 } 160 }