Merge "Make sure the app is still app after launching. Bug:8755950" into jb-mr2-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
4e0ae8d6fd
@@ -18,9 +18,12 @@ package com.android.compatibilitytest;
|
|||||||
|
|
||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
import android.app.ActivityManager.ProcessErrorStateInfo;
|
import android.app.ActivityManager.ProcessErrorStateInfo;
|
||||||
|
import android.app.ActivityManager.RunningAppProcessInfo;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.test.InstrumentationTestCase;
|
import android.test.InstrumentationTestCase;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -28,9 +31,11 @@ import android.util.Log;
|
|||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application Compatibility Test that launches an application and detects crashes.
|
* Application Compatibility Test that launches an application and detects
|
||||||
|
* crashes.
|
||||||
*/
|
*/
|
||||||
public class AppCompatibility extends InstrumentationTestCase {
|
public class AppCompatibility extends InstrumentationTestCase {
|
||||||
|
|
||||||
@@ -83,7 +88,9 @@ public class AppCompatibility extends InstrumentationTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Actual test case that launches the package and throws an exception on the first error.
|
* Actual test case that launches the package and throws an exception on the
|
||||||
|
* first error.
|
||||||
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public void testAppStability() throws Exception {
|
public void testAppStability() throws Exception {
|
||||||
@@ -91,9 +98,11 @@ public class AppCompatibility extends InstrumentationTestCase {
|
|||||||
if (packageName != null) {
|
if (packageName != null) {
|
||||||
Log.d(TAG, "Launching app " + packageName);
|
Log.d(TAG, "Launching app " + packageName);
|
||||||
Collection<ProcessErrorStateInfo> err = launchActivity(packageName);
|
Collection<ProcessErrorStateInfo> err = launchActivity(packageName);
|
||||||
// Make sure there are no errors when launching the application, otherwise raise an
|
// Make sure there are no errors when launching the application,
|
||||||
|
// otherwise raise an
|
||||||
// exception with the first error encountered.
|
// exception with the first error encountered.
|
||||||
assertNull(getFirstError(err), err);
|
assertNull(getFirstError(err), err);
|
||||||
|
assertTrue("App crashed after launch.", processStillUp(packageName));
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Missing argument, use " + PACKAGE_TO_LAUNCH +
|
Log.d(TAG, "Missing argument, use " + PACKAGE_TO_LAUNCH +
|
||||||
" to specify the package to launch");
|
" to specify the package to launch");
|
||||||
@@ -102,6 +111,7 @@ public class AppCompatibility extends InstrumentationTestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the first error in collection and return the long message for it.
|
* Gets the first error in collection and return the long message for it.
|
||||||
|
*
|
||||||
* @param in {@link Collection} of {@link ProcessErrorStateInfo} to parse.
|
* @param in {@link Collection} of {@link ProcessErrorStateInfo} to parse.
|
||||||
* @return {@link String} the long message of the error.
|
* @return {@link String} the long message of the error.
|
||||||
*/
|
*/
|
||||||
@@ -118,8 +128,11 @@ public class AppCompatibility extends InstrumentationTestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Launches and activity and queries for errors.
|
* 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.
|
* @param packageName {@link String} the package name of the application to
|
||||||
|
* launch.
|
||||||
|
* @return {@link Collection} of {@link ProcessErrorStateInfo} detected
|
||||||
|
* during the app launch.
|
||||||
*/
|
*/
|
||||||
private Collection<ProcessErrorStateInfo> launchActivity(String packageName) {
|
private Collection<ProcessErrorStateInfo> launchActivity(String packageName) {
|
||||||
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
||||||
@@ -129,14 +142,20 @@ public class AppCompatibility extends InstrumentationTestCase {
|
|||||||
Intent intent = mPackageManager.getLaunchIntentForPackage(packageName);
|
Intent intent = mPackageManager.getLaunchIntentForPackage(packageName);
|
||||||
// Skip if the apk does not have a launch intent.
|
// Skip if the apk does not have a launch intent.
|
||||||
if (intent == null) {
|
if (intent == null) {
|
||||||
|
Log.d(TAG, "Skipping " + packageName + "; missing launch intent");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We check for any Crash or ANR dialogs that are already up, and we ignore them. This is
|
// We check for any Crash or ANR dialogs that are already up, and we
|
||||||
// so that we don't report crashes that were caused by prior apps (which those particular
|
// ignore them. This is
|
||||||
// tests should have caught and reported already). Otherwise, test failures would cascade
|
// so that we don't report crashes that were caused by prior apps (which
|
||||||
// from the initial broken app to many/all of the tests following that app's launch.
|
// those particular
|
||||||
final Collection<ProcessErrorStateInfo> preErr = mActivityManager.getProcessesInErrorState();
|
// tests should have caught and reported already). Otherwise, test
|
||||||
|
// failures would cascade
|
||||||
|
// from the initial broken app to many/all of the tests following that
|
||||||
|
// app's launch.
|
||||||
|
final Collection<ProcessErrorStateInfo> preErr =
|
||||||
|
mActivityManager.getProcessesInErrorState();
|
||||||
|
|
||||||
// Launch Activity
|
// Launch Activity
|
||||||
mContext.startActivity(intent);
|
mContext.startActivity(intent);
|
||||||
@@ -155,15 +174,43 @@ public class AppCompatibility extends InstrumentationTestCase {
|
|||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
// See if there are any errors. We wait until down here to give ANRs as much time as
|
// See if there are any errors. We wait until down here to give ANRs as
|
||||||
|
// much time as
|
||||||
// possible to occur.
|
// possible to occur.
|
||||||
final Collection<ProcessErrorStateInfo> postErr =
|
final Collection<ProcessErrorStateInfo> postErr =
|
||||||
mActivityManager.getProcessesInErrorState();
|
mActivityManager.getProcessesInErrorState();
|
||||||
// Take the difference between the error processes we see now, and the ones that were
|
// Take the difference between the error processes we see now, and the
|
||||||
|
// ones that were
|
||||||
// present when we started
|
// present when we started
|
||||||
if (preErr != null && postErr != null) {
|
if (preErr != null && postErr != null) {
|
||||||
postErr.removeAll(preErr);
|
postErr.removeAll(preErr);
|
||||||
}
|
}
|
||||||
return postErr;
|
return postErr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a given package is still running.
|
||||||
|
*
|
||||||
|
* @param packageName {@link String} package to look for
|
||||||
|
* @return True if package is running, false otherwise.
|
||||||
|
*/
|
||||||
|
private boolean processStillUp(String packageName) {
|
||||||
|
try {
|
||||||
|
PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, 0);
|
||||||
|
String processName = packageInfo.applicationInfo.processName;
|
||||||
|
List<RunningAppProcessInfo> runningApps = mActivityManager.getRunningAppProcesses();
|
||||||
|
for (RunningAppProcessInfo app : runningApps) {
|
||||||
|
if (app.processName.equalsIgnoreCase(processName)) {
|
||||||
|
Log.d(TAG, "Found process " + app.processName);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log.d(TAG, "Failed to find process " + processName + " with package name "
|
||||||
|
+ packageName);
|
||||||
|
} catch (NameNotFoundException e) {
|
||||||
|
Log.w(TAG, "Failed to find package " + packageName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user