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