JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestParenting03AWT.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 java.lang.reflect.*;
32
33import org.junit.Assert;
34import org.junit.BeforeClass;
35import org.junit.Test;
36import org.junit.FixMethodOrder;
37import org.junit.runners.MethodSorters;
38
39import java.awt.BorderLayout;
40import java.awt.Button;
41import java.awt.Container;
42import java.awt.Dimension;
43import java.awt.Frame;
44
45import com.jogamp.opengl.*;
46
47import com.jogamp.opengl.util.Animator;
48import com.jogamp.newt.*;
49import com.jogamp.newt.opengl.*;
50import com.jogamp.newt.opengl.util.NEWTDemoListener;
51import com.jogamp.newt.awt.NewtCanvasAWT;
52
53import java.io.IOException;
54
55import com.jogamp.opengl.test.junit.util.*;
56import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
57
58/**
59 * <p>
60 * The demo code uses {@link NewtReparentingKeyAdapter} including {@link NEWTDemoListener} functionality.
61 * </p>
62 * <p>
63 * Manual invocation via main allows setting each tests's duration in milliseconds, e.g.{@code -duration 10000}, and many more, see {@link #main(String[])}
64 * </p>
65 */
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
67public class TestParenting03AWT extends UITestCase {
68 static Dimension glSize, fSize;
69 static long durationPerTest = 1100;
70 static long waitAdd2nd = 500;
71 static GLCapabilities glCaps;
72
73 @BeforeClass
74 public static void initClass() {
75 glSize = new Dimension(400,200);
76 fSize = new Dimension(3*400,2*200);
77 glCaps = new GLCapabilities(null);
78 }
79
80 @Test
81 public void test01AWTOneNewtChilds01() throws InterruptedException, InvocationTargetException {
82 testImpl(false);
83 }
84
85 @Test
86 public void test02AWTTwoNewtChilds01() throws InterruptedException, InvocationTargetException {
87 testImpl(true);
88 }
89
90 public void testImpl(final boolean use2nd) throws InterruptedException, InvocationTargetException {
91 final Frame frame1 = new Frame("AWT Parent Frame");
92 final GLWindow glWindow1 = GLWindow.create(glCaps);
93 glWindow1.setUpdateFPSFrames(1, null);
94 final NewtCanvasAWT newtCanvasAWT1 = new NewtCanvasAWT(glWindow1);
95 newtCanvasAWT1.setPreferredSize(glSize);
96
97 final GLEventListener demo1 = new GearsES2(1);
98 setDemoFields(demo1, glWindow1, false);
99 glWindow1.addGLEventListener(demo1);
100 glWindow1.addKeyListener(new NewtAWTReparentingKeyAdapter(frame1, newtCanvasAWT1, glWindow1));
101 final GLAnimatorControl animator1 = new Animator(glWindow1);
102 animator1.start();
103
104 GLWindow glWindow2 = null;
105 NewtCanvasAWT newtCanvasAWT2 = null;
106 GLAnimatorControl animator2 = null;
107 if(use2nd) {
108 glWindow2 = GLWindow.create(glCaps);
109 glWindow2.setUpdateFPSFrames(1, null);
110 newtCanvasAWT2 = new NewtCanvasAWT(glWindow2);
111 newtCanvasAWT2.setPreferredSize(glSize);
112
113 final GLEventListener demo2 = new GearsES2(1);
114 setDemoFields(demo2, glWindow2, false);
115 glWindow2.addGLEventListener(demo2);
116 glWindow2.addKeyListener(new NewtAWTReparentingKeyAdapter(frame1, newtCanvasAWT2, glWindow2));
117 animator2 = new Animator(glWindow2);
118 animator2.start();
119 }
120
121 final Container cont1 = new Container();
122 cont1.setLayout(new BorderLayout());
123 cont1.add(new Button("NORTH"), BorderLayout.NORTH);
124 cont1.add(new Button("SOUTH"), BorderLayout.SOUTH);
125 cont1.add(new Button("EAST"), BorderLayout.EAST);
126 cont1.add(new Button("WEST"), BorderLayout.WEST);
127 cont1.add(newtCanvasAWT1, BorderLayout.CENTER);
128 System.err.println("******* Cont1 setVisible");
129 cont1.setVisible(true);
130
131 final Container cont2 = new Container();
132 cont2.setLayout(new BorderLayout());
133 if(use2nd) {
134 cont2.add(new Button("north"), BorderLayout.NORTH);
135 cont2.add(new Button("sourth"), BorderLayout.SOUTH);
136 cont2.add(new Button("east"), BorderLayout.EAST);
137 cont2.add(new Button("west"), BorderLayout.WEST);
138 cont2.add(newtCanvasAWT2, BorderLayout.CENTER);
139 }
140 System.err.println("******* Cont2 setVisible");
141 cont2.setVisible(true);
142
143 frame1.setLayout(new BorderLayout());
144 frame1.add(new Button("NORTH"), BorderLayout.NORTH);
145 frame1.add(new Button("CENTER"), BorderLayout.CENTER);
146 frame1.add(new Button("SOUTH"), BorderLayout.SOUTH);
147 frame1.add(cont1, BorderLayout.EAST);
148 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
149 public void run() {
150 System.err.println("******* Frame setVisible");
151 frame1.setLocation(0, 0);
152 frame1.setSize(fSize);
153 frame1.validate();
154 frame1.setVisible(true);
155 }});
156
157 Assert.assertEquals(newtCanvasAWT1.getNativeWindow(),glWindow1.getParent());
158
159 Assert.assertEquals(true, animator1.isAnimating());
160 Assert.assertEquals(false, animator1.isPaused());
161 Assert.assertNotNull(animator1.getThread());
162
163 if(use2nd) {
164 Assert.assertEquals(true, animator2.isAnimating());
165 Assert.assertEquals(false, animator2.isPaused());
166 Assert.assertNotNull(animator2.getThread());
167
168 Thread.sleep(waitAdd2nd);
169
170 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
171 public void run() {
172 frame1.add(cont2, BorderLayout.WEST);
173 frame1.validate();
174 }});
175 Assert.assertEquals(newtCanvasAWT2.getNativeWindow(),glWindow2.getParent());
176 }
177
178
179 Thread.sleep(durationPerTest);
180
181 animator1.stop();
182 Assert.assertEquals(false, animator1.isAnimating());
183 Assert.assertEquals(false, animator1.isPaused());
184 Assert.assertEquals(null, animator1.getThread());
185
186 if(use2nd) {
187 animator2.stop();
188 Assert.assertEquals(false, animator2.isAnimating());
189 Assert.assertEquals(false, animator2.isPaused());
190 Assert.assertEquals(null, animator2.getThread());
191 }
192
193 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
194 public void run() {
195 frame1.dispose();
196 } } );
197 glWindow1.destroy();
198 if(use2nd) {
199 glWindow2.destroy();
200 }
201 }
202
203 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
204 Assert.assertNotNull(demo);
205 Assert.assertNotNull(glWindow);
206 final Window window = glWindow.getDelegatedWindow();
207 if(debug) {
208 MiscUtils.setFieldIfExists(demo, "glDebug", true);
209 MiscUtils.setFieldIfExists(demo, "glTrace", true);
210 }
211 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
212 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
213 }
214 }
215
216 static int atoi(final String a) {
217 int i=0;
218 try {
219 i = Integer.parseInt(a);
220 } catch (final Exception ex) { ex.printStackTrace(); }
221 return i;
222 }
223
224 public static void main(final String args[]) throws IOException {
225 for(int i=0; i<args.length; i++) {
226 if(args[i].equals("-time")) {
227 durationPerTest = atoi(args[++i]);
228 } else if(args[i].equals("-wait")) {
229 waitAdd2nd = atoi(args[++i]);
230 }
231 }
232 final String tstname = TestParenting03AWT.class.getName();
233 /*
234 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
235 tstname,
236 "filtertrace=true",
237 "haltOnError=false",
238 "haltOnFailure=false",
239 "showoutput=true",
240 "outputtoformatters=true",
241 "logfailedtests=true",
242 "logtestlistenerevents=true",
243 "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
244 "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); */
245 org.junit.runner.JUnitCore.main(tstname);
246 }
247
248}
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 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 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.
AWT specializing demo functionality of NewtReparentingKeyAdapter, includes NEWTDemoListener.
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
Specifying NEWT's Window functionality:
Definition: Window.java:115
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.