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 test.java.awt.regtesthelpers.Util;
  25 
  26 import javax.swing.*;
  27 import java.awt.*;
  28 import java.awt.event.InputEvent;
  29 import java.awt.event.MouseAdapter;
  30 import java.awt.event.MouseEvent;
  31 import java.lang.reflect.InvocationHandler;
  32 import java.lang.reflect.Method;
  33 import java.lang.reflect.Proxy;
  34 
  35 /*
  36  * @test
  37  * @bug 8013468
  38  * @summary Cursor does not update properly when in fullscreen mode on Mac
  39  *    The core reason of the issue was the lack of a mouse entered event in fullscreen
  40  * @requires (os.family == "mac")
  41  * @modules java.desktop/com.apple.eawt
  42  * @library ../../regtesthelpers
  43  * @build Util
  44  * @modules java.desktop/com.apple.eawt
  45  * @author Petr Pchelko area=awt.event
  46  * @run main FullscreenEnterEventTest
  47  */
  48 public class FullscreenEnterEventTest {
  49 
  50     private static String OS = System.getProperty("os.name").toLowerCase();
  51 
  52     private static JFrame frame;
  53 
  54     private static volatile int mouseEnterCount = 0;
  55 
  56     private static volatile boolean windowEnteringFullScreen = false;
  57     private static volatile boolean windowEnteredFullScreen = false;
  58 
  59     public static void main(String[] args) throws Exception {
  60 
  61         if (!OS.contains("mac")) {
  62             System.out.println("The test is applicable only to Mac OS X. Passed");
  63             return;
  64         }
  65 
  66         //Move the mouse out, because it could interfere with the test.
  67         Robot r = Util.createRobot();
  68         r.setAutoDelay(50);
  69         Util.waitForIdle(r);
  70         r.mouseMove(0, 0);
  71         Util.waitForIdle(r);
  72 
  73         SwingUtilities.invokeAndWait(new Runnable() {
  74             @Override
  75             public void run() {
  76                 createAndShowGUI();
  77             }
  78         });
  79 
  80         //Move the mouse away from the frame and check the View-base full screen mode
  81         Util.waitForIdle(r);
  82         r.mouseMove(500, 500);
  83         Util.waitForIdle(r);
  84         mouseEnterCount = 0;
  85         SwingUtilities.invokeAndWait(new Runnable() {
  86             @Override
  87             public void run() {
  88                 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);
  89             }
  90         });
  91         Util.waitForIdle(r);
  92         r.delay(150);
  93         if (mouseEnterCount != 1) {
  94             throw new RuntimeException("No MouseEntered event for view-base full screen. Failed.");
  95         }
  96         SwingUtilities.invokeAndWait(new Runnable() {
  97             @Override
  98             public void run() {
  99                 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);
 100             }
 101         });
 102 
 103         //Test native full screen support
 104         Util.waitForIdle(r);
 105         Point fullScreenButtonPos = frame.getLocation();
 106         fullScreenButtonPos.translate(frame.getWidth() - 10, 10);
 107         r.mouseMove(fullScreenButtonPos.x, fullScreenButtonPos.y);
 108         r.delay(150);
 109         mouseEnterCount = 0;
 110 
 111         //Cant use waitForIdle for full screen transition.
 112         int waitCount = 0;
 113         while (!windowEnteringFullScreen) {
 114             r.mousePress(InputEvent.BUTTON1_MASK);
 115             r.mouseRelease(InputEvent.BUTTON1_MASK);
 116             Thread.sleep(100);
 117             if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed");
 118         }
 119 
 120         waitCount = 0;
 121         while (!windowEnteredFullScreen) {
 122             Thread.sleep(200);
 123             if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed");
 124         }
 125 
 126         if (mouseEnterCount != 1) {
 127             throw new RuntimeException("No MouseEntered event for native full screen. Failed. mouseEnterCount:"+mouseEnterCount);
 128         }
 129     }
 130 
 131     private static void createAndShowGUI() {
 132         frame = new JFrame(" Fullscreen OSX Bug ");
 133         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 134         enableFullScreen(frame);
 135         frame.addMouseListener(new MouseAdapter() {
 136             @Override
 137             public void mouseEntered(MouseEvent e) {
 138                 mouseEnterCount++;
 139             }
 140         });
 141         frame.setBounds(100, 100, 100, 100);
 142         frame.pack();
 143         frame.setVisible(true);
 144 
 145     }
 146 
 147     /*
 148      *  Use reflection to make a test compilable on not Mac OS X
 149      */
 150     private static void enableFullScreen(Window window) {
 151         try {
 152             Class fullScreenUtilities = Class.forName("com.apple.eawt.FullScreenUtilities");
 153             Method setWindowCanFullScreen = fullScreenUtilities.getMethod("setWindowCanFullScreen", Window.class, boolean.class);
 154             setWindowCanFullScreen.invoke(fullScreenUtilities, window, true);
 155             Class fullScreenListener = Class.forName("com.apple.eawt.FullScreenListener");
 156             Object listenerObject = Proxy.newProxyInstance(fullScreenListener.getClassLoader(), new Class[]{fullScreenListener}, new InvocationHandler() {
 157                 @Override
 158                 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 159                     switch (method.getName()) {
 160                         case "windowEnteringFullScreen":
 161                             windowEnteringFullScreen = true;
 162                             break;
 163                         case "windowEnteredFullScreen":
 164                             windowEnteredFullScreen = true;
 165                             break;
 166                     }
 167                     return null;
 168                 }
 169             });
 170             Method addFullScreenListener = fullScreenUtilities.getMethod("addFullScreenListenerTo", Window.class, fullScreenListener);
 171             addFullScreenListener.invoke(fullScreenUtilities, window, listenerObject);
 172         } catch (Exception e) {
 173             throw new RuntimeException("FullScreen utilities not available", e);
 174         }
 175     }
 176 
 177 }