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;
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 com.jogamp.opengl.*;
38 
39 import com.jogamp.newt.*;
40 import com.jogamp.newt.event.*;
41 import com.jogamp.newt.opengl.*;
42 import com.jogamp.newt.util.EDTUtil;
43 
44 import java.io.IOException;
45 
46 import com.jogamp.opengl.test.junit.util.UITestCase;
47 import com.jogamp.opengl.test.junit.util.MiscUtils;
48 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
49 
50 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
51 public class TestDisplayLifecycle01NEWT extends UITestCase {
52     static GLProfile glp;
53     static GLCapabilities caps;
54     static int width, height;
55     static long durationPerTest = 100; // ms
56 
57     @BeforeClass
initClass()58     public static void initClass() {
59         width  = 640;
60         height = 480;
61         glp = GLProfile.getDefault();
62         caps = new GLCapabilities(glp);
63     }
64 
createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height)65     static GLWindow createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height)
66         throws InterruptedException
67     {
68         Assert.assertNotNull(caps);
69 
70         //
71         // Create native windowing resources .. X11/Win/OSX
72         //
73         GLWindow glWindow;
74         if(null!=screen) {
75             final Window window = NewtFactory.createWindow(screen, caps);
76             Assert.assertNotNull(window);
77             glWindow = GLWindow.create(window);
78         } else {
79             glWindow = GLWindow.create(caps);
80         }
81         glWindow.setUpdateFPSFrames(1, null);
82 
83         final GLEventListener demo = new GearsES2();
84         setDemoFields(demo, glWindow);
85         glWindow.addGLEventListener(demo);
86         glWindow.addWindowListener(new TraceWindowAdapter());
87         glWindow.setSize(width, height);
88         return glWindow;
89     }
90 
testDisplayCreate01(final Display display, final Screen screen)91     private void testDisplayCreate01(final Display display, final Screen screen) throws InterruptedException {
92         // start-state == end-state
93         Assert.assertEquals(0,Display.getActiveDisplayNumber());
94         Assert.assertEquals(0,display.getReferenceCount());
95         Assert.assertEquals(false,display.isNativeValid());
96         Assert.assertNotNull(display.getEDTUtil());
97         Assert.assertEquals(false,display.getEDTUtil().isRunning());
98         Assert.assertEquals(0,screen.getReferenceCount());
99         Assert.assertEquals(false,screen.isNativeValid());
100 
101         // Create Window, pending lazy native creation
102         final GLWindow window = createWindow(screen, caps, width, height);
103         Assert.assertEquals(screen,window.getScreen());
104         Assert.assertEquals(0,Display.getActiveDisplayNumber());
105         Assert.assertEquals(0,display.getReferenceCount());
106         Assert.assertEquals(false,display.isNativeValid());
107         Assert.assertEquals(true,display.getEDTUtil().isRunning());
108         Assert.assertEquals(0,screen.getReferenceCount());
109         Assert.assertEquals(false,screen.isNativeValid());
110 
111         Assert.assertNotNull(window.getScreen());
112         Assert.assertEquals(false,window.isNativeValid());
113         Assert.assertEquals(false,window.isVisible());
114 
115         // lazy native creation sequence: Display, Screen and Window
116         Assert.assertEquals(0, window.getTotalFPSFrames());
117         window.setVisible(true);
118 
119         Assert.assertEquals(screen,window.getScreen());
120         Assert.assertEquals(1,Display.getActiveDisplayNumber());
121         Assert.assertEquals(1,display.getReferenceCount());
122         Assert.assertEquals(true,display.isNativeValid());
123         Assert.assertEquals(true,display.getEDTUtil().isRunning());
124         Assert.assertEquals(1,screen.getReferenceCount());
125         Assert.assertEquals(true,screen.isNativeValid());
126         Assert.assertEquals(true,window.isNativeValid());
127         Assert.assertEquals(true,window.isVisible());
128         System.err.println("Frames for setVisible(true) 1: "+window.getTotalFPSFrames());
129         Assert.assertTrue(0 < window.getTotalFPSFrames());
130 
131         while(window.getTotalFPSDuration()<1*durationPerTest) {
132             window.display();
133             Thread.sleep(100);
134         }
135         System.err.println("duration: "+window.getTotalFPSDuration());
136 
137         // just make the Window invisible
138         window.setVisible(false);
139         Assert.assertEquals(true,window.isNativeValid());
140         Assert.assertEquals(false,window.isVisible());
141 
142         // just make the Window visible again
143         window.resetFPSCounter();
144         Assert.assertEquals(0, window.getTotalFPSFrames());
145         window.setVisible(true);
146         Assert.assertEquals(true,window.isNativeValid());
147         Assert.assertEquals(true,window.isVisible());
148         System.err.println("Frames for setVisible(true) 1: "+window.getTotalFPSFrames());
149         Assert.assertTrue(0 < window.getTotalFPSFrames());
150 
151         while(window.getTotalFPSDuration()<2*durationPerTest) {
152             window.display();
153             Thread.sleep(100);
154         }
155         System.err.println("duration: "+window.getTotalFPSDuration());
156 
157         // destruction ..
158         window.destroy();
159         Assert.assertEquals(screen,window.getScreen());
160         Assert.assertEquals(0,Display.getActiveDisplayNumber());
161         Assert.assertEquals(0,display.getReferenceCount());
162         Assert.assertEquals(false,display.isNativeValid());
163         Assert.assertNotNull(display.getEDTUtil());
164         Assert.assertEquals(false,display.getEDTUtil().isRunning());
165         Assert.assertEquals(0,screen.getReferenceCount());
166         Assert.assertEquals(false,screen.isNativeValid());
167         Assert.assertEquals(false,window.isNativeValid());
168         Assert.assertEquals(false,window.isVisible());
169         window.resetFPSCounter();
170         Assert.assertEquals(0, window.getTotalFPSFrames());
171 
172         // a display call shall not change a thing
173         window.display();
174         Assert.assertEquals(0, window.getTotalFPSFrames());
175         Assert.assertEquals(false,window.isNativeValid());
176         Assert.assertEquals(false,window.isVisible());
177 
178         // recover Window
179         window.setVisible(true);
180 
181         Assert.assertEquals(screen,window.getScreen());
182         Assert.assertEquals(1,Display.getActiveDisplayNumber());
183         Assert.assertEquals(1,display.getReferenceCount());
184         Assert.assertEquals(true,display.isNativeValid());
185         Assert.assertEquals(true,display.getEDTUtil().isRunning());
186         Assert.assertEquals(1,screen.getReferenceCount());
187         Assert.assertEquals(true,screen.isNativeValid());
188         Assert.assertEquals(true,window.isNativeValid());
189         Assert.assertEquals(true,window.isVisible());
190         System.err.println("Frames for setVisible(true) 2: "+window.getTotalFPSFrames());
191         Assert.assertTrue(0 < window.getTotalFPSFrames());
192 
193         while(window.getTotalFPSDuration()<1*durationPerTest) {
194             window.display();
195             Thread.sleep(100);
196         }
197         System.err.println("duration: "+window.getTotalFPSDuration());
198 
199         // destruction ..
200         window.destroy();
201         Display.dumpDisplayList("Post destroy(true)");
202 
203         // end-state == start-state
204         Assert.assertEquals(0,Display.getActiveDisplayNumber());
205         Assert.assertEquals(0,display.getReferenceCount());
206         Assert.assertEquals(false,display.isNativeValid());
207         {
208             final EDTUtil edtUtil = display.getEDTUtil();
209             Assert.assertNotNull(edtUtil);
210             Assert.assertEquals(false,edtUtil.isRunning());
211             edtUtil.start();
212             Assert.assertEquals(true,edtUtil.isRunning());
213             edtUtil.invoke(true, null);
214             Assert.assertEquals(true,edtUtil.isRunning());
215             edtUtil.invokeStop(true, null);
216             edtUtil.waitUntilStopped();
217             Assert.assertEquals(false,edtUtil.isRunning());
218         }
219         Assert.assertEquals(0,screen.getReferenceCount());
220         Assert.assertEquals(false,screen.isNativeValid());
221 
222         Assert.assertNotNull(window.getScreen());
223         Assert.assertEquals(false,window.isNativeValid());
224         Assert.assertEquals(false,window.isVisible());
225     }
226 
227     @Test
testDisplayCreate01_AutoDestroyLifecycle()228     public void testDisplayCreate01_AutoDestroyLifecycle() throws InterruptedException {
229         Assert.assertEquals(0,Display.getActiveDisplayNumber());
230 
231         // Create Display/Screen, pending lazy native creation
232         final Display display = NewtFactory.createDisplay(null);
233         final Screen screen  = NewtFactory.createScreen(display, 0); // screen 0
234         testDisplayCreate01(display, screen);
235         testDisplayCreate01(display, screen);
236 
237         Assert.assertEquals(0,Display.getActiveDisplayNumber());
238     }
239 
setDemoFields(final GLEventListener demo, final GLWindow glWindow)240     public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow) {
241         Assert.assertNotNull(demo);
242         Assert.assertNotNull(glWindow);
243         if(!MiscUtils.setFieldIfExists(demo, "window", glWindow)) {
244             MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
245         }
246     }
247 
atoi(final String a)248     static int atoi(final String a) {
249         int i=0;
250         try {
251             i = Integer.parseInt(a);
252         } catch (final Exception ex) { ex.printStackTrace(); }
253         return i;
254     }
255 
main(final String args[])256     public static void main(final String args[]) throws IOException {
257         for(int i=0; i<args.length; i++) {
258             if(args[i].equals("-time")) {
259                 durationPerTest = atoi(args[++i]);
260             }
261         }
262         System.err.println("durationPerTest: "+durationPerTest);
263         final String tstname = TestDisplayLifecycle01NEWT.class.getName();
264         org.junit.runner.JUnitCore.main(tstname);
265     }
266 
267 }
268