JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestNewtKeyEventOrderAWT.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.opengl.GLWindow;
57import com.jogamp.opengl.util.Animator;
58import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
59
60import com.jogamp.opengl.test.junit.util.*;
61
62/**
63 * Testing key event order excl. auto-repeat (Bug 601)
64 *
65 * <p>
66 * Note Event order:
67 * <ol>
68 * <li>{@link #EVENT_KEY_PRESSED}</li>
69 * <li>{@link #EVENT_KEY_RELEASED}</li>
70 * </ol>
71 * </p>
72 */
73@FixMethodOrder(MethodSorters.NAME_ASCENDING)
75 static int width, height;
76 static long durationPerTest = 100;
77 static long awtWaitTimeout = 1000;
78
79 static GLCapabilities glCaps;
80
81 @BeforeClass
82 public static void initClass() {
83 width = 640;
84 height = 480;
85 glCaps = new GLCapabilities(null);
86 }
87
88 @AfterClass
89 public static void release() {
90 }
91
92 @Before
93 public void initTest() {
94 }
95
96 @After
97 public void releaseTest() {
98 }
99
100 @Test(timeout=180000) // TO 3 min
101 public void test01NEWT() throws AWTException, InterruptedException, InvocationTargetException {
102 final GLWindow glWindow = GLWindow.create(glCaps);
103 glWindow.setSize(width, height);
104 glWindow.setVisible(true);
105
106 testImpl(glWindow);
107
108 glWindow.destroy();
109 }
110
111 private void testNewtCanvasAWT_Impl(final boolean onscreen) throws AWTException, InterruptedException, InvocationTargetException {
112 final GLWindow glWindow = GLWindow.create(glCaps);
113
114 // Wrap the window in a canvas.
115 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow);
116 if( !onscreen ) {
117 newtCanvasAWT.setShallUseOffscreenLayer(true);
118 }
119
120 // Add the canvas to a frame, and make it all visible.
121 final JFrame frame1 = new JFrame("Swing AWT Parent Frame: "+ glWindow.getTitle());
122 frame1.getContentPane().add(newtCanvasAWT, BorderLayout.CENTER);
123 frame1.setSize(width, height);
124 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
125 public void run() {
126 frame1.setVisible(true);
127 } } );
128
129 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame1, true, null));
130
131 testImpl(glWindow);
132
133 try {
134 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
135 public void run() {
136 frame1.setVisible(false);
137 frame1.dispose();
138 }});
139 } catch( final Throwable throwable ) {
140 throwable.printStackTrace();
141 Assume.assumeNoException( throwable );
142 }
143 glWindow.destroy();
144 }
145
146 @Test(timeout=180000) // TO 3 min
147 public void test02NewtCanvasAWT_Onscreen() throws AWTException, InterruptedException, InvocationTargetException {
148 if( JAWTUtil.isOffscreenLayerRequired() ) {
149 System.err.println("Platform doesn't support onscreen rendering.");
150 return;
151 }
152 testNewtCanvasAWT_Impl(true);
153 }
154
155 @Test(timeout=180000) // TO 3 min
156 public void test03NewtCanvasAWT_Offsccreen() throws AWTException, InterruptedException, InvocationTargetException {
157 if( !JAWTUtil.isOffscreenLayerSupported() ) {
158 System.err.println("Platform doesn't support offscreen rendering.");
159 return;
160 }
161 testNewtCanvasAWT_Impl(false);
162 }
163
164 static void testKeyEventOrder(final Robot robot, final NEWTKeyAdapter keyAdapter, final int loops) {
165 System.err.println("KEY Event Order Test: "+loops);
166 keyAdapter.reset();
167 for(int i=0; i<loops; i++) {
168 // 1
170 AWTRobotUtil.keyPress(0, robot, true, java.awt.event.KeyEvent.VK_A, 10);
171 AWTRobotUtil.keyPress(0, robot, false, java.awt.event.KeyEvent.VK_A, 100);
172 // 2
174 AWTRobotUtil.keyPress(0, robot, true, java.awt.event.KeyEvent.VK_B, 10);
175 AWTRobotUtil.keyPress(0, robot, false, java.awt.event.KeyEvent.VK_B, 100);
176 // 3 + 4
178 AWTRobotUtil.keyPress(0, robot, true, java.awt.event.KeyEvent.VK_A, 10);
179 AWTRobotUtil.keyPress(0, robot, true, java.awt.event.KeyEvent.VK_B, 10);
180 AWTRobotUtil.keyPress(0, robot, false, java.awt.event.KeyEvent.VK_A, 10);
181 AWTRobotUtil.keyPress(0, robot, false, java.awt.event.KeyEvent.VK_B, 10);
182 // 5 + 6
184 AWTRobotUtil.keyPress(0, robot, true, java.awt.event.KeyEvent.VK_A, 10);
185 AWTRobotUtil.keyPress(0, robot, true, java.awt.event.KeyEvent.VK_B, 10);
186 AWTRobotUtil.keyPress(0, robot, false, java.awt.event.KeyEvent.VK_B, 10);
187 AWTRobotUtil.keyPress(0, robot, false, java.awt.event.KeyEvent.VK_A, 10);
188 }
190 robot.delay(250);
191 // dumpKeyEvents(keyAdapter.getQueued());
192
194
195 final int expTotal = 6*loops; // all typed events
197 expTotal /* press-SI */, expTotal /* release-SI */,
198 0 /* press-AR */, 0 /* release-AR */ );
199
200 }
201
202 void testImpl(final GLWindow glWindow) throws AWTException, InterruptedException, InvocationTargetException {
203 final Robot robot = new Robot();
204 robot.setAutoWaitForIdle(true);
205
206 final GLEventListener demo1 = new RedSquareES2();
207 glWindow.addGLEventListener(demo1);
208
209 final NEWTKeyAdapter glWindow1KA = new NEWTKeyAdapter("GLWindow1");
210 glWindow1KA.setVerbose(false);
211 glWindow.addKeyListener(glWindow1KA);
212
213 Assert.assertEquals(true, NewtTestUtil.waitForRealized(glWindow, true, null));
214
215 // Continuous animation ..
216 final Animator animator = new Animator(glWindow);
217 animator.start();
218
219 Thread.sleep(durationPerTest); // manual testing
220
221 AWTRobotUtil.assertRequestFocusAndWait(null, glWindow, glWindow, null, null); // programmatic
222 AWTRobotUtil.requestFocus(robot, glWindow, false); // within unit framework, prev. tests (TestFocus02SwingAWTRobot) 'confuses' Windows keyboard input
223 glWindow1KA.reset();
224
225 //
226 // Test the key event order w/o auto-repeat
227 //
228 testKeyEventOrder(robot, glWindow1KA, 6);
229
230 // Remove listeners to avoid logging during dispose/destroy.
231 glWindow.removeKeyListener(glWindow1KA);
232
233 // Shutdown the test.
234 animator.stop();
235 }
236
237 static int atoi(final String a) {
238 int i=0;
239 try {
240 i = Integer.parseInt(a);
241 } catch (final Exception ex) { ex.printStackTrace(); }
242 return i;
243 }
244
245 public static void main(final String args[]) throws IOException {
246 for(int i=0; i<args.length; i++) {
247 if(args[i].equals("-time")) {
248 durationPerTest = atoi(args[++i]);
249 }
250 }
251 /**
252 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
253 System.err.println("Press enter to continue");
254 System.err.println(stdin.readLine());
255 */
256 System.out.println("durationPerTest: "+durationPerTest);
257 final String tstname = TestNewtKeyEventOrderAWT.class.getName();
258 org.junit.runner.JUnitCore.main(tstname);
259 }
260
261
262}
AWT Canvas containing a NEWT Window using native parenting.
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
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 int keyPress(final int i, final Robot robot, final boolean press, final int keyCode, final int msDelay)
No validation is performed .
static void assertRequestFocusAndWait(final Robot robot, final Object requestFocus, final Object waitForFocus, final FocusEventCountAdapter gain, final FocusEventCountAdapter lost)
static void waitForIdle(final Robot robot)
Issuing validateAWTEDTIsAlive() before calling Robot#waitForIdle().
synchronized List< EventObject > copyQueue()
synchronized void setVerbose(final boolean v)
Instance starts in verbose mode, call w/ false to disable verbosity.
static void validateKeyEventOrder(final List< EventObject > keyEvents)
static void validateKeyAdapterStats(final NEWTKeyAdapter keyAdapter, final int expPressedCountSI, final int expReleasedCountSI, final int expPressedCountAR, final int expReleasedCountAR)
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
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.