1 /*
   2  * Copyright (c) 2015, 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 import java.awt.Robot;
  25 import java.awt.event.ActionEvent;
  26 import java.awt.event.KeyEvent;
  27 import javax.swing.AbstractAction;
  28 import javax.swing.Action;
  29 import javax.swing.JComboBox;
  30 import javax.swing.JFrame;
  31 import javax.swing.JLabel;
  32 import javax.swing.KeyStroke;
  33 import javax.swing.SwingUtilities;
  34 import sun.swing.UIAction;
  35 
  36 /**
  37  * @test
  38  * @bug 8133039
  39  * @summary Provide public API to sun.swing.UIAction#isEnabled(Object)
  40  * @modules java.desktop/sun.swing
  41  * @author Alexander Scherbatiy
  42  */
  43 public class bug8133039 {
  44 
  45     private static volatile int ACTION_PERFORMED_CALLS = 0;
  46     private static volatile int ACTION_ACCEPTED_CALLS = 0;
  47 
  48     public static void main(String[] args) throws Exception {
  49         testActionNotification();
  50         testPopupAction();
  51     }
  52 
  53     private static void testActionNotification() {
  54 
  55         KeyEvent keyEvent = new KeyEvent(new JLabel("Test"), 0, 0, 0, 0, 'A');
  56         SenderObject rejectedSenderObject = new SenderObject();
  57         SwingUtilities.notifyAction(new TestAction(false), null, keyEvent,
  58                 rejectedSenderObject, 0);
  59 
  60         if (rejectedSenderObject.accepted) {
  61             throw new RuntimeException("The sender is incorrectly accepted!");
  62         }
  63 
  64         if (rejectedSenderObject.performed) {
  65             throw new RuntimeException("The action is incorrectly performed!");
  66         }
  67 
  68         SenderObject acceptedSenderObject = new SenderObject();
  69         SwingUtilities.notifyAction(new TestAction(true), null, keyEvent,
  70                 acceptedSenderObject, 0);
  71 
  72         if (!acceptedSenderObject.accepted) {
  73             throw new RuntimeException("The sender is not accepted!");
  74         }
  75 
  76         if (!acceptedSenderObject.performed) {
  77             throw new RuntimeException("The action is not performed!");
  78         }
  79     }
  80 
  81     private static void testPopupAction() throws Exception {
  82 
  83         SwingUtilities.invokeAndWait(bug8133039::createAndShowGUI);
  84 
  85         Robot robot = new Robot();
  86         robot.setAutoDelay(50);
  87         robot.waitForIdle();
  88 
  89         robot.keyPress(KeyEvent.VK_A);
  90         robot.keyRelease(KeyEvent.VK_A);
  91         robot.waitForIdle();
  92 
  93         if (ACTION_ACCEPTED_CALLS != 1) {
  94             throw new RuntimeException("Method accept is not invoked!");
  95         }
  96 
  97         if (ACTION_PERFORMED_CALLS != 1) {
  98             throw new RuntimeException("Method actionPerformed is not invoked!");
  99         }
 100 
 101         robot.keyPress(KeyEvent.VK_A);
 102         robot.keyRelease(KeyEvent.VK_A);
 103         robot.waitForIdle();
 104 
 105         if (ACTION_ACCEPTED_CALLS != 2) {
 106             throw new RuntimeException("Method accept is not invoked!");
 107         }
 108 
 109         if (ACTION_PERFORMED_CALLS != 1) {
 110             throw new RuntimeException("Method actionPerformed is invoked twice!");
 111         }
 112     }
 113 
 114     private static void createAndShowGUI() {
 115 
 116         JFrame frame = new JFrame();
 117         frame.setSize(300, 300);
 118         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 119 
 120         JComboBox<String> comboBox = new JComboBox<>(new String[]{"1", "2", "3"});
 121 
 122         Action showPopupAction = new ShowPopupAction();
 123         comboBox.getInputMap().put(KeyStroke.getKeyStroke("A"), "showPopup");
 124         comboBox.getActionMap().put("showPopup", showPopupAction);
 125 
 126         frame.getContentPane().add(comboBox);
 127         frame.setVisible(true);
 128     }
 129 
 130     private static class ShowPopupAction extends UIAction {
 131 
 132         public ShowPopupAction() {
 133             super("showPopup");
 134         }
 135 
 136         @Override
 137         public void actionPerformed(ActionEvent e) {
 138             ACTION_PERFORMED_CALLS++;
 139             Object src = e.getSource();
 140             if (src instanceof JComboBox) {
 141                 ((JComboBox) src).showPopup();
 142             }
 143         }
 144 
 145         @Override
 146         public boolean accept(Object sender) {
 147             ACTION_ACCEPTED_CALLS++;
 148             if (sender instanceof JComboBox) {
 149                 JComboBox c = (JComboBox) sender;
 150                 return !c.isPopupVisible();
 151             }
 152             return false;
 153         }
 154     }
 155 
 156     private static class SenderObject {
 157 
 158         private boolean accepted;
 159         private boolean performed;
 160     }
 161 
 162     private static class TestAction extends AbstractAction {
 163 
 164         private final boolean acceptSender;
 165 
 166         public TestAction(boolean acceptSender) {
 167             this.acceptSender = acceptSender;
 168         }
 169 
 170         @Override
 171         public boolean accept(Object sender) {
 172             ((SenderObject) sender).accepted = acceptSender;
 173             return acceptSender;
 174         }
 175 
 176         @Override
 177         public void actionPerformed(ActionEvent e) {
 178             ((SenderObject) e.getSource()).performed = true;
 179         }
 180     }
 181 }