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 
29 package com.jogamp.opengl.test.junit.newt.parenting;
30 
31 import java.io.IOException;
32 import java.lang.reflect.InvocationTargetException;
33 
34 import com.jogamp.opengl.GLCapabilities;
35 import com.jogamp.opengl.GLEventListener;
36 
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.layout.FillLayout;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.Shell;
42 import org.junit.After;
43 import org.junit.Assert;
44 import org.junit.Assume;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.junit.FixMethodOrder;
49 import org.junit.runners.MethodSorters;
50 
51 import com.jogamp.nativewindow.swt.SWTAccessor;
52 import com.jogamp.newt.NewtFactory;
53 import com.jogamp.newt.Window;
54 import com.jogamp.newt.opengl.GLWindow;
55 import com.jogamp.newt.swt.NewtCanvasSWT;
56 import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
57 import com.jogamp.opengl.test.junit.util.MiscUtils;
58 import com.jogamp.opengl.test.junit.util.UITestCase;
59 
60 /**
61  * Simple visibility test ..
62  */
63 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
64 public class TestParenting01aSWT extends UITestCase {
65     static int width, height;
66     static long durationPerTest = 800;
67     static GLCapabilities glCaps;
68 
69     Display display = null;
70     Shell shell = null;
71     Composite composite1 = null;
72     com.jogamp.newt.Display swtNewtDisplay = null;
73 
74     @BeforeClass
initClass()75     public static void initClass() {
76         width  = 640;
77         height = 480;
78         glCaps = new GLCapabilities(null);
79     }
80 
81     @Before
init()82     public void init() {
83         SWTAccessor.invoke(true, new Runnable() {
84             public void run() {
85                 display = new Display();
86                 Assert.assertNotNull( display );
87                 shell = new Shell( display );
88                 Assert.assertNotNull( shell );
89                 shell.setLayout( new FillLayout() );
90                 composite1 = new Composite( shell, SWT.NONE );
91                 composite1.setLayout( new FillLayout() );
92                 Assert.assertNotNull( composite1 );
93             }});
94         swtNewtDisplay = NewtFactory.createDisplay(null, false); // no-reuse
95     }
96 
97     @After
release()98     public void release() {
99         Assert.assertNotNull( display );
100         Assert.assertNotNull( shell );
101         Assert.assertNotNull( composite1 );
102         try {
103             SWTAccessor.invoke(true, new Runnable() {
104                public void run() {
105                 composite1.dispose();
106                 shell.dispose();
107                 display.dispose();
108                }});
109         }
110         catch( final Throwable throwable ) {
111             throwable.printStackTrace();
112             Assume.assumeNoException( throwable );
113         }
114         swtNewtDisplay = null;
115         display = null;
116         shell = null;
117         composite1 = null;
118     }
119 
120     @Test
testWindowParenting01CreateVisibleDestroy1()121     public void testWindowParenting01CreateVisibleDestroy1() throws InterruptedException, InvocationTargetException {
122 
123         final com.jogamp.newt.Screen screen = NewtFactory.createScreen(swtNewtDisplay, 0);
124         final GLWindow glWindow1 = GLWindow.create(screen, glCaps);
125         Assert.assertNotNull(glWindow1);
126         Assert.assertEquals(false, glWindow1.isVisible());
127         Assert.assertEquals(false, glWindow1.isNativeValid());
128         Assert.assertNull(glWindow1.getParent());
129         glWindow1.setTitle("testWindowParenting01CreateVisibleDestroy");
130         final GLEventListener demo1 = new RedSquareES2();
131         setDemoFields(demo1, glWindow1, false);
132         glWindow1.addGLEventListener(demo1);
133 
134         final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite1, 0, glWindow1 );
135         Assert.assertNotNull(canvas1);
136         Assert.assertEquals(false, glWindow1.isVisible());
137         Assert.assertEquals(false, glWindow1.isNativeValid());
138         Assert.assertNull(glWindow1.getParent());
139 
140         SWTAccessor.invoke(true, new Runnable() {
141            public void run() {
142               shell.setText( getSimpleTestName(".") );
143               shell.setSize( 640, 480 );
144               shell.open();
145            }
146         });
147 
148         // visible test
149         Assert.assertEquals(canvas1.getNativeWindow(),glWindow1.getParent());
150 
151         for(int i=0; i*10<durationPerTest; i++) {
152             if( !display.readAndDispatch() ) {
153                 // blocks on linux .. display.sleep();
154                 Thread.sleep(10);
155             }
156         }
157 
158         SWTAccessor.invoke(true, new Runnable() {
159            public void run() {
160                canvas1.setVisible(false);
161            }
162         });
163         Assert.assertEquals(true, glWindow1.isNativeValid());
164 
165         SWTAccessor.invoke(true, new Runnable() {
166            public void run() {
167                canvas1.setVisible(true);
168            }
169         });
170         Assert.assertEquals(true, glWindow1.isNativeValid());
171 
172         canvas1.dispose();
173 
174         Assert.assertEquals(false, glWindow1.isNativeValid());
175     }
176 
setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug)177     public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
178         Assert.assertNotNull(demo);
179         Assert.assertNotNull(glWindow);
180         final Window window = glWindow.getDelegatedWindow();
181         if(debug) {
182             MiscUtils.setFieldIfExists(demo, "glDebug", true);
183             MiscUtils.setFieldIfExists(demo, "glTrace", true);
184         }
185         if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
186             MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
187         }
188     }
189 
atoi(final String a)190     static int atoi(final String a) {
191         int i=0;
192         try {
193             i = Integer.parseInt(a);
194         } catch (final Exception ex) { ex.printStackTrace(); }
195         return i;
196     }
197 
main(final String args[])198     public static void main(final String args[]) throws IOException {
199         for(int i=0; i<args.length; i++) {
200             if(args[i].equals("-time")) {
201                 durationPerTest = atoi(args[++i]);
202             }
203         }
204         final String tstname = TestParenting01aSWT.class.getName();
205         org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
206             tstname,
207             "filtertrace=true",
208             "haltOnError=false",
209             "haltOnFailure=false",
210             "showoutput=true",
211             "outputtoformatters=true",
212             "logfailedtests=true",
213             "logtestlistenerevents=true",
214             "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
215             "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
216     }
217 
218 }
219