ProcessErrorsTest.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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 17package com.android.smoketest; 18 19import com.android.internal.os.RuntimeInit; 20 21import android.app.ActivityManager; 22import android.content.Context; 23import android.server.data.CrashData; 24import android.test.AndroidTestCase; 25import android.util.Log; 26 27import java.util.Iterator; 28import java.util.List; 29 30/** 31 * This smoke test is designed to quickly sniff for any error conditions 32 * encountered after initial startup. 33 */ 34public class ProcessErrorsTest extends AndroidTestCase { 35 36 private final String TAG = "ProcessErrorsTest"; 37 38 protected ActivityManager mActivityManager; 39 40 @Override 41 public void setUp() throws Exception { 42 super.setUp(); 43 mActivityManager = (ActivityManager) 44 getContext().getSystemService(Context.ACTIVITY_SERVICE); 45 } 46 47 public void testSetUpConditions() throws Exception { 48 assertNotNull(mActivityManager); 49 } 50 51 public void testNoProcessErrors() throws Exception { 52 List<ActivityManager.ProcessErrorStateInfo> errList; 53 errList = mActivityManager.getProcessesInErrorState(); 54 55 // note: this contains information about each process that is currently in an error 56 // condition. if the list is empty (null) then "we're good". 57 58 // if the list is non-empty, then it's useful to report the contents of the list 59 // we'll put a copy in the log, and we'll report it back to the framework via the assert. 60 final String reportMsg = reportListContents(errList); 61 if (reportMsg != null) { 62 Log.w(TAG, reportMsg); 63 } 64 65 // report a non-empty list back to the test framework 66 assertNull(reportMsg, errList); 67 } 68 69 /** 70 * This helper function will dump the actual error reports. 71 * 72 * @param errList The error report containing one or more error records. 73 * @return Returns a string containing all of the errors. 74 */ 75 private String reportListContents(List<ActivityManager.ProcessErrorStateInfo> errList) { 76 if (errList == null) return null; 77 78 StringBuilder builder = new StringBuilder(); 79 80 Iterator<ActivityManager.ProcessErrorStateInfo> iter = errList.iterator(); 81 while (iter.hasNext()) { 82 ActivityManager.ProcessErrorStateInfo entry = iter.next(); 83 84 String condition; 85 switch (entry.condition) { 86 case ActivityManager.ProcessErrorStateInfo.CRASHED: 87 condition = "CRASHED"; 88 break; 89 case ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING: 90 condition = "ANR"; 91 break; 92 default: 93 condition = "<unknown>"; 94 break; 95 } 96 97 String stackTrace = null; 98 try { 99 if (entry.crashData != null) { 100 CrashData cd = RuntimeInit.unmarshallException(entry.crashData); 101 stackTrace = cd.toString(); 102 } 103 } catch (RuntimeException e) { } 104 if (stackTrace == null) { 105 stackTrace = "<no stack trace>"; 106 } 107 108 final String entryReport = "Process error " + condition + " " + entry.shortMsg + 109 " detected in " + entry.processName + " " + entry.tag + 110 ". \n" + stackTrace; 111 112 builder.append(entryReport).append(" "); 113 } 114 return builder.toString(); 115 } 116 117} 118