Path: blob/master/test/jdk/java/awt/Desktop/DesktopEventsExceptions/DesktopEventsExceptions.java
41153 views
/*1* Copyright (c) 2018, 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.GraphicsEnvironment;25import java.awt.HeadlessException;26import java.awt.desktop.AboutEvent;27import java.awt.desktop.AppForegroundEvent;28import java.awt.desktop.AppHiddenEvent;29import java.awt.desktop.AppReopenedEvent;30import java.awt.desktop.OpenFilesEvent;31import java.awt.desktop.OpenURIEvent;32import java.awt.desktop.PreferencesEvent;33import java.awt.desktop.PrintFilesEvent;34import java.awt.desktop.QuitEvent;35import java.awt.desktop.ScreenSleepEvent;36import java.awt.desktop.SystemSleepEvent;37import java.awt.desktop.UserSessionEvent;38import java.awt.desktop.UserSessionEvent.Reason;39import java.io.File;40import java.util.Collections;41import java.util.List;4243/**44* @test45* @bug 820322446* @summary tests that the correct exceptions are thrown by the events classes47* in {code java.awt.desktop} package48* @run main/othervm DesktopEventsExceptions49* @run main/othervm -Djava.awt.headless=true DesktopEventsExceptions50*/51public final class DesktopEventsExceptions {5253public static void main(final String[] args) {54// Each element of the list will creates one object to test55final List<Runnable> constructors = List.of(56AboutEvent::new,57AppForegroundEvent::new,58AppHiddenEvent::new,59AppReopenedEvent::new,60QuitEvent::new,61ScreenSleepEvent::new,62SystemSleepEvent::new,63PreferencesEvent::new,64() -> new PrintFilesEvent(Collections.emptyList()),65() -> new UserSessionEvent(Reason.UNSPECIFIED),66() -> new OpenFilesEvent(Collections.emptyList(), ""),67() -> new OpenURIEvent(new File("").toURI())68);6970for (final Runnable test : constructors) {71try {72test.run();73checkHeadless(true);74checkSupported(true);75} catch (HeadlessException ex) {76checkHeadless(false);77} catch (UnsupportedOperationException ex) {78checkSupported(false);79}80}81}8283private static void checkSupported(final boolean isSupported) {84if (isSupported != Desktop.isDesktopSupported()) {85throw new RuntimeException();86}87}8889private static void checkHeadless(final boolean isHeadless) {90if (isHeadless == GraphicsEnvironment.isHeadless()) {91throw new RuntimeException();92}93}94}959697