FileDocCategorySizeDatePackage
AppCompatibility.javaAPI DocAndroid 5.1 API8225Thu Mar 12 22:22:42 GMT 2015com.android.compatibilitytest

AppCompatibility

public class AppCompatibility extends android.test.InstrumentationTestCase
Application Compatibility Test that launches an application and detects crashes.

Fields Summary
private static final String
TAG
private static final String
PACKAGE_TO_LAUNCH
private static final String
APP_LAUNCH_TIMEOUT_MSECS
private static final String
WORKSPACE_LAUNCH_TIMEOUT_MSECS
private int
mAppLaunchTimeout
private int
mWorkspaceLaunchTimeout
private android.content.Context
mContext
private android.app.ActivityManager
mActivityManager
private android.content.pm.PackageManager
mPackageManager
private AppCompatibilityRunner
mRunner
private android.os.Bundle
mArgs
Constructors Summary
Methods Summary
private java.lang.StringgetProcessName(java.lang.String packageName)
Returns the process name that the package is going to use.

param
packageName name of the package
return
process name of the package

        try {
            PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0);
            return pi.applicationInfo.processName;
        } catch (NameNotFoundException e) {
            return packageName;
        }
    
private java.lang.StringgetStackTrace(android.app.ActivityManager.ProcessErrorStateInfo in)
Gets the stack trace for the error.

param
in {@link ProcessErrorStateInfo} to parse.
return
{@link String} the long message of the error.

        if (in == null) {
            return null;
        } else {
            return in.stackTrace;
        }
    
private android.app.ActivityManager.ProcessErrorStateInfolaunchActivity(java.lang.String packageName)
Launches and activity and queries for errors.

param
packageName {@link String} the package name of the application to launch.
return
{@link Collection} of {@link ProcessErrorStateInfo} detected during the app launch.

        // the recommended way to see if this is a tv or not.
        boolean isleanback = !mPackageManager.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
            && !mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory(Intent.CATEGORY_HOME);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Intent intent;
        if (isleanback) {
            Log.d(TAG, "Leanback and relax! " + packageName);
            intent = mPackageManager.getLeanbackLaunchIntentForPackage(packageName);
        } else {
            intent = mPackageManager.getLaunchIntentForPackage(packageName);
        }
        assertNotNull("Skipping " + packageName + "; missing launch intent", intent);

        String processName = getProcessName(packageName);

        // Launch Activity
        mContext.startActivity(intent);

        try {
            Thread.sleep(mAppLaunchTimeout);
        } catch (InterruptedException e) {
            // ignore
        }

        // Send the "home" intent and wait 2 seconds for us to get there
        mContext.startActivity(homeIntent);
        try {
            Thread.sleep(mWorkspaceLaunchTimeout);
        } catch (InterruptedException e) {
            // ignore
        }

        // See if there are any errors. We wait until down here to give ANRs as
        // much time as
        // possible to occur.
        final Collection<ProcessErrorStateInfo> postErr =
                mActivityManager.getProcessesInErrorState();

        if (postErr == null) {
            return null;
        }
        for (ProcessErrorStateInfo error : postErr) {
            if (error.processName.equals(processName)) {
                return error;
            }
        }
        return null;
    
private booleanprocessStillUp(java.lang.String packageName)
Determine if a given package is still running.

param
packageName {@link String} package to look for
return
True if package is running, false otherwise.

        String processName = getProcessName(packageName);
        List<RunningAppProcessInfo> runningApps = mActivityManager.getRunningAppProcesses();
        for (RunningAppProcessInfo app : runningApps) {
            if (app.processName.equalsIgnoreCase(processName)) {
                Log.d(TAG, "Found process " + app.processName);
                return true;
            }
            for (String relatedPackage : app.pkgList) {
                if (relatedPackage.equalsIgnoreCase(processName)) {
                    Log.d(TAG, "Found process " + app.processName);
                    return true;
                }
            }
        }
        Log.d(TAG, "Failed to find process " + processName + " with package name "
                + packageName);
        return false;
    
public voidsetUp()


    
         
        super.setUp();
        mRunner = (AppCompatibilityRunner) getInstrumentation();
        assertNotNull("Could not fetch InstrumentationTestRunner.", mRunner);

        mContext = mRunner.getTargetContext();
        Assert.assertNotNull("Could not get the Context", mContext);

        mActivityManager = (ActivityManager)
                mContext.getSystemService(Context.ACTIVITY_SERVICE);
        Assert.assertNotNull("Could not get Activity Manager", mActivityManager);

        mPackageManager = mContext.getPackageManager();
        Assert.assertNotNull("Missing Package Manager", mPackageManager);

        mArgs = mRunner.getBundle();

        // Parse optional inputs.
        String appLaunchTimeoutMsecs = mArgs.getString(APP_LAUNCH_TIMEOUT_MSECS);
        if (appLaunchTimeoutMsecs != null) {
            mAppLaunchTimeout = Integer.parseInt(appLaunchTimeoutMsecs);
        }
        String workspaceLaunchTimeoutMsecs = mArgs.getString(WORKSPACE_LAUNCH_TIMEOUT_MSECS);
        if (workspaceLaunchTimeoutMsecs != null) {
            mWorkspaceLaunchTimeout = Integer.parseInt(workspaceLaunchTimeoutMsecs);
        }
    
protected voidtearDown()

        super.tearDown();
    
public voidtestAppStability()
Actual test case that launches the package and throws an exception on the first error.

throws
Exception

        String packageName = mArgs.getString(PACKAGE_TO_LAUNCH);
        if (packageName != null) {
            Log.d(TAG, "Launching app " + packageName);
            ProcessErrorStateInfo err = launchActivity(packageName);
            // Make sure there are no errors when launching the application,
            // otherwise raise an
            // exception with the first error encountered.
            assertNull(getStackTrace(err), err);
            assertTrue("App crashed after launch.", processStillUp(packageName));
        } else {
            Log.d(TAG, "Missing argument, use " + PACKAGE_TO_LAUNCH +
                    " to specify the package to launch");
        }