JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestWindowClosingProtocol03NewtAWT.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 com.jogamp.newt.awt.NewtCanvasAWT;
32import com.jogamp.newt.opengl.GLWindow;
33
34import org.junit.Test;
35import org.junit.FixMethodOrder;
36import org.junit.runners.MethodSorters;
37import org.junit.Assert;
38
39import java.lang.reflect.InvocationTargetException;
40
41import javax.swing.JFrame;
42import javax.swing.SwingUtilities;
43import javax.swing.WindowConstants;
44import com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode;
45import com.jogamp.opengl.GLCapabilities;
46import com.jogamp.opengl.GLProfile;
47
48import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
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)
56
57 @Test
58 public void testCloseJFrameNewtCanvasAWT() throws InterruptedException, InvocationTargetException {
59 final JFrame frame = new JFrame("testCloseJFrameNewtCanvasAWT");
60 final TestUtil.WindowClosingListener awtClosingListener = AWTRobotUtil.addClosingListener(frame);
61
62 final GLProfile glp = GLProfile.getGL2ES2();
63 final GLCapabilities caps = new GLCapabilities(glp);
64 final GLWindow glWindow = GLWindow.create(caps);
65 final TestUtil.WindowClosingListener newtClosingListener = NewtTestUtil.addClosingListener(glWindow);
66
67 glWindow.addGLEventListener(new GearsES2());
68
69 final NewtCanvasAWT newtCanvas = new NewtCanvasAWT(glWindow);
70
71 SwingUtilities.invokeAndWait(new Runnable() {
72 public void run() {
73 frame.getContentPane().add(newtCanvas);
74 frame.pack();
75 frame.setSize(512, 512);
76 frame.validate();
77 frame.setVisible(true);
78 } });
79 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
80 Assert.assertEquals(true, NewtTestUtil.waitForRealized(glWindow, true, null));
81 Assert.assertEquals(true, frame.isDisplayable());
82 Assert.assertEquals(true, frame.isVisible());
83 Assert.assertEquals(true, newtCanvas.isValid());
84 Assert.assertEquals(true, newtCanvas.isDisplayable());
85 Assert.assertEquals(true, glWindow.isNativeValid());
86
87 //
88 // close with op: DO_NOTHING_ON_CLOSE -> NOP / HIDE (default)
89 //
90 {
91 Assert.assertEquals(WindowConstants.HIDE_ON_CLOSE, frame.getDefaultCloseOperation());
92 final WindowClosingMode op = newtCanvas.getDefaultCloseOperation();
93 Assert.assertEquals(WindowClosingMode.DO_NOTHING_ON_CLOSE, op);
94 }
95
96 Thread.sleep(300);
97
98 Assert.assertEquals(true, AWTRobotUtil.closeWindow(frame, false, awtClosingListener, null)); // hide
99 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, false, null)); // hide -> invisible
100 Assert.assertEquals(true, frame.isDisplayable());
101 Assert.assertEquals(false, frame.isVisible());
102 Assert.assertEquals(true, newtCanvas.isValid());
103 Assert.assertEquals(true, newtCanvas.isDisplayable());
104 Assert.assertEquals(true, glWindow.isNativeValid());
105 Assert.assertEquals(true, awtClosingListener.isWindowClosing());
106 Assert.assertEquals(false, awtClosingListener.isWindowClosed());
107 Assert.assertEquals(true, newtClosingListener.isWindowClosing());
108 Assert.assertEquals(false, newtClosingListener.isWindowClosed());
109 awtClosingListener.reset();
110 newtClosingListener.reset();
111
112 SwingUtilities.invokeAndWait(new Runnable() {
113 public void run() {
114 frame.setVisible(true);
115 } });
116 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
117 Assert.assertEquals(true, NewtTestUtil.waitForRealized(glWindow, true, null));
118 Assert.assertEquals(true, frame.isDisplayable());
119 Assert.assertEquals(true, frame.isVisible());
120
121 //
122 // close with op (JFrame): DISPOSE_ON_CLOSE -- newtCanvas -- glWindow --> dispose
123 //
124 {
125 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
126 Assert.assertEquals(WindowConstants.DISPOSE_ON_CLOSE, frame.getDefaultCloseOperation());
127 final WindowClosingMode op = newtCanvas.getDefaultCloseOperation();
128 Assert.assertEquals(WindowClosingMode.DISPOSE_ON_CLOSE, op);
129 }
130
131 Thread.sleep(300);
132
133 Assert.assertEquals(true, AWTRobotUtil.closeWindow(frame, true, awtClosingListener, null));
134 Assert.assertEquals(true, NewtTestUtil.waitForRealized(glWindow, false, null));
135 Assert.assertEquals(false, frame.isDisplayable());
136 Assert.assertEquals(false, frame.isVisible());
137 Assert.assertEquals(false, newtCanvas.isValid());
138 Assert.assertEquals(false, newtCanvas.isDisplayable());
139 Assert.assertEquals(false, glWindow.isNativeValid());
140 Assert.assertEquals(true, awtClosingListener.isWindowClosing());
141 Assert.assertEquals(true, awtClosingListener.isWindowClosed());
142 Assert.assertEquals(true, newtClosingListener.isWindowClosing());
143 Assert.assertEquals(true, newtClosingListener.isWindowClosed());
144 }
145
146 public static void main(final String[] args) {
147 final String tstname = TestWindowClosingProtocol03NewtAWT.class.getName();
148 org.junit.runner.JUnitCore.main(tstname);
149 }
150
151}
AWT Canvas containing a NEWT Window using native parenting.
WindowClosingMode getDefaultCloseOperation()
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
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 getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
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 TestUtil.WindowClosingListener addClosingListener(final Window win)
static boolean waitForRealized(final Screen screen, final boolean realized, 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.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.