Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Desktop/SecurityTest/DesktopSecurityTest.java
41155 views
1
/*
2
* Copyright (c) 2005, 2020, 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.Desktop;
25
import java.awt.desktop.AppForegroundEvent;
26
import java.awt.desktop.AppForegroundListener;
27
import java.awt.desktop.AppHiddenEvent;
28
import java.awt.desktop.AppHiddenListener;
29
import java.awt.desktop.AppReopenedListener;
30
import java.awt.desktop.QuitStrategy;
31
import java.awt.desktop.ScreenSleepEvent;
32
import java.awt.desktop.ScreenSleepListener;
33
import java.awt.desktop.SystemSleepEvent;
34
import java.awt.desktop.SystemSleepListener;
35
import java.awt.desktop.UserSessionEvent;
36
import java.awt.desktop.UserSessionListener;
37
import java.io.File;
38
import java.io.FileWriter;
39
import java.io.Writer;
40
import java.net.URI;
41
import java.nio.file.Files;
42
import java.nio.file.Paths;
43
44
/**
45
* @test
46
* @bug 6255196 8143227
47
* @key headful
48
* @summary Tests that Desktop.browse() throws SecurityException without
49
* permission of java.awt.AWTPermission showWindowWithoutWarningBanner
50
* @run main/othervm/policy=desktop.policy DesktopSecurityTest
51
*/
52
public final class DesktopSecurityTest {
53
54
public static void main(String[] args) throws Exception {
55
if(!Desktop.isDesktopSupported()){
56
System.out.println("Desktop is not supported");
57
return;
58
}
59
60
URI webURI = URI.create(System.getProperty("java.vendor.url",
61
"http://www.sun.com"));
62
File testFile = new File("JDIC-test.txt").getAbsoluteFile();
63
try {
64
try (Writer writer = new FileWriter(testFile)) {
65
writer.write("temp file used to test print() method of Desktop.");
66
writer.flush();
67
}
68
test(webURI, testFile);
69
} finally {
70
Files.delete(Paths.get(testFile.getAbsolutePath()));
71
}
72
}
73
74
private static void test(URI webURI, File testFile) throws Exception {
75
Desktop desktop = Desktop.getDesktop();
76
for (Desktop.Action action : Desktop.Action.values()) {
77
if (!desktop.isSupported(action)) {
78
continue; // skip if this action is unsupported.
79
}
80
81
try {
82
switch (action) {
83
case OPEN -> desktop.open(testFile);
84
case EDIT -> desktop.edit(testFile);
85
case PRINT -> desktop.print(testFile);
86
case MAIL -> desktop.mail();
87
case BROWSE -> desktop.browse(webURI);
88
case APP_EVENT_FOREGROUND -> {
89
desktop.addAppEventListener(new AppForegroundListener() {
90
@Override
91
public void appRaisedToForeground(AppForegroundEvent e) {
92
}
93
@Override
94
public void appMovedToBackground(AppForegroundEvent e) {
95
}
96
});
97
}
98
case APP_EVENT_HIDDEN -> {
99
desktop.addAppEventListener(new AppHiddenListener() {
100
@Override
101
public void appHidden(AppHiddenEvent e) {
102
}
103
@Override
104
public void appUnhidden(AppHiddenEvent e) {
105
}
106
});
107
}
108
case APP_EVENT_REOPENED -> {
109
desktop.addAppEventListener((AppReopenedListener) e -> {});
110
}
111
case APP_EVENT_SCREEN_SLEEP -> {
112
desktop.addAppEventListener(new ScreenSleepListener() {
113
@Override
114
public void screenAboutToSleep(ScreenSleepEvent e) {
115
}
116
@Override
117
public void screenAwoke(ScreenSleepEvent e) {
118
}
119
});
120
}
121
case APP_EVENT_SYSTEM_SLEEP -> {
122
desktop.addAppEventListener(new SystemSleepListener() {
123
@Override
124
public void systemAboutToSleep(SystemSleepEvent e) {
125
}
126
@Override
127
public void systemAwoke(SystemSleepEvent e) {
128
}
129
});
130
}
131
case APP_EVENT_USER_SESSION -> {
132
desktop.addAppEventListener(new UserSessionListener() {
133
@Override
134
public void userSessionDeactivated(UserSessionEvent e) {
135
}
136
@Override
137
public void userSessionActivated(UserSessionEvent e) {
138
}
139
});
140
}
141
case APP_ABOUT -> {
142
desktop.setAboutHandler(e -> {});
143
}
144
case APP_PREFERENCES -> {
145
desktop.setPreferencesHandler(e -> {});
146
}
147
case APP_OPEN_FILE -> {
148
desktop.setOpenFileHandler(e -> {});
149
}
150
case APP_PRINT_FILE -> {
151
desktop.setPrintFileHandler(e -> {});
152
}
153
case APP_OPEN_URI -> {
154
desktop.setOpenURIHandler(e -> {});
155
}
156
case APP_QUIT_HANDLER -> {
157
desktop.setQuitHandler((e, response) -> {});
158
}
159
case APP_QUIT_STRATEGY -> {
160
desktop.setQuitStrategy(QuitStrategy.NORMAL_EXIT);
161
}
162
case APP_SUDDEN_TERMINATION -> {
163
desktop.enableSuddenTermination();
164
}
165
case APP_REQUEST_FOREGROUND -> {
166
desktop.requestForeground(true);
167
}
168
case APP_HELP_VIEWER -> {
169
desktop.openHelpViewer();
170
}
171
case APP_MENU_BAR -> {
172
desktop.setDefaultMenuBar(null);
173
}
174
case BROWSE_FILE_DIR -> {
175
desktop.browseFileDirectory(testFile.getParentFile());
176
}
177
case MOVE_TO_TRASH -> {
178
// The test have permission to create/delete files, skip
179
continue;
180
}
181
default -> throw new IllegalStateException(
182
"Unexpected value: " + action);
183
}
184
// no exception has been thrown.
185
throw new RuntimeException(
186
"SecurityException wax expected for: " + action);
187
} catch (SecurityException ignored) {
188
// expected
189
}
190
}
191
}
192
}
193
194