Jogamp
622f51e45b9db349d5790b1ff7fa41bc73cc2f08
[jogl.git] / src / test / com / jogamp / opengl / test / junit / jogl / demos / es2 / awt / TestGearsES2AWT.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.jogl.demos.es2.awt;
30
31 import javax.media.opengl.*;
32
33 import com.jogamp.opengl.util.Animator;
34
35 import javax.media.opengl.awt.GLCanvas;
36
37 import com.jogamp.common.os.Platform;
38 import com.jogamp.newt.event.awt.AWTKeyAdapter;
39 import com.jogamp.newt.event.awt.AWTWindowAdapter;
40 import com.jogamp.newt.event.TraceKeyAdapter;
41 import com.jogamp.newt.event.TraceWindowAdapter;
42 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
43 import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
44 import com.jogamp.opengl.test.junit.util.MiscUtils;
45 import com.jogamp.opengl.test.junit.util.UITestCase;
46 import com.jogamp.opengl.test.junit.util.QuitAdapter;
47
48 import java.awt.BorderLayout;
49 import java.awt.Button;
50 import java.awt.Component;
51 import java.awt.Container;
52 import java.awt.Dimension;
53 import java.awt.EventQueue;
54 import java.awt.Frame;
55 import java.awt.TextArea;
56 import java.awt.event.ComponentEvent;
57 import java.awt.event.ComponentListener;
58 import java.io.BufferedReader;
59 import java.io.IOException;
60 import java.io.InputStreamReader;
61 import java.lang.reflect.InvocationTargetException;
62
63 import org.junit.Assert;
64 import org.junit.Assume;
65 import org.junit.BeforeClass;
66 import org.junit.AfterClass;
67 import org.junit.Test;
68 import org.junit.FixMethodOrder;
69 import org.junit.runners.MethodSorters;
70
71 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
72 public class TestGearsES2AWT extends UITestCase {
73     public enum FrameLayout { None, TextOnBottom, BorderCenterSurrounded, DoubleBorderCenterSurrounded };
74     public enum ResizeBy { Component, Frame };
75
76     static long duration = 500; // ms
77     static int width = 640, height = 480;
78     static int xpos = 10, ypos = 10;
79     static FrameLayout frameLayout = FrameLayout.None;
80     static ResizeBy resizeBy = ResizeBy.Component;
81
82     static boolean forceES2 = false;
83     static boolean forceGL3 = false;
84     static boolean mainRun = false;
85     static boolean shallUseOffscreenFBOLayer = false;
86     static boolean shallUseOffscreenPBufferLayer = false;
87     static boolean useMSAA = false;
88     static boolean useStencil = false;
89     static boolean shutdownRemoveGLCanvas = true;
90     static boolean shutdownDisposeFrame = true;
91     static boolean shutdownSystemExit = false;
92     static int swapInterval = 1;
93     static boolean exclusiveContext = false;
94     static boolean useAnimator = true;
95     static Thread awtEDT;
96     static java.awt.Dimension rwsize = null;
97
98     @BeforeClass
99     public static void initClass() {
100         try {
101             EventQueue.invokeAndWait(new Runnable() {
102                 public void run() {
103                     awtEDT = Thread.currentThread();
104                 } } );
105         } catch (Exception e) {
106             e.printStackTrace();
107             Assert.assertNull(e);
108         }
109     }
110
111     @AfterClass
112     public static void releaseClass() {
113     }
114
115     static void setComponentSize(final Frame frame, final Component comp, final java.awt.Dimension new_sz) {
116         try {
117             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
118                 public void run() {
119                     comp.setMinimumSize(new_sz);
120                     comp.setPreferredSize(new_sz);
121                     comp.setSize(new_sz);
122                     if( null != frame ) {
123                         frame.pack();
124                     }
125                 } } );
126         } catch( Throwable throwable ) {
127             throwable.printStackTrace();
128             Assume.assumeNoException( throwable );
129         }
130     }
131     static void setFrameSize(final Frame frame, final boolean frameLayout, final java.awt.Dimension new_sz) {
132         try {
133             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
134                 public void run() {
135                     frame.setSize(new_sz);
136                     if( frameLayout ) {
137                         frame.validate();
138                     }
139                 } } );
140         } catch( Throwable throwable ) {
141             throwable.printStackTrace();
142             Assume.assumeNoException( throwable );
143         }
144     }
145
146     static void setSize(final ResizeBy resizeBy, final Frame frame, final boolean frameLayout, final Component comp, final java.awt.Dimension new_sz) {
147         switch( resizeBy ) {
148             case Component:
149                 setComponentSize(frameLayout ? frame : null, comp, new_sz);
150                 break;
151             case Frame:
152                 setFrameSize(frame, frameLayout, new_sz);
153                 break;
154         }
155     }
156
157     private void setTitle(final Frame frame, final GLCanvas glc, final GLCapabilitiesImmutable caps) {
158         final String capsA = caps.isBackgroundOpaque() ? "opaque" : "transl";
159         final java.awt.Rectangle b = glc.getBounds();
160         frame.setTitle("GLCanvas["+capsA+"], swapI "+swapInterval+", win: ["+b.x+"/"+b.y+" "+b.width+"x"+b.height+"], pix: "+glc.getSurfaceWidth()+"x"+glc.getSurfaceHeight());
161     }
162
163     protected void runTestGL(final GLCapabilities caps, final ResizeBy resizeBy, FrameLayout frameLayout) throws InterruptedException, InvocationTargetException {
164         final Frame frame = new Frame("GearsES2 AWT Test");
165         Assert.assertNotNull(frame);
166
167         final GLCanvas glCanvas = new GLCanvas(caps);
168         Assert.assertNotNull(glCanvas);
169         setSize(resizeBy, frame, false, glCanvas, new Dimension(width, height));
170         frame.setLocation(xpos, ypos);
171
172         switch( frameLayout) {
173             case None:
174                 frame.add(glCanvas);
175                 break;
176             case TextOnBottom:
177                 final TextArea ta = new TextArea(2, 20);
178                 ta.append("0123456789");
179                 ta.append(Platform.getNewline());
180                 ta.append("Some Text");
181                 ta.append(Platform.getNewline());
182                 frame.setLayout(new BorderLayout());
183                 frame.add(ta, BorderLayout.SOUTH);
184                 frame.add(glCanvas, BorderLayout.CENTER);
185                 break;
186             case BorderCenterSurrounded:
187                 frame.setLayout(new BorderLayout());
188                 frame.add(new Button("NORTH"), BorderLayout.NORTH);
189                 frame.add(new Button("SOUTH"), BorderLayout.SOUTH);
190                 frame.add(new Button("EAST"), BorderLayout.EAST);
191                 frame.add(new Button("WEST"), BorderLayout.WEST);
192                 frame.add(glCanvas, BorderLayout.CENTER);
193                 break;
194             case DoubleBorderCenterSurrounded:
195                 Container c = new Container();
196                 c.setLayout(new BorderLayout());
197                 c.add(new Button("north"), BorderLayout.NORTH);
198                 c.add(new Button("south"), BorderLayout.SOUTH);
199                 c.add(new Button("east"), BorderLayout.EAST);
200                 c.add(new Button("west"), BorderLayout.WEST);
201                 c.add(glCanvas, BorderLayout.CENTER);
202
203                 frame.setLayout(new BorderLayout());
204                 frame.add(new Button("NORTH"), BorderLayout.NORTH);
205                 frame.add(new Button("SOUTH"), BorderLayout.SOUTH);
206                 frame.add(new Button("EAST"), BorderLayout.EAST);
207                 frame.add(new Button("WEST"), BorderLayout.WEST);
208                 frame.add(c, BorderLayout.CENTER);
209                 break;
210         }
211         setTitle(frame, glCanvas, caps);
212
213         frame.addComponentListener(new ComponentListener() {
214             @Override
215             public void componentResized(ComponentEvent e) {
216                 setTitle(frame, glCanvas, caps);
217             }
218
219             @Override
220             public void componentMoved(ComponentEvent e) {
221                 setTitle(frame, glCanvas, caps);
222             }
223
224             @Override
225             public void componentShown(ComponentEvent e) { }
226
227             @Override
228             public void componentHidden(ComponentEvent e) { }
229         });
230
231         final GearsES2 demo = new GearsES2(swapInterval);
232         glCanvas.addGLEventListener(demo);
233
234         final SnapshotGLEventListener snap = new SnapshotGLEventListener();
235         glCanvas.addGLEventListener(snap);
236
237         final Animator animator = useAnimator ? new Animator(glCanvas) : null;
238         if( useAnimator && exclusiveContext ) {
239             animator.setExclusiveContext(awtEDT);
240         }
241         final QuitAdapter quitAdapter = new QuitAdapter();
242         new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glCanvas).addTo(glCanvas);
243         new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glCanvas).addTo(frame);
244
245         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
246             public void run() {
247                if( ResizeBy.Frame == resizeBy ) {
248                    frame.validate();
249                } else {
250                    frame.pack();
251                }
252                frame.setVisible(true);
253             }});
254         Assert.assertEquals(true,  AWTRobotUtil.waitForVisible(frame, true));
255         Assert.assertEquals(true,  AWTRobotUtil.waitForRealized(glCanvas, true));
256
257         if( useAnimator ) {
258             animator.start();
259             Assert.assertTrue(animator.isStarted());
260             Assert.assertTrue(animator.isAnimating());
261             Assert.assertEquals(exclusiveContext ? awtEDT : null, glCanvas.getExclusiveContextThread());
262             animator.setUpdateFPSFrames(60, System.err);
263         }
264
265         System.err.println("canvas pos/siz: "+glCanvas.getX()+"/"+glCanvas.getY()+" "+glCanvas.getSurfaceWidth()+"x"+glCanvas.getSurfaceHeight());
266
267         snap.setMakeSnapshot();
268
269         if( null != rwsize ) {
270             Thread.sleep(500); // 500ms delay
271             setSize(resizeBy, frame, true, glCanvas, rwsize);
272             System.err.println("window resize pos/siz: "+glCanvas.getX()+"/"+glCanvas.getY()+" "+glCanvas.getSurfaceWidth()+"x"+glCanvas.getSurfaceHeight());
273         }
274
275         snap.setMakeSnapshot();
276
277         final long t0 = System.currentTimeMillis();
278         long t1 = t0;
279         while(!quitAdapter.shouldQuit() && t1 - t0 < duration) {
280             Thread.sleep(100);
281             t1 = System.currentTimeMillis();
282         }
283
284         Assert.assertNotNull(frame);
285         Assert.assertNotNull(glCanvas);
286
287         if( useAnimator ) {
288             Assert.assertNotNull(animator);
289             Assert.assertEquals(exclusiveContext ? awtEDT : null, glCanvas.getExclusiveContextThread());
290             animator.stop();
291             Assert.assertFalse(animator.isAnimating());
292             Assert.assertFalse(animator.isStarted());
293             Assert.assertEquals(null, glCanvas.getExclusiveContextThread());
294         }
295
296         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
297             public void run() {
298                 frame.setVisible(false);
299             }});
300         Assert.assertEquals(false, frame.isVisible());
301         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
302             public void run() {
303                 if(shutdownRemoveGLCanvas) {
304                     frame.remove(glCanvas);
305                 }
306                 if(shutdownDisposeFrame) {
307                     frame.dispose();
308                 }
309                 if(shutdownSystemExit) {
310                     System.exit(0);
311                 }
312             }});
313     }
314
315     @Test
316     public void test01() throws InterruptedException, InvocationTargetException {
317         final GLProfile glp;
318         if(forceGL3) {
319             glp = GLProfile.get(GLProfile.GL3);
320         } else if(forceES2) {
321             glp = GLProfile.get(GLProfile.GLES2);
322         } else {
323             glp = GLProfile.getGL2ES2();
324         }
325         final GLCapabilities caps = new GLCapabilities(glp);
326         if(useMSAA) {
327             caps.setNumSamples(4);
328             caps.setSampleBuffers(true);
329         }
330         if(useStencil) {
331             caps.setStencilBits(1);
332         }
333         if(shallUseOffscreenFBOLayer) {
334             caps.setOnscreen(false);
335         }
336         if(shallUseOffscreenPBufferLayer) {
337             caps.setPBuffer(true);
338         }
339         runTestGL(caps, resizeBy, frameLayout);
340     }
341
342     @Test
343     public void test02_GLES2() throws InterruptedException, InvocationTargetException {
344         if(mainRun) return;
345
346         if( !GLProfile.isAvailable(GLProfile.GLES2) ) {
347             System.err.println("GLES2 n/a");
348             return;
349         }
350         final GLProfile glp = GLProfile.get(GLProfile.GLES2);
351         final GLCapabilities caps = new GLCapabilities( glp );
352         runTestGL(caps, resizeBy, frameLayout);
353     }
354
355     @Test
356     public void test03_GL3() throws InterruptedException, InvocationTargetException {
357         if(mainRun) return;
358
359         if( !GLProfile.isAvailable(GLProfile.GL3) ) {
360             System.err.println("GL3 n/a");
361             return;
362         }
363         final GLProfile glp = GLProfile.get(GLProfile.GL3);
364         final GLCapabilities caps = new GLCapabilities( glp );
365         runTestGL(caps, resizeBy, frameLayout);
366     }
367
368     public static void main(String args[]) {
369         boolean waitForKey = false;
370         int rw=-1, rh=-1;
371
372         mainRun = true;
373         for(int i=0; i<args.length; i++) {
374             if(args[i].equals("-time")) {
375                 i++;
376                 duration = MiscUtils.atol(args[i], duration);
377             } else if(args[i].equals("-width")) {
378                 i++;
379                 width = MiscUtils.atoi(args[i], width);
380             } else if(args[i].equals("-height")) {
381                 i++;
382                 height = MiscUtils.atoi(args[i], height);
383             } else if(args[i].equals("-x")) {
384                 i++;
385                 xpos = MiscUtils.atoi(args[i], xpos);
386             } else if(args[i].equals("-y")) {
387                 i++;
388                 ypos = MiscUtils.atoi(args[i], ypos);
389             } else if(args[i].equals("-rwidth")) {
390                 i++;
391                 rw = MiscUtils.atoi(args[i], rw);
392             } else if(args[i].equals("-rheight")) {
393                 i++;
394                 rh = MiscUtils.atoi(args[i], rh);
395             } else if(args[i].equals("-layout")) {
396                 i++;
397                 frameLayout = FrameLayout.valueOf(args[i]);
398             } else if(args[i].equals("-resizeBy")) {
399                 i++;
400                 resizeBy = ResizeBy.valueOf(args[i]);
401             } else if(args[i].equals("-es2")) {
402                 forceES2 = true;
403             } else if(args[i].equals("-gl3")) {
404                 forceGL3 = true;
405             } else if(args[i].equals("-vsync")) {
406                 i++;
407                 swapInterval = MiscUtils.atoi(args[i], swapInterval);
408             } else if(args[i].equals("-exclctx")) {
409                 exclusiveContext = true;
410             } else if(args[i].equals("-noanim")) {
411                 useAnimator  = false;
412             } else if(args[i].equals("-layeredFBO")) {
413                 shallUseOffscreenFBOLayer = true;
414             } else if(args[i].equals("-layeredPBuffer")) {
415                 shallUseOffscreenPBufferLayer = true;
416             } else if(args[i].equals("-msaa")) {
417                 useMSAA = true;
418             } else if(args[i].equals("-stencil")) {
419                 useStencil = true;
420             } else if(args[i].equals("-wait")) {
421                 waitForKey = true;
422             } else if(args[i].equals("-shutdownKeepGLCanvas")) {
423                 shutdownRemoveGLCanvas = false;
424             } else if(args[i].equals("-shutdownKeepFrame")) {
425                 shutdownDisposeFrame = false;
426             } else if(args[i].equals("-shutdownKeepAll")) {
427                 shutdownRemoveGLCanvas = false;
428                 shutdownDisposeFrame = false;
429             } else if(args[i].equals("-shutdownSystemExit")) {
430                 shutdownSystemExit = true;
431             }
432         }
433         if( 0 < rw && 0 < rh ) {
434             rwsize = new Dimension(rw, rh);
435         }
436
437         System.err.println("resize "+rwsize);
438         System.err.println("frameLayout "+frameLayout);
439         System.err.println("resizeBy "+resizeBy);
440         System.err.println("forceES2 "+forceES2);
441         System.err.println("forceGL3 "+forceGL3);
442         System.err.println("swapInterval "+swapInterval);
443         System.err.println("exclusiveContext "+exclusiveContext);
444         System.err.println("useMSAA "+useMSAA);
445         System.err.println("useAnimator "+useAnimator);
446
447         System.err.println("shallUseOffscreenFBOLayer     "+shallUseOffscreenFBOLayer);
448         System.err.println("shallUseOffscreenPBufferLayer "+shallUseOffscreenPBufferLayer);
449
450         if(waitForKey) {
451             BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
452             System.err.println("Press enter to continue");
453             try {
454                 System.err.println(stdin.readLine());
455             } catch (IOException e) { }
456         }
457         org.junit.runner.JUnitCore.main(TestGearsES2AWT.class.getName());
458     }
459 }
http://JogAmp.org git info: FAQ, tutorial and man pages.