1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.smoketest; 18 19 import android.app.ActivityManager; 20 import android.app.ActivityManager.ProcessErrorStateInfo; 21 import android.content.Context; 22 import android.content.ComponentName; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.test.AndroidTestCase; 27 import android.util.Log; 28 29 import java.util.ArrayList; 30 import java.util.Collection; 31 import java.util.Collections; 32 import java.util.Iterator; 33 import java.util.LinkedHashSet; 34 import java.util.List; 35 import java.util.Set; 36 37 /** 38 * This smoke test is designed to check for crashes and ANRs in an attempt to quickly determine if 39 * all minimal functionality in the build is working properly. 40 */ 41 public class ProcessErrorsTest extends AndroidTestCase { 42 43 private static final String TAG = "ProcessErrorsTest"; 44 45 private final Intent mHomeIntent; 46 47 protected ActivityManager mActivityManager; 48 protected PackageManager mPackageManager; 49 50 /** 51 * Used to buffer asynchronously-caused crashes and ANRs so that we can have a big fail-party 52 * in the catch-all testCase. 53 */ 54 private static final Collection<ProcessError> mAsyncErrors = 55 Collections.synchronizedSet(new LinkedHashSet<ProcessError>()); 56 ProcessErrorsTest()57 public ProcessErrorsTest() { 58 mHomeIntent = new Intent(Intent.ACTION_MAIN); 59 mHomeIntent.addCategory(Intent.CATEGORY_HOME); 60 mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 61 } 62 63 /** 64 * {@inheritDoc} 65 */ 66 @Override setUp()67 public void setUp() throws Exception { 68 super.setUp(); 69 // First, make sure we have a Context 70 assertNotNull("getContext() returned null!", getContext()); 71 72 mActivityManager = (ActivityManager) 73 getContext().getSystemService(Context.ACTIVITY_SERVICE); 74 mPackageManager = getContext().getPackageManager(); 75 } 76 testSetUpConditions()77 public void testSetUpConditions() throws Exception { 78 assertNotNull(mActivityManager); 79 assertNotNull(mPackageManager); 80 } 81 testNoProcessErrorsAfterBoot()82 public void testNoProcessErrorsAfterBoot() throws Exception { 83 final String reportMsg = checkForProcessErrors(); 84 if (reportMsg != null) { 85 Log.w(TAG, reportMsg); 86 } 87 88 // report a non-empty list back to the test framework 89 assertNull(reportMsg, reportMsg); 90 } 91 92 /** 93 * A test that runs all Launcher-launchable activities and verifies that no ANRs or crashes 94 * happened while doing so. 95 */ testRunAllActivities()96 public void testRunAllActivities() throws Exception { 97 final Set<ProcessError> errSet = new LinkedHashSet<ProcessError>(); 98 99 for (ResolveInfo app : getLauncherActivities(mPackageManager)) { 100 final Collection<ProcessError> errProcs = runOneActivity(app); 101 if (errProcs != null) { 102 errSet.addAll(errProcs); 103 } 104 } 105 106 if (!errSet.isEmpty()) { 107 fail(String.format("Got %d errors:\n%s", errSet.size(), 108 reportWrappedListContents(errSet))); 109 } 110 } 111 112 /** 113 * This test checks for asynchronously-caused errors (crashes or ANRs) and fails in case any 114 * were found. This prevents us from needing to fail unrelated testcases when, for instance 115 * a background thread causes a crash or ANR. 116 * <p /> 117 * Because this behavior depends on the contents of static member {@link mAsyncErrors}, we clear 118 * that state here as a side-effect so that if two successive runs happen in the same process, 119 * the asynchronous errors in the second test run won't include errors produced during the first 120 * run. 121 */ testZZReportAsyncErrors()122 public void testZZReportAsyncErrors() throws Exception { 123 try { 124 if (!mAsyncErrors.isEmpty()) { 125 fail(String.format("Got %d asynchronous errors:\n%s", mAsyncErrors.size(), 126 reportWrappedListContents(mAsyncErrors))); 127 } 128 } finally { 129 // Reset state just in case we should get another set of runs in the same process 130 mAsyncErrors.clear(); 131 } 132 } 133 134 135 /** 136 * A method to run the specified Activity and return a {@link Collection} of the Activities that 137 * were in an error state, as listed by {@link ActivityManager.getProcessesInErrorState()}. 138 * <p /> 139 * The method will launch the app, wait for 7 seconds, check for apps in the error state, send 140 * the Home intent, wait for 2 seconds, and then return. 141 */ runOneActivity(ResolveInfo app)142 public Collection<ProcessError> runOneActivity(ResolveInfo app) { 143 final long appLaunchWait = 7000; 144 final long homeLaunchWait = 2000; 145 146 Log.i(TAG, String.format("Running activity %s/%s", app.activityInfo.packageName, 147 app.activityInfo.name)); 148 149 // We check for any Crash or ANR dialogs that are already up, and we ignore them. This is 150 // so that we don't report crashes that were caused by prior apps (which those particular 151 // tests should have caught and reported already). 152 final Collection<ProcessError> preErrProcs = 153 ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); 154 155 // launch app, and wait 7 seconds for it to start/settle 156 final Intent intent = intentForActivity(app); 157 getContext().startActivity(intent); 158 try { 159 Thread.sleep(appLaunchWait); 160 } catch (InterruptedException e) { 161 // ignore 162 } 163 164 // Send the "home" intent and wait 2 seconds for us to get there 165 getContext().startActivity(mHomeIntent); 166 try { 167 Thread.sleep(homeLaunchWait); 168 } catch (InterruptedException e) { 169 // ignore 170 } 171 172 // See if there are any errors. We wait until down here to give ANRs as much time as 173 // possible to occur. 174 final Collection<ProcessError> errProcs = 175 ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); 176 177 // Distinguish the asynchronous crashes/ANRs from the synchronous ones by checking the 178 // crash package name against the package name for {@code app} 179 if (errProcs != null) { 180 Iterator<ProcessError> errIter = errProcs.iterator(); 181 while (errIter.hasNext()) { 182 ProcessError err = errIter.next(); 183 if (!packageMatches(app, err)) { 184 // async! Drop into mAsyncErrors and don't report now 185 mAsyncErrors.add(err); 186 errIter.remove(); 187 } 188 } 189 } 190 // Take the difference between the remaining current error processes and the ones that were 191 // present when we started. The result is guaranteed to be: 192 // 1) Errors that are pertinent to this app's package 193 // 2) Errors that are pertinent to this particular app invocation 194 if (errProcs != null && preErrProcs != null) { 195 errProcs.removeAll(preErrProcs); 196 } 197 198 return errProcs; 199 } 200 checkForProcessErrors()201 private String checkForProcessErrors() throws Exception { 202 List<ProcessErrorStateInfo> errList; 203 errList = mActivityManager.getProcessesInErrorState(); 204 205 // note: this contains information about each process that is currently in an error 206 // condition. if the list is empty (null) then "we're good". 207 208 // if the list is non-empty, then it's useful to report the contents of the list 209 final String reportMsg = reportListContents(errList); 210 return reportMsg; 211 } 212 213 /** 214 * A helper function that checks whether the specified error could have been caused by the 215 * specified app. 216 * 217 * @param app The app to check against 218 * @param err The error that we're considering 219 */ packageMatches(ResolveInfo app, ProcessError err)220 private static boolean packageMatches(ResolveInfo app, ProcessError err) { 221 final String appPkg = app.activityInfo.packageName; 222 final String errPkg = err.info.processName; 223 Log.d(TAG, String.format("packageMatches(%s, %s)", appPkg, errPkg)); 224 return appPkg.equals(errPkg); 225 } 226 227 /** 228 * A helper function to query the provided {@link PackageManager} for a list of Activities that 229 * can be launched from Launcher. 230 */ getLauncherActivities(PackageManager pm)231 static List<ResolveInfo> getLauncherActivities(PackageManager pm) { 232 final Intent launchable = new Intent(Intent.ACTION_MAIN); 233 launchable.addCategory(Intent.CATEGORY_LAUNCHER); 234 final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0); 235 return activities; 236 } 237 238 /** 239 * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying 240 * an activity to be launched. 241 */ intentForActivity(ResolveInfo app)242 static Intent intentForActivity(ResolveInfo app) { 243 final ComponentName component = new ComponentName(app.activityInfo.packageName, 244 app.activityInfo.name); 245 final Intent intent = new Intent(Intent.ACTION_MAIN); 246 intent.setComponent(component); 247 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 248 intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 249 return intent; 250 } 251 252 /** 253 * Report error reports for {@link ProcessErrorStateInfo} instances that are wrapped inside of 254 * {@link ProcessError} instances. Just unwraps and calls 255 * {@see reportListContents(Collection<ProcessErrorStateInfo>)}. 256 */ reportWrappedListContents(Collection<ProcessError> errList)257 static String reportWrappedListContents(Collection<ProcessError> errList) { 258 List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size()); 259 for (ProcessError err : errList) { 260 newList.add(err.info); 261 } 262 return reportListContents(newList); 263 } 264 265 /** 266 * This helper function will dump the actual error reports. 267 * 268 * @param errList The error report containing one or more error records. 269 * @return Returns a string containing all of the errors. 270 */ reportListContents(Collection<ProcessErrorStateInfo> errList)271 private static String reportListContents(Collection<ProcessErrorStateInfo> errList) { 272 if (errList == null) return null; 273 274 StringBuilder builder = new StringBuilder(); 275 276 Iterator<ProcessErrorStateInfo> iter = errList.iterator(); 277 while (iter.hasNext()) { 278 ProcessErrorStateInfo entry = iter.next(); 279 280 String condition; 281 switch (entry.condition) { 282 case ActivityManager.ProcessErrorStateInfo.CRASHED: 283 condition = "a CRASH"; 284 break; 285 case ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING: 286 condition = "an ANR"; 287 break; 288 default: 289 condition = "an unknown error"; 290 break; 291 } 292 293 builder.append(String.format("Process %s encountered %s (%s)", entry.processName, 294 condition, entry.shortMsg)); 295 if (entry.condition == ActivityManager.ProcessErrorStateInfo.CRASHED) { 296 builder.append(String.format(" with stack trace:\n%s\n", entry.stackTrace)); 297 } 298 builder.append("\n"); 299 } 300 return builder.toString(); 301 } 302 303 /** 304 * A {@link ProcessErrorStateInfo} wrapper class that hashes how we want (so that equivalent 305 * crashes are considered equal). 306 */ 307 static class ProcessError { 308 public final ProcessErrorStateInfo info; 309 ProcessError(ProcessErrorStateInfo newInfo)310 public ProcessError(ProcessErrorStateInfo newInfo) { 311 info = newInfo; 312 } 313 fromCollection(Collection<ProcessErrorStateInfo> in)314 public static Collection<ProcessError> fromCollection(Collection<ProcessErrorStateInfo> in) 315 { 316 if (in == null) { 317 return null; 318 } 319 320 List<ProcessError> out = new ArrayList<ProcessError>(in.size()); 321 for (ProcessErrorStateInfo info : in) { 322 out.add(new ProcessError(info)); 323 } 324 return out; 325 } 326 strEquals(String a, String b)327 private boolean strEquals(String a, String b) { 328 if ((a == null) && (b == null)) { 329 return true; 330 } else if ((a == null) || (b == null)) { 331 return false; 332 } else { 333 return a.equals(b); 334 } 335 } 336 337 @Override equals(Object other)338 public boolean equals(Object other) { 339 if (other == null) return false; 340 if (!(other instanceof ProcessError)) return false; 341 ProcessError peOther = (ProcessError) other; 342 343 return (info.condition == peOther.info.condition) 344 && strEquals(info.longMsg, peOther.info.longMsg) 345 && (info.pid == peOther.info.pid) 346 && strEquals(info.processName, peOther.info.processName) 347 && strEquals(info.shortMsg, peOther.info.shortMsg) 348 && strEquals(info.stackTrace, peOther.info.stackTrace) 349 && strEquals(info.tag, peOther.info.tag) 350 && (info.uid == peOther.info.uid); 351 } 352 hash(Object obj)353 private int hash(Object obj) { 354 if (obj == null) { 355 return 13; 356 } else { 357 return obj.hashCode(); 358 } 359 } 360 361 @Override hashCode()362 public int hashCode() { 363 int code = 17; 364 code += info.condition; 365 code *= hash(info.longMsg); 366 code += info.pid; 367 code *= hash(info.processName); 368 code *= hash(info.shortMsg); 369 code *= hash(info.stackTrace); 370 code *= hash(info.tag); 371 code += info.uid; 372 return code; 373 } 374 } 375 } 376