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 org.junit.Assert;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.FixMethodOrder;
35 import org.junit.runners.MethodSorters;
36 
37 import java.awt.Button;
38 import java.awt.BorderLayout;
39 import java.awt.Frame;
40 
41 import com.jogamp.opengl.*;
42 import javax.swing.SwingUtilities;
43 
44 import com.jogamp.opengl.util.Animator;
45 import com.jogamp.opengl.util.FPSAnimator;
46 import com.jogamp.newt.*;
47 import com.jogamp.newt.opengl.*;
48 import com.jogamp.newt.awt.NewtCanvasAWT;
49 
50 import java.io.IOException;
51 import java.lang.reflect.InvocationTargetException;
52 
53 import com.jogamp.opengl.test.junit.util.*;
54 import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
55 
56 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
57 public class TestParenting01bAWT extends UITestCase {
58     static int width, height;
59     static long durationPerTest = 800;
60     static long waitReparent = 0;
61     static GLCapabilities glCaps;
62 
63     @BeforeClass
initClass()64     public static void initClass() {
65         width  = 640;
66         height = 480;
67         glCaps = new GLCapabilities(null);
68     }
69 
70     @Test
test01AWTWinHopFrame2FrameFPS25Animator()71     public void test01AWTWinHopFrame2FrameFPS25Animator() throws InterruptedException, InvocationTargetException {
72         testAWTWinHopFrame2FrameImpl(25);
73     }
74 
75     @Test
test02AWTWinHopFrame2FrameStdAnimator()76     public void test02AWTWinHopFrame2FrameStdAnimator() throws InterruptedException, InvocationTargetException {
77         testAWTWinHopFrame2FrameImpl(0);
78     }
79 
testAWTWinHopFrame2FrameImpl(final int fps)80     public void testAWTWinHopFrame2FrameImpl(final int fps) throws InterruptedException, InvocationTargetException {
81         final GLWindow glWindow1 = GLWindow.create(glCaps);
82         glWindow1.setUndecorated(true);
83         final GLEventListener demo1 = new RedSquareES2();
84         setDemoFields(demo1, glWindow1, false);
85         glWindow1.addGLEventListener(demo1);
86 
87         final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
88 
89         final Frame frame1 = new Frame("AWT Parent Frame");
90         frame1.setLayout(new BorderLayout());
91         frame1.add(new Button("North"), BorderLayout.NORTH);
92         frame1.add(new Button("South"), BorderLayout.SOUTH);
93         frame1.add(new Button("East"), BorderLayout.EAST);
94         frame1.add(new Button("West"), BorderLayout.WEST);
95         SwingUtilities.invokeAndWait(new Runnable() {
96            public void run() {
97                frame1.setSize(width, height);
98                frame1.setLocation(0, 0);
99                frame1.setVisible(true);
100            }
101         });
102 
103         final Frame frame2 = new Frame("AWT Parent Frame");
104         frame2.setLayout(new BorderLayout());
105         frame2.add(new Button("North"), BorderLayout.NORTH);
106         frame2.add(new Button("South"), BorderLayout.SOUTH);
107         frame2.add(new Button("East"), BorderLayout.EAST);
108         frame2.add(new Button("West"), BorderLayout.WEST);
109         SwingUtilities.invokeAndWait(new Runnable() {
110            public void run() {
111                frame2.setSize(width, height);
112                frame2.setLocation(640, 480);
113                frame2.setVisible(true);
114            }
115         });
116 
117         SwingUtilities.invokeAndWait(new Runnable() {
118            public void run() {
119                frame1.add(newtCanvasAWT, BorderLayout.CENTER);
120                frame1.validate();
121            }
122         });
123         Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
124 
125         GLAnimatorControl animator1;
126         if(fps>0) {
127             animator1 = new FPSAnimator(glWindow1, fps);
128         } else {
129             animator1 = new Animator(glWindow1);
130         }
131         animator1.start();
132 
133         int state;
134         for(state=0; state<3; state++) {
135             Thread.sleep(durationPerTest);
136             switch(state) {
137                 case 0:
138                     SwingUtilities.invokeAndWait(new Runnable() {
139                         public void run() {
140                             frame1.remove(newtCanvasAWT);
141                             frame2.add(newtCanvasAWT, BorderLayout.CENTER);
142                             frame1.validate();
143                             frame2.validate();
144                         }
145                     });
146                     break;
147                 case 1:
148                     SwingUtilities.invokeAndWait(new Runnable() {
149                         public void run() {
150                             frame2.remove(newtCanvasAWT);
151                             frame1.add(newtCanvasAWT, BorderLayout.CENTER);
152                             frame2.validate();
153                             frame1.validate();
154                         }
155                     });
156                     break;
157             }
158         }
159 
160         Assert.assertEquals(true, animator1.isAnimating());
161         Assert.assertEquals(false, animator1.isPaused());
162         Assert.assertNotNull(animator1.getThread());
163         animator1.stop();
164         Assert.assertEquals(false, animator1.isAnimating());
165         Assert.assertEquals(false, animator1.isPaused());
166         Assert.assertEquals(null, animator1.getThread());
167 
168         SwingUtilities.invokeAndWait(new Runnable() {
169             public void run() {
170                 frame1.dispose();
171                 frame2.dispose();
172             } } );
173         glWindow1.destroy();
174     }
175 
setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug)176     public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
177         Assert.assertNotNull(demo);
178         Assert.assertNotNull(glWindow);
179         final Window window = glWindow.getDelegatedWindow();
180         if(debug) {
181             MiscUtils.setFieldIfExists(demo, "glDebug", true);
182             MiscUtils.setFieldIfExists(demo, "glTrace", true);
183         }
184         if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
185             MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
186         }
187     }
188 
atoi(final String a)189     static int atoi(final String a) {
190         int i=0;
191         try {
192             i = Integer.parseInt(a);
193         } catch (final Exception ex) { ex.printStackTrace(); }
194         return i;
195     }
196 
main(final String args[])197     public static void main(final String args[]) throws IOException {
198         for(int i=0; i<args.length; i++) {
199             if(args[i].equals("-time")) {
200                 durationPerTest = atoi(args[++i]);
201             } else if(args[i].equals("-wait")) {
202                 waitReparent = atoi(args[++i]);
203             }
204         }
205         final String tstname = TestParenting01bAWT.class.getName();
206         org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
207             tstname,
208             "filtertrace=true",
209             "haltOnError=false",
210             "haltOnFailure=false",
211             "showoutput=true",
212             "outputtoformatters=true",
213             "logfailedtests=true",
214             "logtestlistenerevents=true",
215             "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
216             "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
217     }
218 
219 }
220