AI 147049: Adding MultiApp Startup performance CTS test case.
  This is only the Test Case code.  The changes to include it as part of the test suite will come in a later CL.

Automated import of CL 147049
diff --git a/tests/tests/performance/Android.mk b/tests/tests/performance/Android.mk
new file mode 100644
index 0000000..fdaa168
--- /dev/null
+++ b/tests/tests/performance/Android.mk
@@ -0,0 +1,29 @@
+# Copyright (C) 2008 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_MODULE_TAGS := tests
+
+LOCAL_JAVA_LIBRARIES := android.test.runner
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := CtsPerformanceTestCases
+
+LOCAL_INSTRUMENTATION_FOR := CtsTestStubs
+
+include $(BUILD_PACKAGE)
diff --git a/tests/tests/performance/AndroidManifest.xml b/tests/tests/performance/AndroidManifest.xml
new file mode 100644
index 0000000..375311f
--- /dev/null
+++ b/tests/tests/performance/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2007 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.cts.performance">
+
+    <uses-permission android:name="android.permission.GET_TASKS" />
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation android:name="android.test.InstrumentationTestRunner"
+                     android:targetPackage="com.android.calculator2"
+                     android:label="CTS tests of android.performance"/>
+</manifest>
diff --git a/tests/tests/performance/src/android/performance/cts/MultiAppStartupTest.java b/tests/tests/performance/src/android/performance/cts/MultiAppStartupTest.java
new file mode 100644
index 0000000..d102497
--- /dev/null
+++ b/tests/tests/performance/src/android/performance/cts/MultiAppStartupTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2009 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.performance.cts;
+
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningTaskInfo;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Handler;
+import android.test.InstrumentationTestCase;
+
+
+import java.lang.Runnable;
+import java.lang.String;
+import java.lang.System;
+import java.util.List;
+
+public class MultiAppStartupTest extends InstrumentationTestCase {
+    private static final String PACKAGE_UNDER_TEST = "com.android.calculator2";
+    private static final String ACTIVITY_UNDER_TEST = "Calculator";
+    private static final int ACTIVITY_STARTUP_WAIT_TIME = 1000;
+
+    private Intent buildIntent(final String pkgName, String className, boolean isMain) {
+        final String fullClassName = pkgName + "." + className;
+        Intent intent = new Intent();
+        intent.setClassName(pkgName, fullClassName);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        if (isMain) {
+            intent.setAction(Intent.ACTION_MAIN);
+            intent.addCategory(Intent.CATEGORY_LAUNCHER);
+        }
+        return intent;
+    }
+
+    private void launchActivity(final String pkgName, String className, boolean isMain) {
+        Context ctx = getInstrumentation().getContext();
+        ctx.startActivity(buildIntent(pkgName, className, isMain));
+    }
+
+    private long launchActivityUnderTest() {
+        long start = System.currentTimeMillis();
+        Intent i = buildIntent(PACKAGE_UNDER_TEST,
+                               ACTIVITY_UNDER_TEST,
+                               true);
+        Activity a = getInstrumentation().startActivitySync(i);
+        long end = System.currentTimeMillis();
+        long diff = end - start;
+        a.finish();
+        return diff;
+    }
+
+    public void testMultipleApps() throws InterruptedException {
+        // Measure how long the initial startup of the application takes
+        long initialStartDuration =  launchActivityUnderTest();
+
+        // Re-launch the activity.  It was finished in
+        // launchActivityUnderTest, so this ensures that it is around
+        // for the ActivityManager to possibly kill it.
+        launchActivity(PACKAGE_UNDER_TEST,
+                       ACTIVITY_UNDER_TEST,
+                       true);
+
+        // Then launch a few more
+        launchActivity("com.android.browser", "BrowserActivity", true);
+        Thread.sleep(ACTIVITY_STARTUP_WAIT_TIME);
+        launchActivity("com.android.mms", "ui.ConversationList", true);
+        Thread.sleep(ACTIVITY_STARTUP_WAIT_TIME);
+        launchActivity("com.android.alarmclock", "AlarmClock", true);
+        Thread.sleep(ACTIVITY_STARTUP_WAIT_TIME);
+        launchActivity("com.android.contacts", "TwelveKeyDialer", false);
+        Thread.sleep(ACTIVITY_STARTUP_WAIT_TIME);
+        launchActivity("com.android.contacts", "RecentCallsListActivity", false);
+        Thread.sleep(ACTIVITY_STARTUP_WAIT_TIME);
+        launchActivity("com.android.calendar", "LaunchActivity", true);
+        Thread.sleep(ACTIVITY_STARTUP_WAIT_TIME);
+
+        long finalStartDuration = launchActivityUnderTest();
+
+        // assure that the time to re-start the application is less
+        // than the original start time.
+        assertTrue("Restart of inital app took to long: " +
+                   finalStartDuration + " " + initialStartDuration,
+                   finalStartDuration < initialStartDuration);
+
+        // TODO: Change this check to use RunningProcesses from
+        // ActivityManager which should provide better results.
+    }
+}