Monkeying Around

Bug 5898005

Spot check the package, category, seed, and verbosity flags.
These tests are not very strict but make sure things appear to
be working as expected.

Change-Id: Ia5dd9662e8164d6151c37f6eeebd32d5f9dd97ec
diff --git a/CtsTestCaseList.mk b/CtsTestCaseList.mk
index ff8b352..ae6da8b 100644
--- a/CtsTestCaseList.mk
+++ b/CtsTestCaseList.mk
@@ -29,6 +29,7 @@
 	CtsDelegatingAccessibilityService \
 	CtsDeviceAdmin \
 	CtsMonkeyApp \
+	CtsMonkeyApp2 \
 	CtsTestStubs \
 	SignatureTest \
 	TestDeviceSetup \
diff --git a/development/ide/eclipse/.classpath b/development/ide/eclipse/.classpath
index ac00919..591c417 100644
--- a/development/ide/eclipse/.classpath
+++ b/development/ide/eclipse/.classpath
@@ -4,8 +4,11 @@
     <classpathentry kind="lib" path="prebuilt/common/tradefed/tradefed-prebuilt.jar"/>
     <classpathentry kind="src" path="cts/apps/CtsVerifier/src"/>
     <classpathentry kind="src" path="cts/apps/CtsVerifier/tests/src"/>
+    <classpathentry kind="src" path="cts/hostsidetests/appsecurity/src"/>
+    <classpathentry kind="src" path="cts/hostsidetests/appsecurity/test-apps/AppWithData/src"/>
     <classpathentry kind="src" path="cts/hostsidetests/monkey/src"/>
     <classpathentry kind="src" path="cts/hostsidetests/monkey/test-apps/CtsMonkeyApp/src"/>
+    <classpathentry kind="src" path="cts/hostsidetests/monkey/test-apps/CtsMonkeyApp2/src"/>
     <classpathentry kind="src" path="cts/libs/vogar-expect/src"/>
     <classpathentry kind="src" path="cts/tests/ApiDemosReferenceTest/src"/>
     <classpathentry kind="src" path="cts/tests/ProcessTest/src"/>
@@ -15,8 +18,6 @@
     <classpathentry kind="src" path="cts/tests/SignatureTest/tests/src"/>
     <classpathentry kind="src" path="cts/tests/acceleration/src"/>
     <classpathentry kind="src" path="cts/tests/accessibilityservice/src"/>
-    <classpathentry kind="src" path="cts/tests/appsecurity-tests/src"/>
-    <classpathentry kind="src" path="cts/tests/appsecurity-tests/test-apps/AppWithData/src"/>
     <classpathentry kind="src" path="cts/tests/core/runner/src"/>
     <classpathentry kind="src" path="cts/tests/deviceadmin/src"/>
     <classpathentry kind="src" path="cts/tests/src"/>
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/AbstractMonkeyTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/AbstractMonkeyTest.java
new file mode 100644
index 0000000..0bc2c10
--- /dev/null
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/AbstractMonkeyTest.java
@@ -0,0 +1,47 @@
+package com.android.cts.monkey;
+
+import com.android.cts.tradefed.build.CtsBuildHelper;
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.testtype.IBuildReceiver;
+
+import java.io.File;
+
+abstract class AbstractMonkeyTest extends DeviceTestCase implements IBuildReceiver {
+
+    static final String[] PKGS = {"com.android.cts.monkey", "com.android.cts.monkey2"};
+    static final String[] APKS = {"CtsMonkeyApp.apk", "CtsMonkeyApp2.apk"};
+
+    CtsBuildHelper mBuild;
+    ITestDevice mDevice;
+
+    @Override
+    public void setBuild(IBuildInfo buildInfo) {
+        mBuild = CtsBuildHelper.createBuildHelper(buildInfo);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mDevice = getDevice();
+        for (int i = 0; i < PKGS.length; i++) {
+            mDevice.uninstallPackage(PKGS[i]);
+            File app = mBuild.getTestApp(APKS[i]);
+            mDevice.installPackage(app, false);
+        }
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        for (int i = 0; i < PKGS.length; i++) {
+            mDevice.uninstallPackage(PKGS[i]);
+        }
+    }
+
+    void clearLogCat() throws DeviceNotAvailableException {
+        mDevice.executeAdbCommand("logcat", "-c");
+    }
+}
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/CategoryTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/CategoryTest.java
new file mode 100644
index 0000000..3932653
--- /dev/null
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/CategoryTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2012 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 com.android.cts.monkey;
+
+public class CategoryTest extends AbstractMonkeyTest {
+
+    public void testDefaultCategories() throws Exception {
+        String out = mDevice.executeShellCommand("monkey -v -p " + PKGS[0] + " 5000");
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.MonkeyActivity"));
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.BaboonActivity"));
+    }
+
+    public void testSingleCategory() throws Exception {
+        String out = mDevice.executeShellCommand("monkey -v -p " + PKGS[0]
+                + " -c android.intent.category.LAUNCHER 5000");
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.MonkeyActivity"));
+        assertFalse(out.contains("cmp=com.android.cts.monkey/.BaboonActivity"));
+
+        out = mDevice.executeShellCommand("monkey -v -p " + PKGS[0]
+                + " -c android.intent.category.MONKEY 5000");
+        assertFalse(out.contains("cmp=com.android.cts.monkey/.MonkeyActivity"));
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.BaboonActivity"));
+    }
+
+    public void testMultipleCategories() throws Exception {
+        String out = mDevice.executeShellCommand("monkey -v -p " + PKGS[0]
+                + " -c android.intent.category.LAUNCHER"
+                + " -c android.intent.category.MONKEY 5000");
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.MonkeyActivity"));
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.BaboonActivity"));
+    }
+}
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
index 02ceebd..2bf27ed 100644
--- a/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
@@ -16,55 +16,22 @@
 
 package com.android.cts.monkey;
 
-import com.android.cts.tradefed.build.CtsBuildHelper;
-import com.android.tradefed.build.IBuildInfo;
 import com.android.tradefed.device.DeviceNotAvailableException;
-import com.android.tradefed.device.ITestDevice;
-import com.android.tradefed.testtype.DeviceTestCase;
-import com.android.tradefed.testtype.IBuildReceiver;
 
-import java.io.File;
 import java.util.Scanner;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class MonkeyTest extends DeviceTestCase implements IBuildReceiver {
-
-    private static final String PKG = "com.android.cts.monkey";
-    private static final String APK = "CtsMonkeyApp.apk";
+public class MonkeyTest extends AbstractMonkeyTest {
 
     private static final Pattern LOG_PATTERN =
             Pattern.compile("I/MonkeyActivity\\([\\d ]+\\): (.*)");
     private static final String MONKEY = "@(>.<)@";
     private static final String HUMAN = "(^_^)";
 
-    private CtsBuildHelper mBuild;
-    private ITestDevice mDevice;
-
-    @Override
-    public void setBuild(IBuildInfo buildInfo) {
-        mBuild = CtsBuildHelper.createBuildHelper(buildInfo);
-    }
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        mDevice = getDevice();
-        mDevice.uninstallPackage(PKG);
-
-        File app = mBuild.getTestApp(APK);
-        mDevice.installPackage(app, false);
-    }
-
-    @Override
-    protected void tearDown() throws Exception {
-        super.tearDown();
-        mDevice.uninstallPackage(PKG);
-    }
-
     public void testIsMonkey() throws Exception {
         clearLogCat();
-        mDevice.executeShellCommand("monkey -v -p " + PKG + " 500");
+        mDevice.executeShellCommand("monkey -p " + PKGS[0] + " 500");
         assertIsUserAMonkey(true);
     }
 
@@ -93,8 +60,4 @@
             s.close();
         }
     }
-
-    private void clearLogCat() throws DeviceNotAvailableException {
-        mDevice.executeAdbCommand("logcat", "-c");
-    }
 }
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/PackageTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/PackageTest.java
new file mode 100644
index 0000000..aa6106b
--- /dev/null
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/PackageTest.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2012 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 com.android.cts.monkey;
+
+public class PackageTest extends AbstractMonkeyTest {
+
+    public void testSinglePackage() throws Exception {
+        String out = mDevice.executeShellCommand("monkey -v -p " + PKGS[0] + " 5000");
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.MonkeyActivity"));
+        assertFalse(out.contains("cmp=com.android.cts.monkey2/.ChimpActivity"));
+
+        out = mDevice.executeShellCommand("monkey -v -p " + PKGS[1] + " 5000");
+        assertFalse(out.contains("cmp=com.android.cts.monkey/.MonkeyActivity"));
+        assertTrue(out.contains("cmp=com.android.cts.monkey2/.ChimpActivity"));
+    }
+
+    public void testMultiplePackages() throws Exception {
+        String out = mDevice.executeShellCommand("monkey -v -p " + PKGS[0]
+                + " -p " + PKGS[1] + " 5000");
+        assertTrue(out.contains("cmp=com.android.cts.monkey/.MonkeyActivity"));
+        assertTrue(out.contains("cmp=com.android.cts.monkey2/.ChimpActivity"));
+    }
+}
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/SeedTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/SeedTest.java
new file mode 100644
index 0000000..062fb7d
--- /dev/null
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/SeedTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2012 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 com.android.cts.monkey;
+
+import java.util.Scanner;
+
+public class SeedTest extends AbstractMonkeyTest {
+
+    public void testSeed() throws Exception {
+        String cmd1 = "monkey -s 1337 -v -p " + PKGS[0] + " 500";
+        String out1 = mDevice.executeShellCommand(cmd1);
+        String out2 = mDevice.executeShellCommand(cmd1);
+        assertOutputs(out1, out2);
+
+        String cmd2 = "monkey -s 3007 -v -p " + PKGS[0] + " 125";
+        String out3 = mDevice.executeShellCommand(cmd2);
+        String out4 = mDevice.executeShellCommand(cmd2);
+        assertOutputs(out3, out4);
+    }
+
+    private void assertOutputs(String out1, String out2) {
+        Scanner s1 = new Scanner(out1);
+        Scanner s2 = new Scanner(out2);
+        while (s1.hasNextLine()) {
+            assertTrue(s2.hasNextLine());
+
+            String line1 = s1.nextLine().trim();
+            String line2 = s2.nextLine().trim();
+
+            if (line1.startsWith("//[calendar_time") || line1.startsWith("## Network stats")) {
+                // Skip these lines since they have timestamps.
+                continue;
+            }
+
+            assertEquals(line1, line2);
+        }
+        assertFalse(s2.hasNextLine());
+    }
+}
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/VerbosityTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/VerbosityTest.java
new file mode 100644
index 0000000..2956191
--- /dev/null
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/VerbosityTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2012 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 com.android.cts.monkey;
+
+public class VerbosityTest extends AbstractMonkeyTest {
+
+    public void testVerbosity() throws Exception {
+        String v0 = mDevice.executeShellCommand("monkey -s 1337 -p " + PKGS[0] + " 500");
+        assertTrue(v0.contains("Events injected"));
+        assertFalse(v0.contains("Sending Touch"));
+        assertFalse(v0.contains("Sending Trackball"));
+        assertFalse(v0.contains("Switch"));
+        assertFalse(v0.contains("Sleeping"));
+
+        String v1 = mDevice.executeShellCommand("monkey -v -p " + PKGS[0] + " 500");
+        assertTrue(v1.contains("Events injected"));
+        assertTrue(v1.contains("Sending Touch"));
+        assertTrue(v1.contains("Sending Trackball"));
+        assertTrue(v1.contains("Switch"));
+        assertFalse(v1.contains("Sleeping"));
+
+        String v2 = mDevice.executeShellCommand("monkey -v -v -p " + PKGS[0] + " 500");
+        assertTrue(v2.contains("Events injected"));
+        assertTrue(v2.contains("Sending Touch"));
+        assertTrue(v2.contains("Sending Trackball"));
+        assertTrue(v2.contains("Switch"));
+        assertTrue(v2.contains("Sleeping"));
+
+        assertTrue(v0.length() < v1.length());
+        assertTrue(v1.length() < v2.length());
+    }
+}
diff --git a/hostsidetests/monkey/test-apps/CtsMonkeyApp/AndroidManifest.xml b/hostsidetests/monkey/test-apps/CtsMonkeyApp/AndroidManifest.xml
index 073d244..55f3d6f 100644
--- a/hostsidetests/monkey/test-apps/CtsMonkeyApp/AndroidManifest.xml
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp/AndroidManifest.xml
@@ -24,6 +24,13 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        
+        <activity android:name=".BaboonActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.MONKEY" />
+            </intent-filter>
+        </activity>
 
     </application>
 </manifest>
diff --git a/hostsidetests/monkey/test-apps/CtsMonkeyApp/src/com/android/cts/monkey/BaboonActivity.java b/hostsidetests/monkey/test-apps/CtsMonkeyApp/src/com/android/cts/monkey/BaboonActivity.java
new file mode 100644
index 0000000..f7b8f47
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp/src/com/android/cts/monkey/BaboonActivity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2012 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 com.android.cts.monkey;
+
+import android.app.Activity;
+
+public class BaboonActivity extends Activity {
+}
diff --git a/hostsidetests/monkey/test-apps/CtsMonkeyApp2/Android.mk b/hostsidetests/monkey/test-apps/CtsMonkeyApp2/Android.mk
new file mode 100644
index 0000000..2f3abd2
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp2/Android.mk
@@ -0,0 +1,27 @@
+# Copyright (C) 2012 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 := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_SDK_VERSION := current
+
+LOCAL_PACKAGE_NAME := CtsMonkeyApp2
+
+include $(BUILD_PACKAGE)
diff --git a/hostsidetests/monkey/test-apps/CtsMonkeyApp2/AndroidManifest.xml b/hostsidetests/monkey/test-apps/CtsMonkeyApp2/AndroidManifest.xml
new file mode 100644
index 0000000..34bca75
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp2/AndroidManifest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 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.monkey2">
+
+    <application>
+
+        <activity android:name=".ChimpActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+
+    </application>
+</manifest>
diff --git a/hostsidetests/monkey/test-apps/CtsMonkeyApp2/src/com/android/cts/monkey2/ChimpActivity.java b/hostsidetests/monkey/test-apps/CtsMonkeyApp2/src/com/android/cts/monkey2/ChimpActivity.java
new file mode 100644
index 0000000..ef9fcc3
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp2/src/com/android/cts/monkey2/ChimpActivity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2012 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 com.android.cts.monkey2;
+
+import android.app.Activity;
+
+public class ChimpActivity extends Activity {
+}