Path: blob/master/test/jdk/java/awt/Desktop/SecurityTest/DesktopSecurityTest.java
41155 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Desktop;24import java.awt.desktop.AppForegroundEvent;25import java.awt.desktop.AppForegroundListener;26import java.awt.desktop.AppHiddenEvent;27import java.awt.desktop.AppHiddenListener;28import java.awt.desktop.AppReopenedListener;29import java.awt.desktop.QuitStrategy;30import java.awt.desktop.ScreenSleepEvent;31import java.awt.desktop.ScreenSleepListener;32import java.awt.desktop.SystemSleepEvent;33import java.awt.desktop.SystemSleepListener;34import java.awt.desktop.UserSessionEvent;35import java.awt.desktop.UserSessionListener;36import java.io.File;37import java.io.FileWriter;38import java.io.Writer;39import java.net.URI;40import java.nio.file.Files;41import java.nio.file.Paths;4243/**44* @test45* @bug 6255196 814322746* @key headful47* @summary Tests that Desktop.browse() throws SecurityException without48* permission of java.awt.AWTPermission showWindowWithoutWarningBanner49* @run main/othervm/policy=desktop.policy DesktopSecurityTest50*/51public final class DesktopSecurityTest {5253public static void main(String[] args) throws Exception {54if(!Desktop.isDesktopSupported()){55System.out.println("Desktop is not supported");56return;57}5859URI webURI = URI.create(System.getProperty("java.vendor.url",60"http://www.sun.com"));61File testFile = new File("JDIC-test.txt").getAbsoluteFile();62try {63try (Writer writer = new FileWriter(testFile)) {64writer.write("temp file used to test print() method of Desktop.");65writer.flush();66}67test(webURI, testFile);68} finally {69Files.delete(Paths.get(testFile.getAbsolutePath()));70}71}7273private static void test(URI webURI, File testFile) throws Exception {74Desktop desktop = Desktop.getDesktop();75for (Desktop.Action action : Desktop.Action.values()) {76if (!desktop.isSupported(action)) {77continue; // skip if this action is unsupported.78}7980try {81switch (action) {82case OPEN -> desktop.open(testFile);83case EDIT -> desktop.edit(testFile);84case PRINT -> desktop.print(testFile);85case MAIL -> desktop.mail();86case BROWSE -> desktop.browse(webURI);87case APP_EVENT_FOREGROUND -> {88desktop.addAppEventListener(new AppForegroundListener() {89@Override90public void appRaisedToForeground(AppForegroundEvent e) {91}92@Override93public void appMovedToBackground(AppForegroundEvent e) {94}95});96}97case APP_EVENT_HIDDEN -> {98desktop.addAppEventListener(new AppHiddenListener() {99@Override100public void appHidden(AppHiddenEvent e) {101}102@Override103public void appUnhidden(AppHiddenEvent e) {104}105});106}107case APP_EVENT_REOPENED -> {108desktop.addAppEventListener((AppReopenedListener) e -> {});109}110case APP_EVENT_SCREEN_SLEEP -> {111desktop.addAppEventListener(new ScreenSleepListener() {112@Override113public void screenAboutToSleep(ScreenSleepEvent e) {114}115@Override116public void screenAwoke(ScreenSleepEvent e) {117}118});119}120case APP_EVENT_SYSTEM_SLEEP -> {121desktop.addAppEventListener(new SystemSleepListener() {122@Override123public void systemAboutToSleep(SystemSleepEvent e) {124}125@Override126public void systemAwoke(SystemSleepEvent e) {127}128});129}130case APP_EVENT_USER_SESSION -> {131desktop.addAppEventListener(new UserSessionListener() {132@Override133public void userSessionDeactivated(UserSessionEvent e) {134}135@Override136public void userSessionActivated(UserSessionEvent e) {137}138});139}140case APP_ABOUT -> {141desktop.setAboutHandler(e -> {});142}143case APP_PREFERENCES -> {144desktop.setPreferencesHandler(e -> {});145}146case APP_OPEN_FILE -> {147desktop.setOpenFileHandler(e -> {});148}149case APP_PRINT_FILE -> {150desktop.setPrintFileHandler(e -> {});151}152case APP_OPEN_URI -> {153desktop.setOpenURIHandler(e -> {});154}155case APP_QUIT_HANDLER -> {156desktop.setQuitHandler((e, response) -> {});157}158case APP_QUIT_STRATEGY -> {159desktop.setQuitStrategy(QuitStrategy.NORMAL_EXIT);160}161case APP_SUDDEN_TERMINATION -> {162desktop.enableSuddenTermination();163}164case APP_REQUEST_FOREGROUND -> {165desktop.requestForeground(true);166}167case APP_HELP_VIEWER -> {168desktop.openHelpViewer();169}170case APP_MENU_BAR -> {171desktop.setDefaultMenuBar(null);172}173case BROWSE_FILE_DIR -> {174desktop.browseFileDirectory(testFile.getParentFile());175}176case MOVE_TO_TRASH -> {177// The test have permission to create/delete files, skip178continue;179}180default -> throw new IllegalStateException(181"Unexpected value: " + action);182}183// no exception has been thrown.184throw new RuntimeException(185"SecurityException wax expected for: " + action);186} catch (SecurityException ignored) {187// expected188}189}190}191}192193194