Path: blob/master/test/jdk/javax/swing/Action/8133039/bug8133039.java
41155 views
/*1* Copyright (c) 2015, 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.Robot;24import java.awt.event.ActionEvent;25import java.awt.event.KeyEvent;26import javax.swing.AbstractAction;27import javax.swing.Action;28import javax.swing.JComboBox;29import javax.swing.JFrame;30import javax.swing.JLabel;31import javax.swing.KeyStroke;32import javax.swing.SwingUtilities;33import sun.swing.UIAction;3435/**36* @test37* @key headful38* @bug 813303939* @summary Provide public API to sun.swing.UIAction#isEnabled(Object)40* @modules java.desktop/sun.swing41* @author Alexander Scherbatiy42*/43public class bug8133039 {4445private static volatile int ACTION_PERFORMED_CALLS = 0;46private static volatile int ACTION_ACCEPTED_CALLS = 0;47private static JFrame frame;4849public static void main(String[] args) throws Exception {50try {51testActionNotification();52testPopupAction();53} finally {54if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());55}56}5758private static void testActionNotification() {5960KeyEvent keyEvent = new KeyEvent(new JLabel("Test"), 0, 0, 0, 0, 'A');61SenderObject rejectedSenderObject = new SenderObject();62SwingUtilities.notifyAction(new TestAction(false), null, keyEvent,63rejectedSenderObject, 0);6465if (rejectedSenderObject.accepted) {66throw new RuntimeException("The sender is incorrectly accepted!");67}6869if (rejectedSenderObject.performed) {70throw new RuntimeException("The action is incorrectly performed!");71}7273SenderObject acceptedSenderObject = new SenderObject();74SwingUtilities.notifyAction(new TestAction(true), null, keyEvent,75acceptedSenderObject, 0);7677if (!acceptedSenderObject.accepted) {78throw new RuntimeException("The sender is not accepted!");79}8081if (!acceptedSenderObject.performed) {82throw new RuntimeException("The action is not performed!");83}84}8586private static void testPopupAction() throws Exception {8788SwingUtilities.invokeAndWait(bug8133039::createAndShowGUI);8990Robot robot = new Robot();91robot.setAutoDelay(100);92robot.waitForIdle();9394robot.keyPress(KeyEvent.VK_A);95robot.keyRelease(KeyEvent.VK_A);96robot.waitForIdle();9798if (ACTION_ACCEPTED_CALLS != 1) {99throw new RuntimeException("Method accept is not invoked!");100}101102if (ACTION_PERFORMED_CALLS != 1) {103throw new RuntimeException("Method actionPerformed is not invoked!");104}105106robot.keyPress(KeyEvent.VK_A);107robot.keyRelease(KeyEvent.VK_A);108robot.waitForIdle();109110if (ACTION_ACCEPTED_CALLS != 2) {111throw new RuntimeException("Method accept is not invoked!");112}113114if (ACTION_PERFORMED_CALLS != 1) {115throw new RuntimeException("Method actionPerformed is invoked twice!");116}117}118119private static void createAndShowGUI() {120121frame = new JFrame();122frame.setSize(300, 300);123frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);124125JComboBox<String> comboBox = new JComboBox<>(new String[]{"1", "2", "3"});126127Action showPopupAction = new ShowPopupAction();128comboBox.getInputMap().put(KeyStroke.getKeyStroke("A"), "showPopup");129comboBox.getActionMap().put("showPopup", showPopupAction);130131frame.getContentPane().add(comboBox);132frame.setLocationRelativeTo(null);133frame.setVisible(true);134}135136private static class ShowPopupAction extends UIAction {137138public ShowPopupAction() {139super("showPopup");140}141142@Override143public void actionPerformed(ActionEvent e) {144ACTION_PERFORMED_CALLS++;145Object src = e.getSource();146if (src instanceof JComboBox) {147((JComboBox) src).showPopup();148}149}150151@Override152public boolean accept(Object sender) {153ACTION_ACCEPTED_CALLS++;154if (sender instanceof JComboBox) {155JComboBox c = (JComboBox) sender;156return !c.isPopupVisible();157}158return false;159}160}161162private static class SenderObject {163164private boolean accepted;165private boolean performed;166}167168private static class TestAction extends AbstractAction {169170private final boolean acceptSender;171172public TestAction(boolean acceptSender) {173this.acceptSender = acceptSender;174}175176@Override177public boolean accept(Object sender) {178((SenderObject) sender).accepted = acceptSender;179return acceptSender;180}181182@Override183public void actionPerformed(ActionEvent e) {184((SenderObject) e.getSource()).performed = true;185}186}187}188189190