JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
NewtTestUtil.java
Go to the documentation of this file.
1/**
2 * Copyright 2019 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
29
30package com.jogamp.opengl.test.junit.util;
31
32import java.util.concurrent.atomic.AtomicInteger;
33
34import com.jogamp.nativewindow.util.InsetsImmutable;
35import com.jogamp.newt.Screen;
36import com.jogamp.newt.Window;
37import com.jogamp.newt.event.WindowEvent;
38
39import jogamp.newt.WindowImplAccess;
40
41public class NewtTestUtil extends TestUtil {
42 public static class NEWTWindowClosingAdapter
43 extends com.jogamp.newt.event.WindowAdapter implements TestUtil.WindowClosingListener
44 {
45 AtomicInteger closing = new AtomicInteger(0);
46 AtomicInteger closed = new AtomicInteger(0);
47
48 @Override
49 public void reset() {
50 closing.set(0);
51 closed.set(0);
52 }
53 @Override
54 public int getWindowClosingCount() {
55 return closing.get();
56 }
57 @Override
58 public int getWindowClosedCount() {
59 return closed.get();
60 }
61 @Override
62 public boolean isWindowClosing() {
63 return 0 < closing.get();
64 }
65 @Override
66 public boolean isWindowClosed() {
67 return 0 < closed.get();
68 }
69 @Override
70 public void windowDestroyNotify(final WindowEvent e) {
71 closing.incrementAndGet();
72 System.err.println("NEWTWindowClosingAdapter.windowDestroyNotify: "+this);
73 }
74 @Override
75 public void windowDestroyed(final WindowEvent e) {
76 closed.incrementAndGet();
77 System.err.println("NEWTWindowClosingAdapter.windowDestroyed: "+this);
78 }
79 @Override
80 public String toString() {
81 return "NEWTWindowClosingAdapter[closing "+closing+", closed "+closed+"]";
82 }
83 }
84 /**
85 *
86 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
87 * @return True if the Window became the global focused Window within TIME_OUT
88 */
89 public static boolean waitForFocus(final Window win, final Runnable waitAction) throws InterruptedException {
90 int wait;
91 for (wait=0; wait<POLL_DIVIDER && !win.hasFocus(); wait++) {
92 if( null != waitAction ) {
93 waitAction.run();
94 } else {
95 Thread.sleep(TIME_SLICE);
96 }
97 }
98 return wait<POLL_DIVIDER;
99 }
100
101 /**
102 *
103 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
104 * @return True if the Window became the global focused Window within TIME_OUT
105 */
106 public static boolean waitForFocus(final Window win, final FocusEventCountAdapter gain,
107 final FocusEventCountAdapter lost, final Runnable waitAction) throws InterruptedException {
108 if(!waitForFocus(win, waitAction)) {
109 return false;
110 }
111 return TestUtil.waitForFocus(gain, lost, waitAction);
112 }
113
114 /**
115 *
116 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
117 * @return True if the Window receives the expected surface size within TIME_OUT
118 */
119 public static boolean waitForSize(final Window window, final int width, final int height, final Runnable waitAction) throws InterruptedException {
120 int wait;
121 for (wait=0; wait<POLL_DIVIDER && ( width != window.getSurfaceWidth() || height != window.getSurfaceHeight() ) ; wait++) {
122 if( null != waitAction ) {
123 waitAction.run();
124 } else {
125 Thread.sleep(TIME_SLICE);
126 }
127 }
128 return wait<POLL_DIVIDER;
129 }
130
131 /**
132 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
133 * @return True if the Component becomes <code>visible</code> within TIME_OUT
134 */
135 public static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction) throws InterruptedException {
136 int wait;
137 for (wait=0; wait<POLL_DIVIDER && visible != win.isVisible(); wait++) {
138 if( null != waitAction ) {
139 waitAction.run();
140 } else {
141 Thread.sleep(TIME_SLICE);
142 }
143 }
144 return wait<POLL_DIVIDER;
145 }
146
147 /**
148 * @param screen the Screen to wait for
149 * @param realized true if waiting for component to become realized, otherwise false
150 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
151 * @return True if the Component becomes realized (not displayable, native invalid) within TIME_OUT
152 * @throws InterruptedException
153 */
154 public static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction) throws InterruptedException {
155 final long t0 = System.currentTimeMillis();
156 long t1 = t0;
157 while( (t1-t0) < TIME_OUT && realized != screen.isNativeValid() ) {
158 if( null != waitAction ) {
159 waitAction.run();
160 } else {
161 Thread.sleep(TIME_SLICE);
162 }
163 t1 = System.currentTimeMillis();
164 }
165 return (t1-t0) < TIME_OUT;
166 }
167 /**
168 * @param win the Window to wait for
169 * @param realized true if waiting for component to become realized, otherwise false
170 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
171 * @return True if the Component becomes realized (not displayable, native invalid) within TIME_OUT
172 * @throws InterruptedException
173 */
174 public static boolean waitForRealized(final Window win, final boolean realized, final Runnable waitAction) throws InterruptedException {
175 final long t0 = System.currentTimeMillis();
176 long t1 = t0;
177 while( (t1-t0) < TIME_OUT && realized != win.isNativeValid() ) {
178 if( null != waitAction ) {
179 waitAction.run();
180 } else {
181 Thread.sleep(TIME_SLICE);
182 }
183 t1 = System.currentTimeMillis();
184 }
185 return (t1-t0) < TIME_OUT;
186 }
187
188 public static TestUtil.WindowClosingListener addClosingListener(final Window win) {
189 final NewtTestUtil.NEWTWindowClosingAdapter ncl = new NewtTestUtil.NEWTWindowClosingAdapter();
190 win.addWindowListener(ncl);
191 return ncl;
192 }
193
194 /**
195 * Programmatically issue windowClosing on AWT or NEWT.
196 * Wait until the window is closing within TIME_OUT.
197 * @param willClose indicating that the window will close, hence this method waits for the window to be closed
198 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
199 * @param obj either an AWT Window (Frame, JFrame) or NEWT Window
200 * @param wcl the WindowClosingListener to determine whether the AWT or NEWT widget has been closed. It should be attached
201 * to the widget ASAP before any other listener, e.g. via {@link #addClosingListener(Object)}.
202 * The WindowClosingListener will be reset before attempting to close the widget.
203 *
204 * @return True if the Window is closing and closed (if willClose is true), each within TIME_OUT
205 * @throws InterruptedException
206 */
207 public static boolean closeWindow(final Window win, final boolean willClose, final TestUtil.WindowClosingListener closingListener, final Runnable waitAction) throws InterruptedException {
208 closingListener.reset();
209 WindowImplAccess.windowDestroyNotify(win);
210 return waitUntilClosed(willClose, closingListener, waitAction);
211 }
212
213 /**
214 * Validates whether the window position is on the given position within tolerances.
215 * <p>
216 * Since WM may not obey our positional request exactly, we allow a tolerance of 2 times insets[left/top], or 64 pixels, whichever is greater.
217 * </p>
218 */
219 public static boolean hasPositionMax2xInsetsOr64Pix(final Window win, final int shouldX, final int shouldY) {
220 final int maxDX, maxDY;
221 {
222 final InsetsImmutable insets = win.getInsets();
223 maxDX = Math.max(64, insets.getLeftWidth() * 2);
224 maxDY = Math.max(64, insets.getTopHeight() * 2);
225 }
226 return hasPosition(win, shouldX, shouldY, maxDX, maxDY);
227 }
228
229 /**
230 * Validates whether the window position is within the expected position including given tolerance.
231 */
232 public static boolean hasPosition(final Window win, final int expX, final int expY, final int maxDX, final int maxDY) {
233 final int dx = Math.abs(expX - win.getX());
234 final int dy = Math.abs(expY - win.getY());
235 final boolean ok = dx <= maxDX && dy <= maxDY ;
236 if( !ok ) {
237 System.err.println("Position OFF: abs( exp "+expX+"/"+expY+" - has "+win.getX()+"/"+win.getY()+" ) = "+dx+"/"+dy+" > "+maxDX+"/"+maxDY);
238 } else {
239 System.err.println("Position OK : abs( exp "+expX+"/"+expY+" - has "+win.getX()+"/"+win.getY()+" ) = "+dx+"/"+dy+" <= "+maxDX+"/"+maxDY);
240 }
241 return ok;
242 }
243}
244
245
246
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
NEWT Window events are provided for notification purposes ONLY.
void windowDestroyNotify(final WindowEvent e)
Window destruction has been requested.
void windowDestroyed(final WindowEvent e)
Window has been destroyed.
static TestUtil.WindowClosingListener addClosingListener(final Window win)
static boolean hasPosition(final Window win, final int expX, final int expY, final int maxDX, final int maxDY)
Validates whether the window position is within the expected position including given tolerance.
static boolean hasPositionMax2xInsetsOr64Pix(final Window win, final int shouldX, final int shouldY)
Validates whether the window position is on the given position within tolerances.
static boolean waitForFocus(final Window win, final Runnable waitAction)
static boolean closeWindow(final Window win, final boolean willClose, final TestUtil.WindowClosingListener closingListener, final Runnable waitAction)
Programmatically issue windowClosing on AWT or NEWT.
static boolean waitForSize(final Window window, final int width, final int height, final Runnable waitAction)
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForFocus(final Window win, final FocusEventCountAdapter gain, final FocusEventCountAdapter lost, final Runnable waitAction)
static boolean waitForRealized(final Window win, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction)
static boolean waitForFocus(final FocusEventCountAdapter gain, final FocusEventCountAdapter lost, final Runnable waitAction)
Definition: TestUtil.java:50
static boolean waitUntilClosed(final boolean willClose, final TestUtil.WindowClosingListener closingListener, final Runnable waitAction)
Wait until the window is closing within TIME_OUT.
Definition: TestUtil.java:77
int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
InsetsImmutable getInsets()
Returns the insets defined as the width and height of the window decoration on the left,...
int getX()
Returns the current x position of this window, relative to it's parent.
Immutable insets representing rectangular window decoration insets on all four edges in window units.
Specifying NEWT's Window functionality:
Definition: Window.java:115
void addWindowListener(WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.