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