Wait for mMediaProjection to be initialized in CapturedActivity
bug:32020820

Change-Id: I2d0f53c164c6b827e7d70a5777bc13566117fc53
diff --git a/tests/tests/view/src/android/view/cts/SurfaceViewSyncTests.java b/tests/tests/view/src/android/view/cts/SurfaceViewSyncTests.java
index ac21481..61c30a0 100644
--- a/tests/tests/view/src/android/view/cts/SurfaceViewSyncTests.java
+++ b/tests/tests/view/src/android/view/cts/SurfaceViewSyncTests.java
@@ -233,7 +233,7 @@
     // Tests
     ///////////////////////////////////////////////////////////////////////////
 
-    public void verifyTest(AnimationTestCase testCase) {
+    public void verifyTest(AnimationTestCase testCase) throws InterruptedException {
         CapturedActivity.TestResult result = getActivity().runTest(testCase);
         saveFailureCaptures(result.failures);
 
@@ -253,7 +253,7 @@
 
     /** Draws a moving 10x10 black rectangle, validates 100 pixels of black are seen each frame */
     @Test
-    public void testSmallRect() {
+    public void testSmallRect() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 context -> new View(context) {
                     // draw a single pixel
@@ -282,7 +282,7 @@
      * approximate to avoid rounding brittleness.
      */
     @Test
-    public void testEmptySurfaceView() {
+    public void testEmptySurfaceView() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 sEmptySurfaceViewFactory,
                 new FrameLayout.LayoutParams(100, 100, Gravity.LEFT | Gravity.TOP),
@@ -292,7 +292,7 @@
     }
 
     @Test
-    public void testSurfaceViewSmallScale() {
+    public void testSurfaceViewSmallScale() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 sGreenSurfaceViewFactory,
                 new FrameLayout.LayoutParams(640, 480, Gravity.LEFT | Gravity.TOP),
@@ -301,7 +301,7 @@
     }
 
     @Test
-    public void testSurfaceViewBigScale() {
+    public void testSurfaceViewBigScale() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 sGreenSurfaceViewFactory,
                 new FrameLayout.LayoutParams(640, 480, Gravity.LEFT | Gravity.TOP),
@@ -310,7 +310,7 @@
     }
 
     @Test
-    public void testVideoSurfaceViewTranslate() {
+    public void testVideoSurfaceViewTranslate() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 sVideoViewFactory,
                 new FrameLayout.LayoutParams(640, 480, Gravity.LEFT | Gravity.TOP),
@@ -319,7 +319,7 @@
     }
 
     @Test
-    public void testVideoSurfaceViewRotated() {
+    public void testVideoSurfaceViewRotated() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 sVideoViewFactory,
                 new FrameLayout.LayoutParams(100, 100, Gravity.LEFT | Gravity.TOP),
@@ -331,7 +331,7 @@
     }
 
     @Test
-    public void testVideoSurfaceViewEdgeCoverage() {
+    public void testVideoSurfaceViewEdgeCoverage() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 sVideoViewFactory,
                 new FrameLayout.LayoutParams(640, 480, Gravity.CENTER),
@@ -349,7 +349,7 @@
     }
 
     @Test
-    public void testVideoSurfaceViewCornerCoverage() {
+    public void testVideoSurfaceViewCornerCoverage() throws InterruptedException {
         verifyTest(new AnimationTestCase(
                 sVideoViewFactory,
                 new FrameLayout.LayoutParams(640, 480, Gravity.CENTER),
diff --git a/tests/tests/view/src/android/view/cts/surfacevalidator/CapturedActivity.java b/tests/tests/view/src/android/view/cts/surfacevalidator/CapturedActivity.java
index 7c2aacd..82d113d 100644
--- a/tests/tests/view/src/android/view/cts/surfacevalidator/CapturedActivity.java
+++ b/tests/tests/view/src/android/view/cts/surfacevalidator/CapturedActivity.java
@@ -21,8 +21,6 @@
 import android.content.pm.PackageManager;
 import android.graphics.Bitmap;
 import android.graphics.Point;
-import android.graphics.Rect;
-import android.graphics.drawable.Drawable;
 import android.hardware.display.DisplayManager;
 import android.hardware.display.VirtualDisplay;
 import android.media.MediaPlayer;
@@ -40,6 +38,11 @@
 
 import android.view.cts.R;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import static org.junit.Assert.*;
+
+
 public class CapturedActivity extends Activity {
     public static class TestResult {
         public int passFrames;
@@ -67,6 +70,7 @@
 
     private final Handler mHandler = new Handler(Looper.getMainLooper());
     private volatile boolean mOnWatch;
+    private CountDownLatch mCountDownLatch;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -83,6 +87,7 @@
         mProjectionManager =
                 (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
 
+        mCountDownLatch = new CountDownLatch(1);
         startActivityForResult(mProjectionManager.createScreenCaptureIntent(), PERMISSION_CODE);
 
         mMediaPlayer = MediaPlayer.create(this, R.raw.colors_video);
@@ -121,9 +126,10 @@
         Log.d(TAG, "onActivityResult");
         mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
         mMediaProjection.registerCallback(new MediaProjectionCallback(), null);
+        mCountDownLatch.countDown();
     }
 
-    public TestResult runTest(AnimationTestCase animationTestCase) {
+    public TestResult runTest(AnimationTestCase animationTestCase) throws InterruptedException {
         TestResult testResult = new TestResult();
         if (mOnWatch) {
             /**
@@ -138,6 +144,9 @@
             return testResult;
         }
 
+        assertTrue("Can't initialize mediaProjection",
+                mCountDownLatch.await(TIME_OUT_MS, TimeUnit.MILLISECONDS));
+
         mHandler.post(() -> {
             Log.d(TAG, "Setting up test case");
 
@@ -189,11 +198,7 @@
         }, END_DELAY_MS);
 
         synchronized (mLock) {
-            try {
-                mLock.wait(TIME_OUT_MS);
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
+            mLock.wait(TIME_OUT_MS);
         }
         Log.d(TAG, "Test finished, passFrames " + testResult.passFrames
                 + ", failFrames " + testResult.failFrames);