JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestParenting01aAWT.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.Container;
40import java.awt.Frame;
41
42import com.jogamp.opengl.*;
43import javax.swing.SwingUtilities;
44
45import com.jogamp.opengl.util.Animator;
46import com.jogamp.newt.opengl.*;
47import com.jogamp.newt.awt.NewtCanvasAWT;
48
49import java.io.IOException;
50import java.lang.reflect.InvocationTargetException;
51
52import com.jogamp.opengl.test.junit.util.*;
53import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
54
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56public class TestParenting01aAWT extends UITestCase {
57 static int width, height;
58 static long durationPerTest = 800;
59 static long waitReparent = 0;
60 static GLCapabilities glCaps;
61
62 @BeforeClass
63 public static void initClass() throws InterruptedException {
64 width = 640;
65 height = 480;
66 glCaps = new GLCapabilities(null);
67 // Thread.sleep(10000);
68 }
69
70 @Test
71 public void test01WindowParenting01CreateVisibleDestroy1() throws InterruptedException, InvocationTargetException {
72 final GLWindow glWindow1 = GLWindow.create(glCaps);
73 Assert.assertNotNull(glWindow1);
74 Assert.assertEquals(false, glWindow1.isVisible());
75 Assert.assertEquals(false, glWindow1.isNativeValid());
76 Assert.assertNull(glWindow1.getParent());
77 glWindow1.setTitle("testWindowParenting01CreateVisibleDestroy");
78 final GLEventListener demo1 = new RedSquareES2();
79 setDemoFields(demo1, glWindow1, false);
80 glWindow1.addGLEventListener(demo1);
81
82 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
83 Assert.assertNotNull(newtCanvasAWT);
84 Assert.assertEquals(false, glWindow1.isVisible());
85 Assert.assertEquals(false, glWindow1.isNativeValid());
86 Assert.assertNull(glWindow1.getParent());
87
88 final Frame frame1 = new Frame("AWT Parent Frame");
89 frame1.setLayout(new BorderLayout());
90 frame1.add(new Button("North"), BorderLayout.NORTH);
91 frame1.add(new Button("South"), BorderLayout.SOUTH);
92 frame1.add(new Button("East"), BorderLayout.EAST);
93 frame1.add(new Button("West"), BorderLayout.WEST);
94
95 final Container container1 = new Container();
96 container1.setLayout(new BorderLayout());
97 container1.add(new Button("north"), BorderLayout.NORTH);
98 container1.add(new Button("south"), BorderLayout.SOUTH);
99 container1.add(new Button("east"), BorderLayout.EAST);
100 container1.add(new Button("west"), BorderLayout.WEST);
101 container1.add(newtCanvasAWT, BorderLayout.CENTER);
102
103 frame1.add(container1, BorderLayout.CENTER);
104
105 // visible test
106 SwingUtilities.invokeAndWait(new Runnable() {
107 public void run() {
108 frame1.setSize(width, height);
109 frame1.setVisible(true);
110 }
111 });
112 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
113
114 final Animator animator1 = new Animator(glWindow1);
115 animator1.setUpdateFPSFrames(1, null);
116 animator1.start();
117 Assert.assertTrue(NewtTestUtil.waitForRealized(glWindow1, true, null));
118 Assert.assertTrue(NewtTestUtil.waitForVisible(glWindow1, true, null));
119
120 while(animator1.isAnimating() && animator1.getTotalFPSDuration()<durationPerTest) {
121 Thread.sleep(100);
122 }
123 animator1.stop();
124 Assert.assertEquals(false, animator1.isAnimating());
125
126 SwingUtilities.invokeAndWait(new Runnable() {
127 public void run() {
128 frame1.setVisible(false);
129 } } );
130 Assert.assertEquals(true, glWindow1.isNativeValid());
131
132 SwingUtilities.invokeAndWait(new Runnable() {
133 public void run() {
134 frame1.setVisible(true);
135 } } );
136 Assert.assertEquals(true, glWindow1.isNativeValid());
137
138 final boolean wasOnscreen = glWindow1.getChosenCapabilities().isOnscreen();
139
140 SwingUtilities.invokeAndWait(new Runnable() {
141 public void run() {
142 frame1.remove(newtCanvasAWT);
143 } } );
144 // Assert.assertNull(glWindow1.getParent());
145 if( wasOnscreen ) {
146 Assert.assertEquals(true, glWindow1.isNativeValid());
147 } // else OK to be destroyed - due to offscreen/onscreen transition
148
149 SwingUtilities.invokeAndWait(new Runnable() {
150 public void run() {
151 frame1.dispose();
152 } } );
153 if( wasOnscreen ) {
154 Assert.assertEquals(true, glWindow1.isNativeValid());
155 } // else OK to be destroyed - due to offscreen/onscreen transition
156
157 glWindow1.destroy();
158 Assert.assertEquals(false, glWindow1.isNativeValid());
159 }
160
161 @Test
162 public void test02WindowParenting02CreateVisibleDestroy2Defered() throws InterruptedException, InvocationTargetException {
163 final GLWindow glWindow1 = GLWindow.create(glCaps);
164 Assert.assertNotNull(glWindow1);
165 Assert.assertEquals(false, glWindow1.isVisible());
166 Assert.assertEquals(false, glWindow1.isNativeValid());
167 Assert.assertNull(glWindow1.getParent());
168 final GLEventListener demo1 = new RedSquareES2();
169 setDemoFields(demo1, glWindow1, false);
170 glWindow1.addGLEventListener(demo1);
171
172 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
173 Assert.assertNotNull(newtCanvasAWT);
174 Assert.assertEquals(false, glWindow1.isVisible());
175 Assert.assertEquals(false, glWindow1.isNativeValid());
176 Assert.assertNull(glWindow1.getParent());
177
178 final Frame frame = new Frame("AWT Parent Frame");
179 Assert.assertNotNull(frame);
180
181 // visible test
182 SwingUtilities.invokeAndWait(new Runnable() {
183 public void run() {
184 frame.setSize(width, height);
185 frame.setVisible(true);
186 }
187 });
188
189 SwingUtilities.invokeAndWait(new Runnable() {
190 public void run() {
191 frame.add(newtCanvasAWT);
192 frame.validate();
193 }
194 });
195 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
196
197 final Animator animator1 = new Animator(glWindow1);
198 animator1.setUpdateFPSFrames(1, null);
199 animator1.start();
200 while(animator1.isAnimating() && animator1.getTotalFPSDuration()<durationPerTest) {
201 Thread.sleep(100);
202 }
203 animator1.stop();
204 Assert.assertEquals(false, animator1.isAnimating());
205
206 SwingUtilities.invokeAndWait(new Runnable() {
207 public void run() {
208 frame.dispose();
209 } } );
210 glWindow1.destroy();
211 }
212
213 @Test
214 public void test03WindowParenting02CreateVisibleDestroy3Odd() throws InterruptedException, InvocationTargetException {
215 final GLWindow glWindow1 = GLWindow.create(glCaps);
216 final GLEventListener demo1 = new RedSquareES2();
217 setDemoFields(demo1, glWindow1, false);
218 glWindow1.addGLEventListener(demo1);
219
220 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
221
222 final Frame frame = new Frame("AWT Parent Frame");
223 Assert.assertNotNull(frame);
224
225 // visible test
226 SwingUtilities.invokeAndWait(new Runnable() {
227 public void run() {
228 frame.setSize(width, height);
229 frame.setVisible(true);
230 }
231 });
232
233 SwingUtilities.invokeAndWait(new Runnable() {
234 public void run() {
235 frame.add(newtCanvasAWT);
236 frame.validate();
237 }
238 });
239
240 final Animator animator1 = new Animator(glWindow1);
241 animator1.setUpdateFPSFrames(1, null);
242 animator1.start();
243 Assert.assertEquals(true, animator1.isStarted());
244 Assert.assertEquals(true, animator1.isAnimating());
245 while(animator1.isAnimating() && animator1.getTotalFPSDuration()<durationPerTest) {
246 Thread.sleep(100);
247 }
248
249 Assert.assertEquals(true, animator1.isAnimating());
250
251 SwingUtilities.invokeAndWait(new Runnable() {
252 public void run() {
253 frame.dispose();
254 } } );
255 glWindow1.destroy();
256 }
257
258 @Test
259 public void test04WindowParenting03ReparentNewtWin2Top() throws InterruptedException, InvocationTargetException {
260 final GLWindow glWindow1 = GLWindow.create(glCaps);
261 final GLEventListener demo1 = new RedSquareES2();
262 setDemoFields(demo1, glWindow1, false);
263 glWindow1.addGLEventListener(demo1);
264
265 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
266
267 final Frame frame = new Frame("AWT Parent Frame");
268 SwingUtilities.invokeAndWait(new Runnable() {
269 public void run() {
270 frame.setSize(width, height);
271 frame.setLocation(640, 480);
272 frame.setVisible(true);
273 }
274 });
275
276 SwingUtilities.invokeAndWait(new Runnable() {
277 public void run() {
278 frame.add(newtCanvasAWT);
279 frame.validate();
280 }
281 });
282
283 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
284
285 final Animator animator1 = new Animator(glWindow1);
286 animator1.setUpdateFPSFrames(1, null);
287 animator1.start();
288
289 int state = 0;
290 while(animator1.isAnimating() && animator1.getTotalFPSDuration()<3*durationPerTest) {
291 Thread.sleep(durationPerTest);
292 switch(state) {
293 case 0:
294 System.err.println("Reparent CHILD -> TOP: "+glWindow1.reparentWindow(null, -1, -1, 0 /* hints */));
295 Assert.assertEquals(true, glWindow1.isNativeValid());
296 Assert.assertNull(glWindow1.getParent());
297 break;
298 case 1:
299 System.err.println("Reparent TOP -> CHILD: "+glWindow1.reparentWindow(newtCanvasAWT.getNativeWindow(), -1, -1, 0 /* hints */));
300 Assert.assertEquals(true, glWindow1.isNativeValid());
301 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
302 break;
303 }
304 state++;
305 }
306
307 animator1.stop();
308 Assert.assertEquals(false, animator1.isAnimating());
309
310 SwingUtilities.invokeAndWait(new Runnable() {
311 public void run() {
312 frame.dispose();
313 } } );
314 glWindow1.destroy();
315 }
316
317 @Test
318 public void test05WindowParenting04ReparentNewtWin2TopLayouted() throws InterruptedException, InvocationTargetException {
319 final GLWindow glWindow1 = GLWindow.create(glCaps);
320 final GLEventListener demo1 = new RedSquareES2();
321 setDemoFields(demo1, glWindow1, false);
322 glWindow1.addGLEventListener(demo1);
323
324 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
325
326 final Frame frame = new Frame("AWT Parent Frame");
327 frame.setLayout(new BorderLayout());
328 frame.add(new Button("North"), BorderLayout.NORTH);
329 frame.add(new Button("South"), BorderLayout.SOUTH);
330 frame.add(new Button("East"), BorderLayout.EAST);
331 frame.add(new Button("West"), BorderLayout.WEST);
332
333 SwingUtilities.invokeAndWait(new Runnable() {
334 public void run() {
335 frame.setSize(width, height);
336 frame.setLocation(640, 480);
337 frame.setVisible(true);
338 }
339 });
340
341 SwingUtilities.invokeAndWait(new Runnable() {
342 public void run() {
343 frame.add(newtCanvasAWT, BorderLayout.CENTER);
344 frame.validate();
345 }
346 });
347
348 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
349
350 final Animator animator1 = new Animator(glWindow1);
351 animator1.setUpdateFPSFrames(1, null);
352 animator1.start();
353
354 int state = 0;
355 while(animator1.isAnimating() && animator1.getTotalFPSDuration()<3*durationPerTest) {
356 Thread.sleep(durationPerTest);
357 switch(state) {
358 case 0:
359 System.err.println("Reparent CHILD -> TOP: "+glWindow1.reparentWindow(null, -1, -1, 0 /* hints */));
360 Assert.assertEquals(true, glWindow1.isNativeValid());
361 Assert.assertNull(glWindow1.getParent());
362 break;
363 case 1:
364 System.err.println("Reparent TOP -> CHILD: "+glWindow1.reparentWindow(newtCanvasAWT.getNativeWindow(), -1, -1, 0 /* hints */));
365 Assert.assertEquals(true, glWindow1.isNativeValid());
366 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
367 break;
368 }
369 state++;
370 }
371
372 animator1.stop();
373 Assert.assertEquals(false, animator1.isAnimating());
374
375 SwingUtilities.invokeAndWait(new Runnable() {
376 public void run() {
377 frame.dispose();
378 } } );
379 glWindow1.destroy();
380 }
381
382 @Test
383 public void test06WindowParenting05ReparentAWTWinHopFrame2Frame() throws InterruptedException, InvocationTargetException {
384 final GLWindow glWindow1 = GLWindow.create(glCaps);
385 glWindow1.setUndecorated(true);
386 final GLEventListener demo1 = new RedSquareES2();
387 setDemoFields(demo1, glWindow1, false);
388 glWindow1.addGLEventListener(demo1);
389
390 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
391
392 final Frame frame1 = new Frame("AWT Parent Frame");
393 frame1.setLayout(new BorderLayout());
394 frame1.add(new Button("North"), BorderLayout.NORTH);
395 frame1.add(new Button("South"), BorderLayout.SOUTH);
396 frame1.add(new Button("East"), BorderLayout.EAST);
397 frame1.add(new Button("West"), BorderLayout.WEST);
398 SwingUtilities.invokeAndWait(new Runnable() {
399 public void run() {
400 frame1.setSize(width, height);
401 frame1.setLocation(0, 0);
402 frame1.setVisible(true);
403 }
404 });
405
406 final Frame frame2 = new Frame("AWT Parent Frame");
407 frame2.setLayout(new BorderLayout());
408 frame2.add(new Button("North"), BorderLayout.NORTH);
409 frame2.add(new Button("South"), BorderLayout.SOUTH);
410 frame2.add(new Button("East"), BorderLayout.EAST);
411 frame2.add(new Button("West"), BorderLayout.WEST);
412 SwingUtilities.invokeAndWait(new Runnable() {
413 public void run() {
414 frame2.setSize(width, height);
415 frame2.setLocation(640, 480);
416 frame2.setVisible(true);
417 }
418 });
419
420 SwingUtilities.invokeAndWait(new Runnable() {
421 public void run() {
422 frame1.add(newtCanvasAWT, BorderLayout.CENTER);
423 frame1.validate();
424 }
425 });
426
427 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
428
429 final Animator animator1 = new Animator(glWindow1);
430 animator1.setUpdateFPSFrames(1, null);
431 animator1.start();
432
433 int state = 0;
434 while(animator1.isAnimating() && animator1.getTotalFPSDuration()<3*durationPerTest) {
435 Thread.sleep(durationPerTest);
436 switch(state) {
437 case 0:
438 SwingUtilities.invokeAndWait(new Runnable() {
439 public void run() {
440 frame1.remove(newtCanvasAWT);
441 frame2.add(newtCanvasAWT, BorderLayout.CENTER);
442 frame1.validate();
443 frame2.validate();
444 }
445 });
446 break;
447 case 1:
448 SwingUtilities.invokeAndWait(new Runnable() {
449 public void run() {
450 frame2.remove(newtCanvasAWT);
451 frame1.add(newtCanvasAWT, BorderLayout.CENTER);
452 frame2.validate();
453 frame1.validate();
454 }
455 });
456 break;
457 }
458 state++;
459 }
460
461 animator1.stop();
462 Assert.assertEquals(false, animator1.isAnimating());
463
464 SwingUtilities.invokeAndWait(new Runnable() {
465 public void run() {
466 frame1.dispose();
467 frame2.dispose();
468 } } );
469 glWindow1.destroy();
470 }
471
472 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
473 Assert.assertNotNull(demo);
474 Assert.assertNotNull(glWindow);
475 if(debug) {
476 MiscUtils.setFieldIfExists(demo, "glDebug", true);
477 MiscUtils.setFieldIfExists(demo, "glTrace", true);
478 }
479 if(!MiscUtils.setFieldIfExists(demo, "window", glWindow.getDelegatedWindow())) {
480 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
481 }
482 }
483
484 static int atoi(final String a) {
485 int i=0;
486 try {
487 i = Integer.parseInt(a);
488 } catch (final Exception ex) { ex.printStackTrace(); }
489 return i;
490 }
491
492 public static void main(final String args[]) throws IOException {
493 for(int i=0; i<args.length; i++) {
494 if(args[i].equals("-time")) {
495 durationPerTest = atoi(args[++i]);
496 } else if(args[i].equals("-wait")) {
497 waitReparent = atoi(args[++i]);
498 }
499 }
500 final String tstname = TestParenting01aAWT.class.getName();
501 org.junit.runner.JUnitCore.main(tstname);
502 }
503
504}
AWT Canvas containing a NEWT Window using native parenting.
NativeWindow getNativeWindow()
Returns the associated NativeWindow of this NativeWindowHolder, which is identical to getNativeSurfac...
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final NativeWindow getParent()
Definition: GLWindow.java:282
final ReparentOperation reparentWindow(final NativeWindow newParent, final int x, final int y, final int hints)
Change this window's parent window.
Definition: GLWindow.java:582
final void setTitle(final String title)
Definition: GLWindow.java:297
final CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
Definition: GLWindow.java:266
final void setUndecorated(final boolean value)
Definition: GLWindow.java:337
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.
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
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction)
final void setUpdateFPSFrames(final int frames, final PrintStream out)
synchronized boolean isStarted()
Indicates whether this animator has been started.
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
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.
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.