Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mouse/EnterExitEvents/FullscreenEnterEventTest.java
47311 views
/*1* Copyright (c) 2013, 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 sun.misc.OSEnvironment;24import test.java.awt.regtesthelpers.Util;2526import javax.swing.*;27import java.awt.*;28import java.awt.event.InputEvent;29import java.awt.event.MouseAdapter;30import java.awt.event.MouseEvent;31import java.lang.reflect.InvocationHandler;32import java.lang.reflect.Method;33import java.lang.reflect.Proxy;3435/*36* @test37* @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* @library ../../regtesthelpers42* @build Util43* @author Petr Pchelko area=awt.event44* @run main FullscreenEnterEventTest45*/46public class FullscreenEnterEventTest {4748private static String OS = System.getProperty("os.name").toLowerCase();4950private static JFrame frame;5152private static volatile int mouseEnterCount = 0;5354private static volatile boolean windowEnteringFullScreen = false;55private static volatile boolean windowEnteredFullScreen = false;5657public static void main(String[] args) throws Exception {5859if (!OS.contains("mac")) {60System.out.println("The test is applicable only to Mac OS X. Passed");61return;62}6364//Move the mouse out, because it could interfere with the test.65Robot r = Util.createRobot();66Util.waitForIdle(r);67r.mouseMove(0, 0);68Util.waitForIdle(r);6970SwingUtilities.invokeAndWait(new Runnable() {71@Override72public void run() {73createAndShowGUI();74}75});7677//Move the mouse away from the frame and check the View-base full screen mode78Util.waitForIdle(r);79r.mouseMove(500, 500);80Util.waitForIdle(r);81mouseEnterCount = 0;82SwingUtilities.invokeAndWait(new Runnable() {83@Override84public void run() {85GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);86}87});88Util.waitForIdle(r);89if (mouseEnterCount != 1) {90throw new RuntimeException("No MouseEntered event for view-base full screen. Failed.");91}92SwingUtilities.invokeAndWait(new Runnable() {93@Override94public void run() {95GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);96}97});9899//Test native full screen support100Util.waitForIdle(r);101Point fullScreenButtonPos = frame.getLocation();102fullScreenButtonPos.translate(frame.getWidth() - 10, 10);103r.mouseMove(fullScreenButtonPos.x, fullScreenButtonPos.y);104mouseEnterCount = 0;105106//Cant use waitForIdle for full screen transition.107int waitCount = 0;108while (!windowEnteringFullScreen) {109r.mousePress(InputEvent.BUTTON1_MASK);110r.mouseRelease(InputEvent.BUTTON1_MASK);111Thread.sleep(100);112if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed");113}114115waitCount = 0;116while (!windowEnteredFullScreen) {117Thread.sleep(200);118if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed");119}120121if (mouseEnterCount != 1) {122throw new RuntimeException("No MouseEntered event for native full screen. Failed.");123}124}125126private static void createAndShowGUI() {127frame = new JFrame(" Fullscreen OSX Bug ");128frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);129enableFullScreen(frame);130frame.addMouseListener(new MouseAdapter() {131@Override132public void mouseEntered(MouseEvent e) {133mouseEnterCount++;134}135});136frame.setBounds(100, 100, 100, 100);137frame.pack();138frame.setVisible(true);139140}141142/*143* Use reflection to make a test compilable on not Mac OS X144*/145private static void enableFullScreen(Window window) {146try {147Class fullScreenUtilities = Class.forName("com.apple.eawt.FullScreenUtilities");148Method setWindowCanFullScreen = fullScreenUtilities.getMethod("setWindowCanFullScreen", Window.class, boolean.class);149setWindowCanFullScreen.invoke(fullScreenUtilities, window, true);150Class fullScreenListener = Class.forName("com.apple.eawt.FullScreenListener");151Object listenerObject = Proxy.newProxyInstance(fullScreenListener.getClassLoader(), new Class[]{fullScreenListener}, new InvocationHandler() {152@Override153public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {154switch (method.getName()) {155case "windowEnteringFullScreen":156windowEnteringFullScreen = true;157break;158case "windowEnteredFullScreen":159windowEnteredFullScreen = true;160break;161}162return null;163}164});165Method addFullScreenListener = fullScreenUtilities.getMethod("addFullScreenListenerTo", Window.class, fullScreenListener);166addFullScreenListener.invoke(fullScreenUtilities, window, listenerObject);167} catch (Exception e) {168throw new RuntimeException("FullScreen utilities not available", e);169}170}171172}173174175