Jogamp
Bump scripts to jre1.8.0_66
[jogl.git] / src / test / com / jogamp / opengl / test / junit / newt / parenting / TestParenting01cSwingAWT.java
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
37 import java.awt.Button;
38 import java.awt.BorderLayout;
39 import java.awt.Container;
40 import javax.swing.JFrame;
41 import javax.swing.JPanel;
42 import javax.swing.SwingUtilities;
43
44 import javax.media.opengl.*;
45
46 import com.jogamp.opengl.util.Animator;
47 import com.jogamp.newt.*;
48 import com.jogamp.newt.opengl.*;
49 import com.jogamp.newt.awt.NewtCanvasAWT;
50
51 import java.io.IOException;
52
53 import com.jogamp.opengl.test.junit.util.*;
54 import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
55
56 public class TestParenting01cSwingAWT extends UITestCase {
57     static int width, height;
58     static long durationPerTest = 800;
59     static long waitReparent = 0;
60     static GLCapabilities glCaps;
61
62     @BeforeClass
63     public static void initClass() {
64         width  = 640;
65         height = 480;
66         glCaps = new GLCapabilities(null);
67     }
68
69     @Test
70     public void testWindowParenting01CreateVisibleDestroy1() throws InterruptedException, InvocationTargetException {
71         /**
72          * JFrame . JPanel . Container . NewtCanvasAWT . GLWindow
73          */
74         GLWindow glWindow1 = GLWindow.create(glCaps);
75         Assert.assertNotNull(glWindow1);
76         Assert.assertEquals(false, glWindow1.isVisible());
77         Assert.assertEquals(false, glWindow1.isNativeValid());
78         Assert.assertNull(glWindow1.getParent());
79         glWindow1.setTitle("testWindowParenting01CreateVisibleDestroy");
80         GLEventListener demo1 = new RedSquareES2();
81         setDemoFields(demo1, glWindow1, false);
82         glWindow1.addGLEventListener(demo1);
83         Animator animator1 = new Animator(glWindow1);
84         animator1.setUpdateFPSFrames(1, null);
85         animator1.start();
86         
87         final GLWindow _glWindow1 = glWindow1;
88         final GLRunnable _glRunnable = new GLRunnableDummy();
89         Thread disturbanceThread = new Thread(new Runnable() {
90             public void run() {
91                 System.out.println("$");
92                 while(true) 
93                 {
94                    try {
95                        _glWindow1.invoke(true, _glRunnable);
96                        Thread.sleep(100);
97                    } catch (Throwable t) {}
98                }
99             }
100         });
101         disturbanceThread.start();
102
103
104         final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
105         Assert.assertNotNull(newtCanvasAWT);
106         Assert.assertEquals(false, glWindow1.isVisible());
107         Assert.assertEquals(false, glWindow1.isNativeValid());
108         Assert.assertNull(glWindow1.getParent());
109
110         final Container container1 = new Container();
111         container1.setLayout(new BorderLayout());
112         container1.add(new Button("north"), BorderLayout.NORTH);
113         container1.add(new Button("south"), BorderLayout.SOUTH);
114         container1.add(new Button("east"), BorderLayout.EAST);
115         container1.add(new Button("west"), BorderLayout.WEST);
116         container1.add(newtCanvasAWT, BorderLayout.CENTER);
117
118         final JPanel jPanel1 = new JPanel();
119         jPanel1.setLayout(new BorderLayout());
120         jPanel1.add(new Button("north"), BorderLayout.NORTH);
121         jPanel1.add(new Button("south"), BorderLayout.SOUTH);
122         jPanel1.add(new Button("east"), BorderLayout.EAST);
123         jPanel1.add(new Button("west"), BorderLayout.WEST);
124         jPanel1.add(container1, BorderLayout.CENTER);
125
126         final JFrame jFrame1 = new JFrame("Swing Parent JFrame");
127         // jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
128         jFrame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // equivalent to Frame, use windowClosing event!
129         jFrame1.setContentPane(jPanel1);
130         jFrame1.setSize(width, height);
131         System.out.println("Demos: 1 - Visible");
132         jFrame1.setVisible(true); // from here on, we need to run modifications on EDT
133
134         // visible test
135         Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
136
137         while(animator1.isAnimating() && animator1.getTotalFPSDuration()<durationPerTest) {
138             Thread.sleep(100);
139         }
140         System.out.println("Demos: 2 - StopAnimator");
141         animator1.stop();
142         Assert.assertEquals(false, animator1.isAnimating());
143
144         SwingUtilities.invokeAndWait(new Runnable() {
145                 public void run() {
146             System.out.println("Demos: 3 - !Visible");
147                     jFrame1.setVisible(false);
148                 } });
149         Assert.assertEquals(true, glWindow1.isNativeValid());
150
151         SwingUtilities.invokeAndWait(new Runnable() {
152                 public void run() {
153             System.out.println("Demos: 4 - Visible");
154                     jFrame1.setVisible(true);
155                 } });
156         Assert.assertEquals(true, glWindow1.isNativeValid());
157
158         SwingUtilities.invokeAndWait(new Runnable() {
159                 public void run() {
160             System.out.println("Demos: 5 - X Container");
161                     jPanel1.remove(container1);
162                     jFrame1.validate();
163                 } });
164         // Assert.assertNull(glWindow1.getParent());
165         Assert.assertEquals(true, glWindow1.isNativeValid());
166
167         SwingUtilities.invokeAndWait(new Runnable() {
168                 public void run() {
169                     jFrame1.dispose();
170                 } });
171         Assert.assertEquals(true, glWindow1.isNativeValid());
172
173         glWindow1.destroy();
174         Assert.assertEquals(false, glWindow1.isNativeValid());
175     }
176
177     @Test
178     public void testWindowParenting05ReparentAWTWinHopFrame2Frame() throws InterruptedException, InvocationTargetException {
179         /**
180          * JFrame . JPanel . Container . NewtCanvasAWT . GLWindow
181          */
182         GLWindow glWindow1 = GLWindow.create(glCaps);
183         Assert.assertNotNull(glWindow1);
184         Assert.assertEquals(false, glWindow1.isVisible());
185         Assert.assertEquals(false, glWindow1.isNativeValid());
186         Assert.assertNull(glWindow1.getParent());
187         glWindow1.setTitle("testWindowParenting01CreateVisibleDestroy");
188         GLEventListener demo1 = new RedSquareES2();
189         setDemoFields(demo1, glWindow1, false);
190         glWindow1.addGLEventListener(demo1);
191         Animator animator1 = new Animator(glWindow1);
192         animator1.setUpdateFPSFrames(1, null);
193         animator1.start();
194         
195         final GLWindow _glWindow1 = glWindow1;
196         final GLRunnable _glRunnable = new GLRunnableDummy();
197         Thread disturbanceThread = new Thread(new Runnable() {
198             public void run() {
199                 System.out.println("$");
200                 while(true) 
201                 {
202                    try {
203                        _glWindow1.invoke(true, _glRunnable);
204                        Thread.sleep(100);
205                    } catch (Throwable t) {}
206                }
207             }
208         });
209         disturbanceThread.start();
210
211         final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1);
212         Assert.assertNotNull(newtCanvasAWT);
213         Assert.assertEquals(false, glWindow1.isVisible());
214         Assert.assertEquals(false, glWindow1.isNativeValid());
215         Assert.assertNull(glWindow1.getParent());
216
217         final Container container1 = new Container();
218         container1.setLayout(new BorderLayout());
219         container1.add(new Button("north"), BorderLayout.NORTH);
220         container1.add(new Button("south"), BorderLayout.SOUTH);
221         container1.add(new Button("east"), BorderLayout.EAST);
222         container1.add(new Button("west"), BorderLayout.WEST);
223         container1.add(newtCanvasAWT, BorderLayout.CENTER);
224
225         final JPanel jPanel1 = new JPanel();
226         jPanel1.setLayout(new BorderLayout());
227         jPanel1.add(new Button("north"), BorderLayout.NORTH);
228         jPanel1.add(new Button("south"), BorderLayout.SOUTH);
229         jPanel1.add(new Button("east"), BorderLayout.EAST);
230         jPanel1.add(new Button("west"), BorderLayout.WEST);
231         jPanel1.add(container1, BorderLayout.CENTER);
232
233         final JFrame jFrame1 = new JFrame("Swing Parent JFrame");
234         // jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
235         jFrame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // equivalent to Frame, use windowClosing event!
236         jFrame1.setContentPane(jPanel1);
237         jFrame1.setLocation(0, 0);
238         jFrame1.setSize(width, height);
239         jFrame1.setVisible(true); // from here on, we need to run modifications on EDT
240
241         final JPanel jPanel2 = new JPanel();
242         jPanel2.setLayout(new BorderLayout());
243         jPanel2.add(new Button("north"), BorderLayout.NORTH);
244         jPanel2.add(new Button("south"), BorderLayout.SOUTH);
245         jPanel2.add(new Button("east"), BorderLayout.EAST);
246         jPanel2.add(new Button("west"), BorderLayout.WEST);
247
248         final JFrame jFrame2 = new JFrame("Swing Parent JFrame");
249         // jFrame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
250         jFrame2.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // equivalent to Frame, use windowClosing event!
251         jFrame2.setContentPane(jPanel2);
252         jFrame2.setLocation(640, 480);
253         jFrame2.setSize(width, height);
254         jFrame2.setVisible(true); // from here on, we need to run modifications on EDT
255
256         // visible test
257         Assert.assertEquals(newtCanvasAWT.getNativeWindow(),glWindow1.getParent());
258
259         int state = 0;
260         while(animator1.isAnimating() && animator1.getTotalFPSDuration()<3*durationPerTest) {
261             Thread.sleep(durationPerTest);
262             switch(state) {
263                 case 0:
264                     SwingUtilities.invokeAndWait(new Runnable() {
265                             public void run() {
266                                 container1.remove(newtCanvasAWT);
267                                 jPanel2.add(newtCanvasAWT, BorderLayout.CENTER);
268                                 jFrame1.validate();
269                                 jFrame2.validate();
270                             } });
271                     break;
272                 case 1:
273                     SwingUtilities.invokeAndWait(new Runnable() {
274                             public void run() {
275                                 jPanel2.remove(newtCanvasAWT);
276                                 container1.add(newtCanvasAWT, BorderLayout.CENTER);
277                                 jFrame1.validate();
278                                 jFrame2.validate();                                
279                             } });
280                     break;
281             }
282             state++;
283         }
284
285         animator1.stop();
286         Assert.assertEquals(false, animator1.isAnimating());
287
288         SwingUtilities.invokeAndWait(new Runnable() {
289                 public void run() {
290                     jFrame1.setVisible(false);
291                     jFrame2.setVisible(false);
292                 } });
293         Assert.assertEquals(true, glWindow1.isNativeValid());
294
295         SwingUtilities.invokeAndWait(new Runnable() {
296                 public void run() {
297                     jFrame1.dispose();
298                     jFrame2.dispose();
299                 } });
300         Assert.assertEquals(true, glWindow1.isNativeValid());
301
302         glWindow1.destroy();
303         Assert.assertEquals(false, glWindow1.isNativeValid());
304     }
305
306     public static void setDemoFields(GLEventListener demo, GLWindow glWindow, boolean debug) {
307         Assert.assertNotNull(demo);
308         Assert.assertNotNull(glWindow);
309         Window window = glWindow.getWindow();
310         if(debug) {
311             MiscUtils.setFieldIfExists(demo, "glDebug", true);
312             MiscUtils.setFieldIfExists(demo, "glTrace", true);
313         }
314         if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
315             MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
316         }
317     }
318
319     static int atoi(String a) {
320         int i=0;
321         try {
322             i = Integer.parseInt(a);
323         } catch (Exception ex) { ex.printStackTrace(); }
324         return i;
325     }
326
327     public static void main(String args[]) throws IOException {
328         for(int i=0; i<args.length; i++) {
329             if(args[i].equals("-time")) {
330                 durationPerTest = atoi(args[++i]);
331             } else if(args[i].equals("-wait")) {
332                 waitReparent = atoi(args[++i]);
333             }
334         }
335         System.out.println("durationPerTest "+durationPerTest);
336         System.out.println("waitReparent "+waitReparent);
337         String tstname = TestParenting01cSwingAWT.class.getName();
338         org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
339             tstname,
340             "filtertrace=true",
341             "haltOnError=false",
342             "haltOnFailure=false",
343             "showoutput=true",
344             "outputtoformatters=true",
345             "logfailedtests=true",
346             "logtestlistenerevents=true",
347             "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
348             "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
349     }
350
351 }
http://JogAmp.org git info: FAQ, tutorial and man pages.