Add activity to show the result of tests.

When testing a real MTP device, we cannot obtain test results by using the USB
port because the USB port is occupied by the MTP devcie. The CL adds an activity
to test package that shows test results.

BUG=23536467

Change-Id: I0e53c2de122cb3afd52f22bedd93ece53aaeb525
diff --git a/packages/MtpDocumentsProvider/tests/AndroidManifest.xml b/packages/MtpDocumentsProvider/tests/AndroidManifest.xml
index 50b7521..12fcb87 100644
--- a/packages/MtpDocumentsProvider/tests/AndroidManifest.xml
+++ b/packages/MtpDocumentsProvider/tests/AndroidManifest.xml
@@ -5,9 +5,17 @@
 
     <application>
         <uses-library android:name="android.test.runner" />
+        <activity android:name="com.android.mtp.TestResultActivity"
+                  android:screenOrientation="locked"
+                  android:launchMode="singleInstance">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
     </application>
 
-    <instrumentation android:name="android.test.InstrumentationTestRunner"
+    <instrumentation android:name="com.android.mtp.TestResultInstrumentation"
         android:targetPackage="com.android.mtp"
         android:label="Tests for MtpDocumentsProvider" />
 
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultActivity.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultActivity.java
new file mode 100644
index 0000000..9f2bb2a
--- /dev/null
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultActivity.java
@@ -0,0 +1,63 @@
+/*
+ * 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.
+ */
+
+package com.android.mtp;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.ViewGroup.LayoutParams;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+
+/**
+ * Activity that shows the test results instead of adb while using USB port to connect MTP device.
+ */
+public class TestResultActivity extends Activity {
+    private final static String TAG = "MtpDocumentsProviderTest";
+    private TextView mTextView;
+
+    static void show(Context context, String message) {
+        Log.d(TAG, message);
+        final Intent intent = new Intent(context, TestResultActivity.class);
+        intent.putExtra("message", message);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        context.startActivity(intent);
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final LinearLayout linearLayout = new LinearLayout(this);
+        linearLayout.setOrientation(LinearLayout.VERTICAL);
+        setContentView(linearLayout);
+
+        mTextView = new TextView(this);
+        mTextView.setText(getIntent().getStringExtra("message") + "\n");
+        linearLayout.addView(
+                mTextView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
+    }
+
+    @Override
+    protected void onNewIntent(Intent intent) {
+        super.onNewIntent(intent);
+        mTextView.setText(mTextView.getText() + intent.getStringExtra("message") + "\n");
+    }
+}
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java
new file mode 100644
index 0000000..45a77d8
--- /dev/null
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java
@@ -0,0 +1,48 @@
+package com.android.mtp;
+
+import android.os.Bundle;
+import android.test.InstrumentationTestRunner;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.Test;
+import junit.framework.TestListener;
+
+public class TestResultInstrumentation extends InstrumentationTestRunner implements TestListener {
+    private boolean mHasError = false;
+
+    @Override
+    public void onCreate(Bundle arguments) {
+        super.onCreate(arguments);
+        addTestListener(this);
+    }
+
+    @Override
+    public void addError(Test test, Throwable t) {
+        mHasError = true;
+        show("ERROR", test, t);
+    }
+
+    @Override
+    public void addFailure(Test test, AssertionFailedError t) {
+        mHasError = true;
+        show("FAIL", test, t);
+    }
+
+    @Override
+    public void endTest(Test test) {
+        if (!mHasError) {
+            show("PASS", test, null);
+        }
+    }
+
+    @Override
+    public void startTest(Test test) {
+        mHasError = false;
+    }
+
+    private void show(String tag, Test test, Throwable t) {
+        TestResultActivity.show(
+                getContext(),
+                String.format("[%s] %s %s", tag, test.toString(), t != null ? t.getMessage() : ""));
+    }
+}