1 /** 2 * Copyright 2012 JogAmp Community. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without modification, are 5 * permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this list of 8 * conditions and the following disclaimer. 9 * 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * 24 * The views and conclusions contained in the software and documentation are those of the 25 * authors and should not be interpreted as representing official policies, either expressed 26 * or implied, of JogAmp Community. 27 */ 28 29 package com.jogamp.opengl.test.junit.newt.event; 30 31 import org.junit.After; 32 import org.junit.Assert; 33 import org.junit.AfterClass; 34 import org.junit.Assume; 35 import org.junit.Before; 36 37 import java.awt.AWTException; 38 import java.awt.BorderLayout; 39 import java.awt.Robot; 40 import java.lang.reflect.InvocationTargetException; 41 42 import com.jogamp.opengl.GLCapabilities; 43 import com.jogamp.opengl.GLEventListener; 44 import javax.swing.JFrame; 45 46 import java.io.IOException; 47 48 import jogamp.nativewindow.jawt.JAWTUtil; 49 50 import org.junit.BeforeClass; 51 import org.junit.Test; 52 import org.junit.FixMethodOrder; 53 import org.junit.runners.MethodSorters; 54 55 import com.jogamp.newt.awt.NewtCanvasAWT; 56 import com.jogamp.newt.event.InputEvent; 57 import com.jogamp.newt.event.KeyEvent; 58 import com.jogamp.newt.event.KeyListener; 59 import com.jogamp.newt.opengl.GLWindow; 60 import com.jogamp.opengl.util.Animator; 61 import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2; 62 63 import com.jogamp.opengl.test.junit.util.*; 64 65 /** 66 * Testing key press and release events w/o AUTO-REPEAT 67 */ 68 @FixMethodOrder(MethodSorters.NAME_ASCENDING) 69 public class TestNewtKeyPressReleaseUnmaskRepeatAWT extends UITestCase { 70 static int width, height; 71 static long durationPerTest = 100; 72 static long awtWaitTimeout = 1000; 73 74 static GLCapabilities glCaps; 75 76 @BeforeClass initClass()77 public static void initClass() { 78 width = 640; 79 height = 480; 80 glCaps = new GLCapabilities(null); 81 } 82 83 @AfterClass release()84 public static void release() { 85 } 86 87 @Before initTest()88 public void initTest() { 89 } 90 91 @After releaseTest()92 public void releaseTest() { 93 } 94 95 @Test(timeout=180000) // TO 3 min test01NEWT()96 public void test01NEWT() throws AWTException, InterruptedException, InvocationTargetException { 97 final GLWindow glWindow = GLWindow.create(glCaps); 98 glWindow.setSize(width, height); 99 glWindow.setVisible(true); 100 101 testImpl(glWindow); 102 103 glWindow.destroy(); 104 } 105 testNewtCanvasAWT_Impl(final boolean onscreen)106 private void testNewtCanvasAWT_Impl(final boolean onscreen) throws AWTException, InterruptedException, InvocationTargetException { 107 final GLWindow glWindow = GLWindow.create(glCaps); 108 109 // Wrap the window in a canvas. 110 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow); 111 if( !onscreen ) { 112 newtCanvasAWT.setShallUseOffscreenLayer(true); 113 } 114 115 // Add the canvas to a frame, and make it all visible. 116 final JFrame frame1 = new JFrame("Swing AWT Parent Frame: "+ glWindow.getTitle()); 117 frame1.getContentPane().add(newtCanvasAWT, BorderLayout.CENTER); 118 frame1.setSize(width, height); 119 javax.swing.SwingUtilities.invokeAndWait(new Runnable() { 120 public void run() { 121 frame1.setVisible(true); 122 } } ); 123 124 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame1, true)); 125 126 testImpl(glWindow); 127 128 try { 129 javax.swing.SwingUtilities.invokeAndWait(new Runnable() { 130 public void run() { 131 frame1.setVisible(false); 132 frame1.dispose(); 133 }}); 134 } catch( final Throwable throwable ) { 135 throwable.printStackTrace(); 136 Assume.assumeNoException( throwable ); 137 } 138 glWindow.destroy(); 139 } 140 141 @Test(timeout=180000) // TO 3 min test02NewtCanvasAWT_Onscreen()142 public void test02NewtCanvasAWT_Onscreen() throws AWTException, InterruptedException, InvocationTargetException { 143 if( JAWTUtil.isOffscreenLayerRequired() ) { 144 System.err.println("Platform doesn't support onscreen rendering."); 145 return; 146 } 147 testNewtCanvasAWT_Impl(true); 148 } 149 150 @Test(timeout=180000) // TO 3 min test03NewtCanvasAWT_Offsccreen()151 public void test03NewtCanvasAWT_Offsccreen() throws AWTException, InterruptedException, InvocationTargetException { 152 if( !JAWTUtil.isOffscreenLayerSupported() ) { 153 System.err.println("Platform doesn't support offscreen rendering."); 154 return; 155 } 156 testNewtCanvasAWT_Impl(false); 157 } 158 testImpl(final GLWindow glWindow)159 void testImpl(final GLWindow glWindow) throws AWTException, InterruptedException, InvocationTargetException { 160 final Robot robot = new Robot(); 161 robot.setAutoWaitForIdle(true); 162 163 final GLEventListener demo1 = new RedSquareES2(); 164 glWindow.addGLEventListener(demo1); 165 166 final SimpleKeyPressRelease simpleKeyPressRelease = new SimpleKeyPressRelease(); 167 glWindow.addKeyListener(simpleKeyPressRelease); 168 169 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glWindow, true)); 170 171 // Continuous animation .. 172 final Animator animator = new Animator(glWindow); 173 animator.start(); 174 175 Thread.sleep(durationPerTest); // manual testing 176 177 AWTRobotUtil.assertRequestFocusAndWait(null, glWindow, glWindow, null, null); // programmatic 178 AWTRobotUtil.requestFocus(robot, glWindow, false); // within unit framework, prev. tests (TestFocus02SwingAWTRobot) 'confuses' Windows keyboard input 179 180 // Remove listeners to avoid logging during dispose/destroy. 181 glWindow.removeKeyListener(simpleKeyPressRelease); 182 183 // Shutdown the test. 184 animator.stop(); 185 } 186 atoi(final String a)187 static int atoi(final String a) { 188 int i=0; 189 try { 190 i = Integer.parseInt(a); 191 } catch (final Exception ex) { ex.printStackTrace(); } 192 return i; 193 } 194 195 static class SimpleKeyPressRelease implements KeyListener { 196 int seq; 197 SimpleKeyPressRelease()198 SimpleKeyPressRelease() { 199 reset(); 200 } 201 reset()202 public void reset() { 203 seq=0; 204 } 205 206 @Override keyPressed(final KeyEvent e)207 public void keyPressed(final KeyEvent e) { 208 if( 0 == ( InputEvent.AUTOREPEAT_MASK & e.getModifiers() ) ) { 209 seq++; 210 System.err.println(seq+": "+e); 211 } 212 } 213 214 @Override keyReleased(final KeyEvent e)215 public void keyReleased(final KeyEvent e) { 216 if( 0 == ( InputEvent.AUTOREPEAT_MASK & e.getModifiers() ) ) { 217 seq++; 218 System.err.println(seq+": "+e); 219 } 220 } 221 } 222 main(final String args[])223 public static void main(final String args[]) throws IOException { 224 for(int i=0; i<args.length; i++) { 225 if(args[i].equals("-time")) { 226 durationPerTest = atoi(args[++i]); 227 } 228 } 229 /** 230 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 231 System.err.println("Press enter to continue"); 232 System.err.println(stdin.readLine()); 233 */ 234 System.out.println("durationPerTest: "+durationPerTest); 235 final String tstname = TestNewtKeyPressReleaseUnmaskRepeatAWT.class.getName(); 236 org.junit.runner.JUnitCore.main(tstname); 237 } 238 239 240 } 241