JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug816OSXCALayerPos01AWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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 com.jogamp.opengl.*;
32
33import com.jogamp.opengl.util.Animator;
34
35import com.jogamp.opengl.awt.GLCanvas;
36import javax.swing.BoundedRangeModel;
37import javax.swing.BoxLayout;
38import javax.swing.JFrame;
39import javax.swing.JScrollBar;
40import javax.swing.JScrollPane;
41import javax.swing.JSplitPane;
42import javax.swing.ScrollPaneConstants;
43
44import com.jogamp.common.util.awt.AWTEDTExecutor;
45import com.jogamp.newt.event.awt.AWTWindowAdapter;
46import com.jogamp.newt.event.TraceWindowAdapter;
47import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
48import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
49import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
50import com.jogamp.opengl.test.junit.util.MiscUtils;
51import com.jogamp.opengl.test.junit.util.UITestCase;
52import com.jogamp.opengl.test.junit.util.QuitAdapter;
53
54import java.awt.BorderLayout;
55import java.awt.Button;
56import java.awt.Component;
57import java.awt.Container;
58import java.awt.Dimension;
59import java.awt.FlowLayout;
60import java.awt.Frame;
61import java.awt.GridLayout;
62import java.lang.reflect.InvocationTargetException;
63
64import org.junit.Assert;
65import org.junit.Assume;
66import org.junit.Test;
67import org.junit.FixMethodOrder;
68import org.junit.runners.MethodSorters;
69
70/**
71 * Bug 816: OSX CALayer Positioning Bug.
72 * <p>
73 * Diff. OSX CALayer positioning w/ java6, [7uxx..7u40[, and >= 7u40
74 * </p>
75 */
76@FixMethodOrder(MethodSorters.NAME_ASCENDING)
78 public enum FrameLayout { None, Flow, DoubleBorderCenterSurrounded, Box, Split };
79
80 static long duration = 1600; // ms
81 static final int width = 640, height = 480;
82
83 static boolean forceES2 = false;
84 static boolean forceGL3 = false;
85 static int swapInterval = 1;
86 static java.awt.Dimension rwsize = new Dimension(800, 600);
87
88 static void setComponentSize(final Frame frame, final Component comp1, final java.awt.Dimension new_sz1, final Component comp2, final java.awt.Dimension new_sz2) {
89 try {
90 AWTEDTExecutor.singleton.invoke(true /* wait */, new Runnable() {
91 public void run() {
92 comp1.setMinimumSize(new_sz1);
93 comp1.setPreferredSize(new_sz1);
94 comp1.setSize(new_sz1);
95 if( null != comp2 ) {
96 comp2.setMinimumSize(new_sz2);
97 comp2.setPreferredSize(new_sz2);
98 comp2.setSize(new_sz2);
99 }
100 if( null != frame ) {
101 frame.pack();
102 }
103 } } );
104 } catch( final Throwable throwable ) {
105 throwable.printStackTrace();
106 Assume.assumeNoException( throwable );
107 }
108 }
109 static void setFrameSize(final Frame frame, final boolean frameLayout, final java.awt.Dimension new_sz) {
110 try {
111 AWTEDTExecutor.singleton.invoke(true /* wait */, new Runnable() {
112 public void run() {
113 frame.setSize(new_sz);
114 if( frameLayout ) {
115 frame.validate();
116 }
117 } } );
118 } catch( final Throwable throwable ) {
119 throwable.printStackTrace();
120 Assume.assumeNoException( throwable );
121 }
122 }
123
124 protected void runTestGL(final GLCapabilities caps, final FrameLayout frameLayout, final boolean twoCanvas, final boolean resizeByComp) throws InterruptedException, InvocationTargetException {
125 final JFrame frame = new JFrame("Bug816: "+this.getTestMethodName());
126 Assert.assertNotNull(frame);
127 final Container framePane = frame.getContentPane();
128
129 final GLCanvas glCanvas1 = new GLCanvas(caps);
130 Assert.assertNotNull(glCanvas1);
131 final GLCanvas glCanvas2;
132 if( twoCanvas ) {
133 glCanvas2 = new GLCanvas(caps);
134 Assert.assertNotNull(glCanvas2);
135 } else {
136 glCanvas2 = null;
137 }
138
139 final Dimension glcDim = new Dimension(width/2, height);
140 final Dimension frameDim = new Dimension(twoCanvas ? width + 64: width/2 + 64, height + 64);
141
142 setComponentSize(null, glCanvas1, glcDim, glCanvas2, glcDim);
143
144 switch( frameLayout) {
145 case None: {
146 framePane.add(glCanvas1);
147 }
148 break;
149 case Flow: {
150 final Container c = new Container();
151 c.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
152 c.add(glCanvas1);
153 if( twoCanvas ) {
154 c.add(glCanvas2);
155 }
156 framePane.add(c);
157 }
158 break;
159 case DoubleBorderCenterSurrounded: {
160 final Container c = new Container();
161 c.setLayout(new BorderLayout());
162 c.add(new Button("north"), BorderLayout.NORTH);
163 c.add(new Button("south"), BorderLayout.SOUTH);
164 c.add(new Button("east"), BorderLayout.EAST);
165 c.add(new Button("west"), BorderLayout.WEST);
166 if( twoCanvas ) {
167 final Container c2 = new Container();
168 c2.setLayout(new GridLayout(1, 2));
169 c2.add(glCanvas1);
170 c2.add(glCanvas2);
171 c.add(c2, BorderLayout.CENTER);
172 } else {
173 c.add(glCanvas1, BorderLayout.CENTER);
174 }
175 framePane.setLayout(new BorderLayout());
176 framePane.add(new Button("NORTH"), BorderLayout.NORTH);
177 framePane.add(new Button("SOUTH"), BorderLayout.SOUTH);
178 framePane.add(new Button("EAST"), BorderLayout.EAST);
179 framePane.add(new Button("WEST"), BorderLayout.WEST);
180 framePane.add(c, BorderLayout.CENTER);
181 }
182 break;
183 case Box: {
184 final Container c = new Container();
185 c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS));
186 c.add(glCanvas1);
187 if( twoCanvas ) {
188 c.add(glCanvas2);
189 }
190 framePane.add(c);
191 }
192 break;
193 case Split: {
194 final Dimension sbDim = new Dimension(16, 16);
195 final JScrollPane vsp = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
196 {
197 final JScrollBar vsb = vsp.getVerticalScrollBar();
198 vsb.setPreferredSize(sbDim);
199 final BoundedRangeModel model = vsb.getModel();
200 model.setMinimum(0);
201 model.setMaximum(100);
202 model.setValue(50);
203 model.setExtent(1);
204 vsb.setEnabled(true);
205 }
206 final JScrollPane hsp = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
207 {
208 final JScrollBar hsb = hsp.getHorizontalScrollBar();
209 hsb.setPreferredSize(sbDim);
210 final BoundedRangeModel model = hsb.getModel();
211 model.setMinimum(0);
212 model.setMaximum(100);
213 model.setValue(50);
214 model.setExtent(1);
215 hsb.setEnabled(true);
216 }
217 final JSplitPane horizontalSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
218 twoCanvas ? glCanvas2 : vsp, glCanvas1 );
219 horizontalSplitPane.setResizeWeight(0.5);
220 final JSplitPane verticalSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
221 true, horizontalSplitPane, hsp);
222 verticalSplitPane.setResizeWeight(0.5);
223 framePane.add(verticalSplitPane);
224 }
225 break;
226 }
227 final GearsES2 demo1 = new GearsES2(swapInterval);
228 glCanvas1.addGLEventListener(demo1);
229 if( twoCanvas ) {
230 final RedSquareES2 demo2 = new RedSquareES2(swapInterval);
231 glCanvas2.addGLEventListener(demo2);
232 }
233
234 final Animator animator = new Animator();
235 animator.add(glCanvas1);
236 if( twoCanvas ) {
237 animator.add(glCanvas2);
238 }
239 final QuitAdapter quitAdapter = new QuitAdapter();
240 new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glCanvas1).addTo(frame);
241
242 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
243 public void run() {
244 if( resizeByComp ) {
245 frame.pack();
246 } else {
247 setFrameSize(frame, true, frameDim);
248 }
249 frame.setVisible(true);
250 }});
251 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
252 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glCanvas1, true, null));
253 if( twoCanvas ) {
254 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glCanvas2, true, null));
255 }
256
257 animator.start();
258 Assert.assertTrue(animator.isStarted());
259 Assert.assertTrue(animator.isAnimating());
260
261 System.err.println("canvas1 pos/siz: "+glCanvas1.getX()+"/"+glCanvas1.getY()+" "+glCanvas1.getSurfaceWidth()+"x"+glCanvas1.getSurfaceHeight());
262 if( twoCanvas ) {
263 System.err.println("canvas2 pos/siz: "+glCanvas2.getX()+"/"+glCanvas2.getY()+" "+glCanvas2.getSurfaceWidth()+"x"+glCanvas2.getSurfaceHeight());
264 }
265
266 Thread.sleep(Math.max(1000, duration/2));
267 if( null != rwsize ) {
268 final Dimension compRSizeHalf = new Dimension(rwsize.width/2, rwsize.height);
269 final Dimension frameRSizeHalf = new Dimension(twoCanvas ? rwsize.width + 64: rwsize.width/2 + 64, rwsize.height + 64);
270 if( resizeByComp ) {
271 setComponentSize(frame, glCanvas1, compRSizeHalf, glCanvas2, compRSizeHalf);
272 } else {
273 setFrameSize(frame, true, frameRSizeHalf);
274 }
275 System.err.println("resize canvas1 pos/siz: "+glCanvas1.getX()+"/"+glCanvas1.getY()+" "+glCanvas1.getSurfaceWidth()+"x"+glCanvas1.getSurfaceHeight());
276 if( twoCanvas ) {
277 System.err.println("resize canvas2 pos/siz: "+glCanvas2.getX()+"/"+glCanvas2.getY()+" "+glCanvas2.getSurfaceWidth()+"x"+glCanvas2.getSurfaceHeight());
278 }
279 }
280
281 final long t0 = System.currentTimeMillis();
282 long t1 = t0;
283 while(!quitAdapter.shouldQuit() && t1 - t0 < duration) {
284 Thread.sleep(100);
285 t1 = System.currentTimeMillis();
286 }
287
288 Assert.assertNotNull(frame);
289 Assert.assertNotNull(glCanvas1);
290 if( twoCanvas ) {
291 Assert.assertNotNull(glCanvas2);
292 } else {
293 Assert.assertNull(glCanvas2);
294 }
295
296 Assert.assertNotNull(animator);
297 animator.stop();
298 Assert.assertFalse(animator.isAnimating());
299 Assert.assertFalse(animator.isStarted());
300
301 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
302 public void run() {
303 frame.setVisible(false);
304 }});
305 Assert.assertEquals(false, frame.isVisible());
306 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
307 public void run() {
308 frame.remove(glCanvas1);
309 if( twoCanvas ) {
310 frame.remove(glCanvas2);
311 }
312 frame.dispose();
313 }});
314 }
315
316 static GLProfile getGLP() {
318 }
319
320 @Test
321 public void test00_Compo_None_One() throws InterruptedException, InvocationTargetException {
322 if( testNum != -1 && testNum != 0 ) { return ; }
323 final GLCapabilities caps = new GLCapabilities(getGLP());
324 runTestGL(caps, FrameLayout.None, false /* twoCanvas */, true /* resizeByComp */);
325 }
326
327 @Test
328 public void test01_Compo_Flow_One() throws InterruptedException, InvocationTargetException {
329 if( testNum != -1 && testNum != 1 ) { return ; }
330 final GLCapabilities caps = new GLCapabilities(getGLP());
331 runTestGL(caps, FrameLayout.Flow, false /* twoCanvas */, true /* resizeByComp */);
332 }
333
334 @Test
335 public void test02_Compo_DblBrd_One() throws InterruptedException, InvocationTargetException {
336 if( testNum != -1 && testNum != 2 ) { return ; }
337 final GLCapabilities caps = new GLCapabilities(getGLP());
338 runTestGL(caps, FrameLayout.DoubleBorderCenterSurrounded, false /* twoCanvas */, true /* resizeByComp */);
339 }
340
341 @Test
342 public void test03_Compo_Box_One() throws InterruptedException, InvocationTargetException {
343 if( testNum != -1 && testNum != 3 ) { return ; }
344 final GLCapabilities caps = new GLCapabilities(getGLP());
345 runTestGL(caps, FrameLayout.Box, false /* twoCanvas */, true /* resizeByComp */);
346 }
347
348 @Test
349 public void test04_Compo_Split_One() throws InterruptedException, InvocationTargetException {
350 if( testNum != -1 && testNum != 4 ) { return ; }
351 final GLCapabilities caps = new GLCapabilities(getGLP());
352 runTestGL(caps, FrameLayout.Split, false /* twoCanvas */, true /* resizeByComp */);
353 }
354
355 @Test
356 public void test05_Compo_Flow_Two() throws InterruptedException, InvocationTargetException {
357 if( testNum != -1 && testNum != 5 ) { return ; }
358 final GLCapabilities caps = new GLCapabilities(getGLP());
359 runTestGL(caps, FrameLayout.Flow, true/* twoCanvas */, true /* resizeByComp */);
360 }
361
362 @Test
363 public void test06_Compo_DblBrd_Two() throws InterruptedException, InvocationTargetException {
364 if( testNum != -1 && testNum != 6 ) { return ; }
365 final GLCapabilities caps = new GLCapabilities(getGLP());
366 runTestGL(caps, FrameLayout.DoubleBorderCenterSurrounded, true/* twoCanvas */, true /* resizeByComp */);
367 }
368
369 @Test
370 public void test07_Compo_Box_Two() throws InterruptedException, InvocationTargetException {
371 if( testNum != -1 && testNum != 7 ) { return ; }
372 final GLCapabilities caps = new GLCapabilities(getGLP());
373 runTestGL(caps, FrameLayout.Box, true/* twoCanvas */, true /* resizeByComp */);
374 }
375
376 @Test
377 public void test08_Compo_Split_Two() throws InterruptedException, InvocationTargetException {
378 if( testNum != -1 && testNum != 8 ) { return ; }
379 final GLCapabilities caps = new GLCapabilities(getGLP());
380 runTestGL(caps, FrameLayout.Split, true/* twoCanvas */, true /* resizeByComp */);
381 }
382
383 @Test
384 public void test10_Frame_None_One() throws InterruptedException, InvocationTargetException {
385 if( testNum != -1 && testNum != 10 ) { return ; }
386 final GLCapabilities caps = new GLCapabilities(getGLP());
387 runTestGL(caps, FrameLayout.None, false /* twoCanvas */, false /* resizeByComp */);
388 }
389
390 @Test
391 public void test11_Frame_Flow_One() throws InterruptedException, InvocationTargetException {
392 if( testNum != -1 && testNum != 11 ) { return ; }
393 final GLCapabilities caps = new GLCapabilities(getGLP());
394 runTestGL(caps, FrameLayout.Flow, false /* twoCanvas */, false /* resizeByComp */);
395 }
396
397 @Test
398 public void test12_Frame_DblBrd_One() throws InterruptedException, InvocationTargetException {
399 if( testNum != -1 && testNum != 12 ) { return ; }
400 final GLCapabilities caps = new GLCapabilities(getGLP());
401 runTestGL(caps, FrameLayout.DoubleBorderCenterSurrounded, false /* twoCanvas */, false /* resizeByComp */);
402 }
403
404 @Test
405 public void test13_Frame_Box_One() throws InterruptedException, InvocationTargetException {
406 if( testNum != -1 && testNum != 13 ) { return ; }
407 final GLCapabilities caps = new GLCapabilities(getGLP());
408 runTestGL(caps, FrameLayout.Box, false /* twoCanvas */, false /* resizeByComp */);
409 }
410
411 @Test
412 public void test14_Frame_Split_One() throws InterruptedException, InvocationTargetException {
413 if( testNum != -1 && testNum != 14) { return ; }
414 final GLCapabilities caps = new GLCapabilities(getGLP());
415 runTestGL(caps, FrameLayout.Split, false /* twoCanvas */, false /* resizeByComp */);
416 }
417
418 @Test
419 public void test15_Frame_Flow_Two() throws InterruptedException, InvocationTargetException {
420 if( testNum != -1 && testNum != 15 ) { return ; }
421 final GLCapabilities caps = new GLCapabilities(getGLP());
422 runTestGL(caps, FrameLayout.Flow, true/* twoCanvas */, false /* resizeByComp */);
423 }
424
425 @Test
426 public void test16_Frame_DblBrd_Two() throws InterruptedException, InvocationTargetException {
427 if( testNum != -1 && testNum != 16 ) { return ; }
428 final GLCapabilities caps = new GLCapabilities(getGLP());
429 runTestGL(caps, FrameLayout.DoubleBorderCenterSurrounded, true/* twoCanvas */, false /* resizeByComp */);
430 }
431
432 @Test
433 public void test17_Frame_Box_Two() throws InterruptedException, InvocationTargetException {
434 if( testNum != -1 && testNum != 17 ) { return ; }
435 final GLCapabilities caps = new GLCapabilities(getGLP());
436 runTestGL(caps, FrameLayout.Box, true/* twoCanvas */, false /* resizeByComp */);
437 }
438
439 @Test
440 public void test18_Frame_Split_Two() throws InterruptedException, InvocationTargetException {
441 if( testNum != -1 && testNum != 18 ) { return ; }
442 final GLCapabilities caps = new GLCapabilities(getGLP());
443 runTestGL(caps, FrameLayout.Split, true/* twoCanvas */, false /* resizeByComp */);
444 }
445
446 static int testNum = -1;
447
448 public static void main(final String args[]) {
449 for(int i=0; i<args.length; i++) {
450 if(args[i].equals("-time")) {
451 i++;
452 duration = MiscUtils.atol(args[i], duration);
453 } else if(args[i].equals("-test")) {
454 i++;
455 testNum = MiscUtils.atoi(args[i], 0);
456 } else if(args[i].equals("-noresize")) {
457 rwsize = null;
458 } else if(args[i].equals("-es2")) {
459 forceES2 = true;
460 } else if(args[i].equals("-gl3")) {
461 forceGL3 = true;
462 } else if(args[i].equals("-vsync")) {
463 i++;
464 swapInterval = MiscUtils.atoi(args[i], swapInterval);
465 }
466 }
467
468 System.err.println("resize "+rwsize);
469 System.err.println("forceES2 "+forceES2);
470 System.err.println("forceGL3 "+forceGL3);
471 System.err.println("swapInterval "+swapInterval);
472
473 org.junit.runner.JUnitCore.main(TestBug816OSXCALayerPos01AWT.class.getName());
474 }
475}
synchronized AWTAdapter addTo(final java.awt.Component awtComponent)
Due to the fact that some NEWT com.jogamp.newt.event.NEWTEventListener are mapped to more than one ja...
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getMaxProgrammableCore(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the programmable shader core pipeline only.
Definition: GLProfile.java:854
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:1248
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:1243
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
void runTestGL(final GLCapabilities caps, final FrameLayout frameLayout, final boolean twoCanvas, final boolean resizeByComp)
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)
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
synchronized boolean isStarted()
Indicates whether this animator has been started.
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368