Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Focus/KeyEventForBadFocusOwnerTest/KeyEventForBadFocusOwnerTest.java
41152 views
1
/*
2
* Copyright (c) 2013, 2021, 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
@test
25
@key headful
26
@bug 4476629
27
@library ../../../../javax/swing/regtesthelpers
28
@build Util
29
@summary KeyEvents dispatched to old focus owner that is no longer showing
30
@author [email protected]: area=awt.focus
31
@run main KeyEventForBadFocusOwnerTest
32
*/
33
34
/**
35
* KeyEventForBadFocusOwnerTest.java
36
*
37
* summary: KeyEvents dispatched to old focus owner that is no longer showing
38
*/
39
40
41
import java.awt.Robot;
42
43
import java.awt.event.*;
44
45
import javax.swing.*;
46
import javax.swing.event.*;
47
48
public class KeyEventForBadFocusOwnerTest {
49
final static String ITEM_ONE_TEXT = "one";
50
final static String ITEM_TWO_TEXT = "two";
51
52
volatile static boolean itemOneSelected = false;
53
volatile static boolean itemTwoSelected = false;
54
volatile static boolean unexpectedItemSelected = false;
55
56
static Robot robot;
57
static JFrame frame;
58
59
public static void main(String[] args) throws Exception {
60
try {
61
SwingUtilities.invokeAndWait(new Runnable() {
62
public void run() {
63
frame = new JFrame("TEST");
64
JMenuBar mb = new JMenuBar();
65
JMenu one = new JMenu(ITEM_ONE_TEXT);
66
JMenu two = new JMenu(ITEM_TWO_TEXT);
67
68
mb.add(one);
69
mb.add(two);
70
71
ActionListener al = new ActionListener() {
72
public void actionPerformed(ActionEvent ae) {
73
String itemText = ((JMenuItem)ae.getSource()).getText();
74
System.out.println("--> " + itemText);
75
unexpectedItemSelected = true;
76
}
77
};
78
one.setMnemonic(KeyEvent.VK_O);
79
JMenuItem item = new JMenuItem("one 1");
80
item.setMnemonic(KeyEvent.VK_O);
81
item.addActionListener(al);
82
one.add(item);
83
one.add("two");
84
one.add("three");
85
86
two.setMnemonic(KeyEvent.VK_T);
87
item = new JMenuItem("two 2");
88
item.setMnemonic(KeyEvent.VK_T);
89
item.addActionListener(al);
90
two.add(item);
91
two.add("three");
92
two.add("four");
93
94
PopupMenuListener popupMenuListener = new PopupMenuListener() {
95
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
96
System.out.print(e);
97
System.out.print(e.getSource());
98
String itemText = ((JPopupMenu)e.getSource()).getName();
99
System.out.println("Menu " + itemText + "is opened.");
100
switch(itemText) {
101
case ITEM_ONE_TEXT:
102
itemOneSelected = true;
103
break;
104
case ITEM_TWO_TEXT:
105
itemTwoSelected = true;
106
break;
107
}
108
}
109
110
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
111
public void popupMenuCanceled(PopupMenuEvent e) {}
112
};
113
one.getPopupMenu().setName(ITEM_ONE_TEXT);
114
two.getPopupMenu().setName(ITEM_TWO_TEXT);
115
one.getPopupMenu().addPopupMenuListener(popupMenuListener);
116
two.getPopupMenu().addPopupMenuListener(popupMenuListener);
117
frame.setJMenuBar(mb);
118
frame.setSize(100,100);
119
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
120
frame.setLocationRelativeTo(null);
121
frame.pack();
122
frame.setVisible(true);
123
}
124
});
125
126
127
robot = new Robot();
128
robot.setAutoDelay(100);
129
robot.waitForIdle();
130
robot.delay(1000);
131
132
Util.hitMnemonics(robot, KeyEvent.VK_O);
133
Util.hitMnemonics(robot, KeyEvent.VK_T);
134
135
robot.waitForIdle();
136
Thread.sleep(1000); // workaround for MacOS
137
138
if (unexpectedItemSelected) {
139
throw new Exception("Test failed. KeyEvent dispatched to old focus owner. ");
140
}
141
if (!itemOneSelected || !itemTwoSelected) {
142
throw new Exception("Not all expected events were received");
143
}
144
} finally {
145
SwingUtilities.invokeAndWait(() -> {
146
if (frame != null) {
147
frame.dispose();
148
}
149
});
150
}
151
}
152
}
153
154