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