Android 在ActivityInstrumentationTestCase2中测试open gl调用
我不想在android仪器单元测试中测试一些OpenGL的东西。 如果我没有弄错的话,所有的测试都是在acual设备中运行的,所以我认为opengl调用也应该可以工作 然而,情况并非如此,否则我遗漏了一些东西(我希望如此) 我陈述了一个新项目&一个非常简单的测试项目来评估这一点 以下是我的测试:Android 在ActivityInstrumentationTestCase2中测试open gl调用,android,unit-testing,opengl-es-2.0,Android,Unit Testing,Opengl Es 2.0,我不想在android仪器单元测试中测试一些OpenGL的东西。 如果我没有弄错的话,所有的测试都是在acual设备中运行的,所以我认为opengl调用也应该可以工作 然而,情况并非如此,否则我遗漏了一些东西(我希望如此) 我陈述了一个新项目&一个非常简单的测试项目来评估这一点 以下是我的测试: package com.example.test; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.Int
package com.example.test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import android.opengl.GLES20;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import com.example.HelloTesingActivity;
public class AndroidTest extends ActivityInstrumentationTestCase2<HelloTesingActivity> {
public AndroidTest(String pkg, Class<HelloTesingActivity> activityClass) {
super(pkg, activityClass);
}
private HelloTesingActivity mActivity;
public AndroidTest() {
super("com.example", HelloTesingActivity.class);
}
public void testTrue(){
assertTrue(true);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
}
public void testPreConditions() throws Exception {
assertNotNull(mActivity); // passes
}
/*
* FAILS
*/
@UiThreadTest // ensures this test is run in the main UI thread.
public void testGlCreateTexture(){
IntBuffer buffer = newIntBuffer();
GLES20.glGenTextures(1, buffer);
assertFalse(buffer.get() == 0); // this fails
}
/**
* just a helper to setup a correct buffer for open gl to write the values into
* @return
*/
private IntBuffer newIntBuffer() {
ByteBuffer buff = ByteBuffer.allocateDirect(4);
buff.order(ByteOrder.nativeOrder());
buff.position(0);
return buff.asIntBuffer();
}
/*
* FAILS
*/
@UiThreadTest
public void testGlCalls(){
GLES20.glActiveTexture(1); // set the texture unit to 1 since 0 is the default case
IntBuffer value = newIntBuffer();
GLES20.glGetIntegerv(GLES20.GL_ACTIVE_TEXTURE, value );
assertEquals(1, value.get()); // this fails with expected: 1 but was: 0
}
}
您注释了要在UIThread上运行的测试,但是OpenGL调用应该在GLThread上运行。恐怕没有注释来运行这些测试。您注释了要在UIThread上运行的测试,但是OpenGL调用应该在GLThread上。抱歉,没有运行这些测试的注释。我也想测试GL代码,下面是我的操作方法:
import java.util.concurrent.CountDownLatch;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.test.ActivityInstrumentationTestCase2;
/**
* <p>Extend ActivityInstrumentationTestCase2 for testing GL. Subclasses can
* use {@link #runOnGLThread(Runnable)} and {@link #getGL()} to test from the
* GL thread.</p>
*
* <p>Note: assumes a dummy activity, the test overrides the activity view and
* renderer. This framework is intended to test independent GL code.</p>
*
* @author Darrell Anderson
*/
public abstract class GLTestCase<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
private final Object mLock = new Object();
private Activity mActivity = null;
private GLSurfaceView mGLSurfaceView = null;
private GL10 mGL = null;
// ------------------------------------------------------------
// Expose GL context and GL thread.
// ------------------------------------------------------------
public GLSurfaceView getGLSurfaceView() {
return mGLSurfaceView;
}
public GL10 getGL() {
return mGL;
}
/**
* Run on the GL thread. Blocks until finished.
*/
public void runOnGLThreadAndWait(final Runnable runnable) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mGLSurfaceView.queueEvent(new Runnable() {
public void run() {
runnable.run();
latch.countDown();
}
});
latch.await(); // wait for runnable to finish
}
// ------------------------------------------------------------
// Normal users should not care about code below this point.
// ------------------------------------------------------------
public GLTestCase(String pkg, Class<T> activityClass) {
super(pkg, activityClass);
}
public GLTestCase(Class<T> activityClass) {
super(activityClass);
}
/**
* Dummy renderer, exposes the GL context for {@link #getGL()}.
*/
private class MockRenderer implements GLSurfaceView.Renderer {
@Override
public void onDrawFrame(GL10 gl) {
;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
;
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
synchronized(mLock) {
mGL = gl;
mLock.notifyAll();
}
}
}
/**
* On the first call, set up the GL context.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
// If the activity hasn't changed since last setUp, assume the
// surface is still there.
final Activity activity = getActivity(); // launches activity
if (activity == mActivity) {
mGLSurfaceView.onResume();
return; // same activity, assume surface is still there
}
// New or different activity, set up for GL.
mActivity = activity;
mGLSurfaceView = new GLSurfaceView(activity);
mGL = null;
// Attach the renderer to the view, and the view to the activity.
mGLSurfaceView.setRenderer(new MockRenderer());
activity.runOnUiThread(new Runnable() {
public void run() {
activity.setContentView(mGLSurfaceView);
}
});
// Wait for the renderer to get the GL context.
synchronized(mLock) {
while (mGL == null) {
mLock.wait();
}
}
}
@Override
protected void tearDown() throws Exception {
mGLSurfaceView.onPause();
super.tearDown();
}
}
import java.util.concurrent.CountDownLatch;
导入javax.microedition.khronos.egl.EGLConfig;
导入javax.microedition.khronos.opengles.GL10;
导入android.app.Activity;
导入android.opengl.GLSurfaceView;
导入android.test.ActivityInstrumentationTestCase2;
/**
*扩展ActivityInstrumentationTestCase2以测试GL。子类可以
*使用{@link#runOnGLThread(Runnable)}和{@link#getGL()}从
*GL螺纹
*
*注意:假设为虚拟活动,测试将覆盖活动视图,并
*渲染器。该框架旨在测试独立的GL代码
*
*@作者达雷尔·安德森
*/
公共抽象类GLTestCase扩展了ActivityInstrumentationTestCase2{
私有最终对象mLock=新对象();
私有活动mActivity=null;
私有GLSurfaceView mGLSurfaceView=null;
私有GL10 mGL=null;
// ------------------------------------------------------------
//公开GL上下文和GL线程。
// ------------------------------------------------------------
公共GLSurfaceView getGLSurfaceView(){
返回mGLSurfaceView;
}
公共GL10 getGL(){
返回mGL;
}
/**
*在GL螺纹上运行。阻塞直到完成。
*/
public void runnglthreadandwait(final Runnable Runnable)抛出中断异常{
最终倒计时闩锁=新倒计时闩锁(1);
mGLSurfaceView.queueEvent(新的Runnable(){
公开募捐{
runnable.run();
倒计时();
}
});
latch.wait();//等待runnable完成
}
// ------------------------------------------------------------
//普通用户不应该关心低于这一点的代码。
// ------------------------------------------------------------
公共GLTestCase(字符串包,类activityClass){
超级(包装,活动类);
}
公共GLTestCase(类activityClass){
超级(活动类);
}
/**
*虚拟渲染器,公开{@link#getGL()}的GL上下文。
*/
私有类MockRenderer实现GLSurfaceView.Renderer{
@凌驾
公共框架(GL10 gl){
;
}
@凌驾
表面上的公共空隙已更改(GL10 gl,整型宽度,整型高度){
;
}
@凌驾
已创建曲面上的公共void(GL10 gl、EGLConfig配置){
已同步(mLock){
mGL=gl;
mLock.notifyAll();
}
}
}
/**
*在第一次调用时,设置总账上下文。
*/
@凌驾
受保护的void setUp()引发异常{
super.setUp();
//如果自上次设置以来活动没有更改,则假定
//表面仍然存在。
最终活动活动=getActivity();//启动活动
如果(活动==mActivity){
mGLSurfaceView.onResume();
return;//相同的活动,假设surface仍然存在
}
//为总账设置的新活动或不同活动。
活动性=活动性;
mGLSurfaceView=新的GLSurfaceView(活动);
mGL=null;
//将渲染器附加到视图,将视图附加到活动。
setRenderer(新的MockRenderer());
activity.runOnUiThread(新的Runnable(){
公开募捐{
activity.setContentView(mGLSurfaceView);
}
});
//等待渲染器获取GL上下文。
已同步(mLock){
while(mGL==null){
mLock.wait();
}
}
}
@凌驾
受保护的void tearDown()引发异常{
mGLSurfaceView.onPause();
super.tearDown();
}
}
我也想测试GL代码,下面是我的操作方法:
import java.util.concurrent.CountDownLatch;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.test.ActivityInstrumentationTestCase2;
/**
* <p>Extend ActivityInstrumentationTestCase2 for testing GL. Subclasses can
* use {@link #runOnGLThread(Runnable)} and {@link #getGL()} to test from the
* GL thread.</p>
*
* <p>Note: assumes a dummy activity, the test overrides the activity view and
* renderer. This framework is intended to test independent GL code.</p>
*
* @author Darrell Anderson
*/
public abstract class GLTestCase<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
private final Object mLock = new Object();
private Activity mActivity = null;
private GLSurfaceView mGLSurfaceView = null;
private GL10 mGL = null;
// ------------------------------------------------------------
// Expose GL context and GL thread.
// ------------------------------------------------------------
public GLSurfaceView getGLSurfaceView() {
return mGLSurfaceView;
}
public GL10 getGL() {
return mGL;
}
/**
* Run on the GL thread. Blocks until finished.
*/
public void runOnGLThreadAndWait(final Runnable runnable) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
mGLSurfaceView.queueEvent(new Runnable() {
public void run() {
runnable.run();
latch.countDown();
}
});
latch.await(); // wait for runnable to finish
}
// ------------------------------------------------------------
// Normal users should not care about code below this point.
// ------------------------------------------------------------
public GLTestCase(String pkg, Class<T> activityClass) {
super(pkg, activityClass);
}
public GLTestCase(Class<T> activityClass) {
super(activityClass);
}
/**
* Dummy renderer, exposes the GL context for {@link #getGL()}.
*/
private class MockRenderer implements GLSurfaceView.Renderer {
@Override
public void onDrawFrame(GL10 gl) {
;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
;
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
synchronized(mLock) {
mGL = gl;
mLock.notifyAll();
}
}
}
/**
* On the first call, set up the GL context.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
// If the activity hasn't changed since last setUp, assume the
// surface is still there.
final Activity activity = getActivity(); // launches activity
if (activity == mActivity) {
mGLSurfaceView.onResume();
return; // same activity, assume surface is still there
}
// New or different activity, set up for GL.
mActivity = activity;
mGLSurfaceView = new GLSurfaceView(activity);
mGL = null;
// Attach the renderer to the view, and the view to the activity.
mGLSurfaceView.setRenderer(new MockRenderer());
activity.runOnUiThread(new Runnable() {
public void run() {
activity.setContentView(mGLSurfaceView);
}
});
// Wait for the renderer to get the GL context.
synchronized(mLock) {
while (mGL == null) {
mLock.wait();
}
}
}
@Override
protected void tearDown() throws Exception {
mGLSurfaceView.onPause();
super.tearDown();
}
}
import java.util.concurrent.CountDownLatch;
导入javax.microedition.khronos.egl.EGLConfig;
导入javax.microedition.khronos.opengles.GL10;
导入android.app.Activity;
导入android.opengl.GLSurfaceView;
导入android.test.ActivityInstrumentationTestCase2;
/**
*扩展ActivityInstrumentationTestCase2以测试GL。子类可以
*使用{@link#runOnGLThread(Runnable)}和{@link#getGL()}从
*GL螺纹
*
*注意:假设为虚拟活动,测试将覆盖活动视图,并
*渲染器。该框架旨在测试独立的GL代码
*
*@作者达雷尔·安德森
*/
公共抽象类GLTestCase扩展了ActivityInstrumentationTestCase2{
私有最终对象mLock=新对象();
私有活动mActivity=null;
私有GLSurfaceView mGLSurfaceView=null;
私有GL10 mGL=null;
// ------------------------------------------------------------
//公开GL上下文和GL线程。
// ------------------------------------------------------------
公共GLSurfaceView getGLSurfaceView(){
返回mGLSurfaceView;
}
公共GL10 getGL(){
返回mGL;
}