Add PixelCopy tests with a video source

Bug: 27708453
Change-Id: Ieb1621f2ff2440024680ba8e8e775a5ffa69be1a
diff --git a/tests/tests/view/AndroidManifest.xml b/tests/tests/view/AndroidManifest.xml
index 31126f8..2862de5 100644
--- a/tests/tests/view/AndroidManifest.xml
+++ b/tests/tests/view/AndroidManifest.xml
@@ -147,6 +147,14 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="android.view.cts.PixelCopyVideoSourceActivity"
+            android:label="PixelCopyVideoSourceActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
+                <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
+            </intent-filter>
+        </activity>
+
         <activity android:name="android.view.cts.FocusFinderCtsActivity"
             android:label="FocusFinderCtsActivity">
             <intent-filter>
diff --git a/tests/tests/view/res/raw/colorgrid_video.mp4 b/tests/tests/view/res/raw/colorgrid_video.mp4
new file mode 100644
index 0000000..1be8bee
--- /dev/null
+++ b/tests/tests/view/res/raw/colorgrid_video.mp4
Binary files differ
diff --git a/tests/tests/view/src/android/view/cts/PixelCopyTests.java b/tests/tests/view/src/android/view/cts/PixelCopyTests.java
index b54ecb9..1555d00 100644
--- a/tests/tests/view/src/android/view/cts/PixelCopyTests.java
+++ b/tests/tests/view/src/android/view/cts/PixelCopyTests.java
@@ -28,6 +28,7 @@
 import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.MediumTest;
 import android.support.test.rule.ActivityTestRule;
+import android.util.Log;
 import android.view.PixelCopy;
 import android.view.Surface;
 import android.view.SurfaceView;
@@ -54,10 +55,16 @@
 
 @MediumTest
 public class PixelCopyTests {
+    private static final String TAG = "PixelCopyTests";
+
     @Rule
     public ActivityTestRule<GLSurfaceViewCtsActivity> mGLSurfaceViewActivityRule =
             new ActivityTestRule<>(GLSurfaceViewCtsActivity.class, false, false);
 
+    @Rule
+    public ActivityTestRule<PixelCopyVideoSourceActivity> mVideoSourceActivityRule =
+            new ActivityTestRule<>(PixelCopyVideoSourceActivity.class, false, false);
+
     private Instrumentation mInstrumentation;
 
     @Before
@@ -163,6 +170,36 @@
         }
     }
 
+    @Test
+    public void testVideoProducer() throws InterruptedException {
+        PixelCopyVideoSourceActivity activity =
+                mVideoSourceActivityRule.launchActivity(null);
+        if (!activity.canPlayVideo()) {
+            Log.i(TAG, "Skipping testVideoProducer, video codec isn't supported");
+        }
+        // This returns when the video has been prepared and playback has
+        // been started, it doesn't necessarily means a frame has actually been
+        // produced. There sadly isn't a callback for that.
+        // So we'll try for up to 300ms after this event to acquire a frame, otherwise
+        // it's considered a timeout.
+        activity.waitForPlaying();
+        SynchronousPixelCopy copyHelper = new SynchronousPixelCopy();
+        Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
+        int copyResult = PixelCopy.ERROR_SOURCE_NO_DATA;
+        for (int i = 0; i < 30; i++) {
+            copyResult = copyHelper.request(activity.getVideoView(), bitmap);
+            if (copyResult != PixelCopy.ERROR_SOURCE_NO_DATA) {
+                break;
+            }
+            Thread.sleep(10);
+        }
+        assertEquals(PixelCopy.SUCCESS, copyResult);
+        // A large threshold is used because decoder accuracy is covered in the
+        // media CTS tests, so we are mainly interested in verifying that rotation
+        // and YUV->RGB conversion were handled properly.
+        assertBitmapQuadColor(bitmap, Color.RED, Color.GREEN, Color.BLUE, Color.BLACK, 30);
+    }
+
     private static int getPixelFloatPos(Bitmap bitmap, float xpos, float ypos) {
         return bitmap.getPixel((int) (bitmap.getWidth() * xpos), (int) (bitmap.getHeight() * ypos));
     }
@@ -176,6 +213,22 @@
         assertEquals("Bottom right", bottomRight, getPixelFloatPos(bitmap, .75f, .75f));
     }
 
+    private void assertBitmapQuadColor(Bitmap bitmap, int topLeft, int topRight,
+            int bottomLeft, int bottomRight, int threshold) {
+        // Just quickly sample 4 pixels in the various regions.
+        assertTrue("Top left", pixelsAreSame(topLeft, getPixelFloatPos(bitmap, .25f, .25f), threshold));
+        assertTrue("Top right", pixelsAreSame(topRight, getPixelFloatPos(bitmap, .75f, .25f), threshold));
+        assertTrue("Bottom left", pixelsAreSame(bottomLeft, getPixelFloatPos(bitmap, .25f, .75f), threshold));
+        assertTrue("Bottom right", pixelsAreSame(bottomRight, getPixelFloatPos(bitmap, .75f, .75f), threshold));
+    }
+
+    private boolean pixelsAreSame(int ideal, int given, int threshold) {
+        int error = Math.abs(Color.red(ideal) - Color.red(given));
+        error += Math.abs(Color.green(ideal) - Color.green(given));
+        error += Math.abs(Color.blue(ideal) - Color.blue(given));
+        return (error < threshold);
+    }
+
     private static class QuadColorGLRenderer implements Renderer {
 
         private final int mTopLeftColor;
diff --git a/tests/tests/view/src/android/view/cts/PixelCopyVideoSourceActivity.java b/tests/tests/view/src/android/view/cts/PixelCopyVideoSourceActivity.java
new file mode 100644
index 0000000..08e3951
--- /dev/null
+++ b/tests/tests/view/src/android/view/cts/PixelCopyVideoSourceActivity.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.view.cts;
+
+import android.app.Activity;
+import android.cts.util.MediaUtils;
+import android.net.Uri;
+import android.os.Bundle;
+import android.widget.VideoView;
+
+import java.util.concurrent.CountDownLatch;
+
+public class PixelCopyVideoSourceActivity extends Activity {
+    private VideoView mVideoView;
+    private CountDownLatch mVideoPlayingFence = new CountDownLatch(1);
+    private boolean mCanPlayVideo;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        mVideoView = new VideoView(this);
+        mVideoView.setOnPreparedListener(mp -> {
+            mp.setLooping(true);
+            mVideoView.start();
+            mVideoPlayingFence.countDown();
+        });
+        mCanPlayVideo = MediaUtils.hasCodecsForResource(this, R.raw.colorgrid_video);
+        if (mCanPlayVideo) {
+            Uri uri = Uri.parse("android.resource://android.view.cts/" + R.raw.colorgrid_video);
+            mVideoView.setVideoURI(uri);
+        }
+        setContentView(mVideoView);
+    }
+
+    public boolean canPlayVideo() {
+        return mCanPlayVideo;
+    }
+
+    public void waitForPlaying() throws InterruptedException {
+        mVideoPlayingFence.await();
+    }
+
+    public VideoView getVideoView() {
+        return mVideoView;
+    }
+}