OSDN Git Service

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