Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c3e93a8f authored by Daichi Hirono's avatar Daichi Hirono
Browse files

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
parent 75344663
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -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" />

+63 −0
Original line number Diff line number Diff line
/*
 * 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");
    }
}
+48 −0
Original line number Diff line number Diff line
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() : ""));
    }
}