Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/MenuBar/TestNoScreenMenuBar.java
41149 views
1
/*
2
* Copyright (c) 2018, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/**
27
* @test
28
* @key headful
29
* @bug 8146310
30
* @summary [macosx] setDefaultMenuBar does not initialize screen menu bar
31
* @author Alan Snyder
32
* @library /test/lib
33
* @run main/othervm TestNoScreenMenuBar
34
* @requires (os.family == "mac")
35
*/
36
37
import java.awt.AWTException;
38
import java.awt.Desktop;
39
import java.awt.Frame;
40
import java.awt.Menu;
41
import java.awt.MenuBar;
42
import java.awt.Robot;
43
import java.awt.event.InputEvent;
44
import java.io.IOException;
45
import java.lang.reflect.InvocationTargetException;
46
47
import javax.swing.JMenu;
48
import javax.swing.JMenuBar;
49
import javax.swing.JMenuItem;
50
import javax.swing.SwingUtilities;
51
52
import jdk.test.lib.process.ProcessTools;
53
54
public class TestNoScreenMenuBar
55
{
56
static TestNoScreenMenuBar theTest;
57
private Robot robot;
58
private Process process;
59
private boolean isActionPerformed;
60
61
public TestNoScreenMenuBar()
62
{
63
try {
64
robot = new Robot();
65
robot.setAutoDelay(50);
66
} catch (AWTException ex) {
67
throw new RuntimeException(ex);
68
}
69
70
// activate another java application
71
openOtherApplication();
72
robot.delay(2000);
73
74
// The failure mode is installing the default menu bar while the application is inactive
75
Desktop desktop = Desktop.getDesktop();
76
desktop.setDefaultMenuBar(createMenuBar());
77
78
robot.delay(500);
79
desktop.requestForeground(true);
80
robot.delay(500);
81
}
82
83
JMenuBar createMenuBar()
84
{
85
JMenuBar mb = new JMenuBar();
86
// A very long name makes it more likely that the robot will hit the menu
87
JMenu menu = new JMenu("TestTestTestTestTestTestTestTestTestTest");
88
mb.add(menu);
89
JMenuItem item = new JMenuItem("TestTestTestTestTestTestTestTestTestTest");
90
item.addActionListener(ev -> {
91
isActionPerformed = true;
92
});
93
menu.add(item);
94
return mb;
95
}
96
97
void dispose()
98
{
99
closeOtherApplication();
100
Desktop.getDesktop().setDefaultMenuBar(null);
101
}
102
103
private void performMenuItemTest()
104
{
105
// Find the menu on the screen menu bar
106
// The location depends upon the application name which is the name of the first menu.
107
// Unfortunately, the application name can vary based on how the application is run.
108
// The work around is to make the menu and the menu item names very long.
109
110
int menuBarX = 250;
111
int menuBarY = 11;
112
int menuItemX = menuBarX;
113
int menuItemY = 34;
114
115
robot.mouseMove(menuBarX, menuBarY);
116
robot.delay(100);
117
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
118
robot.delay(100);
119
robot.mouseMove(menuItemX, menuItemY);
120
robot.delay(100);
121
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
122
robot.waitForIdle();
123
124
waitForAction();
125
}
126
127
private synchronized void waitForAction()
128
{
129
try {
130
for (int i = 0; i < 10; i++) {
131
if (isActionPerformed) {
132
return;
133
}
134
wait(100);
135
}
136
} catch (InterruptedException ex) {
137
}
138
throw new RuntimeException("Test failed: menu item action was not performed");
139
}
140
141
private void openOtherApplication() {
142
process = execute();
143
}
144
145
private void closeOtherApplication() {
146
if (process != null) {
147
process.destroyForcibly();
148
}
149
}
150
151
private Process execute() {
152
try {
153
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
154
TestNoScreenMenuBar.class.getSimpleName(), "mark");
155
return ProcessTools.startProcess("Other frame", pb);
156
} catch (IOException ex) {
157
throw new RuntimeException("Unable to execute command");
158
}
159
}
160
161
private static void runSwing(Runnable r)
162
{
163
try {
164
SwingUtilities.invokeAndWait(r);
165
} catch (InterruptedException e) {
166
} catch (InvocationTargetException e) {
167
throw new RuntimeException(e);
168
}
169
}
170
171
public static void main(String[] args)
172
{
173
if (!System.getProperty("os.name").contains("OS X")) {
174
System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
175
return;
176
}
177
if (args.length != 0) {
178
Frame frame = new Frame();
179
MenuBar mb = new MenuBar();
180
mb.add(new Menu("Hello"));
181
frame.setMenuBar(mb);
182
frame.setSize(300, 300);
183
frame.setLocationRelativeTo(null);
184
frame.setVisible(true);
185
frame.toFront();
186
return;
187
}
188
System.setProperty("apple.laf.useScreenMenuBar", "true");
189
try {
190
runSwing(() -> theTest = new TestNoScreenMenuBar());
191
theTest.performMenuItemTest();
192
} finally {
193
if (theTest != null) {
194
runSwing(() -> theTest.dispose());
195
}
196
}
197
}
198
}
199
200