Path: blob/master/test/jdk/java/awt/Mouse/EnterExitEvents/FullscreenEnterEventTest.java
41153 views
/*1* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import test.java.awt.regtesthelpers.Util;2425import javax.swing.*;26import java.awt.*;27import java.awt.event.InputEvent;28import java.awt.event.MouseAdapter;29import java.awt.event.MouseEvent;30import java.lang.reflect.InvocationHandler;31import java.lang.reflect.Method;32import java.lang.reflect.Proxy;3334/**35* @test36* @key headful37* @bug 801346838* @summary Cursor does not update properly when in fullscreen mode on Mac39* The core reason of the issue was the lack of a mouse entered event in fullscreen40* @requires (os.family == "mac")41* @modules java.desktop/com.apple.eawt42* @library ../../regtesthelpers43* @build Util44* @modules java.desktop/com.apple.eawt45* @author Petr Pchelko area=awt.event46* @run main FullscreenEnterEventTest47*/4849public class FullscreenEnterEventTest {5051private static String OS = System.getProperty("os.name").toLowerCase();5253private static JFrame frame;5455private static volatile int mouseEnterCount = 0;5657private static volatile boolean windowEnteringFullScreen = false;58private static volatile boolean windowEnteredFullScreen = false;5960public static void main(String[] args) throws Exception {6162if (!OS.contains("mac")) {63System.out.println("The test is applicable only to Mac OS X. Passed");64return;65}6667//Move the mouse out, because it could interfere with the test.68Robot r = Util.createRobot();69r.setAutoDelay(50);70Util.waitForIdle(r);71r.mouseMove(0, 0);72Util.waitForIdle(r);7374SwingUtilities.invokeAndWait(new Runnable() {75@Override76public void run() {77createAndShowGUI();78}79});8081//Move the mouse away from the frame and check the View-base full screen mode82Util.waitForIdle(r);83r.mouseMove(500, 500);84Util.waitForIdle(r);85mouseEnterCount = 0;86SwingUtilities.invokeAndWait(new Runnable() {87@Override88public void run() {89GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);90}91});92Util.waitForIdle(r);93r.delay(150);94if (mouseEnterCount != 1) {95throw new RuntimeException("No MouseEntered event for view-base full screen. Failed.");96}97SwingUtilities.invokeAndWait(new Runnable() {98@Override99public void run() {100GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);101}102});103104//Test native full screen support105Util.waitForIdle(r);106Point fullScreenButtonPos = frame.getLocation();107fullScreenButtonPos.translate(frame.getWidth() - 10, 10);108r.mouseMove(fullScreenButtonPos.x, fullScreenButtonPos.y);109r.delay(150);110mouseEnterCount = 0;111112//Cant use waitForIdle for full screen transition.113int waitCount = 0;114while (!windowEnteringFullScreen) {115r.mousePress(InputEvent.BUTTON1_MASK);116r.mouseRelease(InputEvent.BUTTON1_MASK);117Thread.sleep(100);118if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed");119}120121waitCount = 0;122while (!windowEnteredFullScreen) {123Thread.sleep(200);124if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed");125}126127if (mouseEnterCount != 1) {128throw new RuntimeException("No MouseEntered event for native full screen. Failed. mouseEnterCount:"+mouseEnterCount);129}130}131132private static void createAndShowGUI() {133frame = new JFrame(" Fullscreen OSX Bug ");134frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);135enableFullScreen(frame);136frame.addMouseListener(new MouseAdapter() {137@Override138public void mouseEntered(MouseEvent e) {139mouseEnterCount++;140}141});142frame.setBounds(100, 100, 100, 100);143frame.pack();144frame.setVisible(true);145146}147148/*149* Use reflection to make a test compilable on not Mac OS X150*/151private static void enableFullScreen(Window window) {152try {153Class fullScreenUtilities = Class.forName("com.apple.eawt.FullScreenUtilities");154Method setWindowCanFullScreen = fullScreenUtilities.getMethod("setWindowCanFullScreen", Window.class, boolean.class);155setWindowCanFullScreen.invoke(fullScreenUtilities, window, true);156Class fullScreenListener = Class.forName("com.apple.eawt.FullScreenListener");157Object listenerObject = Proxy.newProxyInstance(fullScreenListener.getClassLoader(), new Class[]{fullScreenListener}, new InvocationHandler() {158@Override159public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {160switch (method.getName()) {161case "windowEnteringFullScreen":162windowEnteringFullScreen = true;163break;164case "windowEnteredFullScreen":165windowEnteredFullScreen = true;166break;167}168return null;169}170});171Method addFullScreenListener = fullScreenUtilities.getMethod("addFullScreenListenerTo", Window.class, fullScreenListener);172addFullScreenListener.invoke(fullScreenUtilities, window, listenerObject);173} catch (Exception e) {174throw new RuntimeException("FullScreen utilities not available", e);175}176}177178}179180181