JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestParenting02AWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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.parenting;
30
31import org.junit.Assert;
32import org.junit.BeforeClass;
33import org.junit.Test;
34import org.junit.FixMethodOrder;
35import org.junit.runners.MethodSorters;
36
37import java.awt.Button;
38import java.awt.BorderLayout;
39import java.awt.Frame;
40
41import com.jogamp.opengl.*;
42
43import com.jogamp.newt.*;
44import com.jogamp.newt.event.*;
45import com.jogamp.newt.opengl.*;
46import com.jogamp.newt.awt.NewtCanvasAWT;
47
48import java.io.IOException;
49import java.lang.reflect.InvocationTargetException;
50
51import com.jogamp.opengl.test.junit.util.*;
52import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
53
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55public class TestParenting02AWT extends UITestCase {
56 static int width, height;
57 static long durationPerTest = 500;
58 static long waitReparent = 300;
59 static boolean verbose = false;
60
61 @BeforeClass
62 public static void initClass() {
63 width = 640;
64 height = 480;
65 }
66
67 @Test
68 public void test01NewtChildOnAWTParentLayouted() throws InterruptedException, InvocationTargetException {
69 runNewtChildOnAWTParent(true, false);
70 }
71
72 @Test
73 public void test02NewtChildOnAWTParentLayoutedDef() throws InterruptedException, InvocationTargetException {
74 runNewtChildOnAWTParent(true, true);
75 }
76
77 @Test
78 public void test03NewtChildOnAWTParentDirect() throws InterruptedException, InvocationTargetException {
79 runNewtChildOnAWTParent(false, false);
80 }
81
82 @Test
83 public void test04NewtChildOnAWTParentDirectDef() throws InterruptedException, InvocationTargetException {
84 runNewtChildOnAWTParent(false, true);
85 }
86
87 public void runNewtChildOnAWTParent(final boolean useLayout, final boolean deferredPeer) throws InterruptedException, InvocationTargetException {
88 final NEWTEventFiFo eventFifo = new NEWTEventFiFo();
89
90 // setup NEWT GLWindow ..
91 final GLWindow glWindow = GLWindow.create(new GLCapabilities(null));
92 Assert.assertNotNull(glWindow);
93 glWindow.setTitle("NEWT - CHILD");
94 glWindow.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo)));
95 glWindow.addWindowListener(new TraceWindowAdapter(new WindowAction(eventFifo)));
96 final GLEventListener demo = new GearsES2();
97 setDemoFields(demo, glWindow, false);
98 glWindow.addGLEventListener(demo);
99
100 // attach NEWT GLWindow to AWT Canvas
101 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow);
102 Assert.assertNotNull(newtCanvasAWT);
103 Assert.assertEquals(false, glWindow.isVisible());
104 Assert.assertEquals(false, glWindow.isNativeValid());
105 Assert.assertNull(glWindow.getParent());
106
107 final Frame frame = new Frame("AWT Parent Frame");
108 Assert.assertNotNull(frame);
109 if(useLayout) {
110 frame.setLayout(new BorderLayout());
111 frame.add(new Button("North"), BorderLayout.NORTH);
112 frame.add(new Button("South"), BorderLayout.SOUTH);
113 frame.add(new Button("East"), BorderLayout.EAST);
114 frame.add(new Button("West"), BorderLayout.WEST);
115 if(!deferredPeer) {
116 frame.add(newtCanvasAWT, BorderLayout.CENTER);
117 }
118 } else {
119 if(!deferredPeer) {
120 frame.add(newtCanvasAWT);
121 }
122 }
123
124 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
125 public void run() {
126 // frame.setSize(width, height);
127 frame.setBounds(100, 100, width, height);
128 frame.setVisible(true);
129 }});
130 // X11: true, Windows: false - Assert.assertEquals(true, glWindow.isVisible());
131
132 if(deferredPeer) {
133 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
134 public void run() {
135 if(useLayout) {
136 frame.add(newtCanvasAWT, BorderLayout.CENTER);
137 } else {
138 frame.add(newtCanvasAWT);
139 }
140 frame.validate();
141 }});
142 }
143
144 // Since it is not defined when AWT's addNotify call happen
145 // we just have to wait for it in this junit test
146 // because we have assertions on the state.
147 // Regular application shall not need to do that.
148 do {
149 Thread.yield();
150 // 1st display .. creation
151 glWindow.display();
152 } while(!glWindow.isNativeValid()) ;
153
154 final boolean wasOnscreen = glWindow.getChosenCapabilities().isOnscreen();
155
156 Assert.assertEquals(true, glWindow.isNativeValid());
157 Assert.assertNotNull(glWindow.getParent());
158 if(verbose) {
159 System.out.println("+++++++++++++++++++ 1st ADDED");
160 }
161 Thread.sleep(waitReparent);
162
163 if(useLayout) {
164 // test some fancy re-layout ..
165 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
166 public void run() {
167 frame.remove(newtCanvasAWT);
168 frame.validate();
169 }});
170 Assert.assertEquals(false, glWindow.isVisible());
171 if( wasOnscreen ) {
172 Assert.assertEquals(true, glWindow.isNativeValid());
173 } // else OK to be destroyed - due to offscreen/onscreen transition
174 Assert.assertNull(glWindow.getParent());
175 if(verbose) {
176 System.out.println("+++++++++++++++++++ REMOVED!");
177 }
178 Thread.sleep(waitReparent);
179
180 // should recreate properly ..
181 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
182 public void run() {
183 frame.add(newtCanvasAWT, BorderLayout.CENTER);
184 frame.validate();
185 }});
186 glWindow.display();
187 Assert.assertEquals(true, glWindow.isVisible());
188 Assert.assertEquals(true, glWindow.isNativeValid());
189 Assert.assertNotNull(glWindow.getParent());
190 if(verbose) {
191 System.out.println("+++++++++++++++++++ 2nd ADDED");
192 }
193 Thread.sleep(waitReparent);
194 }
195
196 long duration = durationPerTest;
197 final long step = 20;
198 NEWTEvent event;
199 boolean shouldQuit = false;
200
201 while (duration>0 && !shouldQuit) {
202 glWindow.display();
203 Thread.sleep(step);
204 duration -= step;
205
206 while( null != ( event = eventFifo.get() ) ) {
207 final Window source = (Window) event.getSource();
208 if(event instanceof KeyEvent) {
209 final KeyEvent keyEvent = (KeyEvent) event;
210 switch(keyEvent.getKeyChar()) {
211 case 'q':
212 shouldQuit = true;
213 break;
214 case 'f':
215 source.setFullscreen(!source.isFullscreen());
216 break;
217 }
218 }
219 }
220 }
221 if(verbose) {
222 System.out.println("+++++++++++++++++++ END");
223 }
224 Thread.sleep(waitReparent);
225
226 glWindow.destroy();
227 if(useLayout) {
228 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
229 public void run() {
230 frame.remove(newtCanvasAWT);
231 frame.validate();
232 }});
233 }
234 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
235 public void run() {
236 frame.dispose();
237 } } );
238 }
239
240 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
241 Assert.assertNotNull(demo);
242 Assert.assertNotNull(glWindow);
243 final Window window = glWindow.getDelegatedWindow();
244 if(debug) {
245 MiscUtils.setFieldIfExists(demo, "glDebug", true);
246 MiscUtils.setFieldIfExists(demo, "glTrace", true);
247 }
248 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
249 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
250 }
251 }
252
253 static int atoi(final String a) {
254 int i=0;
255 try {
256 i = Integer.parseInt(a);
257 } catch (final Exception ex) { ex.printStackTrace(); }
258 return i;
259 }
260
261 public static void main(final String args[]) throws IOException {
262 verbose = true;
263 for(int i=0; i<args.length; i++) {
264 if(args[i].equals("-time")) {
265 durationPerTest = atoi(args[++i]);
266 } else if(args[i].equals("-wait")) {
267 waitReparent = atoi(args[++i]);
268 }
269 }
270 final String tstname = TestParenting02AWT.class.getName();
271 org.junit.runner.JUnitCore.main(tstname);
272 /*
273 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
274 tstname,
275 "filtertrace=true",
276 "haltOnError=false",
277 "haltOnFailure=false",
278 "showoutput=true",
279 "outputtoformatters=true",
280 "logfailedtests=true",
281 "logtestlistenerevents=true",
282 "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
283 "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); */
284 }
285
286}
AWT Canvas containing a NEWT Window using native parenting.
final char getKeyChar()
Returns the UTF-16 character reflecting the key symbol incl.
Definition: KeyEvent.java:161
synchronized NEWTEvent get()
Remove NEWTEvent from head.
NEWT events are provided for notification purposes ONLY; The NEWT will automatically handle the even...
Definition: NEWTEvent.java:52
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final NativeWindow getParent()
Definition: GLWindow.java:282
final void setTitle(final String title)
Definition: GLWindow.java:297
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 addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
final CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
Definition: GLWindow.java:266
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final Window getDelegatedWindow()
If the implementation uses delegation, return the delegated Window instance, otherwise return this in...
Definition: GLWindow.java:277
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.
void runNewtChildOnAWTParent(final boolean useLayout, final boolean deferredPeer)
static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:193
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.
Specifying NEWT's Window functionality:
Definition: Window.java:115
boolean setFullscreen(boolean fullscreen)
Enable or disable fullscreen mode for this window.
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.