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 java.io.IOException;
43 
44 import com.jogamp.opengl.test.junit.util.UITestCase;
45 import com.jogamp.opengl.test.junit.util.MiscUtils;
46 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
47 
48 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
49 public class TestGLWindows01NEWT extends UITestCase {
50     static GLProfile glp;
51     static int width, height;
52     static long durationPerTest = 100; // ms
53 
54     @BeforeClass
initClass()55     public static void initClass() {
56         width  = 640;
57         height = 480;
58         glp = GLProfile.getDefault();
59     }
60 
createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height, final boolean onscreen, final boolean addGLEventListenerAfterVisible)61     static GLWindow createWindow(final Screen screen, final GLCapabilities caps,
62                                  final int width, final int height, final boolean onscreen, final boolean addGLEventListenerAfterVisible)
63         throws InterruptedException
64     {
65         Assert.assertNotNull(caps);
66         caps.setOnscreen(onscreen);
67         // System.out.println("Requested: "+caps);
68 
69         //
70         // Create native windowing resources .. X11/Win/OSX
71         //
72         GLWindow glWindow;
73         if(null!=screen) {
74             glWindow = GLWindow.create(screen, caps);
75             Assert.assertNotNull(glWindow);
76         } else {
77             glWindow = GLWindow.create(caps);
78             Assert.assertNotNull(glWindow);
79         }
80 
81         Assert.assertEquals(false,glWindow.isVisible());
82         Assert.assertEquals(false,glWindow.isNativeValid());
83 
84         final GLEventListener demo = new GearsES2();
85         setDemoFields(demo, glWindow);
86         if(!addGLEventListenerAfterVisible) {
87             glWindow.addGLEventListener(demo);
88         }
89         glWindow.addWindowListener(new TraceWindowAdapter());
90 
91         glWindow.setSize(width, height);
92 
93         Assert.assertEquals(0, glWindow.getTotalFPSFrames());
94         glWindow.setVisible(true);
95         Assert.assertEquals(true,glWindow.isVisible());
96         Assert.assertEquals(true,glWindow.isNativeValid());
97         System.out.println("Frames for initial setVisible(true): "+glWindow.getTotalFPSFrames());
98         Assert.assertTrue(0 < glWindow.getTotalFPSFrames());
99 
100         //
101         // Create native OpenGL resources .. XGL/WGL/CGL ..
102         // equivalent to GLAutoDrawable methods: setVisible(true)
103         //
104         final GLCapabilitiesImmutable caps2 = glWindow.getChosenGLCapabilities();
105         Assert.assertNotNull(caps2);
106         Assert.assertTrue(caps2.getGreenBits()>=5);
107         Assert.assertTrue(caps2.getBlueBits()>=5);
108         Assert.assertTrue(caps2.getRedBits()>=5);
109         Assert.assertEquals(caps2.isOnscreen(),onscreen);
110 
111         if(addGLEventListenerAfterVisible) {
112             glWindow.addGLEventListener(demo);
113             glWindow.display();
114         }
115 
116         return glWindow;
117     }
118 
destroyWindow(final GLWindow glWindow)119     static void destroyWindow(final GLWindow glWindow) {
120         if(null!=glWindow) {
121             glWindow.destroy();
122             Assert.assertEquals(false,glWindow.isNativeValid());
123             Assert.assertEquals(false,glWindow.isVisible());
124         }
125     }
126 
127 
128     @Test
test01WindowSimple()129     public void test01WindowSimple() throws InterruptedException {
130         final GLCapabilities caps = new GLCapabilities(glp);
131         Assert.assertNotNull(caps);
132         final GLWindow window = createWindow(null, caps, width, height,
133                                        true /* onscreen */, false /*addGLEventListenerAfterVisible*/);
134         System.out.println("Created: "+window);
135         int state;
136         for(state=0; state*100<durationPerTest; state++) {
137             Thread.sleep(100);
138         }
139         System.out.println("duration: "+window.getTotalFPSDuration());
140         destroyWindow(window);
141     }
142 
143     @Test
test02WindowSimple()144     public void test02WindowSimple() throws InterruptedException {
145         final GLCapabilities caps = new GLCapabilities(glp);
146         Assert.assertNotNull(caps);
147         final GLWindow window = createWindow(null, caps, width, height,
148                                        true /* onscreen */, true /*addGLEventListenerAfterVisible*/);
149         System.out.println("Created: "+window);
150         int state;
151         for(state=0; state*100<durationPerTest; state++) {
152             Thread.sleep(100);
153         }
154         System.out.println("duration: "+window.getTotalFPSDuration());
155         destroyWindow(window);
156     }
157 
158     @Test
test10WindowNativeRecreateSimple()159     public void test10WindowNativeRecreateSimple() throws InterruptedException {
160         final GLCapabilities caps = new GLCapabilities(glp);
161         Assert.assertNotNull(caps);
162         final GLWindow window = createWindow(null, caps, width, height,
163                                        true /* onscreen */, false /*addGLEventListenerAfterVisible*/);
164 
165         Assert.assertEquals(true,window.isNativeValid());
166         Assert.assertEquals(true,window.isVisible());
167         window.destroy();
168         Assert.assertEquals(false,window.isNativeValid());
169         Assert.assertEquals(false,window.isVisible());
170 
171         window.display();
172         Assert.assertEquals(false,window.isNativeValid());
173         Assert.assertEquals(false,window.isVisible());
174 
175         window.setVisible(true);
176         Assert.assertEquals(true,window.isNativeValid());
177         Assert.assertEquals(true,window.isVisible());
178 
179         window.setVisible(false);
180         Assert.assertEquals(true,window.isNativeValid());
181         Assert.assertEquals(false,window.isVisible());
182 
183         destroyWindow(window);
184     }
185 
186     @Test
test11WindowNativeRecreateSimple()187     public void test11WindowNativeRecreateSimple() throws InterruptedException {
188         final GLCapabilities caps = new GLCapabilities(glp);
189         Assert.assertNotNull(caps);
190         final GLWindow window = createWindow(null, caps, width, height,
191                                        true /* onscreen */, true /*addGLEventListenerAfterVisible*/);
192 
193         Assert.assertEquals(true,window.isNativeValid());
194         Assert.assertEquals(true,window.isVisible());
195         window.destroy();
196         Assert.assertEquals(false,window.isNativeValid());
197         Assert.assertEquals(false,window.isVisible());
198 
199         window.display();
200         Assert.assertEquals(false,window.isNativeValid());
201         Assert.assertEquals(false,window.isVisible());
202 
203         window.setVisible(true);
204         Assert.assertEquals(true,window.isNativeValid());
205         Assert.assertEquals(true,window.isVisible());
206 
207         window.setVisible(false);
208         Assert.assertEquals(true,window.isNativeValid());
209         Assert.assertEquals(false,window.isVisible());
210 
211         destroyWindow(window);
212     }
213 
214     @Test
test21WindowDestroyWinTwiceA()215     public void test21WindowDestroyWinTwiceA() throws InterruptedException {
216         final GLCapabilities caps = new GLCapabilities(glp);
217         Assert.assertNotNull(caps);
218         final GLWindow window = createWindow(null, caps, width, height,
219                                        true /* onscreen */, false /*addGLEventListenerAfterVisible*/);
220         int state;
221         for(state=0; state*100<durationPerTest; state++) {
222             Thread.sleep(100);
223         }
224         System.out.println("duration: "+window.getTotalFPSDuration());
225         destroyWindow(window);
226     }
227 
228     @Test
test22WindowTwoWinOneDisplay()229     public void test22WindowTwoWinOneDisplay() throws InterruptedException {
230         final GLCapabilities caps = new GLCapabilities(glp);
231         Assert.assertNotNull(caps);
232 
233         final Display display = NewtFactory.createDisplay(null); // local display
234         Assert.assertNotNull(display);
235 
236         final Screen screen  = NewtFactory.createScreen(display, 0); // screen 0
237         Assert.assertNotNull(screen);
238         final GLWindow window1 = createWindow(screen, caps, width, height,
239                                        true /* onscreen */, false /*addGLEventListenerAfterVisible*/);
240         Assert.assertNotNull(window1);
241 
242         final GLWindow window2 = createWindow(screen, caps, width, height,
243                                        true /* onscreen */, false /*addGLEventListenerAfterVisible*/);
244         Assert.assertNotNull(window2);
245 
246         Assert.assertEquals(1,Display.getActiveDisplayNumber());
247         Assert.assertEquals(1,display.getReferenceCount());
248         Assert.assertEquals(true,display.isNativeValid());
249         Assert.assertNotNull(display.getEDTUtil());
250         Assert.assertEquals(true,display.getEDTUtil().isRunning());
251 
252         Assert.assertEquals(2,screen.getReferenceCount());
253         Assert.assertEquals(true,screen.isNativeValid());
254 
255         int state;
256         for(state=0; state*100<durationPerTest; state++) {
257             Thread.sleep(100);
258         }
259         System.out.println("duration1: "+window1.getTotalFPSDuration());
260         System.out.println("duration2: "+window2.getTotalFPSDuration());
261 
262         destroyWindow(window1);
263         destroyWindow(window2);
264 
265         Assert.assertEquals(0,Display.getActiveDisplayNumber());
266 
267         Assert.assertEquals(0,display.getReferenceCount());
268         Assert.assertEquals(false,display.isNativeValid());
269         Assert.assertNotNull(display.getEDTUtil());
270         Assert.assertEquals(false,display.getEDTUtil().isRunning());
271 
272         Assert.assertEquals(0,screen.getReferenceCount());
273         Assert.assertEquals(false,screen.isNativeValid());
274     }
275 
276     @Test
test23WindowTwoWinTwoDisplays()277     public void test23WindowTwoWinTwoDisplays() throws InterruptedException {
278         final GLCapabilities caps = new GLCapabilities(glp);
279         Assert.assertNotNull(caps);
280 
281         final Display display1 = NewtFactory.createDisplay(null, false); // local display
282         Assert.assertNotNull(display1);
283         final Display display2 = NewtFactory.createDisplay(null, false); // local display
284         Assert.assertNotNull(display2);
285         Assert.assertNotSame(display1, display2);
286 
287         final Screen screen1  = NewtFactory.createScreen(display1, 0); // screen 0
288         Assert.assertNotNull(screen1);
289         final GLWindow window1 = createWindow(screen1, caps, width, height,
290                                        true /* onscreen */, false /*addGLEventListenerAfterVisible*/);
291         Assert.assertNotNull(window1);
292 
293         final Screen screen2  = NewtFactory.createScreen(display2, 0); // screen 0
294         Assert.assertNotNull(screen2);
295         final GLWindow window2 = createWindow(screen2, caps, width, height,
296                                        true /* onscreen */, false /*addGLEventListenerAfterVisible*/);
297         Assert.assertNotNull(window2);
298 
299         Assert.assertEquals(2,Display.getActiveDisplayNumber());
300 
301         Assert.assertEquals(1,display1.getReferenceCount());
302         Assert.assertEquals(true,display1.isNativeValid());
303         Assert.assertNotNull(display1.getEDTUtil());
304         Assert.assertEquals(true,display1.getEDTUtil().isRunning());
305         Assert.assertEquals(1,screen1.getReferenceCount());
306         Assert.assertEquals(true,screen1.isNativeValid());
307 
308         Assert.assertEquals(1,display2.getReferenceCount());
309         Assert.assertEquals(true,display2.isNativeValid());
310         Assert.assertNotNull(display2.getEDTUtil());
311         Assert.assertEquals(true,display2.getEDTUtil().isRunning());
312         Assert.assertEquals(1,screen2.getReferenceCount());
313         Assert.assertEquals(true,screen2.isNativeValid());
314 
315         int state;
316         for(state=0; state*100<durationPerTest; state++) {
317             Thread.sleep(100);
318         }
319         System.out.println("duration1: "+window1.getTotalFPSDuration());
320         System.out.println("duration2: "+window2.getTotalFPSDuration());
321 
322         // It is observed that some X11 drivers, eg ATI, fglrx 8.78.6,
323         // are quite sensitive to multiple Display connections (NEWT Display -> X11 Display).
324         // In such cases, closing displays shall happen in the same order as
325         // opening them, otherwise some driver related bug appears.
326         // You may test this, ie just reverse the destroy order below.
327         // See also native test: jogl/test/native/displayMultiple02.c
328         destroyWindow(window1);
329         destroyWindow(window2);
330 
331         Assert.assertEquals(0,Display.getActiveDisplayNumber());
332 
333         Assert.assertEquals(0,display1.getReferenceCount());
334         Assert.assertEquals(false,display1.isNativeValid());
335         Assert.assertNotNull(display1.getEDTUtil());
336         Assert.assertEquals(false,display1.getEDTUtil().isRunning());
337         Assert.assertEquals(0,screen1.getReferenceCount());
338         Assert.assertEquals(false,screen1.isNativeValid());
339 
340         Assert.assertEquals(0,display2.getReferenceCount());
341         Assert.assertEquals(false,display2.isNativeValid());
342         Assert.assertNotNull(display2.getEDTUtil());
343         Assert.assertEquals(false,display2.getEDTUtil().isRunning());
344         Assert.assertEquals(0,screen2.getReferenceCount());
345         Assert.assertEquals(false,screen2.isNativeValid());
346     }
347 
setDemoFields(final GLEventListener demo, final GLWindow glWindow)348     public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow) {
349         Assert.assertNotNull(demo);
350         Assert.assertNotNull(glWindow);
351         if(!MiscUtils.setFieldIfExists(demo, "window", glWindow)) {
352             MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
353         }
354     }
355 
atoi(final String a)356     static int atoi(final String a) {
357         int i=0;
358         try {
359             i = Integer.parseInt(a);
360         } catch (final Exception ex) { ex.printStackTrace(); }
361         return i;
362     }
363 
main(final String args[])364     public static void main(final String args[]) throws IOException {
365         for(int i=0; i<args.length; i++) {
366             if(args[i].equals("-time")) {
367                 durationPerTest = atoi(args[++i]);
368             }
369         }
370         System.out.println("durationPerTest: "+durationPerTest);
371         final String tstname = TestGLWindows01NEWT.class.getName();
372         org.junit.runner.JUnitCore.main(tstname);
373     }
374 
375 }
376