JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestNewtKeyPressReleaseUnmaskRepeatAWT.java
Go to the documentation of this file.
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
29package com.jogamp.opengl.test.junit.newt.event;
30
31import org.junit.After;
32import org.junit.Assert;
33import org.junit.AfterClass;
34import org.junit.Assume;
35import org.junit.Before;
36
37import java.awt.AWTException;
38import java.awt.BorderLayout;
39import java.awt.Robot;
40import java.lang.reflect.InvocationTargetException;
41
42import com.jogamp.opengl.GLCapabilities;
43import com.jogamp.opengl.GLEventListener;
44import javax.swing.JFrame;
45
46import java.io.IOException;
47
48import jogamp.nativewindow.jawt.JAWTUtil;
49
50import org.junit.BeforeClass;
51import org.junit.Test;
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55import com.jogamp.newt.awt.NewtCanvasAWT;
56import com.jogamp.newt.event.InputEvent;
57import com.jogamp.newt.event.KeyEvent;
58import com.jogamp.newt.event.KeyListener;
59import com.jogamp.newt.opengl.GLWindow;
60import com.jogamp.opengl.util.Animator;
61import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
62
63import 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)
70 static int width, height;
71 static long durationPerTest = 100;
72 static long awtWaitTimeout = 1000;
73
74 static GLCapabilities glCaps;
75
76 @BeforeClass
77 public static void initClass() {
78 width = 640;
79 height = 480;
80 glCaps = new GLCapabilities(null);
81 }
82
83 @AfterClass
84 public static void release() {
85 }
86
87 @Before
88 public void initTest() {
89 }
90
91 @After
92 public void releaseTest() {
93 }
94
95 @Test(timeout=180000) // TO 3 min
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
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, null));
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
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
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
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, NewtTestUtil.waitForRealized(glWindow, true, null));
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
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
198 SimpleKeyPressRelease() {
199 reset();
200 }
201
202 public void reset() {
203 seq=0;
204 }
205
206 @Override
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
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
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}
AWT Canvas containing a NEWT Window using native parenting.
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
final int getModifiers()
Return the modifier bits of this event, e.g.
static final int AUTOREPEAT_MASK
Event is caused by auto-repeat.
Definition: InputEvent.java:71
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void removeKeyListener(final KeyListener l)
Definition: GLWindow.java:912
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
static void requestFocus(final Robot robot, final Object obj)
FIXME: AWTRobotUtil Cleanup: Use specific type for argument object.
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
static void assertRequestFocusAndWait(final Robot robot, final Object requestFocus, final Object waitForFocus, final FocusEventCountAdapter gain, final FocusEventCountAdapter lost)
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.