Jogamp
Bump scripts to jre1.8.0_66
[jogl.git] / src / test / com / jogamp / opengl / test / junit / newt / parenting / TestParenting02NEWT.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
30 package com.jogamp.opengl.test.junit.newt.parenting;
31
32 import org.junit.Assert;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35
36 import javax.media.opengl.*;
37 import javax.media.nativewindow.*;
38
39 import com.jogamp.newt.*;
40 import com.jogamp.newt.event.*;
41 import com.jogamp.newt.opengl.*;
42
43 import java.io.IOException;
44
45 import com.jogamp.opengl.test.junit.util.*;
46 import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
47 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
48 // import com.jogamp.opengl.test.junit.jogl.demos.es1.RedSquareES1;
49 // import com.jogamp.opengl.test.junit.jogl.demos.es1.GearsES1;
50
51 public class TestParenting02NEWT extends UITestCase {
52     static int width, height;
53     static long durationPerTest = 500;
54
55     @BeforeClass
56     public static void initClass() {
57         GLProfile.initSingleton(true);
58         width  = 640;
59         height = 480;
60     }
61
62     static Window createWindow(Screen screen, Capabilities caps) {
63         Assert.assertNotNull(caps);
64         Window window = NewtFactory.createWindow(screen, caps) ;
65         Assert.assertNotNull(window);
66         return window;
67     }
68
69     static Window createWindow(NativeWindow parent, Capabilities caps) {
70         Assert.assertNotNull(caps);
71         Window window = NewtFactory.createWindow(parent, caps);
72         window.setUndecorated(true);
73         Assert.assertNotNull(window);
74         return window;
75     }
76
77     static void destroyWindow(Display display, Screen screen, Window window, GLWindow glWindow) {
78         if(null!=glWindow) {
79             glWindow.destroy();
80         }
81         if(null!=window) {
82             window.destroy();
83         }
84         if(null!=screen) {
85             screen.destroy();
86         }
87         if(null!=display) {
88             display.destroy();
89         }
90     }
91
92     @Test
93     public void testWindowParenting01NewtOnNewtParentChildDraw() throws InterruptedException {
94         GLCapabilities caps = new GLCapabilities(null);
95         Assert.assertNotNull(caps);
96         Display display = NewtFactory.createDisplay(null); // local display
97         Assert.assertNotNull(display);
98         Screen screen  = NewtFactory.createScreen(display, 0); // screen 0
99         Assert.assertNotNull(screen);
100
101         int x = 1;
102         int y = 1;
103
104         NEWTEventFiFo eventFifo = new NEWTEventFiFo();
105
106         Window window1 = createWindow(screen, caps);
107         Assert.assertNotNull(window1);
108         GLWindow glWindow1 = GLWindow.create(window1);
109         Assert.assertNotNull(glWindow1);
110         glWindow1.setSize(width, height);
111         Assert.assertEquals(width,glWindow1.getWidth());
112         Assert.assertEquals(height,glWindow1.getHeight());
113         glWindow1.setTitle("testWindowParenting01NewtOnNewtParentChildDraw - PARENT");
114         glWindow1.setPosition(x,y);
115         glWindow1.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo)));
116         glWindow1.addWindowListener(new TraceWindowAdapter());
117
118         GLEventListener demo1 = new RedSquareES2();
119         setDemoFields(demo1, window1, glWindow1, false);
120         // glWindow1.addGLEventListener(demo1);
121
122         glWindow1.setVisible(true);
123         CapabilitiesImmutable capsChosen = glWindow1.getGraphicsConfiguration().getNativeGraphicsConfiguration().getChosenCapabilities();
124         Assert.assertNotNull(capsChosen);
125         Assert.assertTrue(capsChosen.isOnscreen()==true);
126
127         Window window2 = createWindow(window1, caps);
128         Assert.assertNotNull(window2);
129         GLWindow glWindow2 = GLWindow.create(window2);
130         Assert.assertNotNull(glWindow2);
131         glWindow2.setSize(width/2, height/2);
132         //Assert.assertEquals(width/2,glWindow2.getWidth());
133         //Assert.assertEquals(height/2,glWindow2.getHeight());
134         glWindow2.setTitle("testWindowParenting01NewtOnNewtParentChildDraw - CHILD");
135         glWindow2.setPosition(glWindow1.getWidth()/2, glWindow1.getHeight()/2);
136         glWindow2.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo)));
137         glWindow2.addWindowListener(new TraceWindowAdapter(new WindowAction(eventFifo)));
138         // glWindow2.addMouseListener(new TraceMouseAdapter());
139
140         GLEventListener demo2 = new GearsES2();
141         setDemoFields(demo2, window2, glWindow2, false);
142         // glWindow2.addGLEventListener(demo2);
143
144         glWindow2.setVisible(true);
145         capsChosen = glWindow2.getGraphicsConfiguration().getNativeGraphicsConfiguration().getChosenCapabilities();
146         Assert.assertNotNull(capsChosen);
147         Assert.assertTrue(capsChosen.isOnscreen()==true);
148
149         glWindow1.addGLEventListener(demo1);
150         glWindow2.addGLEventListener(demo2);
151
152         boolean shouldQuit = false;
153         long duration = durationPerTest;
154         long step = 20;
155         NEWTEvent event;
156
157         while (duration>0 && !shouldQuit) {
158             glWindow1.display();
159             glWindow2.display();
160             duration -= step;
161             x += 1;
162             y += 1;
163             // glWindow1.setPosition(x,y);
164             glWindow2.setPosition(glWindow1.getWidth()/2,glWindow1.getHeight()/2-y);
165             Thread.sleep(step);
166
167             while( null != ( event = (NEWTEvent) eventFifo.get() ) ) {
168                 Window source = (Window) event.getSource();
169                 if(WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY == event.getEventType()) {
170                     shouldQuit = true;
171                 } else if(event instanceof KeyEvent) {
172                     KeyEvent keyEvent = (KeyEvent) event;
173                     switch(keyEvent.getKeyChar()) {
174                         case 'q':
175                             shouldQuit = true;
176                             break;
177                         case 'f':
178                             source.setFullscreen(!source.isFullscreen());
179                             break;
180                     }
181                 } 
182             }
183         }
184         destroyWindow(null, null, window2, glWindow2);
185         destroyWindow(display, screen, window1, glWindow1);
186     }
187
188     public static void setDemoFields(GLEventListener demo, Window window, GLWindow glWindow, boolean debug) {
189         Assert.assertNotNull(demo);
190         Assert.assertNotNull(window);
191         if(debug) {
192             MiscUtils.setFieldIfExists(demo, "glDebug", true);
193             MiscUtils.setFieldIfExists(demo, "glTrace", true);
194         }
195         if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
196             MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
197         }
198     }
199
200     static int atoi(String a) {
201         int i=0;
202         try {
203             i = Integer.parseInt(a);
204         } catch (Exception ex) { ex.printStackTrace(); }
205         return i;
206     }
207
208     public static void main(String args[]) throws IOException {
209         for(int i=0; i<args.length; i++) {
210             if(args[i].equals("-time")) {
211                 durationPerTest = atoi(args[++i]);
212             }
213         }
214         String tstname = TestParenting02NEWT.class.getName();
215         org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
216             tstname,
217             "filtertrace=true",
218             "haltOnError=false",
219             "haltOnFailure=false",
220             "showoutput=true",
221             "outputtoformatters=true",
222             "logfailedtests=true",
223             "logtestlistenerevents=true",
224             "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
225             "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
226     }
227
228 }
http://JogAmp.org git info: FAQ, tutorial and man pages.