29package com.jogamp.opengl.test.junit.jogl.awt;
31import com.jogamp.opengl.GLAutoDrawable;
32import com.jogamp.opengl.GLCapabilities;
33import com.jogamp.opengl.GLEventListener;
34import com.jogamp.opengl.GLProfile;
35import com.jogamp.opengl.awt.GLCanvas;
37import com.jogamp.common.os.Platform;
38import com.jogamp.common.util.VersionNumber;
39import com.jogamp.common.util.awt.AWTEDTExecutor;
40import com.jogamp.opengl.util.Animator;
41import com.jogamp.opengl.util.AnimatorBase;
42import com.jogamp.opengl.util.FPSAnimator;
44import com.jogamp.opengl.test.junit.util.UITestCase;
45import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
47import com.jogamp.opengl.test.junit.util.MiscUtils;
49import java.applet.Applet;
50import java.awt.BorderLayout;
52import java.awt.Insets;
53import java.awt.event.WindowAdapter;
54import java.awt.event.WindowEvent;
55import java.lang.reflect.InvocationTargetException;
57import org.junit.Assert;
58import org.junit.Assume;
60import org.junit.FixMethodOrder;
61import org.junit.runners.MethodSorters;
81@FixMethodOrder(MethodSorters.NAME_ASCENDING)
83 static long durationPerTest = 1000;
84 static final int width = 512;
85 static final int height = 512;
91 public void test00NoAnimator() throws InterruptedException, InvocationTargetException {
92 testImpl(
null, 0,
false);
96 public void test01Animator() throws InterruptedException, InvocationTargetException {
118 private static void setFrameTitle(
final Frame frame,
final String msg) {
119 System.err.println(
"About to setTitle: <"+msg+
"> CT "+Thread.currentThread().getName()+
", "+
120 frame+
", displayable "+frame.isDisplayable()+
121 ", valid "+frame.isValid()+
", visible "+frame.isVisible());
123 AWTEDTExecutor.singleton.invoke(
true,
new Runnable() {
129 void testImpl(
final AnimatorBase animator,
final int restartPeriod,
final boolean restartOnCurrentThread)
throws InterruptedException, InvocationTargetException {
130 final Frame frame1 =
new Frame(
"Frame 1");
131 final Applet applet1 =
new Applet() {
132 private static final long serialVersionUID = 1L;
135 final VersionNumber version170 =
new VersionNumber(1, 7, 0);
136 final boolean osxCALayerAWTModBug = Platform.OSType.MACOS == Platform.getOSType() &&
137 0 > Platform.getJavaVersionNumber().compareTo(version170);
138 System.err.println(
"OSX CALayer AWT-Mod Bug "+osxCALayerAWTModBug);
139 System.err.println(
"OSType "+Platform.getOSType());
140 System.err.println(
"Java Version "+Platform.getJavaVersionNumber());
142 Assert.assertNotNull(frame1);
143 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
145 frame1.setLayout(
null);
148 final Insets insets = frame1.getInsets();
149 final int w = width + insets.left + insets.right;
150 final int h = height + insets.top + insets.bottom;
151 frame1.setSize(w, h);
153 final int usableH = h - insets.top - insets.bottom;
154 applet1.setBounds((w - width)/2, insets.top + (usableH - height)/2, width, height);
156 frame1.setLocation(0, 0);
157 frame1.setTitle(
"Generic Title");
161 frame1.addWindowListener(
new WindowAdapter() {
162 public void windowClosing(
final WindowEvent e) {
163 dispose(frame1, applet1);
167 gle1 =
new GLEventListener() {
168 boolean justInitialized =
true;
171 public void init(
final GLAutoDrawable drawable) {
172 justInitialized =
true;
173 if( !osxCALayerAWTModBug ) {
174 System.err.println(
"*Init*: CT "+Thread.currentThread().getName());
175 setFrameTitle(frame1,
"INIT");
176 frame1.setResizable(
false);
181 public void dispose(
final GLAutoDrawable drawable) {
182 System.err.println(
"*Dispose*: CT "+Thread.currentThread().getName());
183 setFrameTitle(frame1,
"DISPOSE");
187 public void display(
final GLAutoDrawable drawable) {
188 if( !osxCALayerAWTModBug || !justInitialized ) {
189 System.err.println(
"*Display*: CT "+Thread.currentThread().getName());
190 setFrameTitle(frame1,
"f "+frameCount+
", fps "+(
null != animator ? animator.getLastFPS() : 0));
191 frame1.setResizable(
false);
194 justInitialized =
false;
198 public void reshape(
final GLAutoDrawable drawable,
final int x,
final int y,
final int width,
final int height) {
199 if( !osxCALayerAWTModBug || !justInitialized ) {
200 System.err.println(
"*Reshape*: CT "+Thread.currentThread().getName());
201 setFrameTitle(frame1,
"RESHAPE");
205 gle2 =
new GearsES2();
207 GLCanvas glCanvas = createGLCanvas();
208 glCanvas.addGLEventListener(gle1);
209 glCanvas.addGLEventListener(gle2);
211 if(
null != animator) {
212 System.err.println(
"About to start Animator: CT "+Thread.currentThread().getName());
213 animator.setUpdateFPSFrames(60, System.err);
214 animator.add(glCanvas);
218 attachGLCanvas(applet1, glCanvas,
false);
220 System.err.println(
"About to setVisible.0 CT "+Thread.currentThread().getName());
222 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
224 System.err.println(
"About to setVisible.1.0 CT "+Thread.currentThread().getName());
225 frame1.setVisible(
true);
226 System.err.println(
"About to setVisible.1.X CT "+Thread.currentThread().getName());
228 }
catch (
final Throwable t) {
230 Assume.assumeNoException(t);
232 System.err.println(
"About to setVisible.X CT "+Thread.currentThread().getName());
234 final long sleep = 0 < restartPeriod ? restartPeriod : 100;
235 long togo = durationPerTest;
237 if(
null == animator) {
240 if(0 < restartPeriod) {
241 glCanvas = restart(applet1, glCanvas, restartOnCurrentThread);
249 dispose(frame1, applet1);
250 if(
null != animator) {
260 GLCanvas createGLCanvas() {
261 System.err.println(
"*** createGLCanvas.0");
262 final GLCapabilities caps =
new GLCapabilities(GLProfile.getDefault());
266 final GLCanvas glCanvas =
new GLCanvas(caps);
267 glCanvas.setBounds(0, 0, width, height);
268 Assert.assertNotNull(glCanvas);
269 System.err.println(
"*** createGLCanvas.X");
274 void dispose(
final Frame frame,
final Applet applet) {
276 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
278 frame.remove(applet);
281 }
catch (
final Throwable t) {
283 Assume.assumeNoException(t);
287 GLCanvas restart(
final Applet applet, GLCanvas glCanvas,
final boolean restartOnCurrentThread)
throws InterruptedException {
288 glCanvas.disposeGLEventListener(gle1,
true);
289 glCanvas.disposeGLEventListener(gle2,
true);
290 detachGLCanvas(applet, glCanvas, restartOnCurrentThread);
292 glCanvas = createGLCanvas();
294 attachGLCanvas(applet, glCanvas, restartOnCurrentThread);
295 glCanvas.addGLEventListener(gle1);
296 glCanvas.addGLEventListener(gle2);
301 void attachGLCanvas(
final Applet applet,
final GLCanvas glCanvas,
final boolean restartOnCurrentThread) {
302 System.err.println(
"*** attachGLCanvas.0 on-current-thread "+restartOnCurrentThread+
", currentThread "+Thread.currentThread().getName());
303 if( restartOnCurrentThread ) {
304 applet.setLayout(
new BorderLayout());
305 applet.add(glCanvas, BorderLayout.CENTER);
309 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
311 applet.setLayout(
new BorderLayout());
312 applet.add(glCanvas, BorderLayout.CENTER);
315 }
catch (
final Throwable t) {
317 Assume.assumeNoException(t);
320 System.err.println(
"*** attachGLCanvas.X");
323 void detachGLCanvas(
final Applet applet,
final GLCanvas glCanvas,
final boolean restartOnCurrentThread) {
324 System.err.println(
"*** detachGLCanvas.0 on-current-thread "+restartOnCurrentThread+
", currentThread "+Thread.currentThread().getName());
325 if( restartOnCurrentThread ) {
326 applet.remove(glCanvas);
330 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
332 applet.remove(glCanvas);
335 }
catch (
final Throwable t) {
337 Assume.assumeNoException(t);
340 System.err.println(
"*** detachGLCanvas.X");
343 public static void main(
final String args[]) {
344 for(
int i=0; i<args.length; i++) {
345 if(args[i].equals(
"-time")) {
346 durationPerTest =
MiscUtils.
atoi(args[++i], (
int)durationPerTest);
BUG on OSX/CALayer w/ Java6: If frame.setTitle() is issued right after initialization the call hangs ...
void test02FPSAnimator_RestartOnAWTEDT()
static void main(final String args[])
static int atoi(final String str, final int def)
An Animator subclass which attempts to achieve a target frames-per-second rate to avoid using all CPU...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.