JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestParenting01cAWT.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.newt.*;
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 TestParenting01cAWT extends UITestCase {
57 static int width, height;
58 static long durationPerTest = 800;
59 static GLCapabilities glCaps;
60
61 @BeforeClass
62 public static void initClass() {
63 width = 640;
64 height = 480;
65 glCaps = new GLCapabilities(null);
66 }
67
68 @Test
69 public void test01CreateVisibleDestroy1() throws InterruptedException, InvocationTargetException {
70 int i;
71
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 for(i=0; i*100<durationPerTest; i++) {
115 Thread.sleep(100);
116 }
117
118 SwingUtilities.invokeAndWait(new Runnable() {
119 public void run() {
120 frame1.setVisible(false);
121 }
122 });
123 Assert.assertEquals(true, glWindow1.isNativeValid());
124
125 SwingUtilities.invokeAndWait(new Runnable() {
126 public void run() {
127 frame1.setVisible(true);
128 }
129 });
130 Assert.assertEquals(true, glWindow1.isNativeValid());
131
132 final boolean wasOnscreen = glWindow1.getChosenCapabilities().isOnscreen();
133
134 SwingUtilities.invokeAndWait(new Runnable() {
135 public void run() {
136 frame1.remove(newtCanvasAWT);
137 }
138 });
139 // Assert.assertNull(glWindow1.getParent());
140 if( wasOnscreen ) {
141 Assert.assertEquals(true, glWindow1.isNativeValid());
142 } // else OK to be destroyed - due to offscreen/onscreen transition
143
144 SwingUtilities.invokeAndWait(new Runnable() {
145 public void run() {
146 frame1.dispose();
147 } } );
148 if( wasOnscreen ) {
149 Assert.assertEquals(true, glWindow1.isNativeValid());
150 } // else OK to be destroyed - due to offscreen/onscreen transition
151
152 glWindow1.destroy();
153 Assert.assertEquals(false, glWindow1.isNativeValid());
154 }
155
156 @Test
157 public void test02AWTWinHopFrame2Frame() throws InterruptedException, InvocationTargetException {
158 final GLWindow glWindow1 = GLWindow.create(glCaps);
159 glWindow1.setUndecorated(true);
160 final GLEventListener demo1 = new RedSquareES2();
161 setDemoFields(demo1, glWindow1, false);
162 glWindow1.addGLEventListener(demo1);
163
164 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
165
166 final Frame frame1 = new Frame("AWT Parent Frame");
167 frame1.setLayout(new BorderLayout());
168 frame1.add(new Button("North"), BorderLayout.NORTH);
169 frame1.add(new Button("South"), BorderLayout.SOUTH);
170 frame1.add(new Button("East"), BorderLayout.EAST);
171 frame1.add(new Button("West"), BorderLayout.WEST);
172 SwingUtilities.invokeAndWait(new Runnable() {
173 public void run() {
174 frame1.setSize(width, height);
175 frame1.setLocation(0, 0);
176 frame1.setVisible(true);
177 }
178 });
179
180 final Frame frame2 = new Frame("AWT Parent Frame");
181 frame2.setLayout(new BorderLayout());
182 frame2.add(new Button("North"), BorderLayout.NORTH);
183 frame2.add(new Button("South"), BorderLayout.SOUTH);
184 frame2.add(new Button("East"), BorderLayout.EAST);
185 frame2.add(new Button("West"), BorderLayout.WEST);
186 SwingUtilities.invokeAndWait(new Runnable() {
187 public void run() {
188 frame2.setSize(width, height);
189 frame2.setLocation(640, 480);
190 frame2.setVisible(true);
191 }
192 });
193
194 SwingUtilities.invokeAndWait(new Runnable() {
195 public void run() {
196 frame1.add(newtCanvasAWT, BorderLayout.CENTER);
197 frame1.validate();
198 }
199 });
200 Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
201
202 int state;
203 for(state=0; state<3; state++) {
204 Thread.sleep(durationPerTest);
205 switch(state) {
206 case 0:
207 SwingUtilities.invokeAndWait(new Runnable() {
208 public void run() {
209 frame1.remove(newtCanvasAWT);
210 frame2.add(newtCanvasAWT, BorderLayout.CENTER);
211 frame1.validate();
212 frame2.validate();
213 }
214 });
215 break;
216 case 1:
217 SwingUtilities.invokeAndWait(new Runnable() {
218 public void run() {
219 frame2.remove(newtCanvasAWT);
220 frame1.add(newtCanvasAWT, BorderLayout.CENTER);
221 frame1.validate();
222 frame2.validate();
223 }
224 });
225 break;
226 }
227 }
228
229 SwingUtilities.invokeAndWait(new Runnable() {
230 public void run() {
231 frame1.dispose();
232 frame2.dispose();
233 } } );
234 glWindow1.destroy();
235 }
236
237 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
238 Assert.assertNotNull(demo);
239 Assert.assertNotNull(glWindow);
240 final Window window = glWindow.getDelegatedWindow();
241 if(debug) {
242 MiscUtils.setFieldIfExists(demo, "glDebug", true);
243 MiscUtils.setFieldIfExists(demo, "glTrace", true);
244 }
245 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
246 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
247 }
248 }
249
250 static int atoi(final String a) {
251 int i=0;
252 try {
253 i = Integer.parseInt(a);
254 } catch (final Exception ex) { ex.printStackTrace(); }
255 return i;
256 }
257
258 public static void main(final String args[]) throws IOException {
259 for(int i=0; i<args.length; i++) {
260 if(args[i].equals("-time")) {
261 durationPerTest = atoi(args[++i]);
262 }
263 }
264 final String tstname = TestParenting01cAWT.class.getName();
265 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
266 tstname,
267 "filtertrace=true",
268 "haltOnError=false",
269 "haltOnFailure=false",
270 "showoutput=true",
271 "outputtoformatters=true",
272 "logfailedtests=true",
273 "logtestlistenerevents=true",
274 "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
275 "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
276 }
277
278}
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 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
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.
Specifying NEWT's Window functionality:
Definition: Window.java:115
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.