media bitstreams test: use MediaPreparer to dl test input

Bug: 65165250
Test: cts-tradefed run cts -m CtsMediaStressTestCases
Test: cts-tradefed run cts -m CtsMediaBitStreamsTestCases
Merged-In: I598edc53d0779d80e65f975d9556f158f6a19fbb
Change-Id: I446d483a604ee898a26b28b11789fa27d5ca4364
diff --git a/tests/tests/mediastress/preconditions/src/android/mediastress/cts/preconditions/MediaPreparer.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparer.java
similarity index 94%
rename from tests/tests/mediastress/preconditions/src/android/mediastress/cts/preconditions/MediaPreparer.java
rename to common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparer.java
index e7cef51..3bdcd2b 100644
--- a/tests/tests/mediastress/preconditions/src/android/mediastress/cts/preconditions/MediaPreparer.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparer.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package android.mediastress.cts.preconditions;
+package com.android.compatibility.common.tradefed.targetprep;
 
 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
 import com.android.compatibility.common.tradefed.targetprep.PreconditionPreparer;
@@ -60,6 +60,10 @@
             description = "Whether to skip the media files precondition")
     protected boolean mSkipMediaDownload = false;
 
+    @Option(name = "media-download-only",
+            description = "Only download media files; do not run instrumentation or copy files")
+    protected boolean mMediaDownloadOnly = false;
+
     /*
      * The pathnames of the device's directories that hold media files for the tests.
      * These depend on the device's mount point, which is retrieved in the MediaPreparer's run
@@ -147,6 +151,10 @@
         }
     }
 
+    public static File getDefaultMediaDir() {
+        return new File(System.getProperty("java.io.tmpdir"), MEDIA_FOLDER_NAME);
+    }
+
     /*
      * Returns true if all necessary media files exist on the device, and false otherwise.
      *
@@ -277,17 +285,19 @@
             logInfo("Skipping media preparation");
             return; // skip this precondition
         }
-        setMountPoint(device);
-        setMaxRes(device, buildInfo);
-        if (mediaFilesExistOnDevice(device)) {
-            // if files already on device, do nothing
-            logInfo("Media files found on the device");
-            return;
+        if (!mMediaDownloadOnly) {
+            setMountPoint(device);
+            setMaxRes(device, buildInfo);
+            if (mediaFilesExistOnDevice(device)) {
+                // if files already on device, do nothing
+                logInfo("Media files found on the device");
+                return;
+            }
         }
         if (mLocalMediaPath == null) {
             // Option 'local-media-path' has not been defined
             // Get directory to store media files on this host
-            File mediaFolder = new File(System.getProperty("java.io.tmpdir"), MEDIA_FOLDER_NAME);
+            File mediaFolder = getDefaultMediaDir();
             if(!mediaFolder.exists() || mediaFolder.list().length == 0){
                 // If directory already exists and contains files, it has been created by previous
                 // runs of MediaPreparer. Assume media files exist inside.
@@ -299,7 +309,9 @@
             updateLocalMediaPath(device, mediaFolder);
         }
         logInfo("Media files located on host at: %s", mLocalMediaPath);
-        copyMediaFiles(device);
+        if (!mMediaDownloadOnly) {
+            copyMediaFiles(device);
+        }
     }
 
     // Initialize maximum resolution of media files to copy
diff --git a/tests/tests/mediastress/preconditions/tests/src/android/mediastress/cts/preconditions/MediaPreparerTest.java b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparerTest.java
similarity index 98%
rename from tests/tests/mediastress/preconditions/tests/src/android/mediastress/cts/preconditions/MediaPreparerTest.java
rename to common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparerTest.java
index a175112..05d328d 100644
--- a/tests/tests/mediastress/preconditions/tests/src/android/mediastress/cts/preconditions/MediaPreparerTest.java
+++ b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparerTest.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package android.mediastress.cts.preconditions;
+package com.android.compatibility.common.tradefed.targetprep;
 
 import com.android.ddmlib.IDevice;
 import com.android.tradefed.build.BuildInfo;
diff --git a/hostsidetests/media/bitstreams/AndroidTest.xml b/hostsidetests/media/bitstreams/AndroidTest.xml
index 2603aba..9c8da50 100644
--- a/hostsidetests/media/bitstreams/AndroidTest.xml
+++ b/hostsidetests/media/bitstreams/AndroidTest.xml
@@ -15,6 +15,9 @@
 -->
 <configuration description="Config for CTS Sample host test cases">
     <option name="config-descriptor:metadata" key="component" value="media" />
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.MediaPreparer">
+        <option name="media-download-only" value="true" />
+    </target_preparer>
     <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
         <option name="cleanup-apks" value="true" />
         <option name="test-file-name" value="CtsMediaBitstreamsDeviceSideTestApp.apk" />
diff --git a/hostsidetests/media/bitstreams/src/android/media/cts/bitstreams/MediaBitstreamsTest.java b/hostsidetests/media/bitstreams/src/android/media/cts/bitstreams/MediaBitstreamsTest.java
index 669a508..46a39fc 100644
--- a/hostsidetests/media/bitstreams/src/android/media/cts/bitstreams/MediaBitstreamsTest.java
+++ b/hostsidetests/media/bitstreams/src/android/media/cts/bitstreams/MediaBitstreamsTest.java
@@ -16,6 +16,7 @@
 package android.media.cts.bitstreams;
 
 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
+import com.android.compatibility.common.tradefed.targetprep.MediaPreparer;
 import com.android.compatibility.common.util.MetricsReportLog;
 import com.android.compatibility.common.util.ResultType;
 import com.android.compatibility.common.util.ResultUnit;
@@ -32,6 +33,7 @@
 import com.android.tradefed.util.FileUtil;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -66,7 +68,7 @@
     @Option(name = MediaBitstreams.OPT_HOST_BITSTREAMS_PATH,
             description = "Absolute path of Ittiam bitstreams (host)",
             mandatory = true)
-    private File mHostBitstreamsPath = new File(MediaBitstreams.DEFAULT_HOST_BITSTREAMS_PATH);
+    private File mHostBitstreamsPath = getDefaultBitstreamsDir();
 
     @Option(name = MediaBitstreams.OPT_DEVICE_BITSTREAMS_PATH,
             description = "Absolute path of Ittiam bitstreams (device)")
@@ -129,6 +131,22 @@
     private IAbi mAbi;
     private ITestDevice mDevice;
 
+    static File getDefaultBitstreamsDir() {
+        File mediaDir = MediaPreparer.getDefaultMediaDir();
+        File[] subDirs = mediaDir.listFiles(new FileFilter() {
+            @Override
+            public boolean accept(File child) {
+                return child.isDirectory();
+            }
+        });
+        if (subDirs != null && subDirs.length == 1) {
+            File parent = new File(mediaDir, subDirs[0].getName());
+            return new File(parent, MediaBitstreams.DEFAULT_HOST_BITSTREAMS_PATH);
+        } else {
+            return new File(MediaBitstreams.DEFAULT_HOST_BITSTREAMS_PATH);
+        }
+    }
+
     static Collection<Object[]> bitstreams(String prefix, BitstreamPackage packageToRun) {
         final String dynConfXml = new File("/", MediaBitstreams.DYNAMIC_CONFIG_XML).toString();
         try (InputStream is = MediaBitstreamsTest.class.getResourceAsStream(dynConfXml)) {
diff --git a/tests/tests/mediastress/AndroidTest.xml b/tests/tests/mediastress/AndroidTest.xml
index bc39044..181e58e 100644
--- a/tests/tests/mediastress/AndroidTest.xml
+++ b/tests/tests/mediastress/AndroidTest.xml
@@ -15,7 +15,7 @@
 -->
 <configuration description="Config for CTS Media Stress test cases">
     <option name="config-descriptor:metadata" key="component" value="media" />
-    <target_preparer class="android.mediastress.cts.preconditions.MediaPreparer" />
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.MediaPreparer" />
     <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
         <option name="cleanup-apks" value="true" />
         <option name="test-file-name" value="CtsMediaStressTestCases.apk" />
diff --git a/tests/tests/mediastress/preconditions/Android.mk b/tests/tests/mediastress/preconditions/Android.mk
deleted file mode 100644
index 573f083..0000000
--- a/tests/tests/mediastress/preconditions/Android.mk
+++ /dev/null
@@ -1,34 +0,0 @@
-# Copyright (C) 2015 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.
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-
-LOCAL_JAVA_LIBRARIES := compatibility-host-util cts-tradefed tradefed
-
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_MODULE := compatibility-host-media-preconditions
-
-# Tag this module as a cts test artifact
-LOCAL_COMPATIBILITY_SUITE := cts
-
-LOCAL_SDK_VERSION := current
-
-include $(BUILD_HOST_JAVA_LIBRARY)
-
-include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/tests/mediastress/preconditions/tests/Android.mk b/tests/tests/mediastress/preconditions/tests/Android.mk
deleted file mode 100644
index e2dae48..0000000
--- a/tests/tests/mediastress/preconditions/tests/Android.mk
+++ /dev/null
@@ -1,31 +0,0 @@
-# Copyright (C) 2015 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.
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-
-LOCAL_STATIC_JAVA_LIBRARIES := easymock
-
-LOCAL_JAVA_LIBRARIES := compatibility-host-util cts-tradefed tradefed compatibility-host-media-preconditions
-
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_MODULE := compatibility-host-media-preconditions-tests
-
-LOCAL_SDK_VERSION := current
-
-include $(BUILD_HOST_JAVA_LIBRARY)
diff --git a/tests/tests/mediastress/preconditions/tests/run_tests.sh b/tests/tests/mediastress/preconditions/tests/run_tests.sh
deleted file mode 100755
index b04bde8..0000000
--- a/tests/tests/mediastress/preconditions/tests/run_tests.sh
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/bin/bash
-
-# Copyright (C) 2015 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.
-
-# Helper script for running unit tests for compatibility libraries
-
-CTS_DIR=$(dirname ${0})/../../../../..
-source ${CTS_DIR}/test_defs.sh
-
-JARS="
-    compatibility-host-util\
-    cts-tradefed\
-    compatibility-host-media-preconditions\
-    compatibility-host-media-preconditions-tests"
-
-run_tests "android.mediastress.cts.preconditions.MediaPreparerTest" "${JARS}" "${@}"
diff --git a/tests/tests/mediastress/preconditions/app/Android.mk b/tools/cts-media-preparer-app/Android.mk
similarity index 100%
rename from tests/tests/mediastress/preconditions/app/Android.mk
rename to tools/cts-media-preparer-app/Android.mk
diff --git a/tests/tests/mediastress/preconditions/app/AndroidManifest.xml b/tools/cts-media-preparer-app/AndroidManifest.xml
similarity index 100%
rename from tests/tests/mediastress/preconditions/app/AndroidManifest.xml
rename to tools/cts-media-preparer-app/AndroidManifest.xml
diff --git a/tests/tests/mediastress/preconditions/app/src/android/mediastress/cts/preconditions/app/MediaPreparerAppTest.java b/tools/cts-media-preparer-app/src/android/mediastress/cts/preconditions/app/MediaPreparerAppTest.java
similarity index 100%
rename from tests/tests/mediastress/preconditions/app/src/android/mediastress/cts/preconditions/app/MediaPreparerAppTest.java
rename to tools/cts-media-preparer-app/src/android/mediastress/cts/preconditions/app/MediaPreparerAppTest.java