Loading tests/SmokeTest/tests/src/com/android/smoketest/ProcessErrorsTest.java +123 −53 Original line number Original line Diff line number Diff line Loading @@ -28,14 +28,15 @@ import android.util.Log; import java.util.ArrayList; import java.util.ArrayList; import java.util.Collection; import java.util.Collection; import java.util.HashSet; import java.util.Collections; import java.util.Iterator; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.List; import java.util.Set; import java.util.Set; /** /** * This smoke test is designed to quickly sniff for any error conditions * This smoke test is designed to check for crashes and ANRs in an attempt to quickly determine if * encountered after initial startup. * all minimal functionality in the build is working properly. */ */ public class ProcessErrorsTest extends AndroidTestCase { public class ProcessErrorsTest extends AndroidTestCase { Loading @@ -46,15 +47,28 @@ public class ProcessErrorsTest extends AndroidTestCase { protected ActivityManager mActivityManager; protected ActivityManager mActivityManager; protected PackageManager mPackageManager; protected PackageManager mPackageManager; /** * Used to buffer asynchronously-caused crashes and ANRs so that we can have a big fail-party * in the catch-all testCase. */ private static final Collection<ProcessError> mAsyncErrors = Collections.synchronizedSet(new LinkedHashSet<ProcessError>()); public ProcessErrorsTest() { public ProcessErrorsTest() { mHomeIntent = new Intent(Intent.ACTION_MAIN); mHomeIntent = new Intent(Intent.ACTION_MAIN); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } } /** * {@inheritDoc} */ @Override @Override public void setUp() throws Exception { public void setUp() throws Exception { super.setUp(); super.setUp(); // First, make sure we have a Context assertNotNull("getContext() returned null!", getContext()); mActivityManager = (ActivityManager) mActivityManager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); getContext().getSystemService(Context.ACTIVITY_SERVICE); mPackageManager = getContext().getPackageManager(); mPackageManager = getContext().getPackageManager(); Loading @@ -75,43 +89,48 @@ public class ProcessErrorsTest extends AndroidTestCase { assertNull(reportMsg, reportMsg); assertNull(reportMsg, reportMsg); } } private String checkForProcessErrors() throws Exception { /** List<ProcessErrorStateInfo> errList; * A test that runs all Launcher-launchable activities and verifies that no ANRs or crashes errList = mActivityManager.getProcessesInErrorState(); * happened while doing so. */ // note: this contains information about each process that is currently in an error public void testRunAllActivities() throws Exception { // condition. if the list is empty (null) then "we're good". final Set<ProcessError> errSet = new LinkedHashSet<ProcessError>(); // if the list is non-empty, then it's useful to report the contents of the list for (ResolveInfo app : getLauncherActivities(mPackageManager)) { final String reportMsg = reportListContents(errList); final Collection<ProcessError> errProcs = runOneActivity(app); return reportMsg; if (errProcs != null) { errSet.addAll(errProcs); } } } /** if (!errSet.isEmpty()) { * A helper function to query the provided {@link PackageManager} for a list of Activities that fail(String.format("Got %d errors:\n%s", errSet.size(), * can be launched from Launcher. reportWrappedListContents(errSet))); */ } static List<ResolveInfo> getLauncherActivities(PackageManager pm) { final Intent launchable = new Intent(Intent.ACTION_MAIN); launchable.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0); return activities; } } /** /** * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying * This test checks for asynchronously-caused errors (crashes or ANRs) and fails in case any * an activity to be launched. * were found. This prevents us from needing to fail unrelated testcases when, for instance * a background thread causes a crash or ANR. * <p /> * Because this behavior depends on the contents of static member {@link mAsyncErrors}, we clear * that state here as a side-effect so that if two successive runs happen in the same process, * the asynchronous errors in the second test run won't include errors produced during the first * run. */ */ static Intent intentForActivity(ResolveInfo app) { public void testZZReportAsyncErrors() throws Exception { // build an Intent to launch the specified app try { final ComponentName component = new ComponentName(app.activityInfo.packageName, if (!mAsyncErrors.isEmpty()) { app.activityInfo.name); fail(String.format("Got %d asynchronous errors:\n%s", mAsyncErrors.size(), final Intent intent = new Intent(Intent.ACTION_MAIN); reportWrappedListContents(mAsyncErrors))); intent.setComponent(component); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } finally { intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); // Reset state just in case we should get another set of runs in the same process return intent; mAsyncErrors.clear(); } } } /** /** * A method to run the specified Activity and return a {@link Collection} of the Activities that * A method to run the specified Activity and return a {@link Collection} of the Activities that Loading @@ -129,8 +148,7 @@ public class ProcessErrorsTest extends AndroidTestCase { // 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 ignore them. This is // so that we don't report crashes that were caused by prior apps (which those particular // so that we don't report crashes that were caused by prior apps (which those particular // tests should have caught and reported already). Otherwise, test failures would cascade // tests should have caught and reported already). // from the initial broken app to many/all of the tests following that app's launch. final Collection<ProcessError> preErrProcs = final Collection<ProcessError> preErrProcs = ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); Loading @@ -155,8 +173,24 @@ public class ProcessErrorsTest extends AndroidTestCase { // possible to occur. // possible to occur. final Collection<ProcessError> errProcs = final Collection<ProcessError> errProcs = ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); // Take the difference between the error processes we see now, and the ones that were // present when we started // Distinguish the asynchronous crashes/ANRs from the synchronous ones by checking the // crash package name against the package name for {@code app} if (errProcs != null) { Iterator<ProcessError> errIter = errProcs.iterator(); while (errIter.hasNext()) { ProcessError err = errIter.next(); if (!packageMatches(app, err)) { // async! Drop into mAsyncErrors and don't report now mAsyncErrors.add(err); errIter.remove(); } } } // Take the difference between the remaining current error processes and the ones that were // present when we started. The result is guaranteed to be: // 1) Errors that are pertinent to this app's package // 2) Errors that are pertinent to this particular app invocation if (errProcs != null && preErrProcs != null) { if (errProcs != null && preErrProcs != null) { errProcs.removeAll(preErrProcs); errProcs.removeAll(preErrProcs); } } Loading @@ -164,27 +198,63 @@ public class ProcessErrorsTest extends AndroidTestCase { return errProcs; return errProcs; } } /** private String checkForProcessErrors() throws Exception { * A test that runs all Launcher-launchable activities and verifies that no ANRs or crashes List<ProcessErrorStateInfo> errList; * happened while doing so. errList = mActivityManager.getProcessesInErrorState(); */ public void testRunAllActivities() throws Exception { final Set<ProcessError> errSet = new HashSet<ProcessError>(); for (ResolveInfo app : getLauncherActivities(mPackageManager)) { // note: this contains information about each process that is currently in an error final Collection<ProcessError> errProcs = runOneActivity(app); // condition. if the list is empty (null) then "we're good". if (errProcs != null) { errSet.addAll(errProcs); // if the list is non-empty, then it's useful to report the contents of the list final String reportMsg = reportListContents(errList); return reportMsg; } } /** * A helper function that checks whether the specified error could have been caused by the * specified app. * * @param app The app to check against * @param err The error that we're considering */ private static boolean packageMatches(ResolveInfo app, ProcessError err) { final String appPkg = app.activityInfo.packageName; final String errPkg = err.info.processName; Log.d(TAG, String.format("packageMatches(%s, %s)", appPkg, errPkg)); return appPkg.equals(errPkg); } } if (!errSet.isEmpty()) { /** fail(String.format("Got %d errors:\n%s", errSet.size(), * A helper function to query the provided {@link PackageManager} for a list of Activities that reportWrappedListContents(errSet))); * can be launched from Launcher. */ static List<ResolveInfo> getLauncherActivities(PackageManager pm) { final Intent launchable = new Intent(Intent.ACTION_MAIN); launchable.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0); return activities; } } /** * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying * an activity to be launched. */ static Intent intentForActivity(ResolveInfo app) { final ComponentName component = new ComponentName(app.activityInfo.packageName, app.activityInfo.name); final Intent intent = new Intent(Intent.ACTION_MAIN); intent.setComponent(component); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); return intent; } } String reportWrappedListContents(Collection<ProcessError> errList) { /** * Report error reports for {@link ProcessErrorStateInfo} instances that are wrapped inside of * {@link ProcessError} instances. Just unwraps and calls * {@see reportListContents(Collection<ProcessErrorStateInfo>)}. */ static String reportWrappedListContents(Collection<ProcessError> errList) { List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size()); List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size()); for (ProcessError err : errList) { for (ProcessError err : errList) { newList.add(err.info); newList.add(err.info); Loading @@ -198,7 +268,7 @@ public class ProcessErrorsTest extends AndroidTestCase { * @param errList The error report containing one or more error records. * @param errList The error report containing one or more error records. * @return Returns a string containing all of the errors. * @return Returns a string containing all of the errors. */ */ private String reportListContents(Collection<ProcessErrorStateInfo> errList) { private static String reportListContents(Collection<ProcessErrorStateInfo> errList) { if (errList == null) return null; if (errList == null) return null; StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder(); Loading tests/SmokeTest/tests/src/com/android/smoketest/SmokeTestRunner.java +9 −3 Original line number Original line Diff line number Diff line Loading @@ -47,9 +47,6 @@ public class SmokeTestRunner extends InstrumentationTestRunner { final PackageManager pm = getTargetContext().getPackageManager(); final PackageManager pm = getTargetContext().getPackageManager(); final List<ResolveInfo> apps = ProcessErrorsTest.getLauncherActivities(pm); final List<ResolveInfo> apps = ProcessErrorsTest.getLauncherActivities(pm); // FIXME: figure out some way to control the reported class names for these anonymous // FIXME: class instances. final TestCase setupTest = new ProcessErrorsTest() { final TestCase setupTest = new ProcessErrorsTest() { @Override @Override public void runTest() throws Exception { public void runTest() throws Exception { Loading Loading @@ -88,6 +85,15 @@ public class SmokeTestRunner extends InstrumentationTestRunner { suite.addTest(appTest); suite.addTest(appTest); } } final TestCase asyncErrorTest = new ProcessErrorsTest() { @Override public void runTest() throws Exception { testZZReportAsyncErrors(); } }; asyncErrorTest.setName("testAsynchronousErrors"); suite.addTest(asyncErrorTest); return suite; return suite; } } } } Loading Loading
tests/SmokeTest/tests/src/com/android/smoketest/ProcessErrorsTest.java +123 −53 Original line number Original line Diff line number Diff line Loading @@ -28,14 +28,15 @@ import android.util.Log; import java.util.ArrayList; import java.util.ArrayList; import java.util.Collection; import java.util.Collection; import java.util.HashSet; import java.util.Collections; import java.util.Iterator; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.List; import java.util.Set; import java.util.Set; /** /** * This smoke test is designed to quickly sniff for any error conditions * This smoke test is designed to check for crashes and ANRs in an attempt to quickly determine if * encountered after initial startup. * all minimal functionality in the build is working properly. */ */ public class ProcessErrorsTest extends AndroidTestCase { public class ProcessErrorsTest extends AndroidTestCase { Loading @@ -46,15 +47,28 @@ public class ProcessErrorsTest extends AndroidTestCase { protected ActivityManager mActivityManager; protected ActivityManager mActivityManager; protected PackageManager mPackageManager; protected PackageManager mPackageManager; /** * Used to buffer asynchronously-caused crashes and ANRs so that we can have a big fail-party * in the catch-all testCase. */ private static final Collection<ProcessError> mAsyncErrors = Collections.synchronizedSet(new LinkedHashSet<ProcessError>()); public ProcessErrorsTest() { public ProcessErrorsTest() { mHomeIntent = new Intent(Intent.ACTION_MAIN); mHomeIntent = new Intent(Intent.ACTION_MAIN); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } } /** * {@inheritDoc} */ @Override @Override public void setUp() throws Exception { public void setUp() throws Exception { super.setUp(); super.setUp(); // First, make sure we have a Context assertNotNull("getContext() returned null!", getContext()); mActivityManager = (ActivityManager) mActivityManager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); getContext().getSystemService(Context.ACTIVITY_SERVICE); mPackageManager = getContext().getPackageManager(); mPackageManager = getContext().getPackageManager(); Loading @@ -75,43 +89,48 @@ public class ProcessErrorsTest extends AndroidTestCase { assertNull(reportMsg, reportMsg); assertNull(reportMsg, reportMsg); } } private String checkForProcessErrors() throws Exception { /** List<ProcessErrorStateInfo> errList; * A test that runs all Launcher-launchable activities and verifies that no ANRs or crashes errList = mActivityManager.getProcessesInErrorState(); * happened while doing so. */ // note: this contains information about each process that is currently in an error public void testRunAllActivities() throws Exception { // condition. if the list is empty (null) then "we're good". final Set<ProcessError> errSet = new LinkedHashSet<ProcessError>(); // if the list is non-empty, then it's useful to report the contents of the list for (ResolveInfo app : getLauncherActivities(mPackageManager)) { final String reportMsg = reportListContents(errList); final Collection<ProcessError> errProcs = runOneActivity(app); return reportMsg; if (errProcs != null) { errSet.addAll(errProcs); } } } /** if (!errSet.isEmpty()) { * A helper function to query the provided {@link PackageManager} for a list of Activities that fail(String.format("Got %d errors:\n%s", errSet.size(), * can be launched from Launcher. reportWrappedListContents(errSet))); */ } static List<ResolveInfo> getLauncherActivities(PackageManager pm) { final Intent launchable = new Intent(Intent.ACTION_MAIN); launchable.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0); return activities; } } /** /** * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying * This test checks for asynchronously-caused errors (crashes or ANRs) and fails in case any * an activity to be launched. * were found. This prevents us from needing to fail unrelated testcases when, for instance * a background thread causes a crash or ANR. * <p /> * Because this behavior depends on the contents of static member {@link mAsyncErrors}, we clear * that state here as a side-effect so that if two successive runs happen in the same process, * the asynchronous errors in the second test run won't include errors produced during the first * run. */ */ static Intent intentForActivity(ResolveInfo app) { public void testZZReportAsyncErrors() throws Exception { // build an Intent to launch the specified app try { final ComponentName component = new ComponentName(app.activityInfo.packageName, if (!mAsyncErrors.isEmpty()) { app.activityInfo.name); fail(String.format("Got %d asynchronous errors:\n%s", mAsyncErrors.size(), final Intent intent = new Intent(Intent.ACTION_MAIN); reportWrappedListContents(mAsyncErrors))); intent.setComponent(component); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } finally { intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); // Reset state just in case we should get another set of runs in the same process return intent; mAsyncErrors.clear(); } } } /** /** * A method to run the specified Activity and return a {@link Collection} of the Activities that * A method to run the specified Activity and return a {@link Collection} of the Activities that Loading @@ -129,8 +148,7 @@ public class ProcessErrorsTest extends AndroidTestCase { // 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 ignore them. This is // so that we don't report crashes that were caused by prior apps (which those particular // so that we don't report crashes that were caused by prior apps (which those particular // tests should have caught and reported already). Otherwise, test failures would cascade // tests should have caught and reported already). // from the initial broken app to many/all of the tests following that app's launch. final Collection<ProcessError> preErrProcs = final Collection<ProcessError> preErrProcs = ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); Loading @@ -155,8 +173,24 @@ public class ProcessErrorsTest extends AndroidTestCase { // possible to occur. // possible to occur. final Collection<ProcessError> errProcs = final Collection<ProcessError> errProcs = ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); // Take the difference between the error processes we see now, and the ones that were // present when we started // Distinguish the asynchronous crashes/ANRs from the synchronous ones by checking the // crash package name against the package name for {@code app} if (errProcs != null) { Iterator<ProcessError> errIter = errProcs.iterator(); while (errIter.hasNext()) { ProcessError err = errIter.next(); if (!packageMatches(app, err)) { // async! Drop into mAsyncErrors and don't report now mAsyncErrors.add(err); errIter.remove(); } } } // Take the difference between the remaining current error processes and the ones that were // present when we started. The result is guaranteed to be: // 1) Errors that are pertinent to this app's package // 2) Errors that are pertinent to this particular app invocation if (errProcs != null && preErrProcs != null) { if (errProcs != null && preErrProcs != null) { errProcs.removeAll(preErrProcs); errProcs.removeAll(preErrProcs); } } Loading @@ -164,27 +198,63 @@ public class ProcessErrorsTest extends AndroidTestCase { return errProcs; return errProcs; } } /** private String checkForProcessErrors() throws Exception { * A test that runs all Launcher-launchable activities and verifies that no ANRs or crashes List<ProcessErrorStateInfo> errList; * happened while doing so. errList = mActivityManager.getProcessesInErrorState(); */ public void testRunAllActivities() throws Exception { final Set<ProcessError> errSet = new HashSet<ProcessError>(); for (ResolveInfo app : getLauncherActivities(mPackageManager)) { // note: this contains information about each process that is currently in an error final Collection<ProcessError> errProcs = runOneActivity(app); // condition. if the list is empty (null) then "we're good". if (errProcs != null) { errSet.addAll(errProcs); // if the list is non-empty, then it's useful to report the contents of the list final String reportMsg = reportListContents(errList); return reportMsg; } } /** * A helper function that checks whether the specified error could have been caused by the * specified app. * * @param app The app to check against * @param err The error that we're considering */ private static boolean packageMatches(ResolveInfo app, ProcessError err) { final String appPkg = app.activityInfo.packageName; final String errPkg = err.info.processName; Log.d(TAG, String.format("packageMatches(%s, %s)", appPkg, errPkg)); return appPkg.equals(errPkg); } } if (!errSet.isEmpty()) { /** fail(String.format("Got %d errors:\n%s", errSet.size(), * A helper function to query the provided {@link PackageManager} for a list of Activities that reportWrappedListContents(errSet))); * can be launched from Launcher. */ static List<ResolveInfo> getLauncherActivities(PackageManager pm) { final Intent launchable = new Intent(Intent.ACTION_MAIN); launchable.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0); return activities; } } /** * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying * an activity to be launched. */ static Intent intentForActivity(ResolveInfo app) { final ComponentName component = new ComponentName(app.activityInfo.packageName, app.activityInfo.name); final Intent intent = new Intent(Intent.ACTION_MAIN); intent.setComponent(component); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); return intent; } } String reportWrappedListContents(Collection<ProcessError> errList) { /** * Report error reports for {@link ProcessErrorStateInfo} instances that are wrapped inside of * {@link ProcessError} instances. Just unwraps and calls * {@see reportListContents(Collection<ProcessErrorStateInfo>)}. */ static String reportWrappedListContents(Collection<ProcessError> errList) { List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size()); List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size()); for (ProcessError err : errList) { for (ProcessError err : errList) { newList.add(err.info); newList.add(err.info); Loading @@ -198,7 +268,7 @@ public class ProcessErrorsTest extends AndroidTestCase { * @param errList The error report containing one or more error records. * @param errList The error report containing one or more error records. * @return Returns a string containing all of the errors. * @return Returns a string containing all of the errors. */ */ private String reportListContents(Collection<ProcessErrorStateInfo> errList) { private static String reportListContents(Collection<ProcessErrorStateInfo> errList) { if (errList == null) return null; if (errList == null) return null; StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder(); Loading
tests/SmokeTest/tests/src/com/android/smoketest/SmokeTestRunner.java +9 −3 Original line number Original line Diff line number Diff line Loading @@ -47,9 +47,6 @@ public class SmokeTestRunner extends InstrumentationTestRunner { final PackageManager pm = getTargetContext().getPackageManager(); final PackageManager pm = getTargetContext().getPackageManager(); final List<ResolveInfo> apps = ProcessErrorsTest.getLauncherActivities(pm); final List<ResolveInfo> apps = ProcessErrorsTest.getLauncherActivities(pm); // FIXME: figure out some way to control the reported class names for these anonymous // FIXME: class instances. final TestCase setupTest = new ProcessErrorsTest() { final TestCase setupTest = new ProcessErrorsTest() { @Override @Override public void runTest() throws Exception { public void runTest() throws Exception { Loading Loading @@ -88,6 +85,15 @@ public class SmokeTestRunner extends InstrumentationTestRunner { suite.addTest(appTest); suite.addTest(appTest); } } final TestCase asyncErrorTest = new ProcessErrorsTest() { @Override public void runTest() throws Exception { testZZReportAsyncErrors(); } }; asyncErrorTest.setName("testAsynchronousErrors"); suite.addTest(asyncErrorTest); return suite; return suite; } } } } Loading