summaryrefslogtreecommitdiffstats
path: root/src/test/com
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2012-07-25 04:23:35 +0200
committerSven Gothel <[email protected]>2012-07-25 04:23:35 +0200
commit7bb5885fc3a904f49e22f0c8cbf747d9b189a7ba (patch)
treee5617fa74ff4a401fa60a080c5b3e8cf4ee21233 /src/test/com
parent00bef95008b02cc71e166da122884402e9381f44 (diff)
SWT Update: SWT GLCanvas creates lazy when resource is ready; Create new NewtCanvasSWT allowing to parent NEWT windows natively.
SWT GLCanvas creates lazy when resource is ready - Ensures drawable and context are created when size > zero and native visualID is valid. The latter is platform dependent. - Note that you cannot utilize custom GLCapabilities w/ this one, since the configurations is already realized - use NewtCanvasSWT. Create new NewtCanvasSWT allowing to parent NEWT windows natively: - Similar to NewtCanvasAWT - Allows attaching / detaching NEWT windows NewtCanvasAWT: Public setNEWTChild(..) fixed Added test cases for the above - tested on Linux, OSX and Windows w/ SWT Note: As usual for OSX, add -XstartOnFirstThread Details: - NEWT Display has new method: 'EDTUtil setEDTUtil(EDTUtil)' allowing to set a custom event dispatch utility. We use this to set our SWTEDTUtil for using NEWT w/ SWT complying w/ SWT threading constraints.
Diffstat (limited to 'src/test/com')
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java233
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTAccessor02GLn.java278
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTJOGLGLCanvas01GLn.java55
-rw-r--r--src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aSWT.java210
-rw-r--r--src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04AWT.java239
-rw-r--r--src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04SWT.java264
6 files changed, 976 insertions, 303 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java
new file mode 100644
index 000000000..6a27e6f27
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java
@@ -0,0 +1,233 @@
+/**
+ * Copyright 2010 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.opengl.test.junit.jogl.swt;
+
+import javax.media.opengl.GLAutoDrawable;
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLCapabilitiesImmutable;
+import javax.media.opengl.GLEventListener;
+import javax.media.opengl.GLProfile;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.After;
+import org.junit.Test;
+
+import com.jogamp.nativewindow.swt.SWTAccessor;
+import com.jogamp.newt.opengl.GLWindow;
+import com.jogamp.newt.swt.NewtCanvasSWT;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.MultisampleDemoES2;
+import com.jogamp.opengl.test.junit.util.UITestCase;
+import com.jogamp.opengl.util.Animator;
+import com.jogamp.opengl.util.GLReadBufferUtil;
+import com.jogamp.opengl.util.texture.TextureIO;
+
+/**
+ * Tests that a basic SWT app can open without crashing under different GL profiles
+ * _and_ custom GLCapabilities.
+ * <p>
+ * Uses JOGL's NewtCanvasSWT, which allows to be a native container of a NEWT Window.<br/>
+ * This method allows utilizing custom GLCapability settings,
+ * independent from the already instantiated SWT visual.
+ * </p>
+ * <p>
+ * Note that {@link SWTAccessor#invoke(boolean, Runnable)} is still used to comply w/
+ * SWT running on Mac OSX, i.e. to enforce UI action on the main thread.
+ * </p>
+ */
+public class TestNewtCanvasSWTGLn extends UITestCase {
+
+ static int duration = 250;
+
+ static final int iwidth = 640;
+ static final int iheight = 480;
+
+ Display display = null;
+ Shell shell = null;
+ Composite composite = null;
+
+ @BeforeClass
+ public static void startup() {
+ System.out.println( "GLProfile " + GLProfile.glAvailabilityToString() );
+ }
+
+ @Before
+ public void init() {
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ display = new Display();
+ Assert.assertNotNull( display );
+ shell = new Shell( display );
+ Assert.assertNotNull( shell );
+ shell.setLayout( new FillLayout() );
+ composite = new Composite( shell, SWT.NONE );
+ composite.setLayout( new FillLayout() );
+ Assert.assertNotNull( composite );
+ }});
+ }
+
+ @After
+ public void release() {
+ Assert.assertNotNull( display );
+ Assert.assertNotNull( shell );
+ Assert.assertNotNull( composite );
+ try {
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ composite.dispose();
+ shell.dispose();
+ display.dispose();
+ }});
+ }
+ catch( Throwable throwable ) {
+ throwable.printStackTrace();
+ Assume.assumeNoException( throwable );
+ }
+ display = null;
+ shell = null;
+ composite = null;
+ }
+
+ protected void runTestAGL( GLCapabilitiesImmutable caps, GLEventListener demo,
+ boolean postAttach, boolean useAnimator ) throws InterruptedException {
+ final GLReadBufferUtil screenshot = new GLReadBufferUtil(false, false);
+
+ final GLWindow glWindow1 = GLWindow.create(caps);
+ Assert.assertNotNull(glWindow1);
+ Assert.assertEquals(false, glWindow1.isVisible());
+ Assert.assertEquals(false, glWindow1.isNativeValid());
+ Assert.assertNull(glWindow1.getParent());
+ glWindow1.addGLEventListener(demo);
+ glWindow1.addGLEventListener(new GLEventListener() {
+ int displayCount = 0;
+ public void init(final GLAutoDrawable drawable) { }
+ public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
+ public void display(final GLAutoDrawable drawable) {
+ if(displayCount < 3) {
+ snapshot(getSimpleTestName("."), displayCount++, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
+ }
+ }
+ public void dispose(final GLAutoDrawable drawable) { }
+ });
+
+ final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite, 0, postAttach ? null : glWindow1 );
+ Assert.assertNotNull( canvas1 );
+
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ shell.setText( getSimpleTestName(".") );
+ shell.setSize( 640, 480 );
+ shell.open();
+ }
+ });
+
+ if(postAttach) {
+ canvas1.setNEWTChild(glWindow1);
+ }
+
+ // canvas1.update();
+
+ Animator anim;
+ if(useAnimator) {
+ anim = new Animator(glWindow1);
+ anim.start();
+ } else {
+ anim = null;
+ }
+
+ long lStartTime = System.currentTimeMillis();
+ long lEndTime = lStartTime + duration;
+ try {
+ while( (System.currentTimeMillis() < lEndTime) && !canvas1.isDisposed() ) {
+ if( !display.readAndDispatch() ) {
+ // blocks on linux .. display.sleep();
+ Thread.sleep(10);
+ }
+ }
+ } catch( Throwable throwable ) {
+ throwable.printStackTrace();
+ Assume.assumeNoException( throwable );
+ }
+ if(null != anim) {
+ anim.stop();
+ }
+
+ canvas1.dispose();
+ }
+
+ @Test
+ public void preAttach_WithAnimator() throws InterruptedException {
+ runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2(), false /* postAttach */, true /* animator */);
+ }
+
+ @Test
+ public void preAttach_NoAnimator() throws InterruptedException {
+ runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2(), false /* postAttach */, false /* animator */);
+ }
+
+ @Test
+ public void postAttach_WithAnimator() throws InterruptedException {
+ runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2(), true /* postAttach */, true /* animator */);
+ }
+
+ @Test
+ public void test_MultisampleAndAlpha() throws InterruptedException {
+ GLCapabilities caps = new GLCapabilities(GLProfile.getGL2ES2());
+ caps.setSampleBuffers(true);
+ caps.setNumSamples(2);
+ runTestAGL( caps, new MultisampleDemoES2(true), false /* postAttach */, false /* animator */);
+ }
+
+ static int atoi(String a) {
+ int i=0;
+ try {
+ i = Integer.parseInt(a);
+ } catch (Exception ex) { ex.printStackTrace(); }
+ return i;
+ }
+
+ public static void main(String args[]) {
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ duration = atoi(args[++i]);
+ }
+ }
+ System.out.println("durationPerTest: "+duration);
+ org.junit.runner.JUnitCore.main(TestNewtCanvasSWTGLn.class.getName());
+ }
+}
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTAccessor02GLn.java b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTAccessor02GLn.java
deleted file mode 100644
index 2e225ec0e..000000000
--- a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTAccessor02GLn.java
+++ /dev/null
@@ -1,278 +0,0 @@
-/**
- * Copyright 2010 JogAmp Community. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are
- * permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this list of
- * conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice, this list
- * of conditions and the following disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation are those of the
- * authors and should not be interpreted as representing official policies, either expressed
- * or implied, of JogAmp Community.
- */
-
-package com.jogamp.opengl.test.junit.jogl.swt;
-
-import javax.media.opengl.GL2ES1;
-import javax.media.opengl.GLContext;
-import javax.media.opengl.GLDrawableFactory;
-import javax.media.opengl.GLProfile;
-
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.ControlEvent;
-import org.eclipse.swt.events.ControlListener;
-import org.eclipse.swt.events.PaintEvent;
-import org.eclipse.swt.events.PaintListener;
-import org.eclipse.swt.graphics.Rectangle;
-import org.eclipse.swt.layout.FillLayout;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.Shell;
-
-import org.junit.Assert;
-import org.junit.Assume;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.After;
-import org.junit.Test;
-
-import com.jogamp.nativewindow.swt.SWTAccessor;
-import com.jogamp.opengl.test.junit.jogl.demos.es1.OneTriangle;
-import com.jogamp.opengl.test.junit.util.UITestCase;
-import javax.media.nativewindow.AbstractGraphicsDevice;
-import javax.media.nativewindow.ProxySurface;
-import javax.media.opengl.GLCapabilities;
-import javax.media.opengl.GLDrawable;
-import org.eclipse.swt.widgets.Canvas;
-
-/**
- * Tests that a basic SWT app can open without crashing under different GL profiles.
- * <p>
- * Uses JOGL's SWTAccessor only.
- * </p>
- * @author Wade Walker, et.al.
- */
-public class TestSWTAccessor02GLn extends UITestCase {
-
- static int duration = 250;
-
- static final int iwidth = 640;
- static final int iheight = 480;
-
- Display display = null;
- Shell shell = null;
- Composite composite = null;
-
- @BeforeClass
- public static void startup() {
- System.out.println( "GLProfile " + GLProfile.glAvailabilityToString() );
- }
-
- @Before
- public void init() {
- SWTAccessor.invoke(true, new Runnable() {
- public void run() {
- display = new Display();
- Assert.assertNotNull( display );
- shell = new Shell( display );
- Assert.assertNotNull( shell );
- shell.setLayout( new FillLayout() );
- composite = new Composite( shell, SWT.NONE );
- composite.setLayout( new FillLayout() );
- Assert.assertNotNull( composite );
- }});
- }
-
- @After
- public void release() {
- Assert.assertNotNull( display );
- Assert.assertNotNull( shell );
- Assert.assertNotNull( composite );
- try {
- SWTAccessor.invoke(true, new Runnable() {
- public void run() {
- composite.dispose();
- shell.dispose();
- display.dispose();
- }});
- }
- catch( Throwable throwable ) {
- throwable.printStackTrace();
- Assume.assumeNoException( throwable );
- }
- display = null;
- shell = null;
- composite = null;
- }
-
- protected void runTestAGL( GLProfile glprofile ) throws InterruptedException {
- GLCapabilities caps = new GLCapabilities(glprofile);
- GLDrawableFactory factory = GLDrawableFactory.getFactory(glprofile);
-
- // need SWT.NO_BACKGROUND to prevent SWT from clearing the window
- // at the wrong times (we use glClear for this instead)
- final Canvas canvas = new Canvas( composite, SWT.NO_BACKGROUND);
- Assert.assertNotNull( canvas );
- SWTAccessor.setRealized(canvas, true);
-
- AbstractGraphicsDevice device = SWTAccessor.getDevice(canvas);
- long nativeWindowHandle = SWTAccessor.getWindowHandle(canvas);
- System.err.println("*** device: " + device);
- System.err.println("*** window handle: 0x" + Long.toHexString(nativeWindowHandle));
-
- final SWTUpstreamHook swtUpstreamHook = new SWTUpstreamHook(canvas);
- canvas.addControlListener(swtUpstreamHook);
-
- final ProxySurface proxySurface = factory.createProxySurface(device, 0, nativeWindowHandle, caps, null, swtUpstreamHook);
- Assert.assertNotNull( proxySurface );
- System.err.println("*** ProxySurface: " + proxySurface);
-
- final GLDrawable drawable = factory.createGLDrawable(proxySurface);
- Assert.assertNotNull( drawable );
- drawable.setRealized(true);
- System.err.println("*** Drawable: " + drawable);
- Assert.assertTrue( drawable.isRealized() );
-
- final GLContext glcontext = drawable.createContext(null);
- // trigger native creation ..
- if( GLContext.CONTEXT_NOT_CURRENT < glcontext.makeCurrent() ) {
- glcontext.release();
- }
-
- final boolean[] sizeMissing = new boolean[] { false };
-
- // fix the viewport when the user resizes the window
- canvas.addListener( SWT.Resize, new Listener() {
- public void handleEvent( Event event ) {
- Rectangle rectangle = canvas.getClientArea();
- boolean glok=false;
- if( GLContext.CONTEXT_NOT_CURRENT < glcontext.makeCurrent() ) {
- glok=true;
- GL2ES1 gl = glcontext.getGL().getGL2ES1();
- OneTriangle.setup( gl, rectangle.width, rectangle.height );
- glcontext.release();
- } else {
- sizeMissing[0] = true;
- }
- System.err.println("resize: glok " + glok);
- }
- });
-
- // draw the triangle when the OS tells us that any part of the window needs drawing
- canvas.addPaintListener( new PaintListener() {
- public void paintControl( PaintEvent paintevent ) {
- Rectangle rectangle = canvas.getClientArea();
- boolean glok=false;
- if( GLContext.CONTEXT_NOT_CURRENT < glcontext.makeCurrent() ) {
- glok=true;
- GL2ES1 gl = glcontext.getGL().getGL2ES1();
- if(sizeMissing[0]) {
- OneTriangle.setup( gl, rectangle.width, rectangle.height);
- sizeMissing[0] = false;
- }
- OneTriangle.render( gl, rectangle.width, rectangle.height);
- drawable.swapBuffers();
- glcontext.release();
- }
- System.err.println("paint: glok " + glok);
- }
- });
-
- shell.setText( getClass().getName() );
- shell.setSize( 640, 480 );
- shell.open();
-
- long lStartTime = System.currentTimeMillis();
- long lEndTime = lStartTime + duration;
- try {
- while( (System.currentTimeMillis() < lEndTime) && !canvas.isDisposed() ) {
- if( !display.readAndDispatch() ) {
- // blocks on linux .. display.sleep();
- Thread.sleep(10);
- }
- }
- } catch( Throwable throwable ) {
- throwable.printStackTrace();
- Assume.assumeNoException( throwable );
- }
- glcontext.destroy();
- drawable.setRealized(false);
- canvas.dispose();
- }
- private static class SWTUpstreamHook implements ProxySurface.UpstreamSurfaceHook, ControlListener {
- private Canvas c;
- Rectangle clientArea;
- public SWTUpstreamHook(Canvas c) {
- this.c = c ;
- this.clientArea = c.getClientArea();
- }
- @Override
- public final void create(ProxySurface s) { /* nop */ }
-
- @Override
- public final void destroy(ProxySurface s) { /* nop */ }
-
- @Override
- public final int getWidth(ProxySurface s) {
- return clientArea.width;
- }
- @Override
- public final int getHeight(ProxySurface s) {
- return clientArea.width;
- }
-
- @Override
- public void controlResized(final ControlEvent arg0) {
- clientArea = c.getClientArea();
- }
- @Override
- public void controlMoved(ControlEvent e) {
- }
- @Override
- public String toString() {
- final String us_s = null != c ? c.toString() : "nil";
- return "SETUpstreamSurfaceHook[upstream: "+us_s+"]";
- }
- };
-
- @Test
- public void test() throws InterruptedException {
- GLProfile glprofile = GLProfile.getGL2ES1();
- runTestAGL( glprofile );
- }
-
- static int atoi(String a) {
- int i=0;
- try {
- i = Integer.parseInt(a);
- } catch (Exception ex) { ex.printStackTrace(); }
- return i;
- }
-
- public static void main(String args[]) {
- for(int i=0; i<args.length; i++) {
- if(args[i].equals("-time")) {
- duration = atoi(args[++i]);
- }
- }
- System.out.println("durationPerTest: "+duration);
- org.junit.runner.JUnitCore.main(TestSWTAccessor02GLn.class.getName());
- }
-}
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTJOGLGLCanvas01GLn.java b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTJOGLGLCanvas01GLn.java
index 19e76419f..4ca1dae0d 100644
--- a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTJOGLGLCanvas01GLn.java
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestSWTJOGLGLCanvas01GLn.java
@@ -49,8 +49,10 @@ import org.junit.Test;
import com.jogamp.nativewindow.swt.SWTAccessor;
import com.jogamp.opengl.swt.GLCanvas;
-import com.jogamp.opengl.test.junit.jogl.demos.es1.OneTriangle;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
import com.jogamp.opengl.test.junit.util.UITestCase;
+import com.jogamp.opengl.util.GLReadBufferUtil;
+import com.jogamp.opengl.util.texture.TextureIO;
/**
* Tests that a basic SWT app can open without crashing under different GL profiles.
@@ -58,6 +60,9 @@ import com.jogamp.opengl.test.junit.util.UITestCase;
* Uses JOGL's new SWT GLCanvas.
* </p>
* <p>
+ * Note: To employ custom GLCapabilities, NewtCanvasSWT shall be used.
+ * </p>
+ * <p>
* Note that {@link SWTAccessor#invoke(boolean, Runnable)} is still used to comply w/
* SWT running on Mac OSX, i.e. to enforce UI action on the main thread.
* </p>
@@ -116,34 +121,32 @@ public class TestSWTJOGLGLCanvas01GLn extends UITestCase {
composite = null;
}
- protected void runTestAGL( GLProfile glprofile ) throws InterruptedException {
- // need SWT.NO_BACKGROUND to prevent SWT from clearing the window
- // at the wrong times (we use glClear for this instead)
- final GLCapabilitiesImmutable caps = new GLCapabilities( glprofile );
+ protected void runTestAGL( GLCapabilitiesImmutable caps, GLEventListener demo ) throws InterruptedException {
+ final GLReadBufferUtil screenshot = new GLReadBufferUtil(false, false);
- final GLCanvas canvas = new GLCanvas( composite, 0, caps, null, null);
+ final GLCanvas canvas = GLCanvas.create( composite, 0, caps, null, null);
Assert.assertNotNull( canvas );
+ canvas.addGLEventListener( demo );
canvas.addGLEventListener(new GLEventListener() {
- public void init(final GLAutoDrawable drawable) {
- System.err.println(Thread.currentThread().getName()+" - SWT Canvas - GLEventListener - init()");
- }
- public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
- System.err.println(Thread.currentThread().getName()+" - SWT Canvas - GLEventListener - reshape()");
- OneTriangle.setup( drawable.getGL().getGL2(), width, height );
- }
+ int displayCount = 0;
+ public void init(final GLAutoDrawable drawable) { }
+ public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
public void display(final GLAutoDrawable drawable) {
- OneTriangle.render( drawable.getGL().getGL2(), drawable.getWidth(), drawable.getHeight());
+ if(displayCount < 3) {
+ snapshot(getSimpleTestName("."), displayCount++, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
+ }
}
- public void dispose(final GLAutoDrawable drawable) {
- System.err.println(Thread.currentThread().getName()+" - SWT Canvas - GLEventListener - dispose()");
- }
- });
+ public void dispose(final GLAutoDrawable drawable) { }
+ });
- shell.setText( getClass().getName() );
- shell.setSize( 640, 480 );
- shell.open();
-
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ shell.setText( getSimpleTestName(".") );
+ shell.setSize( 640, 480 );
+ shell.open();
+ } } );
+
long lStartTime = System.currentTimeMillis();
long lEndTime = lStartTime + duration;
try {
@@ -157,13 +160,15 @@ public class TestSWTJOGLGLCanvas01GLn extends UITestCase {
throwable.printStackTrace();
Assume.assumeNoException( throwable );
}
- canvas.dispose();
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ canvas.dispose();
+ } } );
}
@Test
public void test() throws InterruptedException {
- GLProfile glprofile = GLProfile.getGL2ES1();
- runTestAGL( glprofile );
+ runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2() );
}
static int atoi(String a) {
diff --git a/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aSWT.java b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aSWT.java
new file mode 100644
index 000000000..11aef7c24
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting01aSWT.java
@@ -0,0 +1,210 @@
+/**
+ * Copyright 2012 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.opengl.test.junit.newt.parenting;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLEventListener;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.jogamp.nativewindow.swt.SWTAccessor;
+import com.jogamp.newt.Window;
+import com.jogamp.newt.opengl.GLWindow;
+import com.jogamp.newt.swt.NewtCanvasSWT;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
+import com.jogamp.opengl.test.junit.util.MiscUtils;
+import com.jogamp.opengl.test.junit.util.UITestCase;
+
+/**
+ * Simple visibility test ..
+ */
+public class TestParenting01aSWT extends UITestCase {
+ static int width, height;
+ static long durationPerTest = 800;
+ static GLCapabilities glCaps;
+
+ Display display = null;
+ Shell shell = null;
+ Composite composite1 = null;
+
+ @BeforeClass
+ public static void initClass() {
+ width = 640;
+ height = 480;
+ glCaps = new GLCapabilities(null);
+ }
+
+ @Before
+ public void init() {
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ display = new Display();
+ Assert.assertNotNull( display );
+ shell = new Shell( display );
+ Assert.assertNotNull( shell );
+ shell.setLayout( new FillLayout() );
+ composite1 = new Composite( shell, SWT.NONE );
+ composite1.setLayout( new FillLayout() );
+ Assert.assertNotNull( composite1 );
+ }});
+ }
+
+ @After
+ public void release() {
+ Assert.assertNotNull( display );
+ Assert.assertNotNull( shell );
+ Assert.assertNotNull( composite1 );
+ try {
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ composite1.dispose();
+ shell.dispose();
+ display.dispose();
+ }});
+ }
+ catch( Throwable throwable ) {
+ throwable.printStackTrace();
+ Assume.assumeNoException( throwable );
+ }
+ display = null;
+ shell = null;
+ composite1 = null;
+ }
+
+ @Test
+ public void testWindowParenting01CreateVisibleDestroy1() throws InterruptedException, InvocationTargetException {
+
+ GLWindow glWindow1 = GLWindow.create(glCaps);
+ Assert.assertNotNull(glWindow1);
+ Assert.assertEquals(false, glWindow1.isVisible());
+ Assert.assertEquals(false, glWindow1.isNativeValid());
+ Assert.assertNull(glWindow1.getParent());
+ glWindow1.setTitle("testWindowParenting01CreateVisibleDestroy");
+ GLEventListener demo1 = new RedSquareES2();
+ setDemoFields(demo1, glWindow1, false);
+ glWindow1.addGLEventListener(demo1);
+
+ final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite1, 0, glWindow1 );
+ Assert.assertNotNull(canvas1);
+ Assert.assertEquals(false, glWindow1.isVisible());
+ Assert.assertEquals(false, glWindow1.isNativeValid());
+ Assert.assertNull(glWindow1.getParent());
+
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ shell.setText( getSimpleTestName(".") );
+ shell.setSize( 640, 480 );
+ shell.open();
+ }
+ });
+
+ // visible test
+ Assert.assertEquals(canvas1.getNativeWindow(),glWindow1.getParent());
+
+ for(int i=0; i*10<durationPerTest; i++) {
+ if( !display.readAndDispatch() ) {
+ // blocks on linux .. display.sleep();
+ Thread.sleep(10);
+ }
+ }
+
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ canvas1.setVisible(false);
+ }
+ });
+ Assert.assertEquals(true, glWindow1.isNativeValid());
+
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ canvas1.setVisible(true);
+ }
+ });
+ Assert.assertEquals(true, glWindow1.isNativeValid());
+
+ canvas1.dispose();
+
+ Assert.assertEquals(false, glWindow1.isNativeValid());
+ }
+
+ public static void setDemoFields(GLEventListener demo, GLWindow glWindow, boolean debug) {
+ Assert.assertNotNull(demo);
+ Assert.assertNotNull(glWindow);
+ Window window = glWindow.getDelegatedWindow();
+ if(debug) {
+ MiscUtils.setFieldIfExists(demo, "glDebug", true);
+ MiscUtils.setFieldIfExists(demo, "glTrace", true);
+ }
+ if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
+ MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
+ }
+ }
+
+ static int atoi(String a) {
+ int i=0;
+ try {
+ i = Integer.parseInt(a);
+ } catch (Exception ex) { ex.printStackTrace(); }
+ return i;
+ }
+
+ public static void main(String args[]) throws IOException {
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ durationPerTest = atoi(args[++i]);
+ }
+ }
+ String tstname = TestParenting01aSWT.class.getName();
+ org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
+ tstname,
+ "filtertrace=true",
+ "haltOnError=false",
+ "haltOnFailure=false",
+ "showoutput=true",
+ "outputtoformatters=true",
+ "logfailedtests=true",
+ "logtestlistenerevents=true",
+ "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
+ "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
+ }
+
+}
diff --git a/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04AWT.java b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04AWT.java
new file mode 100644
index 000000000..09b6d048c
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04AWT.java
@@ -0,0 +1,239 @@
+/**
+ * Copyright 2010 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.opengl.test.junit.newt.parenting;
+
+import java.awt.BorderLayout;
+import java.awt.Button;
+import java.awt.Frame;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLEventListener;
+import javax.swing.SwingUtilities;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.jogamp.newt.Window;
+import com.jogamp.newt.awt.NewtCanvasAWT;
+import com.jogamp.newt.opengl.GLWindow;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
+import com.jogamp.opengl.test.junit.util.MiscUtils;
+import com.jogamp.opengl.test.junit.util.UITestCase;
+import com.jogamp.opengl.util.Animator;
+
+/**
+ * Using {@link NewtCanvasAWT#setNEWTChild(Window)} for reparenting, i.e. NEWT/AWT hopping
+ */
+public class TestParenting04AWT extends UITestCase {
+ static int width, height;
+ static long durationPerTest = 800;
+ static GLCapabilities glCaps;
+
+ @BeforeClass
+ public static void initClass() {
+ width = 400;
+ height = 400;
+ glCaps = new GLCapabilities(null);
+ }
+
+ @Test
+ public void winHopFrame2FrameDirectHop() throws InterruptedException, InvocationTargetException {
+ // Will produce some artifacts .. resizing etc
+ winHopFrame2Frame(false);
+ }
+
+ @Test
+ public void winHopFrame2FrameDetachFirst() throws InterruptedException, InvocationTargetException {
+ // Note: detaching first setNEWTChild(null) is much cleaner visually
+ winHopFrame2Frame(true);
+ }
+
+ protected void winHopFrame2Frame(final boolean detachFirst) throws InterruptedException, InvocationTargetException {
+ final GLWindow glWindow1 = GLWindow.create(glCaps);
+ GLEventListener demo1 = new RedSquareES2();
+ setDemoFields(demo1, glWindow1, false);
+ glWindow1.addGLEventListener(demo1);
+ Animator anim1 = new Animator(glWindow1);
+
+ final GLWindow glWindow2 = GLWindow.create(glCaps);
+ GLEventListener demo2 = new GearsES2();
+ setDemoFields(demo2, glWindow2, false);
+ glWindow2.addGLEventListener(demo2);
+ Animator anim2 = new Animator(glWindow2);
+
+ final NewtCanvasAWT canvas1 = new NewtCanvasAWT(glWindow1);
+ final NewtCanvasAWT canvas2 = new NewtCanvasAWT(glWindow2);
+
+ final Frame frame1 = new Frame("AWT Parent Frame");
+ frame1.setLayout(new BorderLayout());
+ frame1.add(new Button("North"), BorderLayout.NORTH);
+ frame1.add(new Button("South"), BorderLayout.SOUTH);
+ frame1.add(new Button("East"), BorderLayout.EAST);
+ frame1.add(new Button("West"), BorderLayout.WEST);
+ frame1.setSize(width, height);
+ frame1.setLocation(0, 0);
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame1.setVisible(true);
+ frame1.validate();
+ }
+ });
+
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame1.add(canvas1, BorderLayout.CENTER);
+ frame1.validate();
+ }
+ });
+ Assert.assertEquals(canvas1.getNativeWindow(),glWindow1.getParent());
+
+ final Frame frame2 = new Frame("AWT Parent Frame");
+ frame2.setLayout(new BorderLayout());
+ frame2.add(new Button("North"), BorderLayout.NORTH);
+ frame2.add(new Button("South"), BorderLayout.SOUTH);
+ frame2.add(new Button("East"), BorderLayout.EAST);
+ frame2.add(new Button("West"), BorderLayout.WEST);
+ frame2.setSize(width, height);
+ frame2.setLocation(width+50, 0);
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame2.setVisible(true);
+ frame2.validate();
+ }
+ });
+
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame2.add(canvas2, BorderLayout.CENTER);
+ frame2.validate();
+ }
+ });
+ Assert.assertEquals(canvas2.getNativeWindow(),glWindow2.getParent());
+
+ anim1.start();
+ anim2.start();
+
+ int state;
+ for(state=0; state<3; state++) {
+ Thread.sleep(durationPerTest);
+ switch(state) {
+ case 0:
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ // 1 -> 2
+ if(detachFirst) {
+ canvas1.setNEWTChild(null);
+ canvas2.setNEWTChild(null);
+ } else {
+ canvas2.setNEWTChild(null); // free g2 of w2
+ }
+ canvas1.setNEWTChild(glWindow2); // put g2 -> w1. free g1 of w1
+ canvas2.setNEWTChild(glWindow1); // put g1 -> w2
+ frame1.validate();
+ frame2.validate();
+ }
+ });
+ break;
+ case 1:
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ // 2 -> 1
+ if(detachFirst) {
+ canvas1.setNEWTChild(null);
+ canvas2.setNEWTChild(null);
+ } else {
+ canvas2.setNEWTChild(null);
+ }
+ canvas1.setNEWTChild(glWindow1);
+ canvas2.setNEWTChild(glWindow2);
+ frame1.validate();
+ frame2.validate();
+ }
+ });
+ break;
+ }
+ }
+
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame1.dispose();
+ frame2.dispose();
+ } } );
+ glWindow1.destroy();
+ glWindow2.destroy();
+ Assert.assertEquals(false, glWindow1.isNativeValid());
+ Assert.assertEquals(false, glWindow2.isNativeValid());
+ }
+
+ public static void setDemoFields(GLEventListener demo, GLWindow glWindow, boolean debug) {
+ Assert.assertNotNull(demo);
+ Assert.assertNotNull(glWindow);
+ Window window = glWindow.getDelegatedWindow();
+ if(debug) {
+ MiscUtils.setFieldIfExists(demo, "glDebug", true);
+ MiscUtils.setFieldIfExists(demo, "glTrace", true);
+ }
+ if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
+ MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
+ }
+ }
+
+ static int atoi(String a) {
+ int i=0;
+ try {
+ i = Integer.parseInt(a);
+ } catch (Exception ex) { ex.printStackTrace(); }
+ return i;
+ }
+
+ public static void main(String args[]) throws IOException {
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ durationPerTest = atoi(args[++i]);
+ }
+ }
+ String tstname = TestParenting04AWT.class.getName();
+ org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
+ tstname,
+ "filtertrace=true",
+ "haltOnError=false",
+ "haltOnFailure=false",
+ "showoutput=true",
+ "outputtoformatters=true",
+ "logfailedtests=true",
+ "logtestlistenerevents=true",
+ "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
+ "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
+ }
+
+}
diff --git a/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04SWT.java b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04SWT.java
new file mode 100644
index 000000000..14a36c573
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/newt/parenting/TestParenting04SWT.java
@@ -0,0 +1,264 @@
+/**
+ * Copyright 2010 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.opengl.test.junit.newt.parenting;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLEventListener;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.jogamp.nativewindow.swt.SWTAccessor;
+import com.jogamp.newt.Window;
+import com.jogamp.newt.opengl.GLWindow;
+import com.jogamp.newt.swt.NewtCanvasSWT;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
+import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
+import com.jogamp.opengl.test.junit.util.MiscUtils;
+import com.jogamp.opengl.test.junit.util.UITestCase;
+import com.jogamp.opengl.util.Animator;
+
+/**
+ * Using {@link NewtCanvasSWT#setNEWTChild(Window)} for reparenting, i.e. NEWT/AWT hopping
+ */
+public class TestParenting04SWT extends UITestCase {
+ static int width, height;
+ static long durationPerTest = 800;
+ static GLCapabilities glCaps;
+
+ Display display = null;
+ Shell shell1 = null;
+ Shell shell2 = null;
+ Composite composite1 = null;
+ Composite composite2 = null;
+
+ @BeforeClass
+ public static void initClass() {
+ width = 400;
+ height = 400;
+ glCaps = new GLCapabilities(null);
+ }
+
+ @Before
+ public void init() {
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ display = new Display();
+ Assert.assertNotNull( display );
+
+ shell1 = new Shell( display );
+ Assert.assertNotNull( shell1 );
+ shell1.setLayout( new FillLayout() );
+ composite1 = new Composite( shell1, SWT.NONE );
+ composite1.setLayout( new FillLayout() );
+ Assert.assertNotNull( composite1 );
+
+ shell2 = new Shell( display );
+ Assert.assertNotNull( shell2 );
+ shell2.setLayout( new FillLayout() );
+ composite2 = new Composite( shell2, SWT.NONE );
+ composite2.setLayout( new FillLayout() );
+ Assert.assertNotNull( composite2 );
+ }});
+ }
+
+ @After
+ public void release() {
+ Assert.assertNotNull( display );
+ Assert.assertNotNull( shell1 );
+ Assert.assertNotNull( shell2 );
+ Assert.assertNotNull( composite1 );
+ Assert.assertNotNull( composite2 );
+ try {
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ composite1.dispose();
+ composite2.dispose();
+ shell1.dispose();
+ shell2.dispose();
+ display.dispose();
+ }});
+ }
+ catch( Throwable throwable ) {
+ throwable.printStackTrace();
+ Assume.assumeNoException( throwable );
+ }
+ display = null;
+ shell1 = null;
+ shell2 = null;
+ composite1 = null;
+ composite2 = null;
+ }
+
+ @Test
+ public void winHopFrame2FrameDirectHop() throws InterruptedException, InvocationTargetException {
+ // Will produce some artifacts .. resizing etc
+ winHopFrame2Frame(false);
+ }
+
+ @Test
+ public void winHopFrame2FrameDetachFirst() throws InterruptedException, InvocationTargetException {
+ // Note: detaching first setNEWTChild(null) is much cleaner visually
+ winHopFrame2Frame(true);
+ }
+
+ protected void winHopFrame2Frame(final boolean detachFirst) throws InterruptedException, InvocationTargetException {
+ final GLWindow glWindow1 = GLWindow.create(glCaps);
+ GLEventListener demo1 = new RedSquareES2();
+ setDemoFields(demo1, glWindow1, false);
+ glWindow1.addGLEventListener(demo1);
+ Animator anim1 = new Animator(glWindow1);
+
+ final GLWindow glWindow2 = GLWindow.create(glCaps);
+ GLEventListener demo2 = new GearsES2();
+ setDemoFields(demo2, glWindow2, false);
+ glWindow2.addGLEventListener(demo2);
+ Animator anim2 = new Animator(glWindow2);
+
+ final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite1, 0, glWindow1 );
+ final NewtCanvasSWT canvas2 = NewtCanvasSWT.create( composite2, 0, glWindow2 );
+
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ shell1.setText( getSimpleTestName(".")+"-Win1" );
+ shell1.setSize( width, height);
+ shell1.setLocation(0, 0);
+ shell1.open();
+ shell2.setText( getSimpleTestName(".")+"-Win2" );
+ shell2.setSize( width, height);
+ shell2.setLocation(width + 50, 0);
+ shell2.open();
+ }
+ });
+ Assert.assertEquals(canvas1.getNativeWindow(),glWindow1.getParent());
+ Assert.assertEquals(canvas2.getNativeWindow(),glWindow2.getParent());
+
+ anim1.start();
+ anim2.start();
+
+ int state;
+ for(state=0; state<3; state++) {
+ for(int i=0; i*10<durationPerTest; i++) {
+ if( !display.readAndDispatch() ) {
+ // blocks on linux .. display.sleep();
+ Thread.sleep(10);
+ }
+ }
+ switch(state) {
+ case 0:
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ // 1 -> 2
+ if(detachFirst) {
+ canvas1.setNEWTChild(null);
+ canvas2.setNEWTChild(null);
+ } else {
+ canvas2.setNEWTChild(null); // free g2 of w2
+ }
+ canvas1.setNEWTChild(glWindow2); // put g2 -> w1. free g1 of w1
+ canvas2.setNEWTChild(glWindow1); // put g1 -> w2
+ } } );
+ break;
+ case 1:
+ SWTAccessor.invoke(true, new Runnable() {
+ public void run() {
+ // 2 -> 1
+ if(detachFirst) {
+ canvas1.setNEWTChild(null);
+ canvas2.setNEWTChild(null);
+ } else {
+ canvas2.setNEWTChild(null);
+ }
+ canvas1.setNEWTChild(glWindow1);
+ canvas2.setNEWTChild(glWindow2);
+ } } );
+ break;
+ }
+ }
+
+ canvas1.dispose();
+ canvas2.dispose();
+ Assert.assertEquals(false, glWindow1.isNativeValid());
+ Assert.assertEquals(false, glWindow2.isNativeValid());
+ }
+
+ public static void setDemoFields(GLEventListener demo, GLWindow glWindow, boolean debug) {
+ Assert.assertNotNull(demo);
+ Assert.assertNotNull(glWindow);
+ Window window = glWindow.getDelegatedWindow();
+ if(debug) {
+ MiscUtils.setFieldIfExists(demo, "glDebug", true);
+ MiscUtils.setFieldIfExists(demo, "glTrace", true);
+ }
+ if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
+ MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
+ }
+ }
+
+ static int atoi(String a) {
+ int i=0;
+ try {
+ i = Integer.parseInt(a);
+ } catch (Exception ex) { ex.printStackTrace(); }
+ return i;
+ }
+
+ public static void main(String args[]) throws IOException {
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ durationPerTest = atoi(args[++i]);
+ }
+ }
+ String tstname = TestParenting04SWT.class.getName();
+ org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
+ tstname,
+ "filtertrace=true",
+ "haltOnError=false",
+ "haltOnFailure=false",
+ "showoutput=true",
+ "outputtoformatters=true",
+ "logfailedtests=true",
+ "logtestlistenerevents=true",
+ "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
+ "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
+ }
+
+}