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 sun.misc.OSEnvironment; 25 import test.java.awt.regtesthelpers.Util; 26 27 import javax.swing.*; 28 import java.awt.*; 29 import java.awt.event.InputEvent; 30 import java.awt.event.MouseAdapter; 31 import java.awt.event.MouseEvent; 32 import java.lang.reflect.InvocationHandler; 33 import java.lang.reflect.Method; 34 import java.lang.reflect.Proxy; 35 36 /* 37 * @test 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 * @library ../../regtesthelpers 42 * @build Util 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 Util.waitForIdle(r); 67 r.mouseMove(0, 0); 68 Util.waitForIdle(r); 69 70 SwingUtilities.invokeAndWait(new Runnable() { 71 @Override 72 public void run() { 73 createAndShowGUI(); 74 } 75 }); 76 77 //Move the mouse away from the frame and check the View-base full screen mode 78 Util.waitForIdle(r); 79 r.mouseMove(500, 500); 80 Util.waitForIdle(r); 81 mouseEnterCount = 0; 82 SwingUtilities.invokeAndWait(new Runnable() { 83 @Override 84 public void run() { 85 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame); 86 } 87 }); 88 Util.waitForIdle(r); 89 if (mouseEnterCount != 1) { 90 throw new RuntimeException("No MouseEntered event for view-base full screen. Failed."); 91 } 92 SwingUtilities.invokeAndWait(new Runnable() { 93 @Override 94 public void run() { 95 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null); 96 } 97 }); 98 99 //Test native full screen support 100 Util.waitForIdle(r); 101 Point fullScreenButtonPos = frame.getLocation(); 102 fullScreenButtonPos.translate(frame.getWidth() - 10, 10); 103 r.mouseMove(fullScreenButtonPos.x, fullScreenButtonPos.y); 104 mouseEnterCount = 0; 105 106 //Cant use waitForIdle for full screen transition. 107 int waitCount = 0; 108 while (!windowEnteringFullScreen) { 109 r.mousePress(InputEvent.BUTTON1_MASK); 110 r.mouseRelease(InputEvent.BUTTON1_MASK); 111 Thread.sleep(100); 112 if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed"); 113 } 114 115 waitCount = 0; 116 while (!windowEnteredFullScreen) { 117 Thread.sleep(200); 118 if (waitCount++ > 10) throw new RuntimeException("Can't enter full screen mode. Failed"); 119 } 120 121 if (mouseEnterCount != 1) { 122 throw new RuntimeException("No MouseEntered event for native full screen. Failed."); 123 } 124 } 125 126 private static void createAndShowGUI() { 127 frame = new JFrame(" Fullscreen OSX Bug "); 128 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 129 enableFullScreen(frame); 130 frame.addMouseListener(new MouseAdapter() { 131 @Override 132 public void mouseEntered(MouseEvent e) { 133 mouseEnterCount++; 134 } 135 }); 136 frame.setBounds(100, 100, 100, 100); 137 frame.pack(); 138 frame.setVisible(true); 139 140 } 141 142 /* 143 * Use reflection to make a test compilable on not Mac OS X 144 */ 145 private static void enableFullScreen(Window window) { 146 try { 147 Class fullScreenUtilities = Class.forName("com.apple.eawt.FullScreenUtilities"); 148 Method setWindowCanFullScreen = fullScreenUtilities.getMethod("setWindowCanFullScreen", Window.class, boolean.class); 149 setWindowCanFullScreen.invoke(fullScreenUtilities, window, true); 150 Class fullScreenListener = Class.forName("com.apple.eawt.FullScreenListener"); 151 Object listenerObject = Proxy.newProxyInstance(fullScreenListener.getClassLoader(), new Class[]{fullScreenListener}, new InvocationHandler() { 152 @Override 153 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 154 switch (method.getName()) { 155 case "windowEnteringFullScreen": 156 windowEnteringFullScreen = true; 157 break; 158 case "windowEnteredFullScreen": 159 windowEnteredFullScreen = true; 160 break; 161 } 162 return null; 163 } 164 }); 165 Method addFullScreenListener = fullScreenUtilities.getMethod("addFullScreenListenerTo", Window.class, fullScreenListener); 166 addFullScreenListener.invoke(fullScreenUtilities, window, listenerObject); 167 } catch (Exception e) { 168 throw new RuntimeException("FullScreen utilities not available", e); 169 } 170 } 171 172 }