Define test case out dir and tag certain modules.

Change-Id: I6cb22c493fd912e361a6fbb39f2f43897183d416
diff --git a/tests/sample/Android.mk b/tests/sample/Android.mk
index 5e8f571..1255032 100755
--- a/tests/sample/Android.mk
+++ b/tests/sample/Android.mk
@@ -16,19 +16,24 @@
 
 include $(CLEAR_VARS)
 
-# Don't include this package in any target.
-LOCAL_MODULE_TAGS := optional
-
+# Don't include this package in any target
+LOCAL_MODULE_TAGS := tests
 # When built, explicitly put it in the data partition.
 LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
 
-LOCAL_STATIC_JAVA_LIBRARIES := ctsdeviceutil ctstestrunner
+LOCAL_DEX_PREOPT := false
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_STATIC_JAVA_LIBRARIES := compatibility-device-util android-support-test
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-# Must match the package name in CtsTestCaseList.mk
+# Tag this module as a cts_v2 test artifact
+LOCAL_COMPATIBILITY_SUITE := cts_v2
+
 LOCAL_PACKAGE_NAME := CtsSampleDeviceTestCases
 
 LOCAL_SDK_VERSION := current
 
-include $(BUILD_CTS_PACKAGE)
+include $(BUILD_PACKAGE)
diff --git a/tests/sample/AndroidManifest.xml b/tests/sample/AndroidManifest.xml
index f07ebbe..194c904 100755
--- a/tests/sample/AndroidManifest.xml
+++ b/tests/sample/AndroidManifest.xml
@@ -18,7 +18,6 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="android.sample.cts">
 
-    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <application>
         <uses-library android:name="android.test.runner" />
         <activity android:name="android.sample.SampleDeviceActivity" >
@@ -34,9 +33,6 @@
         android:name="android.support.test.runner.AndroidJUnitRunner"
         android:label="CTS sample tests"
         android:targetPackage="android.sample.cts" >
-        <meta-data
-            android:name="listener"
-            android:value="com.android.cts.runner.CtsTestRunListener" />
     </instrumentation>
 </manifest>
 
diff --git a/tests/sample/AndroidTest.xml b/tests/sample/AndroidTest.xml
new file mode 100644
index 0000000..f2ec348
--- /dev/null
+++ b/tests/sample/AndroidTest.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<configuration description="Base config for CTS package preparer">
+    <include name="common-config" />
+    <option name="apk-installer:test-file-name" value="CtsSampleDeviceTestCases.apk" />
+    <test class="com.android.tradefed.testtype.InstrumentationTest" >
+        <option name="package" value="android.sample.cts" />
+        <option name="runner" value="android.support.test.runner.AndroidJUnitRunner" />
+    </test>
+</configuration>
diff --git a/tests/sample/src/android/sample/cts/SampleDeviceResultTest.java b/tests/sample/src/android/sample/cts/SampleDeviceResultTest.java
index 6bf883f..7bd434e 100644
--- a/tests/sample/src/android/sample/cts/SampleDeviceResultTest.java
+++ b/tests/sample/src/android/sample/cts/SampleDeviceResultTest.java
@@ -15,21 +15,25 @@
  */
 package android.sample.cts;
 
-import com.android.cts.util.MeasureRun;
-import com.android.cts.util.MeasureTime;
-import com.android.cts.util.ReportLog;
-import com.android.cts.util.ResultType;
-import com.android.cts.util.ResultUnit;
-import com.android.cts.util.Stat;
+import android.sample.SampleDeviceActivity;
+import android.test.ActivityInstrumentationTestCase2;
 
-import android.cts.util.CtsAndroidTestCase;
+import com.android.compatibility.common.util.DeviceReportLog;
+import com.android.compatibility.common.util.MeasureRun;
+import com.android.compatibility.common.util.MeasureTime;
+import com.android.compatibility.common.util.ResultType;
+import com.android.compatibility.common.util.ResultUnit;
+import com.android.compatibility.common.util.Stat;
+
+import java.util.Arrays;
+import java.util.Random;
 
 /**
  * A simple compatibility test which includes results in the report.
  *
  * This test measures the time taken to run a workload and adds in the report.
  */
-public class SampleDeviceResultTest extends CtsAndroidTestCase {
+public class SampleDeviceResultTest extends ActivityInstrumentationTestCase2<SampleDeviceActivity> {
 
     /**
      * The number of times to repeat the test.
@@ -37,86 +41,74 @@
     private static final int REPEAT = 5;
 
     /**
-     * The input number for the factorial.
+     * A {@link Random} to generate random integers to test the sort.
      */
-    private static final int IN = 15;
+    private static final Random random = new Random(12345);
 
     /**
-     * The expected output number for the factorial.
+     * Constructor which passes the class of the activity to be instrumented.
      */
-    private static final long OUT = 1307674368000L;
-
-    /**
-     * Measures the time taken to compute the factorial of 15 with a recursive method.
-     *
-     * @throws Exception
-     */
-    public void testFactorialRecursive() throws Exception {
-        runTest(new MeasureRun() {
-            @Override
-            public void run(int i) throws Exception {
-                // Compute the factorial and assert it is correct.
-                assertEquals("Incorrect result", OUT, factorialRecursive(IN));
-            }
-        });
+    public SampleDeviceResultTest() {
+        super(SampleDeviceActivity.class);
     }
 
     /**
-     * Measures the time taken to compute the factorial of 15 with a iterative method.
-     *
-     * @throws Exception
+     * Creates an array filled with random numbers of the given size.
      */
-    public void testFactorialIterative() throws Exception {
-        runTest(new MeasureRun() {
-            @Override
-            public void run(int i) throws Exception {
-                // Compute the factorial and assert it is correct.
-                assertEquals("Incorrect result", OUT, factorialIterative(IN));
-            }
-        });
-    }
-
-    /**
-     * Computes the factorial of a number with a recursive method.
-     *
-     * @param num The number to compute the factorial of.
-     */
-    private static long factorialRecursive(int num) {
-        if (num <= 0) {
-            return 1;
+    private static int[] createArray(int size) {
+        int[] array = new int[size];
+        for (int i = 0; i < size; i++) {
+            array[i] = random.nextInt();
         }
-        return num * factorialRecursive(num - 1);
+        return array;
     }
 
     /**
-     * Computes the factorial of a number with a iterative method.
-     *
-     * @param num The number to compute the factorial of.
+     * Tests an array is sorted.
      */
-    private static long factorialIterative(int num) {
-        long result = 1;
-        for (int i = 2; i <= num; i++) {
-            result *= i;
+    private static boolean isSorted(int[] array) {
+        int len = array.length;
+        for (int i = 0, j = 1; j < len; i++, j++) {
+            if (array[i] > array[j]) {
+                return false;
+            }
         }
-        return result;
+        return true;
     }
 
     /**
-     * Runs the workload and records the result to the report log.
-     *
-     * @param workload
+     * Measures the time taken to sort an array.
      */
-    private void runTest(MeasureRun workload) throws Exception {
+    public void testSort() throws Exception {
         // MeasureTime runs the workload N times and records the time taken by each run.
-        double[] result = MeasureTime.measure(REPEAT, workload);
+        double[] result = MeasureTime.measure(REPEAT, new MeasureRun() {
+            /**
+             * The size of the array to sort.
+             */
+            private static final int ARRAY_SIZE = 100000;
+            private int[] array;
+            @Override
+            public void prepare(int i) throws Exception {
+                array = createArray(ARRAY_SIZE);
+            }
+            @Override
+            public void run(int i) throws Exception {
+                Arrays.sort(array);
+                assertTrue("Array not sorted", isSorted(array));
+            }
+        });
         // Compute the stats.
         Stat.StatResult stat = Stat.getStat(result);
-        // Get the report for this test and add the results to record.
-        ReportLog log = getReportLog();
-        log.printArray("Times", result, ResultType.LOWER_BETTER, ResultUnit.MS);
-        log.printValue("Min", stat.mMin, ResultType.LOWER_BETTER, ResultUnit.MS);
-        log.printValue("Max", stat.mMax, ResultType.LOWER_BETTER, ResultUnit.MS);
+        // Create a new report to hold the metrics.
+        DeviceReportLog reportLog = new DeviceReportLog();
+        // Add the results to the report.
+        reportLog.addValues("Times", result, ResultType.LOWER_BETTER, ResultUnit.MS);
+        reportLog.addValue("Min", stat.mMin, ResultType.LOWER_BETTER, ResultUnit.MS);
+        reportLog.addValue("Max", stat.mMax, ResultType.LOWER_BETTER, ResultUnit.MS);
         // Every report must have a summary,
-        log.printSummary("Average", stat.mAverage, ResultType.LOWER_BETTER, ResultUnit.MS);
+        reportLog.setSummary("Average", stat.mAverage, ResultType.LOWER_BETTER, ResultUnit.MS);
+        // Submit the report to the given instrumentation.
+        reportLog.submit(getInstrumentation());
     }
+
 }
diff --git a/tests/tests/display/Android.mk b/tests/tests/display/Android.mk
index a42674d..724800f 100644
--- a/tests/tests/display/Android.mk
+++ b/tests/tests/display/Android.mk
@@ -16,19 +16,26 @@
 
 include $(CLEAR_VARS)
 
-# don't include this package in any target
-LOCAL_MODULE_TAGS := optional
-# and when built explicitly put it in the data partition
+# Don't include this package in any target
+LOCAL_MODULE_TAGS := tests
+# When built explicitly put it in the data partition
 LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
 
-LOCAL_STATIC_JAVA_LIBRARIES := ctstestrunner
+LOCAL_DEX_PREOPT := false
+
+LOCAL_PROGUARD_ENABLED := disabled
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-test
+
+# Tag this module as a cts_v2 test artifact
+LOCAL_COMPATIBILITY_SUITE := cts_v2
+
 LOCAL_PACKAGE_NAME := CtsDisplayTestCases
 
 LOCAL_CTS_MODULE_CONFIG := $(LOCAL_PATH)/Old$(CTS_MODULE_TEST_CONFIG)
 
 LOCAL_SDK_VERSION := current
 
-include $(BUILD_CTS_PACKAGE)
+include $(BUILD_PACKAGE)
diff --git a/tests/tests/display/AndroidManifest.xml b/tests/tests/display/AndroidManifest.xml
index bf84219..22cc824 100644
--- a/tests/tests/display/AndroidManifest.xml
+++ b/tests/tests/display/AndroidManifest.xml
@@ -16,9 +16,8 @@
  -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.android.cts.display">
+    package="android.display.cts">
 
-    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <!-- For special presentation windows when testing mode switches. -->
     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
 
@@ -26,12 +25,11 @@
         <uses-library android:name="android.test.runner" />
     </application>
 
-    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
-                     android:targetPackage="com.android.cts.display"
-                     android:label="CTS tests of android.view.display">
-        <meta-data
-            android:name="listener"
-            android:value="com.android.cts.runner.CtsTestRunListener" />
+    <!--  self-instrumenting test package. -->
+    <instrumentation
+        android:name="android.support.test.runner.AndroidJUnitRunner"
+        android:targetPackage="android.display.cts"
+        android:label="CTS tests of android.display">
     </instrumentation>
 
 </manifest>
diff --git a/tests/tests/display/AndroidTest.xml b/tests/tests/display/AndroidTest.xml
index 8fe3efe..0296e8e 100644
--- a/tests/tests/display/AndroidTest.xml
+++ b/tests/tests/display/AndroidTest.xml
@@ -20,7 +20,7 @@
     <option name="run-command:teardown-command" value="settings put global overlay_display_devices &quot;&quot;" />
     <option name="apk-installer:test-file-name" value="CtsDisplayTestCases.apk" />
     <test class="com.android.tradefed.testtype.InstrumentationTest" >
-        <option name="package" value="com.android.cts.display" />
+        <option name="package" value="android.display.cts" />
         <option name="runner" value="android.support.test.runner.AndroidJUnitRunner" />
     </test>
 </configuration>
diff --git a/tests/tests/gesture/Android.mk b/tests/tests/gesture/Android.mk
index 4a97931..b8e6699 100755
--- a/tests/tests/gesture/Android.mk
+++ b/tests/tests/gesture/Android.mk
@@ -16,17 +16,24 @@
 
 include $(CLEAR_VARS)
 
-# don't include this package in any target
-LOCAL_MODULE_TAGS := optional
-# and when built explicitly put it in the data partition
+# Don't include this package in any target
+LOCAL_MODULE_TAGS := tests
+# When built explicitly put it in the data partition
 LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
 
-LOCAL_STATIC_JAVA_LIBRARIES := ctstestrunner
+LOCAL_DEX_PREOPT := false
+
+LOCAL_PROGUARD_ENABLED := disabled
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-test
+
+# Tag this module as a cts_v2 test artifact
+LOCAL_COMPATIBILITY_SUITE := cts_v2
+
 LOCAL_PACKAGE_NAME := CtsGestureTestCases
 
 LOCAL_SDK_VERSION := current
 
-include $(BUILD_CTS_PACKAGE)
+include $(BUILD_PACKAGE)
\ No newline at end of file
diff --git a/tests/tests/gesture/AndroidManifest.xml b/tests/tests/gesture/AndroidManifest.xml
index b288cd2..fb3ee51 100755
--- a/tests/tests/gesture/AndroidManifest.xml
+++ b/tests/tests/gesture/AndroidManifest.xml
@@ -16,19 +16,17 @@
  -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.android.cts.gesture">
+    package="android.gesture.cts">
 
-    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <application>
         <uses-library android:name="android.test.runner" />
     </application>
 
     <!--  self-instrumenting test package. -->
-    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
-                     android:targetPackage="com.android.cts.gesture"
-                     android:label="CTS tests of android.gesture">
-        <meta-data android:name="listener"
-            android:value="com.android.cts.runner.CtsTestRunListener" />
+    <instrumentation
+        android:name="android.support.test.runner.AndroidJUnitRunner"
+        android:targetPackage="android.gesture.cts"
+        android:label="CTS tests of android.gesture">
     </instrumentation>
 
 </manifest>
diff --git a/tests/tests/gesture/AndroidTest.xml b/tests/tests/gesture/AndroidTest.xml
index f4d0fed..e0be497 100644
--- a/tests/tests/gesture/AndroidTest.xml
+++ b/tests/tests/gesture/AndroidTest.xml
@@ -17,7 +17,7 @@
     <include name="common-config" />
     <option name="apk-installer:test-file-name" value="CtsGestureTestCases.apk" />
     <test class="com.android.tradefed.testtype.InstrumentationTest" >
-        <option name="package" value="com.android.cts.gesture" />
+        <option name="package" value="android.gesture.cts" />
         <option name="runner" value="android.support.test.runner.AndroidJUnitRunner" />
     </test>
 </configuration>