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