1 /*
   2 * Copyright (c) 2013, 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 sun.awt.OSInfo;
  25 
  26 import java.awt.*;
  27 import java.awt.Robot;
  28 import java.awt.event.InputEvent;
  29 import java.lang.InterruptedException;
  30 import java.lang.System;
  31 import java.lang.Thread;
  32 import java.lang.reflect.Method;
  33 import java.lang.reflect.Proxy;
  34 import javax.swing.*;
  35 
  36 /**
  37  * @test
  38  * @key headful
  39  * @bug 8024185
  40  * @summary Native Mac OS X full screen does not work after showing the splash
  41  * @requires (os.family == "mac")
  42  * @library ../
  43  * @build GenerateTestImage
  44  * @run main GenerateTestImage
  45  * @author Petr Pchelko area=awt.event
  46  * @run main/othervm -splash:test.png FullScreenAfterSplash
  47  */
  48 
  49 public class FullScreenAfterSplash {
  50 
  51     private static JFrame frame;
  52 
  53     private static volatile boolean windowEnteringFullScreen = false;
  54     private static volatile boolean windowEnteredFullScreen = false;
  55 
  56     public static void main(String[] args) throws Exception {
  57 
  58         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
  59             System.out.println("The test is applicable only to Mac OS X. Passed");
  60             return;
  61         }
  62         try {
  63             //Move the mouse out, because it could interfere with the test.
  64             Robot r = new Robot();
  65             r.mouseMove(0, 0);
  66             sleep();
  67 
  68             SwingUtilities.invokeAndWait(FullScreenAfterSplash::createAndShowGUI);
  69             sleep();
  70 
  71             Point fullScreenButtonPos = frame.getLocation();
  72             fullScreenButtonPos.translate(frame.getWidth() - 10, 10);
  73             r.mouseMove(fullScreenButtonPos.x, fullScreenButtonPos.y);
  74 
  75             //Cant use waitForIdle for full screen transition.
  76             int waitCount = 0;
  77             while (!windowEnteringFullScreen) {
  78                 r.mousePress(InputEvent.BUTTON1_MASK);
  79                 r.mouseRelease(InputEvent.BUTTON1_MASK);
  80                 Thread.sleep(100);
  81                 if (waitCount++ > 10) {
  82                     System.err.println("Can't enter full screen mode. Failed.");
  83                     System.exit(1);
  84                 }
  85             }
  86 
  87             waitCount = 0;
  88             while (!windowEnteredFullScreen) {
  89                 Thread.sleep(100);
  90                 if (waitCount++ > 10) {
  91                     System.err.println("Can't enter full screen mode. Failed.");
  92                     System.exit(1);
  93                 }
  94             }
  95         } finally {
  96             if (frame != null) {
  97                 frame.dispose();
  98             }
  99         }
 100     }
 101 
 102     private static void createAndShowGUI() {
 103         frame = new JFrame(" Fullscreen OSX Bug ");
 104         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 105         enableFullScreen(frame);
 106         frame.setBounds(100, 100, 100, 100);
 107         frame.pack();
 108         frame.setVisible(true);
 109     }
 110 
 111     /*
 112      *  Use reflection to make a test compilable on not Mac OS X
 113      */
 114     private static void enableFullScreen(Window window) {
 115         try {
 116             Class<?> fullScreenUtilities = Class.forName("com.apple.eawt.FullScreenUtilities");
 117             Method setWindowCanFullScreen = fullScreenUtilities.getMethod("setWindowCanFullScreen", Window.class, boolean.class);
 118             setWindowCanFullScreen.invoke(fullScreenUtilities, window, true);
 119             Class fullScreenListener = Class.forName("com.apple.eawt.FullScreenListener");
 120             Object listenerObject = Proxy.newProxyInstance(fullScreenListener.getClassLoader(), new Class[]{fullScreenListener}, (proxy, method, args) -> {
 121                 switch (method.getName()) {
 122                     case "windowEnteringFullScreen":
 123                         windowEnteringFullScreen = true;
 124                         break;
 125                     case "windowEnteredFullScreen":
 126                         windowEnteredFullScreen = true;
 127                         break;
 128                 }
 129                 return null;
 130             });
 131             Method addFullScreenListener = fullScreenUtilities.getMethod("addFullScreenListenerTo", Window.class, fullScreenListener);
 132             addFullScreenListener.invoke(fullScreenUtilities, window, listenerObject);
 133         } catch (Exception e) {
 134             throw new RuntimeException("FullScreen utilities not available", e);
 135         }
 136     }
 137 
 138     private static void sleep() {
 139         try {
 140             Thread.sleep(500);
 141         } catch (InterruptedException ignored) { }
 142     }
 143 }