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.
   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.GraphicsEnvironment;
  26 import java.awt.HeadlessException;
  27 import java.awt.desktop.AboutEvent;
  28 import java.awt.desktop.AppForegroundEvent;
  29 import java.awt.desktop.AppHiddenEvent;
  30 import java.awt.desktop.AppReopenedEvent;
  31 import java.awt.desktop.OpenFilesEvent;
  32 import java.awt.desktop.OpenURIEvent;
  33 import java.awt.desktop.PreferencesEvent;
  34 import java.awt.desktop.PrintFilesEvent;
  35 import java.awt.desktop.QuitEvent;
  36 import java.awt.desktop.ScreenSleepEvent;
  37 import java.awt.desktop.SystemSleepEvent;
  38 import java.awt.desktop.UserSessionEvent;
  39 import java.awt.desktop.UserSessionEvent.Reason;
  40 import java.io.File;
  41 import java.util.Collections;
  42 import java.util.List;
  43 
  44 /**
  45  * @test
  46  * @bug 8203224
  47  * @summary tests that the correct exceptions are thrown by the events classes
  48  *          in {code java.awt.desktop} package
  49  * @run main/othervm DesktopEventsExceptions
  50  * @run main/othervm -Djava.awt.headless=true DesktopEventsExceptions
  51  */
  52 public final class DesktopEventsExceptions {
  53 
  54     public static void main(final String[] args) {
  55         // Each element of the list will creates one object to test
  56         final List<Runnable> constructors = List.of(
  57                 AboutEvent::new,
  58                 AppForegroundEvent::new,
  59                 AppHiddenEvent::new,
  60                 AppReopenedEvent::new,
  61                 QuitEvent::new,
  62                 ScreenSleepEvent::new,
  63                 SystemSleepEvent::new,
  64                 PreferencesEvent::new,
  65                 () -> new PrintFilesEvent(Collections.emptyList()),
  66                 () -> new UserSessionEvent(Reason.UNSPECIFIED),
  67                 () -> new OpenFilesEvent(Collections.emptyList(), ""),
  68                 () -> new OpenURIEvent(new File("").toURI())
  69         );
  70 
  71         for (final Runnable test : constructors) {
  72             try {
  73                 test.run();
  74                 checkHeadless(true);
  75                 checkSupported(true);
  76             } catch (HeadlessException ex) {
  77                 checkHeadless(false);
  78             } catch (UnsupportedOperationException ex) {
  79                 checkSupported(false);
  80             }
  81         }
  82     }
  83 
  84     private static void checkSupported(final boolean isSupported) {
  85         if (isSupported != Desktop.isDesktopSupported()) {
  86             throw new RuntimeException();
  87         }
  88     }
  89 
  90     private static void checkHeadless(final boolean isHeadless) {
  91         if (isHeadless == GraphicsEnvironment.isHeadless()) {
  92             throw new RuntimeException();
  93         }
  94     }
  95 }