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.awt;
30 
31 import java.lang.reflect.InvocationTargetException;
32 import com.jogamp.opengl.GLProfile;
33 import com.jogamp.opengl.GLCapabilities;
34 import com.jogamp.opengl.awt.GLCanvas;
35 import com.jogamp.opengl.util.Animator;
36 
37 import com.jogamp.opengl.test.junit.util.UITestCase;
38 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
39 
40 
41 import java.awt.GraphicsConfiguration;
42 import java.awt.GraphicsDevice;
43 import java.awt.GraphicsEnvironment;
44 import java.awt.Rectangle;
45 import java.awt.Window;
46 import javax.swing.JFrame;
47 
48 import org.junit.Test;
49 import org.junit.FixMethodOrder;
50 import org.junit.runners.MethodSorters;
51 
52 import static org.junit.Assume.*;
53 import static javax.swing.SwingUtilities.*;
54 
55 /**
56  * Tests context creation + display on various kinds of Window implementations.
57  * @author Michael Bien, et. al.
58  */
59 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 public class TestBug551AWT extends UITestCase {
61 
checkGraphicsEnvironment()62     static void checkGraphicsEnvironment() {
63         Rectangle virtualBounds = new Rectangle();
64         final GraphicsEnvironment ge =GraphicsEnvironment.getLocalGraphicsEnvironment();
65         final GraphicsDevice[] gs = ge.getScreenDevices();
66 
67         //write graphics devices to log
68         System.err.println("number of graphics devices " + gs.length);
69         for(int i =0 ; i < gs.length; i++) {
70             System.err.println(gs[i].toString());
71         }
72 
73         //check for bounds
74         for (int j = 0; j < gs.length; j++) {
75             final GraphicsDevice gd = gs[j];
76             final GraphicsConfiguration[] gc = gd.getConfigurations();
77             for (int i=0; i < gc.length; i++) {
78                 System.err.println("graphics configuration for device " + j + " is: " + gc[i].getBounds());
79                 virtualBounds = virtualBounds.union(gc[i].getBounds());
80             }
81         }
82 
83     }
84 
runTestGL()85     protected void runTestGL() throws InterruptedException, InvocationTargetException {
86         final Window window = new JFrame(this.getSimpleTestName(" - "));
87         final GLCapabilities caps = new GLCapabilities(GLProfile.getGL2ES2());
88 
89         // final array as mutable container hack
90         final GLCanvas[] glCanvas = new GLCanvas[1];
91 
92         final Runnable test = new Runnable() {
93             public void run() {
94                 glCanvas[0] = new GLCanvas(caps);
95                 glCanvas[0].addGLEventListener(new GearsES2());
96                 window.add(glCanvas[0]);
97 
98                 // Revalidate size/layout.
99                 // Always validate if component added/removed.
100                 // Ensure 1st paint of GLCanvas will have a valid size, hence drawable gets created.
101                 window.setSize(512, 512);
102                 window.validate();
103 
104                 window.setVisible(true);
105                 glCanvas[0].display();
106             }
107         };
108 
109         final Runnable cleanup = new Runnable() {
110             public void run() {
111                 System.out.println("cleaning up...");
112                 window.setVisible(false);
113                 try {
114                     window.removeAll();
115                 } catch (final Throwable t) {
116                     assumeNoException(t);
117                     t.printStackTrace();
118                 }
119                 window.dispose();
120             }
121 
122         };
123 
124         // AWT / Swing on EDT..
125         invokeAndWait(test);
126 
127         final Animator animator = new Animator(glCanvas[0]);
128         animator.start();
129         Thread.sleep(1000);
130         animator.stop();
131 
132         // AWT / Swing on EDT..
133         invokeAndWait(cleanup);
134     }
135 
136     // @Test
test01Plain()137     public void test01Plain() throws InterruptedException, InvocationTargetException {
138         runTestGL();
139     }
140 
141     @Test
test02WithCheckGraphicsEnvironment()142     public void test02WithCheckGraphicsEnvironment() throws InterruptedException, InvocationTargetException {
143         checkGraphicsEnvironment();
144         runTestGL();
145     }
146 
main(final String args[])147     public static void main(final String args[]) {
148         org.junit.runner.JUnitCore.main(TestBug551AWT.class.getName());
149     }
150 }
151