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