29package com.jogamp.opengl.test.junit.newt.event;
31import java.lang.reflect.*;
32import java.util.HashSet;
33import java.util.Iterator;
36import org.junit.Assert;
37import org.junit.BeforeClass;
39import org.junit.FixMethodOrder;
40import org.junit.runners.MethodSorters;
42import java.awt.AWTException;
43import java.awt.AWTKeyStroke;
44import java.awt.BorderLayout;
45import java.awt.Button;
46import java.awt.Dimension;
48import java.awt.KeyboardFocusManager;
51import com.jogamp.opengl.*;
53import com.jogamp.opengl.util.Animator;
54import com.jogamp.newt.*;
55import com.jogamp.newt.opengl.*;
56import com.jogamp.newt.opengl.util.NEWTDemoListener;
57import com.jogamp.newt.awt.NewtCanvasAWT;
58import com.jogamp.newt.event.KeyAdapter;
59import com.jogamp.newt.event.KeyEvent;
61import java.io.IOException;
63import jogamp.newt.driver.DriverClearFocus;
65import com.jogamp.opengl.test.junit.util.*;
66import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
67import com.jogamp.opengl.test.junit.newt.parenting.NewtAWTReparentingKeyAdapter;
68import com.jogamp.opengl.test.junit.newt.parenting.NewtReparentingKeyAdapter;
82@FixMethodOrder(MethodSorters.NAME_ASCENDING)
84 static Dimension glSize, fSize;
85 static int numFocus = 8;
86 static long durationPerTest = numFocus * 200;
88 static boolean manual =
false;
89 static boolean forceGL3 =
false;
93 glSize =
new Dimension(200,200);
94 fSize =
new Dimension(300,300);
100 testWindowParentingAWTFocusTraversal(
true);
105 testWindowParentingAWTFocusTraversal(
false);
109 final Robot robot =
new Robot();
114 final KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
115 final Set<AWTKeyStroke> bwdKeys = kfm.getDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
116 final AWTKeyStroke newBack = AWTKeyStroke.getAWTKeyStroke(java.awt.event.KeyEvent.VK_BACK_SPACE, 0,
false);
117 Assert.assertNotNull(newBack);
118 final Set<AWTKeyStroke> bwdKeys2 =
new HashSet<AWTKeyStroke>(bwdKeys);
119 bwdKeys2.add(newBack);
120 kfm.setDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, bwdKeys2);
123 final KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
124 final Set<AWTKeyStroke> fwdKeys = kfm.getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
125 final Set<AWTKeyStroke> bwdKeys = kfm.getDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
126 Iterator<AWTKeyStroke> iter;
127 for(iter = fwdKeys.iterator(); iter.hasNext(); ) {
128 System.err.println(
"FTKL.fwd-keys: "+iter.next());
130 for(iter = bwdKeys.iterator(); iter.hasNext(); ) {
131 System.err.println(
"FTKL.bwd-keys: "+iter.next());
135 final Frame frame1 =
new Frame(
"AWT Parent Frame");
136 final Button cWest =
new Button(
"WEST");
137 final Button cEast =
new Button(
"EAST");
141 newtCanvasAWT1.setPreferredSize(glSize);
143 newtCanvasAWT1.setFocusable(
true);
149 cWest.addFocusListener(bWestFA);
151 cEast.addFocusListener(bEastFA);
157 cWest.addKeyListener(bWestKA);
159 cEast.addKeyListener(bEastKA);
163 setDemoFields(demo1, glWindow1,
false);
167 public void keyReleased(
final KeyEvent e) {
172 System.err.println(
"Focus Clear");
177 System.err.println(
"Focus East");
179 java.awt.EventQueue.invokeLater(
new Runnable() {
181 cEast.requestFocusInWindow();
184 }
catch (
final Exception ex) { ex.printStackTrace(); }
186 System.err.println(
"Focus West");
188 java.awt.EventQueue.invokeLater(
new Runnable() {
190 cWest.requestFocusInWindow();
193 }
catch (
final Exception ex) { ex.printStackTrace(); }
201 frame1.setLayout(
new BorderLayout());
202 frame1.setLayout(
new BorderLayout());
203 frame1.add(cWest, BorderLayout.WEST);
204 frame1.add(newtCanvasAWT1, BorderLayout.CENTER);
205 frame1.add(cEast, BorderLayout.EAST);
207 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
209 frame1.setLocation(0, 0);
210 frame1.setSize(fSize);
212 frame1.setVisible(
true);
220 Assert.assertEquals(
true, animator1.
isAnimating());
222 Assert.assertNotNull(animator1.
getThread());
225 Thread.sleep(durationPerTest);
231 Thread.sleep(durationPerTest/numFocus);
240 Assert.assertEquals(
true, glWindow1FA.
focusGained());
241 Assert.assertEquals(
true, bWestFA.
focusLost());
242 Thread.sleep(durationPerTest/numFocus);
248 Assert.assertEquals(
true, glWindow1FA.
focusLost());
249 Thread.sleep(durationPerTest/numFocus);
257 Assert.assertEquals(
true, glWindow1FA.
focusGained());
258 Assert.assertEquals(
true, bEastFA.
focusLost());
259 Thread.sleep(durationPerTest/numFocus);
264 Assert.assertEquals(
true, glWindow1FA.
focusLost());
265 Thread.sleep(durationPerTest/numFocus);
267 System.err.println(
"Test: Direct NewtCanvasAWT focus");
269 java.awt.EventQueue.invokeAndWait(
new Runnable() {
271 newtCanvasAWT1.requestFocus();
274 }
catch (
final Exception ex) { ex.printStackTrace(); }
276 Assert.assertEquals(
true, glWindow1FA.
focusGained());
277 Assert.assertEquals(
true, bWestFA.
focusLost());
278 Thread.sleep(durationPerTest/numFocus);
280 System.err.println(
"Test: Direct AWT Button-West focus");
282 java.awt.EventQueue.invokeAndWait(
new Runnable() {
284 cWest.requestFocus();
287 }
catch (
final Exception ex) { ex.printStackTrace(); }
290 Assert.assertEquals(
true, glWindow1FA.
focusLost());
291 Thread.sleep(durationPerTest/numFocus);
293 System.err.println(
"Test: Direct NEWT-Child request focus");
299 System.err.println(
"glWindow hasFocus "+glWindow1.
hasFocus());
300 System.err.println(
"glWindow1FA "+glWindow1FA);
301 System.err.println(
"bWestFA "+bWestFA);
302 Assert.assertTrue(
"Did not gain focus", ok);
304 Assert.assertEquals(
true, glWindow1FA.
focusGained());
305 Assert.assertEquals(
true, bWestFA.
focusLost());
306 Thread.sleep(durationPerTest/numFocus);
310 Assert.assertEquals(
false, animator1.
isAnimating());
311 Assert.assertEquals(
false, animator1.
isPaused());
312 Assert.assertEquals(
null, animator1.
getThread());
314 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
322 Assert.assertNotNull(demo);
323 Assert.assertNotNull(glWindow);
334 static int atoi(
final String a) {
337 i = Integer.parseInt(a);
338 }
catch (
final Exception ex) { ex.printStackTrace(); }
342 public static void main(
final String args[])
throws IOException {
343 for(
int i=0; i<args.length; i++) {
344 if(args[i].equals(
"-time")) {
345 durationPerTest = atoi(args[++i]);
346 }
else if(args[i].equals(
"-manual")) {
348 }
else if(args[i].equals(
"-gl3")) {
365 org.junit.runner.JUnitCore.
main(tstname);
AWT Canvas containing a NEWT Window using native parenting.
NativeWindow getNativeWindow()
Returns the associated NativeWindow of this NativeWindowHolder, which is identical to getNativeSurfac...
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
final char getKeyChar()
Returns the UTF-16 character reflecting the key symbol incl.
static boolean isPrintableKey(final short uniChar, final boolean isKeyChar)
Returns true if given uniChar represents a printable character, i.e.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
final NativeWindow getParent()
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
boolean hasFocus()
Returns true if this native window owns the focus, otherwise false.
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
final Window getDelegatedWindow()
If the implementation uses delegation, return the delegated Window instance, otherwise return this in...
final void requestFocus()
Request focus for this native window.
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
static final String GL3
The desktop OpenGL core profile 3.x, with x >= 1.
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
Testing focus key traversal of an AWT component tree with NewtCanvasAWT attached.
void testWindowParentingAWTFocusTraversal01Onscreen()
static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug)
void testWindowParentingAWTFocusTraversal02Offscreen()
static void main(final String args[])
void testWindowParentingAWTFocusTraversal(final boolean onscreen)
AWT specializing demo functionality of NewtReparentingKeyAdapter, includes NEWTDemoListener.
static int keyType(final int i, final Robot robot, final int keyCode, final Object obj, final KeyEventCountAdapter counter)
static boolean waitForFocus(final java.awt.Component comp, final Runnable waitAction)
static boolean toFrontAndRequestFocus(Robot robot, final java.awt.Window window)
toFront, call setVisible(true) and toFront(), after positioning the mouse in the middle of the window...
static void clearAWTFocus(Robot robot)
static void assertRequestFocusAndWait(final Robot robot, final Object requestFocus, final Object waitForFocus, final FocusEventCountAdapter gain, final FocusEventCountAdapter lost)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
static boolean waitForFocus(final Window win, final Runnable waitAction)
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction)
Specifying NEWT's Window functionality:
void setUpdateFPSFrames(int frames, PrintStream out)
An animator control interface, which implementation may drive a com.jogamp.opengl....
boolean start()
Starts this animator, if not running.
boolean isPaused()
Indicates whether this animator is started and either manually paused or paused automatically due to ...
boolean isAnimating()
Indicates whether this animator is started and is not paused.
boolean stop()
Stops this animator.
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.