Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenuItem/8152981/MenuItemIconTest.java
38853 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 815298126* @summary Double icons with JMenuItem setHorizontalTextPosition on Win 1027* @requires (os.family == "windows")28* @run main MenuItemIconTest29*/30import java.awt.Color;31import java.awt.Dimension;32import java.awt.Graphics;33import java.awt.Point;34import java.awt.Robot;35import java.awt.image.BufferedImage;36import javax.swing.ImageIcon;37import javax.swing.JFrame;38import javax.swing.JMenuBar;39import javax.swing.JMenuItem;40import javax.swing.SwingConstants;41import javax.swing.SwingUtilities;42import javax.swing.UIManager;43import javax.swing.UnsupportedLookAndFeelException;4445public class MenuItemIconTest {4647private static JFrame frame;48private static Robot robot;49private static String errorMessage = "";50private static JMenuItem menuItem;51private static final int IMAGE_WIDTH_AND_HEIGHT = 25;5253public static void main(String[] args) throws Exception {54robot = new Robot();55String name = UIManager.getSystemLookAndFeelClassName();56try {57UIManager.setLookAndFeel(name);58} catch (ClassNotFoundException | InstantiationException |59IllegalAccessException | UnsupportedLookAndFeelException e) {60throw new RuntimeException("Test Failed");61}62createUI();63robot.waitForIdle();64executeTest();65if (!"".equals(errorMessage)) {66throw new RuntimeException(errorMessage);67}68}6970private static void createUI() throws Exception {71SwingUtilities.invokeAndWait(() -> {72frame = new JFrame();73frame.setTitle("Test");74JMenuBar menuBar = new JMenuBar();75ImageIcon icon = createIcon();76menuItem = new JMenuItem("Command", icon);77menuItem.setHorizontalTextPosition(SwingConstants.LEFT);78menuBar.add(menuItem);79frame.setJMenuBar(menuBar);80frame.setPreferredSize(new Dimension(500, 500));81frame.pack();82frame.setVisible(true);83frame.setLocationRelativeTo(null);84});85}8687private static void checkPixeclColor(int x, int y) {88robot.delay(2000);89robot.mouseMove(x, y);90Color c = robot.getPixelColor(x, y);91if (c.getRed() == 255) {92errorMessage = "Test Failed";93}94robot.delay(5000);95frame.dispose();96}9798protected static ImageIcon createIcon() {99BufferedImage bi = new BufferedImage(IMAGE_WIDTH_AND_HEIGHT,100IMAGE_WIDTH_AND_HEIGHT, BufferedImage.TYPE_INT_ARGB);101Graphics g = bi.createGraphics();102g.setColor(Color.RED);103g.fillOval(0, 0, IMAGE_WIDTH_AND_HEIGHT, IMAGE_WIDTH_AND_HEIGHT);104return new ImageIcon(bi);105}106107private static void executeTest() throws Exception {108Point point = menuItem.getLocationOnScreen();109checkPixeclColor(point.x + IMAGE_WIDTH_AND_HEIGHT / 2,110point.y + IMAGE_WIDTH_AND_HEIGHT / 2);111}112}113114115116