JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestWindowClosingProtocol01AWT.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;
37import java.awt.Frame;
38
39import javax.swing.JFrame;
40import javax.swing.SwingUtilities;
41import javax.swing.WindowConstants;
42import com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode;
43import com.jogamp.opengl.GLCapabilities;
44import com.jogamp.opengl.GLProfile;
45import com.jogamp.opengl.awt.GLCanvas;
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.TestUtil;
50import com.jogamp.opengl.test.junit.util.TestUtil.WindowClosingListener;
51import com.jogamp.opengl.test.junit.util.UITestCase;
52
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55
56 @Test
57 public void testCloseFrameGLCanvas() throws InterruptedException, InvocationTargetException {
58 final Frame frame = new Frame("testCloseFrameGLCanvas AWT");
59 final TestUtil.WindowClosingListener closingListener = AWTRobotUtil.addClosingListener(frame);
60 final GLProfile glp = GLProfile.getGL2ES2();
61 final GLCapabilities caps = new GLCapabilities(glp);
62 final GLCanvas glCanvas = new GLCanvas(caps);
63 glCanvas.addGLEventListener(new GearsES2());
64 SwingUtilities.invokeAndWait(new Runnable() {
65 public void run() {
66 frame.add(glCanvas);
67 frame.pack();
68 frame.setSize(512, 512);
69 frame.validate();
70 frame.setVisible(true);
71 } });
72 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
73 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glCanvas, true, null));
74
75 //
76 // close with op: DO_NOTHING_ON_CLOSE -> NOP (default)
77 //
79 Assert.assertEquals(WindowClosingMode.DO_NOTHING_ON_CLOSE, op);
80
81 Assert.assertEquals(true, AWTRobotUtil.closeWindow(frame, false, closingListener, null)); // nop
82 Thread.sleep(100);
83 Assert.assertEquals(true, frame.isDisplayable());
84 Assert.assertEquals(true, frame.isVisible());
85 Assert.assertEquals(true, glCanvas.isValid());
86 Assert.assertEquals(true, glCanvas.isDisplayable());
87 Assert.assertEquals(true, closingListener.isWindowClosing());
88 Assert.assertEquals(false, closingListener.isWindowClosed());
89
90 //
91 // close with op (GLCanvas): DISPOSE_ON_CLOSE -> dispose
92 //
94 op = glCanvas.getDefaultCloseOperation();
95 Assert.assertEquals(WindowClosingMode.DISPOSE_ON_CLOSE, op);
96
97 Thread.sleep(300);
98
99 Assert.assertEquals(true, AWTRobotUtil.closeWindow(frame, false, closingListener, null)); // no frame close, but GLCanvas's GL resources will be destroyed
100 Thread.sleep(100);
101 Assert.assertEquals(true, frame.isDisplayable());
102 Assert.assertEquals(true, frame.isVisible());
103 Assert.assertEquals(true, closingListener.isWindowClosing());
104 Assert.assertEquals(false, closingListener.isWindowClosed());
105 for (int wait=0; wait<AWTRobotUtil.POLL_DIVIDER && glCanvas.isRealized(); wait++) {
106 Thread.sleep(AWTRobotUtil.TIME_SLICE);
107 }
108 Assert.assertEquals(false, glCanvas.isRealized());
109
110 SwingUtilities.invokeAndWait(new Runnable() {
111 public void run() {
112 frame.dispose();
113 } });
114 }
115
116 @Test
117 public void testCloseJFrameGLCanvas() throws InterruptedException, InvocationTargetException {
118 final JFrame frame = new JFrame("testCloseJFrameGLCanvas AWT");
119 final TestUtil.WindowClosingListener closingListener = AWTRobotUtil.addClosingListener(frame);
120
121 final GLProfile glp = GLProfile.getGL2ES2();
122 final GLCapabilities caps = new GLCapabilities(glp);
123 final GLCanvas glCanvas = new GLCanvas(caps);
124 glCanvas.addGLEventListener(new GearsES2());
125 SwingUtilities.invokeAndWait(new Runnable() {
126 public void run() {
127 frame.getContentPane().add(glCanvas);
128 frame.pack();
129 frame.setSize(512, 512);
130 frame.validate();
131 frame.setVisible(true);
132 } });
133 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
134 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glCanvas, true, null));
135
136 //
137 // close with op: DO_NOTHING_ON_CLOSE -> NOP / HIDE (default)
138 //
139 Assert.assertEquals(WindowConstants.HIDE_ON_CLOSE, frame.getDefaultCloseOperation());
141 Assert.assertEquals(WindowClosingMode.DO_NOTHING_ON_CLOSE, op);
142
143 Thread.sleep(300);
144
145 Assert.assertEquals(true, AWTRobotUtil.closeWindow(frame, false, closingListener, null)); // hide
146 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, false, null)); // hide -> invisible
147 Assert.assertEquals(true, frame.isDisplayable());
148 Assert.assertEquals(false, frame.isVisible());
149 Assert.assertEquals(true, glCanvas.isValid());
150 Assert.assertEquals(true, glCanvas.isDisplayable());
151
152 SwingUtilities.invokeAndWait(new Runnable() {
153 public void run() {
154 frame.setVisible(true);
155 } });
156 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
157 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glCanvas, true, null));
158 Assert.assertEquals(true, frame.isDisplayable());
159 Assert.assertEquals(true, frame.isVisible());
160
161 //
162 // close with op (JFrame): DISPOSE_ON_CLOSE -- GLCanvas --> dispose
163 //
164 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
165 Assert.assertEquals(WindowConstants.DISPOSE_ON_CLOSE, frame.getDefaultCloseOperation());
166 op = glCanvas.getDefaultCloseOperation();
167 Assert.assertEquals(WindowClosingMode.DISPOSE_ON_CLOSE, op);
168
169 Assert.assertEquals(true, AWTRobotUtil.closeWindow(frame, true, closingListener, null));
170 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glCanvas, false, null));
171 Assert.assertEquals(false, frame.isDisplayable());
172 Assert.assertEquals(false, glCanvas.isValid());
173 Assert.assertEquals(false, glCanvas.isDisplayable());
174 Assert.assertEquals(false, glCanvas.isRealized());
175 }
176
177 public static void main(final String[] args) {
178 final String tstname = TestWindowClosingProtocol01AWT.class.getName();
179 org.junit.runner.JUnitCore.main(tstname);
180 }
181
182}
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
WindowClosingMode setDefaultCloseOperation(final WindowClosingMode op)
Definition: GLCanvas.java:491
WindowClosingMode getDefaultCloseOperation()
Definition: GLCanvas.java:486
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
static TestUtil.WindowClosingListener addClosingListener(final java.awt.Window win)
static boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
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)
Window closing mode if triggered by toolkit close operation.
DO_NOTHING_ON_CLOSE
Do nothing on native window close operation.
DISPOSE_ON_CLOSE
Dispose resources on native window close operation.