1 /* 2 * Copyright (c) 2011, 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 /* 25 * @test 26 * @summary <rdar://problem/3338508> Events-mouse: Mouse events don't work at all in fullscreen mode 27 * @summary <rdar://problem/3439508> 1.4.1 v.1: Full Screen Mode: can't catch mouse events 28 * @summary com.apple.junit.java.graphics.fullscreen 29 * @library ../regtesthelpers 30 * @build RobotUtilities 31 * @run main TestFullScreenEvents 32 */ 33 34 import junit.framework.*; 35 import test.java.awt.regtesthelpers.RobotUtilities; 36 import java.awt.*; 37 import java.awt.event.*; 38 import java.util.*; 39 import java.awt.image.BufferStrategy; 40 41 public class TestFullScreenEvents extends TestCase { 42 43 private GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 44 private DisplayMode oldDisplayMode = device.getDisplayMode(); 45 private Graphics currentGraphics; 46 47 private static final int TIMEOUT = 5000; // Wait up to five seconds 48 private BufferStrategy strategy; 49 50 private volatile int eventcount; 51 52 protected Timer ticktock = null; 53 protected TestFrame f = null; 54 55 class TestFrame extends Frame { 56 57 public TestFrame( int width, int height ) throws Exception { 58 59 // Do a bunch of stuff to get us into Fulllscreen 60 DisplayMode newDisplayMode = new DisplayMode( width, height, oldDisplayMode.getBitDepth(), oldDisplayMode.getRefreshRate() ); 61 //System.out.println("setting display mode: " + newDisplayMode.getWidth() + " " + newDisplayMode.getHeight() + " " + newDisplayMode.getBitDepth() + " " + newDisplayMode.getRefreshRate() ); 62 setUndecorated( true ); 63 device.setFullScreenWindow( this ); 64 device.setDisplayMode( newDisplayMode ); 65 createBufferStrategy( 2 ); 66 strategy = getBufferStrategy(); 67 68 // Add in the listeners 69 addKeyListener( new KeyAdapter() { 70 public void keyPressed( KeyEvent e ) { 71 doEvent(); 72 } 73 } ); 74 addMouseListener( new MouseAdapter() { 75 public void mousePressed( MouseEvent e ) { 76 doEvent(); 77 } 78 } ); 79 } 80 81 // events will get us out of the loop below 82 synchronized void doEvent() { 83 eventcount++; 84 notify(); 85 } 86 87 // smart loop waiting for events 88 synchronized public boolean loopForEvents( int total ) throws Exception { 89 long endtime = System.currentTimeMillis() + TIMEOUT; 90 int count = 0; 91 while (eventcount < total) { 92 if (System.currentTimeMillis() < endtime) { 93 Graphics g = getGraphicsContext(); 94 g.setColor( Color.black ); 95 g.fillRect( 0, 0, oldDisplayMode.getWidth(), oldDisplayMode.getHeight() ); 96 g.setColor( Color.white ); 97 g.drawString( (count++) + " Waiting for the Robot to press any key or click mouse...", 100, 100 ); 98 doDisplay(); 99 wait( TIMEOUT / 100 ); 100 } 101 else { 102 break; 103 } 104 } 105 return (eventcount == total); 106 } 107 108 // display logic 109 public Graphics getGraphicsContext() { 110 if (currentGraphics == null) { 111 currentGraphics = strategy.getDrawGraphics(); 112 } 113 return currentGraphics; 114 } 115 116 // more display logic 117 public void doDisplay() throws Exception { 118 currentGraphics.dispose(); 119 currentGraphics = null; 120 strategy.show(); 121 } 122 123 // important cleanup -- this gets us back out of fullscreen 124 public void dispose() { 125 if (oldDisplayMode != null) { 126 //System.out.println("re-setting display mode: " + oldDisplayMode.getWidth() + " " + oldDisplayMode.getHeight() + " " + oldDisplayMode.getBitDepth() + " " + oldDisplayMode.getRefreshRate() ); 127 device.setDisplayMode( oldDisplayMode ); 128 device.setFullScreenWindow( null ); 129 } 130 super.dispose(); 131 } 132 } 133 134 protected void setUp() throws Exception { 135 ticktock = new Timer(); 136 f = new TestFrame( oldDisplayMode.getWidth(), oldDisplayMode.getHeight() ); 137 } 138 139 public void testFullScreenMouseEvent() throws Exception { 140 141 // In a few seconds, click... 142 TimerTask action = new TimerTask() { 143 public void run() { 144 RobotUtilities.clickAt( f, 100, 100 ); 145 } 146 }; 147 ticktock.schedule( action, 2500L ); 148 149 // Meanwhile, wait for the event 150 boolean gotEvent = f.loopForEvents( 1 ); 151 assertTrue( "We should have gotten a mouse click", gotEvent ); 152 } 153 154 public void testFullScreenKeyEvent() throws Exception { 155 156 // In a few seconds, hit the space bar 157 TimerTask action = new TimerTask() { 158 public void run() { 159 RobotUtilities.typeKey( KeyEvent.VK_SPACE ); 160 } 161 }; 162 ticktock.schedule( action, 2500L ); 163 164 // Meanwhile, wait for the event 165 boolean gotEvent = f.loopForEvents( 1 ); 166 assertTrue( "We should have gotten a keyboard space bar pressed", gotEvent ); 167 } 168 169 protected void tearDown() throws Exception { 170 f.dispose(); 171 ticktock.cancel(); 172 } 173 174 public static Test suite() { 175 return new TestSuite( TestFullScreenEvents.class); 176 } 177 178 public static void main (String[] args) throws RuntimeException { 179 TestResult tr = junit.textui.TestRunner.run(suite()); 180 if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 181 throw new RuntimeException("### FAILED: unexpected JUnit errors or failures."); 182 } 183 } 184 }