jdk/test/javax/swing/JMenuItem/8152981/MenuItemIconTest.java
changeset 38995 c1812168a1e9
child 39516 0e1925d06a4d
equal deleted inserted replaced
38994:a6b14a17689b 38995:c1812168a1e9
       
     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 8152981
       
    27  * @summary Double icons with JMenuItem setHorizontalTextPosition on Win 10
       
    28  * @requires (os.family == "windows")
       
    29  * @run main MenuItemIconTest
       
    30  */
       
    31 import java.awt.Color;
       
    32 import java.awt.Dimension;
       
    33 import java.awt.Graphics;
       
    34 import java.awt.Point;
       
    35 import java.awt.Robot;
       
    36 import java.awt.image.BufferedImage;
       
    37 import javax.swing.ImageIcon;
       
    38 import javax.swing.JFrame;
       
    39 import javax.swing.JMenuBar;
       
    40 import javax.swing.JMenuItem;
       
    41 import javax.swing.SwingConstants;
       
    42 import javax.swing.SwingUtilities;
       
    43 import javax.swing.UIManager;
       
    44 import javax.swing.UnsupportedLookAndFeelException;
       
    45 
       
    46 public class MenuItemIconTest {
       
    47 
       
    48     private static JFrame frame;
       
    49     private static Robot robot;
       
    50     private static String errorMessage = "";
       
    51     private static JMenuItem menuItem;
       
    52     private static final int IMAGE_WIDTH_AND_HEIGHT = 25;
       
    53 
       
    54     public static void main(String[] args) throws Exception {
       
    55         robot = new Robot();
       
    56         String name = UIManager.getSystemLookAndFeelClassName();
       
    57         try {
       
    58             UIManager.setLookAndFeel(name);
       
    59         } catch (ClassNotFoundException | InstantiationException |
       
    60                 IllegalAccessException | UnsupportedLookAndFeelException e) {
       
    61             throw new RuntimeException("Test Failed");
       
    62         }
       
    63         createUI();
       
    64         robot.waitForIdle();
       
    65         executeTest();
       
    66         if (!"".equals(errorMessage)) {
       
    67             throw new RuntimeException(errorMessage);
       
    68         }
       
    69     }
       
    70 
       
    71     private static void createUI() throws Exception {
       
    72         SwingUtilities.invokeAndWait(() -> {
       
    73             frame = new JFrame();
       
    74             frame.setTitle("Test");
       
    75             JMenuBar menuBar = new JMenuBar();
       
    76             ImageIcon icon = createIcon();
       
    77             menuItem = new JMenuItem("Command", icon);
       
    78             menuItem.setHorizontalTextPosition(SwingConstants.LEFT);
       
    79             menuBar.add(menuItem);
       
    80             frame.setJMenuBar(menuBar);
       
    81             frame.setPreferredSize(new Dimension(500, 500));
       
    82             frame.pack();
       
    83             frame.setVisible(true);
       
    84             frame.setLocationRelativeTo(null);
       
    85         });
       
    86     }
       
    87 
       
    88     private static void checkPixeclColor(int x, int y) {
       
    89         robot.delay(2000);
       
    90         robot.mouseMove(x, y);
       
    91         Color c = robot.getPixelColor(x, y);
       
    92         if (c.getRed() == 255) {
       
    93             errorMessage = "Test Failed";
       
    94         }
       
    95         robot.delay(5000);
       
    96         frame.dispose();
       
    97     }
       
    98 
       
    99     protected static ImageIcon createIcon() {
       
   100         BufferedImage bi = new BufferedImage(IMAGE_WIDTH_AND_HEIGHT,
       
   101                 IMAGE_WIDTH_AND_HEIGHT, BufferedImage.TYPE_INT_ARGB);
       
   102         Graphics g = bi.createGraphics();
       
   103         g.setColor(Color.RED);
       
   104         g.fillOval(0, 0, IMAGE_WIDTH_AND_HEIGHT, IMAGE_WIDTH_AND_HEIGHT);
       
   105         return new ImageIcon(bi);
       
   106     }
       
   107 
       
   108     private static void executeTest() throws Exception {
       
   109         Point point = menuItem.getLocationOnScreen();
       
   110         checkPixeclColor(point.x + IMAGE_WIDTH_AND_HEIGHT / 2,
       
   111                 point.y + IMAGE_WIDTH_AND_HEIGHT / 2);
       
   112     }
       
   113 }
       
   114