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 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 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 77 public void testSetUpConditions() throws Exception { 78 assertNotNull(mActivityManager); 79 assertNotNull(mPackageManager); 80 } 81 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 */ 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 */ 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 */ 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 if (intent == null) { 158 Log.i(TAG, String.format("Activity %s/%s is disabled, skipping", 159 app.activityInfo.packageName, app.activityInfo.name)); 160 return Collections.EMPTY_LIST; 161 } 162 getContext().startActivity(intent); 163 try { 164 Thread.sleep(appLaunchWait); 165 } catch (InterruptedException e) { 166 // ignore 167 } 168 169 // Send the "home" intent and wait 2 seconds for us to get there 170 getContext().startActivity(mHomeIntent); 171 try { 172 Thread.sleep(homeLaunchWait); 173 } catch (InterruptedException e) { 174 // ignore 175 } 176 177 // See if there are any errors. We wait until down here to give ANRs as much time as 178 // possible to occur. 179 final Collection<ProcessError> errProcs = 180 ProcessError.fromCollection(mActivityManager.getProcessesInErrorState()); 181 182 // Distinguish the asynchronous crashes/ANRs from the synchronous ones by checking the 183 // crash package name against the package name for {@code app} 184 if (errProcs != null) { 185 Iterator<ProcessError> errIter = errProcs.iterator(); 186 while (errIter.hasNext()) { 187 ProcessError err = errIter.next(); 188 if (!packageMatches(app, err)) { 189 // async! Drop into mAsyncErrors and don't report now 190 mAsyncErrors.add(err); 191 errIter.remove(); 192 } 193 } 194 } 195 // Take the difference between the remaining current error processes and the ones that were 196 // present when we started. The result is guaranteed to be: 197 // 1) Errors that are pertinent to this app's package 198 // 2) Errors that are pertinent to this particular app invocation 199 if (errProcs != null && preErrProcs != null) { 200 errProcs.removeAll(preErrProcs); 201 } 202 203 return errProcs; 204 } 205 206 private String checkForProcessErrors() throws Exception { 207 List<ProcessErrorStateInfo> errList; 208 errList = mActivityManager.getProcessesInErrorState(); 209 210 // note: this contains information about each process that is currently in an error 211 // condition. if the list is empty (null) then "we're good". 212 213 // if the list is non-empty, then it's useful to report the contents of the list 214 final String reportMsg = reportListContents(errList); 215 return reportMsg; 216 } 217 218 /** 219 * A helper function that checks whether the specified error could have been caused by the 220 * specified app. 221 * 222 * @param app The app to check against 223 * @param err The error that we're considering 224 */ 225 private static boolean packageMatches(ResolveInfo app, ProcessError err) { 226 final String appPkg = app.activityInfo.packageName; 227 final String errPkg = err.info.processName; 228 Log.d(TAG, String.format("packageMatches(%s, %s)", appPkg, errPkg)); 229 return appPkg.equals(errPkg); 230 } 231 232 /** 233 * A helper function to query the provided {@link PackageManager} for a list of Activities that 234 * can be launched from Launcher. 235 */ 236 static List<ResolveInfo> getLauncherActivities(PackageManager pm) { 237 final Intent launchable = new Intent(Intent.ACTION_MAIN); 238 launchable.addCategory(Intent.CATEGORY_LAUNCHER); 239 final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0); 240 return activities; 241 } 242 243 /** 244 * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying 245 * an activity to be launched. 246 * 247 * @return the {@link Intent} or <code>null</code> if given app is disabled 248 */ 249 Intent intentForActivity(ResolveInfo app) { 250 final ComponentName component = new ComponentName(app.activityInfo.packageName, 251 app.activityInfo.name); 252 if (getContext().getPackageManager().getComponentEnabledSetting(component) == 253 PackageManager.COMPONENT_ENABLED_STATE_DISABLED) { 254 return null; 255 } 256 final Intent intent = new Intent(Intent.ACTION_MAIN); 257 intent.setComponent(component); 258 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 259 intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 260 return intent; 261 } 262 263 /** 264 * Report error reports for {@link ProcessErrorStateInfo} instances that are wrapped inside of 265 * {@link ProcessError} instances. Just unwraps and calls 266 * {@see reportListContents(Collection<ProcessErrorStateInfo>)}. 267 */ 268 static String reportWrappedListContents(Collection<ProcessError> errList) { 269 List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size()); 270 for (ProcessError err : errList) { 271 newList.add(err.info); 272 } 273 return reportListContents(newList); 274 } 275 276 /** 277 * This helper function will dump the actual error reports. 278 * 279 * @param errList The error report containing one or more error records. 280 * @return Returns a string containing all of the errors. 281 */ 282 private static String reportListContents(Collection<ProcessErrorStateInfo> errList) { 283 if (errList == null) return null; 284 285 StringBuilder builder = new StringBuilder(); 286 287 Iterator<ProcessErrorStateInfo> iter = errList.iterator(); 288 while (iter.hasNext()) { 289 ProcessErrorStateInfo entry = iter.next(); 290 291 String condition; 292 switch (entry.condition) { 293 case ActivityManager.ProcessErrorStateInfo.CRASHED: 294 condition = "a CRASH"; 295 break; 296 case ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING: 297 condition = "an ANR"; 298 break; 299 default: 300 condition = "an unknown error"; 301 break; 302 } 303 304 builder.append(String.format("Process %s encountered %s (%s)", entry.processName, 305 condition, entry.shortMsg)); 306 if (entry.condition == ActivityManager.ProcessErrorStateInfo.CRASHED) { 307 builder.append(String.format(" with stack trace:\n%s\n", entry.stackTrace)); 308 } 309 builder.append("\n"); 310 } 311 return builder.toString(); 312 } 313 314 /** 315 * A {@link ProcessErrorStateInfo} wrapper class that hashes how we want (so that equivalent 316 * crashes are considered equal). 317 */ 318 static class ProcessError { 319 public final ProcessErrorStateInfo info; 320 321 public ProcessError(ProcessErrorStateInfo newInfo) { 322 info = newInfo; 323 } 324 325 public static Collection<ProcessError> fromCollection(Collection<ProcessErrorStateInfo> in) 326 { 327 if (in == null) { 328 return null; 329 } 330 331 List<ProcessError> out = new ArrayList<ProcessError>(in.size()); 332 for (ProcessErrorStateInfo info : in) { 333 out.add(new ProcessError(info)); 334 } 335 return out; 336 } 337 338 private boolean strEquals(String a, String b) { 339 if ((a == null) && (b == null)) { 340 return true; 341 } else if ((a == null) || (b == null)) { 342 return false; 343 } else { 344 return a.equals(b); 345 } 346 } 347 348 @Override 349 public boolean equals(Object other) { 350 if (other == null) return false; 351 if (!(other instanceof ProcessError)) return false; 352 ProcessError peOther = (ProcessError) other; 353 354 return (info.condition == peOther.info.condition) 355 && strEquals(info.longMsg, peOther.info.longMsg) 356 && (info.pid == peOther.info.pid) 357 && strEquals(info.processName, peOther.info.processName) 358 && strEquals(info.shortMsg, peOther.info.shortMsg) 359 && strEquals(info.stackTrace, peOther.info.stackTrace) 360 && strEquals(info.tag, peOther.info.tag) 361 && (info.uid == peOther.info.uid); 362 } 363 364 private int hash(Object obj) { 365 if (obj == null) { 366 return 13; 367 } else { 368 return obj.hashCode(); 369 } 370 } 371 372 @Override 373 public int hashCode() { 374 int code = 17; 375 code += info.condition; 376 code *= hash(info.longMsg); 377 code += info.pid; 378 code *= hash(info.processName); 379 code *= hash(info.shortMsg); 380 code *= hash(info.stackTrace); 381 code *= hash(info.tag); 382 code += info.uid; 383 return code; 384 } 385 } 386 } 387