JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSWTAccessor03AWTGLn.java
Go to the documentation of this file.
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
29package com.jogamp.opengl.test.junit.jogl.swt;
30
31import java.awt.Frame;
32import java.lang.reflect.InvocationTargetException;
33
34import com.jogamp.opengl.GL2ES1;
35import com.jogamp.opengl.GLAutoDrawable;
36import com.jogamp.opengl.GLCapabilities;
37import com.jogamp.opengl.GLEventListener;
38import com.jogamp.opengl.GLProfile;
39import com.jogamp.opengl.awt.GLCanvas;
40
41
42import org.eclipse.swt.SWT;
43import org.eclipse.swt.awt.SWT_AWT;
44import org.eclipse.swt.graphics.Rectangle;
45import org.eclipse.swt.layout.FillLayout;
46import org.eclipse.swt.widgets.Composite;
47import org.eclipse.swt.widgets.Display;
48import org.eclipse.swt.widgets.Shell;
49
50import org.junit.Assert;
51import org.junit.Assume;
52import org.junit.BeforeClass;
53import org.junit.Test;
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
56
57import com.jogamp.common.os.Platform;
58import com.jogamp.junit.util.JunitTracer;
59import com.jogamp.nativewindow.swt.SWTAccessor;
60import com.jogamp.opengl.test.junit.jogl.demos.es1.OneTriangle;
61import com.jogamp.opengl.test.junit.util.MiscUtils;
62import com.jogamp.opengl.test.junit.util.UITestCase;
63
64/**
65 * Tests that a basic SWT app can open without crashing under different GL profiles. Uses the AWT GL canvas with
66 * the SWT_AWT bridge.
67 * @author Wade Walker, et.al.
68 */
69@FixMethodOrder(MethodSorters.NAME_ASCENDING)
70public class TestSWTAccessor03AWTGLn extends UITestCase {
71
72 static int duration = 250;
73
74 Display display = null;
75 Shell shell = null;
76 Composite composite = null;
77 Frame frame = null;
78 GLCanvas glcanvas = null;
79
80 @BeforeClass
81 public static void startup() {
82 if( Platform.getOSType() == Platform.OSType.MACOS ) {
83 // NSLocking issues on OSX and AWT, able to freeze whole test suite!
84 // Since this test is merely a technical nature to validate the accessor w/ SWT
85 // we can drop it w/o bothering.
86 JunitTracer.setTestSupported(false);
87 return;
88 }
89 System.out.println( "GLProfile " + GLProfile.glAvailabilityToString() );
90 final Frame f0 = new Frame("Test - AWT 1st");
91 f0.add(new java.awt.Label("AWT was here 1st"));
92 try {
93 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
94 public void run() {
95 f0.pack();
96 f0.setVisible(true);
97 }});
98 } catch (final Exception e) {
99 throw new RuntimeException(e);
100 }
102 setTestSupported(false);
103 }
104 }
105
106 protected void init() throws InterruptedException, InvocationTargetException {
107 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
108 public void run() {
109 display = new Display();
110 Assert.assertNotNull( display );
111 SWTAccessor.printInfo(System.err, display);
112 shell = new Shell( display );
113 Assert.assertNotNull( shell );
114 shell.setLayout( new FillLayout() );
115 composite = new Composite( shell, SWT.EMBEDDED | SWT.NO_BACKGROUND );
116 composite.setLayout( new FillLayout() );
117 Assert.assertNotNull( composite );
118 frame = SWT_AWT.new_Frame( composite );
119 Assert.assertNotNull( frame );
120 }});
121 }
122
123 protected void release() throws InterruptedException, InvocationTargetException {
124 Assert.assertNotNull( display );
125 Assert.assertNotNull( shell );
126 Assert.assertNotNull( composite );
127 Assert.assertNotNull( glcanvas );
128 final Runnable releaseAWT = new Runnable() {
129 public void run() {
130 // deadlocks Java7 on Windows
131 frame.setVisible(false);
132 frame.remove(glcanvas);
133 frame.dispose();
134 frame = null;
135 glcanvas = null;
136 } };
137 // Deadlocks Java7 on Windows
138 // javax.swing.SwingUtilities.invokeAndWait( releaseAWT );
139 releaseAWT.run();
140
141 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
142 public void run() {
143 composite.dispose();
144 shell.close();
145 shell.dispose();
146 display.dispose();
147 display = null;
148 shell = null;
149 composite = null;
150 }});
151 }
152
153 protected void runTestGL( final GLProfile glprofile ) throws InterruptedException, InvocationTargetException {
154 init();
155 try {
156 final GLCapabilities glcapabilities = new GLCapabilities( glprofile );
157 glcanvas = new GLCanvas( glcapabilities );
158 Assert.assertNotNull( glcanvas );
159 frame.add( glcanvas );
160
161 glcanvas.addGLEventListener( new GLEventListener() {
162 /* @Override */
163 public void init( final GLAutoDrawable glautodrawable ) {
164 }
165
166 /* @Override */
167 public void dispose( final GLAutoDrawable glautodrawable ) {
168 }
169
170 /* @Override */
171 public void display( final GLAutoDrawable glautodrawable ) {
172 final Rectangle rectangle = new Rectangle( 0, 0, glautodrawable.getSurfaceWidth(), glautodrawable.getSurfaceHeight() );
173 final GL2ES1 gl = glautodrawable.getGL().getGL2ES1();
174 OneTriangle.render( gl, rectangle.width, rectangle.height );
175 }
176
177 /* @Override */
178 public void reshape( final GLAutoDrawable glautodrawable, final int x, final int y, final int width, final int height ) {
179 final Rectangle rectangle = new Rectangle( 0, 0, glautodrawable.getSurfaceWidth(), glautodrawable.getSurfaceHeight() );
180 final GL2ES1 gl = glautodrawable.getGL().getGL2ES1();
181 OneTriangle.setup( gl, rectangle.width, rectangle.height );
182 }
183 });
184
185 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
186 public void run() {
187 shell.setText( getClass().getName() );
188 shell.setSize( 640, 480 );
189 shell.open();
190 }});
191
192 final long lStartTime = System.currentTimeMillis();
193 final long lEndTime = lStartTime + duration;
194 try {
195 while( (System.currentTimeMillis() < lEndTime) && !composite.isDisposed() ) {
196 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
197 public void run() {
198 if( !display.readAndDispatch() ) {
199 // blocks on linux .. display.sleep();
200 try {
201 Thread.sleep(10);
202 } catch (final InterruptedException e) { }
203 }
204 }});
205 }
206 }
207 catch( final Throwable throwable ) {
208 throwable.printStackTrace();
209 Assume.assumeNoException( throwable );
210 }
211 } finally {
212 release();
213 }
214 }
215
216 @Test
217 public void test() throws InterruptedException, InvocationTargetException {
218 final GLProfile glprofile = GLProfile.getGL2ES1();
219 runTestGL( glprofile );
220 }
221
222 public static void main(final String args[]) {
223 for(int i=0; i<args.length; i++) {
224 if(args[i].equals("-time")) {
225 duration = MiscUtils.atoi(args[++i], duration);
226 }
227 }
228 org.junit.runner.JUnitCore.main( TestSWTAccessor03AWTGLn.class.getName() );
229 }
230}
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
static void printInfo(final PrintStream out, final Display d)
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static String glAvailabilityToString(final AbstractGraphicsDevice device)
Definition: GLProfile.java:333
static GLProfile getGL2ES1(final AbstractGraphicsDevice device)
Returns the GL2ES1 profile implementation, hence compatible w/ GL2ES1.
Definition: GLProfile.java:883
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
A utility class to encapsulate drawing a single triangle for unit tests.
static void setup(final GL2ES1 gl, final int width, final int height)
static void render(final GL2ES1 gl, final int width, final int height)
Tests that a basic SWT app can open without crashing under different GL profiles.
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2ES1 getGL2ES1()
Casts this object to the GL2ES1 interface.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.