Tests for ActivityManager#isUserAMonkey

Bug 5882598

- Add a host side test that installs an activity that logs whether or
  not the user is a monkey. The host executes "adb shell monkey" and
  the test then parses the lines looking for the log line printed out
  by the activity.

- Creates a new top level CTS directory called "hostsidetests" to put
  all host side tests under rather than putting it under "tests"
  since that directory is more for the com.android.cts.stub package
  and I would like to avoid cluttering that directory as we add more
  host side tests.

Change-Id: I9d7b571b0244867e84bdff14418a544c713578e1
diff --git a/hostsidetests/monkey/Android.mk b/hostsidetests/monkey/Android.mk
new file mode 100644
index 0000000..8a73df3
--- /dev/null
+++ b/hostsidetests/monkey/Android.mk
@@ -0,0 +1,31 @@
+# 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_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE := CtsMonkeyTestCases
+
+LOCAL_JAVA_LIBRARIES := cts-tradefed tradefed-prebuilt ddmlib-prebuilt junit
+
+LOCAL_CTS_TEST_PACKAGE := android.monkey
+
+include $(BUILD_CTS_HOST_JAVA_LIBRARY)
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
new file mode 100644
index 0000000..02ceebd
--- /dev/null
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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 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";
+
+    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");
+        assertIsUserAMonkey(true);
+    }
+
+    public void testNotMonkey() throws Exception {
+        clearLogCat();
+        mDevice.executeShellCommand("am start -W -a android.intent.action.MAIN "
+                + "-n com.android.cts.monkey/com.android.cts.monkey.MonkeyActivity");
+        assertIsUserAMonkey(false);
+    }
+
+    private void assertIsUserAMonkey(boolean isMonkey) throws DeviceNotAvailableException {
+        String logs = mDevice.executeAdbCommand("logcat", "-d", "MonkeyActivity:I", "*:S");
+        boolean monkeyLogsFound = false;
+        Scanner s = new Scanner(logs);
+        try {
+            while (s.hasNextLine()) {
+                String line = s.nextLine();
+                Matcher m = LOG_PATTERN.matcher(line);
+                if (m.matches()) {
+                    monkeyLogsFound = true;
+                    assertEquals(isMonkey ? MONKEY : HUMAN, m.group(1));
+                }
+            }
+            assertTrue(monkeyLogsFound);
+        } finally {
+            s.close();
+        }
+    }
+
+    private void clearLogCat() throws DeviceNotAvailableException {
+        mDevice.executeAdbCommand("logcat", "-c");
+    }
+}
diff --git a/hostsidetests/monkey/test-apps/Android.mk b/hostsidetests/monkey/test-apps/Android.mk
new file mode 100644
index 0000000..b189007
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/Android.mk
@@ -0,0 +1,19 @@
+# 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)
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/hostsidetests/monkey/test-apps/CtsMonkeyApp/Android.mk b/hostsidetests/monkey/test-apps/CtsMonkeyApp/Android.mk
new file mode 100644
index 0000000..85a82d8
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp/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 := CtsMonkeyApp
+
+include $(BUILD_PACKAGE)
diff --git a/hostsidetests/monkey/test-apps/CtsMonkeyApp/AndroidManifest.xml b/hostsidetests/monkey/test-apps/CtsMonkeyApp/AndroidManifest.xml
new file mode 100644
index 0000000..073d244
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp/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.monkey">
+
+    <application>
+
+        <activity android:name=".MonkeyActivity">
+            <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/CtsMonkeyApp/src/com/android/cts/monkey/MonkeyActivity.java b/hostsidetests/monkey/test-apps/CtsMonkeyApp/src/com/android/cts/monkey/MonkeyActivity.java
new file mode 100644
index 0000000..fd792a7
--- /dev/null
+++ b/hostsidetests/monkey/test-apps/CtsMonkeyApp/src/com/android/cts/monkey/MonkeyActivity.java
@@ -0,0 +1,34 @@
+/*
+ * 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;
+import android.app.ActivityManager;
+import android.os.Bundle;
+import android.util.Log;
+
+/** @(>.<)@  I'm a monkey! */
+public class MonkeyActivity extends Activity {
+
+    private static final String TAG = "MonkeyActivity";
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Log.i(TAG, ActivityManager.isUserAMonkey() ? "@(>.<)@" : "(^_^)");
+    }
+}