Fix Media Capture Tests Failures by using camera helper

Bug: 29936348
Change-Id: I09a4438039659e6ad9026ad20fb7aaa0ac1bf1bc
diff --git a/tests/androidbvt/Android.mk b/tests/androidbvt/Android.mk
index 970a3df..864f384 100644
--- a/tests/androidbvt/Android.mk
+++ b/tests/androidbvt/Android.mk
@@ -21,9 +21,26 @@
 media_framework_app_base := frameworks/base/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest
 LOCAL_SRC_FILES := $(call all-subdir-java-files)
 LOCAL_JAVA_LIBRARIES := android.test.runner
-LOCAL_STATIC_JAVA_LIBRARIES := android-support-test ub-uiautomator launcher-helper-lib
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-test ub-uiautomator launcher-helper-lib app-helpers
 
 LOCAL_PACKAGE_NAME := AndroidBvtTests
 LOCAL_CERTIFICATE := platform
 
 include $(BUILD_PACKAGE)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SDK_VERSION := current
+LOCAL_SRC_FILES := $(call all-java-files-under, apps)
+LOCAL_JAVA_LIBRARIES := android.test.runner
+LOCAL_STATIC_JAVA_LIBRARIES := \
+    android-support-test \
+    app-helpers \
+    launcher-helper-lib \
+    ub-uiautomator
+LOCAL_PACKAGE_NAME := AndroidAppsBvtTests
+LOCAL_CERTIFICATE := platform
+
+include $(BUILD_PACKAGE)
\ No newline at end of file
diff --git a/tests/androidbvt/src/com/android/androidbvt/MediaCaptureTests.java b/tests/androidbvt/src/com/android/androidbvt/MediaCaptureTests.java
index 518a49d..8dff188 100644
--- a/tests/androidbvt/src/com/android/androidbvt/MediaCaptureTests.java
+++ b/tests/androidbvt/src/com/android/androidbvt/MediaCaptureTests.java
@@ -16,9 +16,12 @@
 
 package com.android.androidbvt;
 
+import android.content.ContentValues;
+import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Environment;
+import android.platform.test.helpers.GoogleCameraHelperImpl;
 import android.provider.MediaStore;
 import android.support.test.InstrumentationRegistry;
 import android.support.test.uiautomator.By;
@@ -37,31 +40,29 @@
  */
 public class MediaCaptureTests extends TestCase {
     private static final int CAPTURE_TIMEOUT = 6000;
-    private static final String DESC_BTN_CAPTURE_PHOTO = "Capture photo";
-    private static final String DESC_BTN_CAPTURE_VIDEO = "Capture video";
     private static final String DESC_BTN_DONE = "Done";
-    private static final String DESC_BTN_PHOTO_MODE = "Open photo mode";
-    private static final String DESC_BTN_VIDEO_MODE = "Open video mode";
     private static final int FILE_CHECK_ATTEMPTS = 5;
-    private static final int VIDEO_LENGTH = 2000;
-
+    private static final long VIDEO_LENGTH = 2000;
+    private static final String CAMERA_DIRECTORY = "/sdcard/DCIM/Camera";
+    private Context mContext;
     private UiDevice mDevice;
+    private GoogleCameraHelperImpl mCameraHelper;
 
     @Override
     public void setUp() throws Exception {
         super.setUp();
         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
         mDevice.freezeRotation();
+        mContext = InstrumentationRegistry.getTargetContext();
+        mCameraHelper = new GoogleCameraHelperImpl(InstrumentationRegistry.getInstrumentation());
+        mCameraHelper.open();
         // if there are any dialogues that pop up, dismiss them
-        UiObject2 maybeOkButton = mDevice.wait(Until.findObject(By.res("android:id/ok_button")),
-                CAPTURE_TIMEOUT);
-        if (maybeOkButton != null) {
-            maybeOkButton.click();
-        }
+        mCameraHelper.dismissInitialDialogs();
     }
 
     @Override
     public void tearDown() throws Exception {
+        mDevice.pressHome();
         mDevice.unfreezeRotation();
         super.tearDown();
     }
@@ -70,68 +71,47 @@
      * Test that the device can capture a photo.
      */
     @LargeTest
-    public void testPhotoCapture() {
-        runCaptureTest(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), "smoke.jpg", false);
+    public void testPhotoCapture() throws InterruptedException {
+        int beforeCount = getValidFileCountFromFilesystem();
+        mCameraHelper.goToCameraMode();
+        // Capture photo using front camera
+        mCameraHelper.goToFrontCamera();
+        mCameraHelper.capturePhoto();
+        Thread.sleep(CAPTURE_TIMEOUT);
+        int afterCount = getValidFileCountFromFilesystem();
+        assertTrue("Camera didnt capture picture using front camera", afterCount > beforeCount);
+        // Capture photo using back camera
+        beforeCount = getValidFileCountFromFilesystem();
+        mCameraHelper.goToBackCamera();
+        mCameraHelper.capturePhoto();
+        Thread.sleep(CAPTURE_TIMEOUT);
+        afterCount = getValidFileCountFromFilesystem();
+        assertTrue("Camera didnt capture picture using back camera", afterCount > beforeCount);
     }
 
     /**
      * Test that the device can capture a video.
      */
     @LargeTest
-    public void testVideoCapture() {
-        runCaptureTest(new Intent(MediaStore.ACTION_VIDEO_CAPTURE), "smoke.avi", true);
-    }
-
-    private void runCaptureTest(Intent intent, String tmpName, boolean isVideo) {
-        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        if (intent.resolveActivity(
-                InstrumentationRegistry.getInstrumentation().getContext().getPackageManager()) != null) {
-            File outputFile = null;
-            try {
-                outputFile = new File(Environment
-                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), tmpName);
-                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFile));
-                InstrumentationRegistry.getInstrumentation().getContext().startActivity(intent);
-                switchCaptureMode(isVideo);
-                pressCaptureButton(isVideo);
-                if (isVideo) {
-                    Thread.sleep(VIDEO_LENGTH);
-                    pressCaptureButton(isVideo);
-                }
-                Thread.sleep(1000);
-                pushButton(DESC_BTN_DONE);
-                long fileLength = outputFile.length();
-                for (int i=0; i<FILE_CHECK_ATTEMPTS; i++) {
-                    if ((fileLength = outputFile.length()) > 0) {
-                        break;
-                    }
-                    Thread.sleep(1000);
-                }
-                assertTrue(fileLength > 0);
-            } catch (InterruptedException e) {
-                fail(e.getLocalizedMessage());
-            } finally {
-                if (outputFile != null) {
-                    outputFile.delete();
-                }
-            }
-        }
-    }
-
-    private void switchCaptureMode(boolean isVideo) {
-        if (isVideo) {
-            pushButton(DESC_BTN_VIDEO_MODE);
-        } else {
-            pushButton(DESC_BTN_PHOTO_MODE);
-        }
-    }
-
-    private void pressCaptureButton(boolean isVideo) {
-        if (isVideo) {
-            pushButton(DESC_BTN_CAPTURE_VIDEO);
-        } else {
-            pushButton(DESC_BTN_CAPTURE_PHOTO);
-        }
+    public void testVideoCapture() throws InterruptedException {
+        int beforeCount = getValidFileCountFromFilesystem();
+        mCameraHelper.goToVideoMode();
+        Thread.sleep(CAPTURE_TIMEOUT);
+        // Capture video using front camera
+        mCameraHelper.goToFrontCamera();
+        mCameraHelper.captureVideo(VIDEO_LENGTH);
+        pushButton(DESC_BTN_DONE);
+        Thread.sleep(CAPTURE_TIMEOUT);
+        int afterCount = getValidFileCountFromFilesystem();
+        assertTrue("Camera didnt capture video", afterCount > beforeCount);
+        // Capture video using back camera
+        beforeCount = getValidFileCountFromFilesystem();
+        mCameraHelper.goToBackCamera();
+        mCameraHelper.captureVideo(VIDEO_LENGTH);
+        pushButton(DESC_BTN_DONE);
+        Thread.sleep(CAPTURE_TIMEOUT);
+        afterCount = getValidFileCountFromFilesystem();
+        assertTrue("Camera didnt capture video", afterCount > beforeCount);
     }
 
     private void pushButton(String desc) {
@@ -141,4 +121,19 @@
             doneBtn.clickAndWait(Until.newWindow(), 500);
         }
     }
+
+    private int getValidFileCountFromFilesystem() {
+        int count = 0;
+        File file = new File(CAMERA_DIRECTORY);
+        for (File child : file.listFiles()) {
+            if (validateSavedFile(child)) {
+                count++;
+            }
+        }
+        return count;
+    }
+
+    private boolean validateSavedFile(File file) {
+        return (file.exists() && file.length() > 0);
+    }
 }