JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestParenting01aSWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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.io.IOException;
32import java.lang.reflect.InvocationTargetException;
33
34import com.jogamp.opengl.GLCapabilities;
35import com.jogamp.opengl.GLEventListener;
36
37import org.eclipse.swt.SWT;
38import org.eclipse.swt.layout.FillLayout;
39import org.eclipse.swt.widgets.Composite;
40import org.eclipse.swt.widgets.Display;
41import org.eclipse.swt.widgets.Shell;
42import org.junit.After;
43import org.junit.Assert;
44import org.junit.Assume;
45import org.junit.Before;
46import org.junit.BeforeClass;
47import org.junit.Test;
48import org.junit.FixMethodOrder;
49import org.junit.runners.MethodSorters;
50
51import com.jogamp.nativewindow.swt.SWTAccessor;
52import com.jogamp.newt.NewtFactory;
53import com.jogamp.newt.Window;
54import com.jogamp.newt.opengl.GLWindow;
55import com.jogamp.newt.swt.NewtCanvasSWT;
56import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
57import com.jogamp.opengl.test.junit.util.MiscUtils;
58import com.jogamp.opengl.test.junit.util.SWTTestUtil;
59import com.jogamp.opengl.test.junit.util.UITestCase;
60
61/**
62 * Simple visibility test ..
63 */
64@FixMethodOrder(MethodSorters.NAME_ASCENDING)
65public class TestParenting01aSWT extends UITestCase {
66 static int width, height;
67 static long durationPerTest = 800;
68 static GLCapabilities glCaps;
69
70 Display display = null;
71 Shell shell = null;
72 Composite composite1 = null;
73 com.jogamp.newt.Display swtNewtDisplay = null;
74
75 @BeforeClass
76 public static void initClass() {
77 width = 640;
78 height = 480;
79 glCaps = new GLCapabilities(null);
80 }
81
82 @Before
83 public void init() {
84 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
85 public void run() {
86 display = new Display();
87 Assert.assertNotNull( display );
88 } } );
89
90 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
91 public void run() {
92 shell = new Shell( display );
93 Assert.assertNotNull( shell );
94 shell.setLayout( new FillLayout() );
95 composite1 = new Composite( shell, SWT.NONE );
96 composite1.setLayout( new FillLayout() );
97 Assert.assertNotNull( composite1 );
98 }});
99 swtNewtDisplay = NewtFactory.createDisplay(null, false); // no-reuse
100 }
101
102 @After
103 public void release() {
104 Assert.assertNotNull( display );
105 Assert.assertNotNull( shell );
106 Assert.assertNotNull( composite1 );
107 try {
108 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
109 public void run() {
110 composite1.dispose();
111 shell.dispose();
112 display.dispose();
113 }});
114 }
115 catch( final Throwable throwable ) {
116 throwable.printStackTrace();
117 Assume.assumeNoException( throwable );
118 }
119 swtNewtDisplay = null;
120 display = null;
121 shell = null;
122 composite1 = null;
123 }
124
125 @Test
126 public void testWindowParenting01CreateVisibleDestroy1() throws InterruptedException, InvocationTargetException {
127
128 final com.jogamp.newt.Screen screen = NewtFactory.createScreen(swtNewtDisplay, 0);
129 final GLWindow glWindow1 = GLWindow.create(screen, glCaps);
130 Assert.assertNotNull(glWindow1);
131 Assert.assertEquals(false, glWindow1.isVisible());
132 Assert.assertEquals(false, glWindow1.isNativeValid());
133 Assert.assertNull(glWindow1.getParent());
134 glWindow1.setTitle("testWindowParenting01CreateVisibleDestroy");
135 final GLEventListener demo1 = new RedSquareES2();
136 setDemoFields(demo1, glWindow1, false);
137 glWindow1.addGLEventListener(demo1);
138
139 final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite1, 0, glWindow1 );
140 Assert.assertNotNull(canvas1);
141 Assert.assertEquals(false, glWindow1.isVisible());
142 Assert.assertEquals(false, glWindow1.isNativeValid());
143 Assert.assertNull(glWindow1.getParent());
144
145 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
146 public void run() {
147 shell.setText( getSimpleTestName(".") );
148 shell.setSize( 640, 480 );
149 shell.open();
150 }
151 });
152
153 // visible test
154 Assert.assertEquals(canvas1.getNativeWindow(),glWindow1.getParent());
155
156 final SWTTestUtil.WaitAction generalWaitAction = new SWTTestUtil.WaitAction(display, true, 16);
157
158 for(int i=0; i*10<durationPerTest; i++) {
159 generalWaitAction.run();
160 }
161
162 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
163 public void run() {
164 canvas1.setVisible(false);
165 }
166 });
167 Assert.assertEquals(true, glWindow1.isNativeValid());
168
169 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
170 public void run() {
171 canvas1.setVisible(true);
172 }
173 });
174 Assert.assertEquals(true, glWindow1.isNativeValid());
175
176 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
177 public void run() {
178 canvas1.dispose();
179 } } );
180
181 Assert.assertEquals(false, glWindow1.isNativeValid());
182 }
183
184 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
185 Assert.assertNotNull(demo);
186 Assert.assertNotNull(glWindow);
187 final Window window = glWindow.getDelegatedWindow();
188 if(debug) {
189 MiscUtils.setFieldIfExists(demo, "glDebug", true);
190 MiscUtils.setFieldIfExists(demo, "glTrace", true);
191 }
192 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
193 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
194 }
195 }
196
197 static int atoi(final String a) {
198 int i=0;
199 try {
200 i = Integer.parseInt(a);
201 } catch (final Exception ex) { ex.printStackTrace(); }
202 return i;
203 }
204
205 public static void main(final String args[]) throws IOException {
206 for(int i=0; i<args.length; i++) {
207 if(args[i].equals("-time")) {
208 durationPerTest = atoi(args[++i]);
209 }
210 }
211 org.junit.runner.JUnitCore.main(TestParenting01aSWT.class.getName());
212 }
213
214}
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
static void invokeOnSWTThread(final org.eclipse.swt.widgets.Display display, final boolean blocking, final Runnable runnable)
Runs the specified action on the SWT UI thread.
static Display createDisplay(final String name)
Create a Display entity.
static Screen createScreen(final Display display, final int index)
Create a Screen entity.
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
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 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
SWT Canvas containing a NEWT Window using native parenting.
NativeWindow getNativeWindow()
Returns the associated NativeWindow of this NativeWindowHolder, which is identical to getNativeSurfac...
static NewtCanvasSWT create(final Composite parent, final int style, final Window child)
Creates an instance using NewtCanvasSWT(Composite, int, Window) on the SWT thread.
void dispose()
Destroys this resource:
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
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.