JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestUtil.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
32public abstract class TestUtil {
33 public static interface WindowClosingListener {
34 void reset();
37 public boolean isWindowClosing();
38 public boolean isWindowClosed();
39 }
40 public static final int RETRY_NUMBER = 5;
41 public static final int TIME_OUT = 2000; // 2s
42 public static final int POLL_DIVIDER = 20; // TO/20
43 public static final int TIME_SLICE = TIME_OUT / POLL_DIVIDER ;
44
45 /**
46 *
47 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
48 * @return True if the Window became the global focused Window within TIME_OUT
49 */
50 public static boolean waitForFocus(final FocusEventCountAdapter gain,
51 final FocusEventCountAdapter lost, final Runnable waitAction) throws InterruptedException {
52 int wait;
53 for (wait=0; wait<POLL_DIVIDER; wait++) {
54 if( ( null == lost || lost.focusLost() ) && ( null == gain || gain.focusGained() ) ) {
55 return true;
56 }
57 if( null != waitAction ) {
58 waitAction.run();
59 } else {
60 Thread.sleep(TIME_SLICE);
61 }
62 }
63 return false;
64 }
65
66 /**
67 * Wait until the window is closing within TIME_OUT.
68 *
69 * @param willClose indicating that the window will close, hence this method waits for the window to be closed
70 * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate
71 * @param wcl the WindowClosingListener to determine whether the AWT or NEWT widget has been closed. It should be attached
72 * to the widget ASAP before any other listener, e.g. via {@link #addClosingListener(Object)}.
73 * The WindowClosingListener will be reset before attempting to close the widget.
74 * @return True if the Window is closing and closed (if willClose is true), each within TIME_OUT
75 * @throws InterruptedException
76 */
77 public static boolean waitUntilClosed(final boolean willClose, final TestUtil.WindowClosingListener closingListener, final Runnable waitAction) throws InterruptedException {
78 int wait;
79 for (wait=0; wait<POLL_DIVIDER && !closingListener.isWindowClosing(); wait++) {
80 if( null != waitAction ) {
81 waitAction.run();
82 } else {
83 Thread.sleep(TIME_SLICE);
84 }
85 }
86 if(wait<POLL_DIVIDER && willClose) {
87 for (wait=0; wait<POLL_DIVIDER && !closingListener.isWindowClosed(); wait++) {
88 if( null != waitAction ) {
89 waitAction.run();
90 } else {
91 Thread.sleep(TIME_SLICE);
92 }
93 }
94 }
95 return wait<POLL_DIVIDER;
96 }
97}
98
99
100
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