29package com.jogamp.opengl.test.junit.util;
31import java.io.BufferedReader;
33import java.io.OutputStream;
34import java.io.StringReader;
35import java.util.ArrayList;
36import java.util.Arrays;
37import java.util.Iterator;
39import java.util.Locale;
40import java.util.StringTokenizer;
41import java.util.concurrent.atomic.AtomicInteger;
43import com.jogamp.nativewindow.NativeWindowFactory;
44import com.jogamp.opengl.GL;
45import com.jogamp.opengl.GLAutoDrawable;
46import com.jogamp.opengl.GLCapabilitiesImmutable;
47import com.jogamp.opengl.GLDrawable;
48import com.jogamp.opengl.GLEventListener;
50import com.jogamp.junit.util.SingletonJunitCase;
51import com.jogamp.opengl.util.GLReadBufferUtil;
52import com.jogamp.opengl.util.texture.TextureIO;
54import org.junit.BeforeClass;
55import org.junit.AfterClass;
56import org.junit.FixMethodOrder;
57import org.junit.runners.MethodSorters;
58import org.junit.runners.model.FrameworkMethod;
59import org.junit.runners.model.TestClass;
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62public abstract class
UITestCase extends SingletonJunitCase {
63 private static volatile boolean resetXRandRIfX11AfterClass =
false;
65 private static volatile int maxMethodNameLen = 0;
68 resetXRandRIfX11AfterClass =
true;
79 @SuppressWarnings(
"unused")
80 public static
int resetXRandRIfX11() {
84 final List<String> outputDevices =
new ArrayList<String>();
86 final StringBuilder out =
new StringBuilder();
87 final String[] cmdlineQuery =
new String[] {
"xrandr",
"-q" };
88 errorCode = processCommand(cmdlineQuery,
null, out,
"xrandr-query> ");
89 if( 0 != errorCode ) {
90 System.err.println(
"XRandR Query Error Code "+errorCode);
91 System.err.println(out.toString());
94 final BufferedReader in =
new BufferedReader(
new StringReader( out.toString() ) );
96 while ( ( line = in.readLine() ) !=
null) {
97 final String lline = line.toLowerCase();
98 if( lline.contains(
"connected") && !lline.contains(
"disconnected") ) {
99 final String od = getFirst(line);
101 outputDevices.add( od );
111 for(
int i=0; i<outputDevices.size(); i++) {
112 final String outputDevice = outputDevices.get(i);
113 final String outputSize =
null;
114 final String[] cmdline;
115 if(
null != outputSize ) {
116 cmdline =
new String[] {
"xrandr",
"--output", outputDevice,
"--mode", outputSize,
"--rotate",
"normal" };
118 cmdline =
new String[] {
"xrandr",
"--output", outputDevice,
"--preferred",
"--rotate",
"normal" };
120 System.err.println(
"XRandR 1.2 Reset: "+Arrays.asList(cmdline));
121 errorCode = processCommand(cmdline, System.err,
null,
"xrandr-1.2-reset> ");
122 if( 0 != errorCode ) {
123 System.err.println(
"XRandR 1.2 Reset Error Code "+errorCode);
138 }
catch (
final Exception e) {
139 System.err.println(
"Caught "+e.getClass().getName()+
": "+e.getMessage());
146 private static String getFirst(
final String line) {
147 final StringTokenizer tok =
new StringTokenizer(line);
148 if( tok.hasMoreTokens() ) {
149 final String s = tok.nextToken().trim();
150 if( s.length() > 0 ) {
157 public static int processCommand(
final String[] cmdline,
final OutputStream outstream,
final StringBuilder outstring,
final String outPrefix) {
159 final Object ioSync =
new Object();
161 synchronized ( ioSync ) {
162 final ProcessBuilder pb =
new ProcessBuilder(cmdline);
163 pb.redirectErrorStream(
true);
164 final Process p = pb.start();
165 final MiscUtils.StreamDump dump;
166 if(
null != outstream ) {
168 }
else if(
null != outstring ) {
171 throw new IllegalArgumentException(
"Output stream and string are null");
174 while( !dump.eos() ) {
178 errorCode = p.exitValue();
180 }
catch (
final Exception e) {
181 System.err.println(
"Caught "+e.getClass().getName()+
": "+e.getMessage());
183 errorCode = Integer.MIN_VALUE;
189 if(0 == maxMethodNameLen) {
191 final TestClass tc =
new TestClass(getClass());
192 final List<FrameworkMethod> testMethods = tc.getAnnotatedMethods(org.junit.Test.class);
193 for(
final Iterator<FrameworkMethod> iter=testMethods.iterator(); iter.hasNext(); ) {
194 final int l = iter.next().getName().length();
195 if( ml < l ) { ml = l; }
197 maxMethodNameLen = ml;
199 return maxMethodNameLen;
210 if( resetXRandRIfX11AfterClass ) {
216 if(
null == fileSuffix) {
219 final int maxSimpleTestNameLen = getMaxTestNameLen()+getClass().getSimpleName().length()+1;
220 final String simpleTestName = this.getSimpleTestName(
".");
221 final String filenameBaseName;
227 }
else if(caps.
isFBO()) {
237 final String F_pfmt = sinkHasAlpha ?
"rgba" :
"rgb_";
243 postSNDetail =
null != postSNDetail ?
"-"+postSNDetail :
"";
245 filenameBaseName = String.format((Locale)
null,
"%-"+maxSimpleTestNameLen+
"s-n%04d%s-%-6s-%s-%s-B%s-F%s_I%s-D%02d-St%02d-Sa%02d_%s-%04dx%04d.%s",
247 scrnm, dblb, F_pfmt, pfmt, depthBits, stencilBits, samples, aaext,
248 width, height, fileSuffix).replace(
' ',
'_');
250 return null != destPath ? destPath + File.separator + filenameBaseName : filenameBaseName;
270 public void snapshot(
final int sn,
final String postSNDetail,
final GL gl,
final GLReadBufferUtil readBufferUtil,
final String fileSuffix,
final String destPath) {
273 final String filename = getSnapshotFilename(sn, postSNDetail,
275 readBufferUtil.
hasAlpha(), fileSuffix, destPath);
276 System.err.println(Thread.currentThread().getName()+
": ** screenshot: "+filename);
279 snapshot(gl, readBufferUtil, filename);
280 }
catch (
final ClassNotFoundException cnfe) {
282 System.err.println(
"Caught ClassNotFoundException: "+cnfe.getMessage());
283 }
catch (
final NoClassDefFoundError cnfe) {
285 System.err.println(
"Caught NoClassDefFoundError: "+cnfe.getMessage());
288 private void snapshot(
final GL gl,
final GLReadBufferUtil readBufferUtil,
final String filename)
throws ClassNotFoundException, NoClassDefFoundError {
289 if(readBufferUtil.readPixels(gl,
false)) {
290 readBufferUtil.write(
new File(filename));
296 private volatile boolean makeShot =
false;
297 private volatile boolean makeShotAlways =
false;
298 private volatile boolean verbose =
false;
299 private final AtomicInteger displayCount =
new AtomicInteger(0);
300 private final AtomicInteger reshapeCount =
new AtomicInteger(0);
301 private volatile String postSNDetail =
null;
303 this.screenshot = screenshot;
314 final GL gl = drawable.
getGL();
315 final boolean _makeShot = makeShot || makeShotAlways;
317 System.err.println(Thread.currentThread().getName()+
": ** display: "+displayCount+
": "+drawable.
getSurfaceWidth()+
"x"+drawable.
getSurfaceHeight()+
", makeShot "+_makeShot);
321 snapshot(displayCount.get(), postSNDetail, gl, screenshot,
TextureIO.
PNG,
null);
323 displayCount.incrementAndGet();
327 System.err.println(Thread.currentThread().getName()+
": ** reshape: "+reshapeCount+
": "+width+
"x"+height+
" - "+drawable.
getSurfaceWidth()+
"x"+drawable.
getSurfaceHeight());
329 reshapeCount.incrementAndGet();
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static final String TYPE_X11
X11 type, as retrieved with getNativeWindowType(boolean).
static String getNativeWindowType(final boolean useCustom)
static synchronized boolean isInitialized()
Returns true if initSingleton() has been called w/o subsequent shutdown(boolean).
abstract GLDrawable getGLReadDrawable()
Returns the read-Drawable this context uses for read framebuffer operations.
final String getName()
return this profiles name
SnapshotGLEventListener()
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void setVerbose(final boolean v)
GLReadBufferUtil getGLReadBufferUtil()
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
SnapshotGLEventListener(final GLReadBufferUtil screenshot)
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void setPostSNDetail(final String v)
void setMakeSnapshotAlways(final boolean v)
String getSnapshotFilename(final int sn, String postSNDetail, final GLCapabilitiesImmutable caps, final int width, final int height, final boolean sinkHasAlpha, String fileSuffix, final String destPath)
static void setResetXRandRIfX11AfterClass()
static final void oneTimeTearDownUITest()
static final void oneTimeSetUpUITest()
void snapshot(final int sn, final String postSNDetail, final GL gl, final GLReadBufferUtil readBufferUtil, final String fileSuffix, final String destPath)
Takes a snapshot of the drawable's current front framebuffer.
static int processCommand(final String[] cmdline, final OutputStream outstream, final StringBuilder outstring, final String outPrefix)
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
int getAlphaBits()
Returns the number of bits for the color buffer's alpha component.
int getBlueBits()
Returns the number of bits for the color buffer's blue component.
boolean isBitmap()
Returns whether bitmap offscreen mode is requested, available or chosen.
int getRedBits()
Returns the number of bits for the color buffer's red component.
int getGreenBits()
Returns the number of bits for the color buffer's green component.
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GLContext getContext()
Returns the GLContext associated which this GL object.
Specifies an immutable set of OpenGL capabilities.
String getSampleExtension()
Returns the extension for full-scene antialiasing (FSAA).
int getNumSamples()
Returns the number of sample buffers to be allocated if sample buffers are enabled,...
boolean getHardwareAccelerated()
Returns whether hardware acceleration is requested, available or chosen.
int getDepthBits()
Returns the number of depth buffer bits.
boolean isPBuffer()
Returns whether pbuffer offscreen mode is requested, available or chosen.
GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
boolean getDoubleBuffered()
Returns whether double-buffering is requested, available or chosen.
boolean isFBO()
Returns whether FBO offscreen mode is requested, available or chosen.
int getStencilBits()
Returns the number of stencil buffer bits.
An abstraction for an OpenGL rendering target.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void glFinish()
Entry point to C language function: void {@native glFinish}() Part of GL_ES_VERSION_2_0,...