Improve OpenGL test reliability

These tests were launching activities, setting a view, and then sleeping
for 1 second. Send a couple extras via an intent to the test activity
so it creates the content view properly in one shot instead. This should
remove the need for sleeping and fix the occasional NPEs about view
being null.

Bug 6485813

Change-Id: I861cf74bdae3c0b46cba7d58c1218acf1d6297a5
diff --git a/development/ide/eclipse/.classpath b/development/ide/eclipse/.classpath
index c074195..2c98b52 100644
--- a/development/ide/eclipse/.classpath
+++ b/development/ide/eclipse/.classpath
@@ -43,6 +43,7 @@
     <classpathentry kind="src" path="cts/tests/tests/media/src"/>
     <classpathentry kind="src" path="cts/tests/tests/mediastress/src"/>
     <classpathentry kind="src" path="cts/tests/tests/net/src"/>
+    <classpathentry kind="src" path="cts/tests/tests/opengl/src"/>
     <classpathentry kind="src" path="cts/tests/tests/openglperf/src"/>
     <classpathentry kind="src" path="cts/tests/tests/os/src"/>
     <classpathentry kind="src" path="cts/tests/tests/performance/src"/>
diff --git a/tests/tests/opengl/src/android/opengl/cts/AttachShaderTest.java b/tests/tests/opengl/src/android/opengl/cts/AttachShaderTest.java
index 3173e6c..034a77a 100644
--- a/tests/tests/opengl/src/android/opengl/cts/AttachShaderTest.java
+++ b/tests/tests/opengl/src/android/opengl/cts/AttachShaderTest.java
@@ -15,14 +15,11 @@
  */
 package android.opengl.cts;
 
+import android.content.Intent;
 import android.opengl.GLES20;
 import android.test.ActivityInstrumentationTestCase2;
 
 public class AttachShaderTest extends ActivityInstrumentationTestCase2<OpenGLES20ActivityOne> {
-    private static final long SLEEP_TIME = 1000l;
-    public AttachShaderTest(Class<OpenGLES20ActivityOne> activityClass) {
-        super(activityClass);
-    }
 
     private OpenGLES20ActivityOne mActivity;
 
@@ -30,11 +27,14 @@
         super(OpenGLES20ActivityOne.class);
     }
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        mActivity = getActivity();
+    private OpenGLES20ActivityOne getShaderActivity(int viewType, int viewIndex) {
+        Intent intent = new Intent();
+        intent.putExtra(OpenGLES20NativeActivity.EXTRA_VIEW_TYPE, viewType);
+        intent.putExtra(OpenGLES20NativeActivity.EXTRA_VIEW_INDEX, viewIndex);
+        setActivityIntent(intent);
+        return getActivity();
     }
+
     /**
      *Test: Attach an two valid shaders to a program
      * <pre>
@@ -43,14 +43,7 @@
      * </pre>
      */
     public void test_glAttachedShaders_validshader() throws Throwable {
-
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 1);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
+        mActivity = getShaderActivity(Constants.SHADER, 1);
         int shaderCount = mActivity.getNoOfAttachedShaders();
         assertEquals(2,shaderCount);
         int error = mActivity.glGetError();
@@ -67,13 +60,7 @@
      */
 
     public void test_glAttachedShaders_invalidshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 2);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
+        mActivity = getShaderActivity(Constants.SHADER, 2);
         int shaderCount = mActivity.getNoOfAttachedShaders();
         assertEquals(1, shaderCount);
         int error = mActivity.glGetError();
@@ -89,13 +76,7 @@
      * @throws Throwable
      */
     public void test_glAttachedShaders_attach_same_shader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 3);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
+        mActivity = getShaderActivity(Constants.SHADER, 3);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_INVALID_OPERATION, error);
     }
@@ -110,13 +91,7 @@
      */
 
     public void test_glAttachedShaders_noshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 4);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
+        mActivity = getShaderActivity(Constants.SHADER, 4);
         int shaderCount = mActivity.getNoOfAttachedShaders();
         assertEquals(0, shaderCount);
         int error = mActivity.glGetError();
@@ -124,79 +99,37 @@
     }
 
     public void test_glAttachShaders_emptyfragshader_emptyfragshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 5);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 5);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_INVALID_OPERATION, error);
     }
 
     public void test_glAttachShaders_emptyfragshader_emptyvertexshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 6);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 6);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_NO_ERROR, error);
     }
 
     public void test_glAttachShaders_emptyvertexshader_emptyvertexshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 7);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 7);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_INVALID_OPERATION, error);
     }
 
     public void test_glAttachShaders_programobject_attach_fragshaderobject() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 8);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 8);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_INVALID_VALUE, error);
     }
 
     public void test_glAttachShaders_invalidshader_attach_valid_handle() throws Throwable{
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 9);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 9);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_INVALID_VALUE, error);
     }
 
     public void test_glAttachShaders_successfulcompile_attach_frag() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 10);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 10);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_NO_ERROR, error);
     }
diff --git a/tests/tests/opengl/src/android/opengl/cts/NativeAttachShaderTest.java b/tests/tests/opengl/src/android/opengl/cts/NativeAttachShaderTest.java
index e6707da..1a51b6e 100755
--- a/tests/tests/opengl/src/android/opengl/cts/NativeAttachShaderTest.java
+++ b/tests/tests/opengl/src/android/opengl/cts/NativeAttachShaderTest.java
@@ -15,14 +15,13 @@
  */
 package android.opengl.cts;
 
+import android.content.Intent;
 import android.opengl.GLES20;
 import android.test.ActivityInstrumentationTestCase2;
 
-public class NativeAttachShaderTest 
+public class NativeAttachShaderTest
         extends ActivityInstrumentationTestCase2<OpenGLES20NativeActivity> {
 
-    private static final long SLEEP_TIME = 1000l;
-
     public NativeAttachShaderTest(Class<OpenGLES20NativeActivity> activityClass) {
         super(activityClass);
     }
@@ -33,10 +32,12 @@
         super(OpenGLES20NativeActivity.class);
     }
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        mActivity = getActivity();
+    private OpenGLES20NativeActivity getShaderActivity(int viewType, int viewIndex) {
+        Intent intent = new Intent();
+        intent.putExtra(OpenGLES20NativeActivity.EXTRA_VIEW_TYPE, viewType);
+        intent.putExtra(OpenGLES20NativeActivity.EXTRA_VIEW_INDEX, viewIndex);
+        setActivityIntent(intent);
+        return getActivity();
     }
 
     /**
@@ -46,17 +47,10 @@
      * error        : GLES20.GL_NO_ERROR
      * </pre>
      */
-
     public void test_glAttachedShaders_validshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 1);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
+        mActivity = getShaderActivity(Constants.SHADER, 1);
         int shaderCount = mActivity.mRenderer.mShaderCount;
-        assertEquals(2,shaderCount);
+        assertEquals(2, shaderCount);
         int error = mActivity.mRenderer.mAttachShaderError;
         assertEquals(GLES20.GL_NO_ERROR, error);
     }
@@ -69,18 +63,9 @@
      * </pre>
      * @throws Throwable
      */
-
     public void test_glAttachedShaders_invalidshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 2);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 2);
         int error = mActivity.mRenderer.mAttachShaderError;
-
         assertEquals(GLES20.GL_INVALID_VALUE, error);
     }
 
@@ -93,13 +78,7 @@
      * @throws Throwable
      */
     public void test_glAttachedShaders_attach_same_shader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 3);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
+        mActivity = getShaderActivity(Constants.SHADER, 3);
         int error = mActivity.mRenderer.mAttachShaderError;
         assertEquals(GLES20.GL_INVALID_OPERATION, error);
     }
@@ -112,44 +91,23 @@
      * </pre>
      * @throws Throwable
      */
-
     public void test_glAttachedShaders_noshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 4);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
+        mActivity = getShaderActivity(Constants.SHADER, 4);
         int shaderCount = GL2JniLibOne.getAttachedShaderCount();
-        assertEquals(0,shaderCount);
+        assertEquals(0, shaderCount);
 
         int error = mActivity.mRenderer.mAttachShaderError;
         assertEquals(GLES20.GL_NO_ERROR, error);
     }
 
     public void test_glAttachShaders_emptyfragshader_emptyfragshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 5);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
-        int error = mActivity.mRenderer.mAttachShaderError;;
+        mActivity = getShaderActivity(Constants.SHADER, 5);
+        int error = mActivity.mRenderer.mAttachShaderError;
         assertEquals(GLES20.GL_INVALID_OPERATION, error);
     }
 
     public void test_glAttachShaders_emptyfragshader_emptyvertexshader() throws Throwable {
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.SHADER, 6);
-            }
-        });
-        Thread.sleep(SLEEP_TIME);
-
+        mActivity = getShaderActivity(Constants.SHADER, 6);
         int error = mActivity.mRenderer.mAttachShaderError;;
         assertEquals(GLES20.GL_NO_ERROR, error);
     }
diff --git a/tests/tests/opengl/src/android/opengl/cts/OpenGLES20ActivityOne.java b/tests/tests/opengl/src/android/opengl/cts/OpenGLES20ActivityOne.java
index a3b7503..4a4e4ca 100644
--- a/tests/tests/opengl/src/android/opengl/cts/OpenGLES20ActivityOne.java
+++ b/tests/tests/opengl/src/android/opengl/cts/OpenGLES20ActivityOne.java
@@ -24,19 +24,24 @@
 import android.view.WindowManager;
 
 public class OpenGLES20ActivityOne extends Activity {
+
+    public static final String EXTRA_VIEW_TYPE = "viewType";
+    public static final String EXTRA_VIEW_INDEX = "viewIndex";
+
     OpenGLES20View view;
     Renderer mRenderer;
     int mRendererType;
-    /** Called when the activity is first created. */
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         Window window = getWindow();
         window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
-    }
 
-    public void setView(int type, int i ) {
-        view = new OpenGLES20View(this,type,i);
+        int viewType = getIntent().getIntExtra(EXTRA_VIEW_TYPE, -1);
+        int viewIndex = getIntent().getIntExtra(EXTRA_VIEW_INDEX, -1);
+
+        view = new OpenGLES20View(this, viewType, viewIndex);
         setContentView(view);
     }
 
diff --git a/tests/tests/opengl/src/android/opengl/cts/OpenGLES20NativeActivity.java b/tests/tests/opengl/src/android/opengl/cts/OpenGLES20NativeActivity.java
index 7028db2..4579ebf 100755
--- a/tests/tests/opengl/src/android/opengl/cts/OpenGLES20NativeActivity.java
+++ b/tests/tests/opengl/src/android/opengl/cts/OpenGLES20NativeActivity.java
@@ -15,26 +15,21 @@
  */
 package android.opengl.cts;
 
-import javax.microedition.khronos.egl.EGLConfig;
-import javax.microedition.khronos.opengles.GL10;
-
-import com.android.cts.opengl.R;
-
 import android.app.Activity;
 import android.content.Context;
 import android.opengl.GLSurfaceView;
 import android.os.Bundle;
 import android.util.Log;
-import android.view.View;
-import android.view.View.OnClickListener;
 import android.view.Window;
 import android.view.WindowManager;
-import android.widget.Button;
-import android.widget.EditText;
-import android.widget.TextView;
+
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
 
 public class OpenGLES20NativeActivity extends Activity {
-    /** Called when the activity is first created. */
+
+    public static final String EXTRA_VIEW_TYPE = "viewType";
+    public static final String EXTRA_VIEW_INDEX = "viewIndex";
 
     int mValue;
 
@@ -48,21 +43,18 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+
         Window window = getWindow();
         window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
-    }
 
-    public void setView(int type, int i ) {
-        view = new OpenGLES20View(this,type,i);
+        int viewType = getIntent().getIntExtra(EXTRA_VIEW_TYPE, -1);
+        int viewIndex = getIntent().getIntExtra(EXTRA_VIEW_INDEX, -1);
+
+        view = new OpenGLES20View(this, viewType, viewIndex);
         setContentView(view);
     }
 
     @Override
-    protected void onDestroy() {
-        super.onDestroy();
-    }
-
-    @Override
     protected void onPause() {
         super.onPause();
         view.onPause();
@@ -107,11 +99,11 @@
     }
 
     public void onDrawFrame(GL10 gl) {
-        
+
     }
 
     public void onSurfaceChanged(GL10 gl, int width, int height) {
-        
+
     }
 
     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
diff --git a/tests/tests/opengl/src/android/opengl/cts/ProgramTest.java b/tests/tests/opengl/src/android/opengl/cts/ProgramTest.java
index ee9a835..4c59070 100644
--- a/tests/tests/opengl/src/android/opengl/cts/ProgramTest.java
+++ b/tests/tests/opengl/src/android/opengl/cts/ProgramTest.java
@@ -15,14 +15,11 @@
  */
 package android.opengl.cts;
 
+import android.content.Intent;
 import android.opengl.GLES20;
 import android.test.ActivityInstrumentationTestCase2;
 
 public class ProgramTest extends ActivityInstrumentationTestCase2<OpenGLES20ActivityOne> {
-    public ProgramTest(Class<OpenGLES20ActivityOne> activityClass) {
-        super(activityClass);
-
-    }
 
     private OpenGLES20ActivityOne mActivity;
 
@@ -30,23 +27,17 @@
         super(OpenGLES20ActivityOne.class);
     }
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        mActivity = getActivity();
+    private OpenGLES20ActivityOne getShaderActivity(int viewType, int viewIndex) {
+        Intent intent = new Intent();
+        intent.putExtra(OpenGLES20NativeActivity.EXTRA_VIEW_TYPE, viewType);
+        intent.putExtra(OpenGLES20NativeActivity.EXTRA_VIEW_INDEX, viewIndex);
+        setActivityIntent(intent);
+        return getActivity();
     }
 
     public void test_glAttachShader_program() throws Throwable {
-
-        mActivity = getActivity();
-        this.runTestOnUiThread(new Runnable() {
-            public void run() {
-                mActivity.setView(Constants.PROGRAM,1);
-            }
-        });
-        Thread.sleep(1000);
+        mActivity = getShaderActivity(Constants.PROGRAM, 1);
         int error = mActivity.glGetError();
         assertEquals(GLES20.GL_INVALID_OPERATION, error);
     }
-
 }