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