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