JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug572AWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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.awt;
30
31import java.awt.Dimension;
32import java.awt.Window;
33import java.lang.reflect.InvocationTargetException;
34
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLProfile;
37import com.jogamp.opengl.awt.GLCanvas;
38import javax.swing.JFrame;
39import javax.swing.SwingUtilities;
40
41import org.junit.Assert;
42import org.junit.Assume;
43import org.junit.Test;
44import org.junit.FixMethodOrder;
45import org.junit.runners.MethodSorters;
46
47import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
48import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
49import com.jogamp.opengl.test.junit.util.UITestCase;
50
51/**
52 * Test realize GLCanvas and setVisible(true) AWT-Frames on AWT-EDT and on current thread (non AWT-EDT)
53 */
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55public class TestBug572AWT extends UITestCase {
56 static long durationPerTest = 150; // ms
57
58 static class Cleanup implements Runnable {
59 Window window;
60
61 public Cleanup(final Window w) {
62 window = w;
63 }
64
65 public void run() {
66 System.err.println("cleaning up...");
67 window.setVisible(false);
68 try {
69 window.removeAll();
70 } catch (final Throwable t) {
71 Assume.assumeNoException(t);
72 t.printStackTrace();
73 }
74 window.dispose();
75 }
76 }
77
78 private void testRealizeGLCanvas(final boolean onAWTEDT, final boolean setFrameSize) throws InterruptedException, InvocationTargetException {
79 final Window window = new JFrame(this.getSimpleTestName(" - "));
81 final GLCanvas glCanvas = new GLCanvas(caps);
82 final SnapshotGLEventListener snapshooter = new SnapshotGLEventListener();
83 snapshooter.setMakeSnapshotAlways(true);
84 glCanvas.addGLEventListener(new GearsES2());
85 glCanvas.addGLEventListener(snapshooter);
86 window.add(glCanvas);
87
88 final Runnable realizeAction = new Runnable() {
89 @Override
90 public void run() {
91 // Revalidate size/layout.
92 // Always validate if component added/removed.
93 // Ensure 1st paint of GLCanvas will have a valid size, hence drawable gets created.
94 if( setFrameSize ) {
95 window.setSize(512, 512);
96 window.validate();
97 } else {
98 final Dimension size = new Dimension(512, 512);
99 glCanvas.setPreferredSize(size);
100 glCanvas.setMinimumSize(size);
101 window.pack();
102 }
103 window.setVisible(true);
104 } };
105 if( onAWTEDT ) {
106 // trigger realization on AWT-EDT, otherwise it won't immediatly ..
107 SwingUtilities.invokeAndWait( realizeAction );
108 } else {
109 // trigger realization on non AWT-EDT, realization will happen at a later time ..
110 realizeAction.run();
111
112 // Wait until it's displayable after issuing initial setVisible(true) on current thread (non AWT-EDT)!
113 Assert.assertTrue("GLCanvas didn't become visible", AWTRobotUtil.waitForVisible(glCanvas, true, null));
114 Assert.assertTrue("GLCanvas didn't become realized", AWTRobotUtil.waitForRealized(glCanvas, true, null)); // implies displayable
115 }
116
117 System.err.println("XXXX-0 "+glCanvas.getDelegatedDrawable().isRealized()+", "+glCanvas);
118
119 Assert.assertTrue("GLCanvas didn't become displayable", glCanvas.isDisplayable());
120 Assert.assertTrue("GLCanvas didn't become realized", glCanvas.isRealized());
121
122 // The AWT-EDT reshape/repaint events happen offthread later ..
123 System.err.println("XXXX-1 reshapeCount "+snapshooter.getReshapeCount());
124 System.err.println("XXXX-1 displayCount "+snapshooter.getDisplayCount());
125
126 // Wait unitl AWT-EDT has issued reshape/repaint
127 for (int wait=0; wait<AWTRobotUtil.POLL_DIVIDER &&
128 ( 0 == snapshooter.getReshapeCount() || 0 == snapshooter.getDisplayCount() );
129 wait++) {
130 Thread.sleep(AWTRobotUtil.TIME_SLICE);
131 }
132 System.err.println("XXXX-2 reshapeCount "+snapshooter.getReshapeCount());
133 System.err.println("XXXX-2 displayCount "+snapshooter.getDisplayCount());
134
135 Assert.assertTrue("GLCanvas didn't reshape", snapshooter.getReshapeCount()>0);
136 Assert.assertTrue("GLCanvas didn't display", snapshooter.getDisplayCount()>0);
137
138 Thread.sleep(durationPerTest);
139
140 // After initial 'setVisible(true)' all AWT manipulation needs to be done
141 // via the AWT EDT, according to the AWT spec.
142
143 // AWT / Swing on EDT..
144 SwingUtilities.invokeAndWait(new Cleanup(window));
145 }
146
147 @Test(timeout = 10000) // 10s timeout
148 public void test01RealizeGLCanvasOnAWTEDTUseFrameSize() throws InterruptedException, InvocationTargetException {
149 testRealizeGLCanvas(true, true);
150 }
151
152 @Test(timeout = 10000) // 10s timeout
153 public void test02RealizeGLCanvasOnAWTEDTUseGLCanvasSize() throws InterruptedException, InvocationTargetException {
154 testRealizeGLCanvas(true, false);
155 }
156
157 @Test(timeout = 10000) // 10s timeout
158 public void test11RealizeGLCanvasOnMainTUseFrameSize() throws InterruptedException, InvocationTargetException {
159 testRealizeGLCanvas(false, true);
160 }
161
162 @Test(timeout = 10000) // 10s timeout
163 public void test12RealizeGLCanvasOnMainTUseGLCanvasSize() throws InterruptedException, InvocationTargetException {
164 testRealizeGLCanvas(false, false);
165 }
166
167 public static void main(final String args[]) {
168 org.junit.runner.JUnitCore.main(TestBug572AWT.class.getName());
169 }
170}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
boolean isRealized()
Returns true if this drawable is realized, otherwise false.
Definition: GLCanvas.java:480
final GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
Definition: GLCanvas.java:1161
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
Test realize GLCanvas and setVisible(true) AWT-Frames on AWT-EDT and on current thread (non AWT-EDT)
static boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
boolean isRealized()
Returns true if this drawable is realized, otherwise false.