Path: blob/master/test/jdk/javax/swing/JMenuItem/8158566/CloseOnMouseClickPropertyTest.java
41153 views
/*1* Copyright (c) 2016, 2017, 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*/2223import java.awt.Point;24import java.awt.Rectangle;25import java.awt.Robot;26import java.awt.event.InputEvent;27import javax.swing.JCheckBoxMenuItem;28import javax.swing.JComponent;29import javax.swing.JFrame;30import javax.swing.JMenu;31import javax.swing.JMenuBar;32import javax.swing.JMenuItem;33import javax.swing.JRadioButtonMenuItem;34import javax.swing.SwingUtilities;35import javax.swing.UIManager;3637/**38* @test39* @key headful40* @bug 8158566 8160879 8160977 815856641* @summary Provide a Swing property which modifies MenuItemUI behaviour42*/4344public class CloseOnMouseClickPropertyTest {4546private static final String CHECK_BOX_PROP = "CheckBoxMenuItem."47+ "doNotCloseOnMouseClick";48private static final String RADIO_BUTTON_PROP = "RadioButtonMenuItem"49+ ".doNotCloseOnMouseClick";5051private static JFrame frame;52private static JMenu menu;5354private static TestItem[] TEST_ITEMS = {55new TestItem(TestType.CHECK_BOX_MENU_ITEM, true, true),56new TestItem(TestType.CHECK_BOX_MENU_ITEM, true, false),57new TestItem(TestType.CHECK_BOX_MENU_ITEM, false, true),58new TestItem(TestType.CHECK_BOX_MENU_ITEM, false, false),5960new TestItem(TestType.CHECK_BOX_MENU_ITEM, null, true),61new TestItem(TestType.CHECK_BOX_MENU_ITEM, null, false),62new TestItem(TestType.CHECK_BOX_MENU_ITEM, true, null),63new TestItem(TestType.CHECK_BOX_MENU_ITEM, false, null),646566new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, true, true),67new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, true, false),68new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, false, true),69new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, false, false),7071new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, true, null),72new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, false, null),73new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, null, true),74new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, null, false),7576new TestItem(TestType.MENU_ITEM, true, true),77new TestItem(TestType.MENU_ITEM, true, false),78new TestItem(TestType.MENU_ITEM, false, true),79new TestItem(TestType.MENU_ITEM, false, false),8081new TestItem(TestType.MENU_ITEM, true, null),82new TestItem(TestType.MENU_ITEM, false, null),83new TestItem(TestType.MENU_ITEM, null, true),84new TestItem(TestType.MENU_ITEM, null, false),85};8687public static void main(String[] args) throws Exception {8889for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {90UIManager.setLookAndFeel(info.getClassName());91for (TestItem testItem : TEST_ITEMS) {92test(testItem);93}94}95}9697private static void test(TestItem item) throws Exception {9899Robot robot = new Robot();100robot.setAutoDelay(50);101SwingUtilities.invokeAndWait(() -> createAndShowGUI(item));102robot.waitForIdle();103104Point point = getClickPoint(true);105robot.mouseMove(point.x, point.y);106robot.mousePress(InputEvent.BUTTON1_MASK);107robot.mouseRelease(InputEvent.BUTTON1_MASK);108robot.waitForIdle();109110point = getClickPoint(false);111robot.mouseMove(point.x, point.y);112robot.mousePress(InputEvent.BUTTON1_MASK);113robot.mouseRelease(InputEvent.BUTTON1_MASK);114robot.waitForIdle();115116SwingUtilities.invokeAndWait(() -> {117JMenuItem menuItem = menu.getItem(0);118boolean isShowing = menuItem.isShowing();119frame.dispose();120if (isShowing ^ item.doNotCloseOnMouseClick()) {121throw new RuntimeException("Property is not taken into account!");122}123});124}125126private static void createAndShowGUI(TestItem testItem) {127128frame = new JFrame();129frame.setSize(300, 300);130frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);131132JMenuBar menuBar = new JMenuBar();133menu = new JMenu("Menu");134JMenuItem menuItem = testItem.getMenuItem();135testItem.setProperties(menuItem);136menu.add(menuItem);137menuBar.add(menu);138139frame.setJMenuBar(menuBar);140frame.setVisible(true);141}142143private static Point getClickPoint(boolean parent) throws Exception {144Point points[] = new Point[1];145146SwingUtilities.invokeAndWait(() -> {147148JComponent comp = parent ? menu : menu.getItem(0);149150Point point = comp.getLocationOnScreen();151Rectangle bounds = comp.getBounds();152point.x += bounds.getWidth() / 2;153point.y += bounds.getHeight() / 2;154155points[0] = point;156});157158return points[0];159}160161enum TestType {162163MENU_ITEM,164CHECK_BOX_MENU_ITEM,165RADIO_BUTTON_MENU_ITEM166}167168static class TestItem {169170TestType type;171Boolean compDoNotCloseOnMouseClick;172Boolean lafDoNotCloseOnMouseClick;173174public TestItem(TestType type,175Boolean compDoNotCloseOnMouseClick,176Boolean lafDoNotCloseOnMouseClick)177{178this.type = type;179this.compDoNotCloseOnMouseClick = compDoNotCloseOnMouseClick;180this.lafDoNotCloseOnMouseClick = lafDoNotCloseOnMouseClick;181}182183boolean doNotCloseOnMouseClick() {184switch (type) {185case MENU_ITEM:186return false;187default:188return compDoNotCloseOnMouseClick != null189? compDoNotCloseOnMouseClick190: lafDoNotCloseOnMouseClick;191}192}193194void setProperties(JMenuItem menuItem) {195switch (type) {196case CHECK_BOX_MENU_ITEM:197menuItem.putClientProperty(CHECK_BOX_PROP, compDoNotCloseOnMouseClick);198UIManager.put(CHECK_BOX_PROP, lafDoNotCloseOnMouseClick);199break;200case RADIO_BUTTON_MENU_ITEM:201menuItem.putClientProperty(RADIO_BUTTON_PROP, compDoNotCloseOnMouseClick);202UIManager.put(RADIO_BUTTON_PROP, lafDoNotCloseOnMouseClick);203break;204default:205menuItem.putClientProperty(CHECK_BOX_PROP, compDoNotCloseOnMouseClick);206menuItem.putClientProperty(RADIO_BUTTON_PROP, compDoNotCloseOnMouseClick);207UIManager.put(CHECK_BOX_PROP, lafDoNotCloseOnMouseClick);208UIManager.put(RADIO_BUTTON_PROP, lafDoNotCloseOnMouseClick);209}210}211212JMenuItem getMenuItem() {213switch (type) {214case CHECK_BOX_MENU_ITEM:215return new JCheckBoxMenuItem("Check Box");216case RADIO_BUTTON_MENU_ITEM:217return new JRadioButtonMenuItem("Radio Button");218default:219return new JMenuItem("Menu Item");220}221}222}223}224225226227