JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestCloseNewtAWT.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.newt;
30
31import org.junit.Test;
32import org.junit.FixMethodOrder;
33import org.junit.runners.MethodSorters;
34import org.junit.Assert;
35
36import java.lang.reflect.InvocationTargetException;
37
38import javax.swing.JFrame;
39import javax.swing.SwingUtilities;
40import javax.swing.WindowConstants;
41import com.jogamp.nativewindow.NativeWindow;
42import com.jogamp.nativewindow.util.Point;
43import com.jogamp.opengl.GLCapabilities;
44import com.jogamp.opengl.GLProfile;
45
46import com.jogamp.newt.Window;
47import com.jogamp.newt.awt.NewtCanvasAWT;
48import com.jogamp.newt.opengl.GLWindow;
49import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
50import com.jogamp.opengl.test.junit.util.NewtTestUtil;
51import com.jogamp.opengl.test.junit.util.TestUtil;
52import com.jogamp.opengl.test.junit.util.UITestCase;
53
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55public class TestCloseNewtAWT extends UITestCase {
56
57 GLWindow newtWindow = null;
58 NewtCanvasAWT newtCanvas = null;
59 JFrame frame = null;
60
61 @SuppressWarnings("serial")
62 class MyCanvas extends NewtCanvasAWT {
63 public MyCanvas(final Window window) {
64 super(window);
65 }
66
67 public void addNotify() {
68 System.err.println("MyCanvas START add: "+Thread.currentThread()+", holds AWTTreeLock: "+Thread.holdsLock(this.getTreeLock()));
69 super.addNotify();
70 System.err.println("MyCanvas END add: "+Thread.currentThread()+", holds AWTTreeLock: "+Thread.holdsLock(this.getTreeLock()));
71 }
72
73 public void removeNotify() {
74 System.err.println("MyCanvas START remove: "+Thread.currentThread()+", holds AWTTreeLock: "+Thread.holdsLock(this.getTreeLock()));
75
76 // trigger critical situation around the AWT TreeLock
77 newtWindow.runOnEDTIfAvail(true, new Runnable() {
78 public void run() {
79 // NEWT EDT while AWT is locked
80 System.err.println("MyCanvas On NEWT-EDT From AWT-EDT: "+Thread.currentThread()+
81 ", holds AWTTreeLock: "+Thread.holdsLock(MyCanvas.this.getTreeLock()));
82
83 // Critical: Within NEWT EDT, while AWT is locked
84 final NativeWindow nw = MyCanvas.this.getNativeWindow();
85 if(null != nw) {
86 final Point p = nw.getLocationOnScreen(null);
87 System.err.println("MyCanvas On NEWT-EDT: position: "+p);
88 } else {
89 System.err.println("MyCanvas On NEWT-EDT: position n/a, null NativeWindow");
90 }
91 }
92 });
93 System.err.println("MyCanvas passed critical: "+Thread.currentThread()+", holds AWTTreeLock: "+Thread.holdsLock(this.getTreeLock()));
94
95 super.removeNotify();
96
97 System.err.println("MyCanvas END remove: "+Thread.currentThread()+", holds AWTTreeLock: "+Thread.holdsLock(this.getTreeLock()));
98 }
99 }
100
101 @Test
102 public void testCloseNewtAWT() throws InterruptedException, InvocationTargetException {
104 newtCanvas = new MyCanvas(newtWindow);
105
106 SwingUtilities.invokeAndWait(new Runnable() {
107 public void run() {
108 frame = new JFrame("NEWT Close Test");
109 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
110 frame.getContentPane().add(newtCanvas);
111 frame.pack();
112 frame.setSize(800, 600);
113 frame.setVisible(true);
114 }
115 });
116 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
117 final GLWindow obj = newtWindow;
118 Assert.assertEquals(true, NewtTestUtil.waitForRealized(obj, true, null));
119 final TestUtil.WindowClosingListener closingListener = AWTRobotUtil.addClosingListener(frame);
120
121 Assert.assertEquals(true, AWTRobotUtil.closeWindow(frame, true, closingListener, null));
122 }
123
124 public static void main(final String[] args) {
125 final String tstname = TestCloseNewtAWT.class.getName();
126 org.junit.runner.JUnitCore.main(tstname);
127 }
128
129}
AWT Canvas containing a NEWT Window using native parenting.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void runOnEDTIfAvail(final boolean wait, final Runnable task)
Definition: GLWindow.java:857
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
static TestUtil.WindowClosingListener addClosingListener(final java.awt.Window win)
static boolean closeWindow(final java.awt.Window win, final boolean willClose, final TestUtil.WindowClosingListener closingListener, final Runnable waitAction)
Programmatically issue windowClosing on AWT or NEWT.
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
Extend the NativeSurface interface with windowing information such as window-handle,...
Point getLocationOnScreen(Point point)
Returns the window's top-left client-area position in the screen.
Specifying NEWT's Window functionality:
Definition: Window.java:115