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 android.app.activity;
18
19import android.app.ActivityManager;
20import android.content.Context;
21import android.content.pm.ConfigurationInfo;
22import android.content.res.Configuration;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.test.suitebuilder.annotation.Suppress;
26
27import java.util.Iterator;
28import java.util.List;
29
30public class ActivityManagerTest extends AndroidTestCase {
31
32    protected Context mContext;
33    protected ActivityManager mActivityManager;
34
35    @Override
36    protected void setUp() throws Exception {
37        super.setUp();
38        mContext = getContext();
39        mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
40    }
41
42    // TODO should write a test for getRecentTasks()
43    // TODO should write a test for getRunningTasks()
44    // TODO should write a test for getMemoryInfo()
45
46    // TODO: Find a way to re-enable this.  It fails if any other app has failed during startup.
47    // This is probably an OK assumption given the desired system status when we run unit tests,
48    // but it's not necessarily the right assumption for a unit test.
49    @Suppress
50    public void disabledTestErrorTasksEmpty() throws Exception {
51
52        List<ActivityManager.ProcessErrorStateInfo> errList;
53
54        errList = mActivityManager.getProcessesInErrorState();
55
56        // test: confirm list is empty
57        assertNull(errList);
58    }
59
60    // TODO: Force an activity into an error state - then see if we can catch it here?
61    @SmallTest
62    public void testErrorTasksWithError() throws Exception {
63
64        List<ActivityManager.ProcessErrorStateInfo> errList;
65
66        // TODO force another process into an error condition.  How?
67
68        // test: confirm error list length is at least 1 under varying query lengths
69//      checkErrorListMax(1,-1);
70
71        errList = mActivityManager.getProcessesInErrorState();
72
73        // test: the list itself is healthy
74        checkErrorListSanity(errList);
75
76        // test: confirm our application shows up in the list
77    }
78
79    // TODO: Force an activity into an ANR state - then see if we can catch it here?
80    @SmallTest
81    public void testErrorTasksWithANR() throws Exception {
82
83        List<ActivityManager.ProcessErrorStateInfo> errList;
84
85        // TODO: force an application into an ANR state
86
87        errList = mActivityManager.getProcessesInErrorState();
88
89        // test: the list itself is healthy
90        checkErrorListSanity(errList);
91
92        // test: confirm our ANR'ing application shows up in the list
93    }
94
95    @SmallTest
96    public void testGetDeviceConfigurationInfo() throws Exception {
97        ConfigurationInfo config = mActivityManager.getDeviceConfigurationInfo();
98        assertNotNull(config);
99        // Validate values against configuration retrieved from resources
100        Configuration vconfig = mContext.getResources().getConfiguration();
101        assertNotNull(vconfig);
102        assertEquals(config.reqKeyboardType, vconfig.keyboard);
103        assertEquals(config.reqTouchScreen, vconfig.touchscreen);
104        assertEquals(config.reqNavigation, vconfig.navigation);
105        if (vconfig.navigation == Configuration.NAVIGATION_NONAV) {
106            assertNotNull(config.reqInputFeatures & ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV);
107        }
108        if (vconfig.keyboard != Configuration.KEYBOARD_UNDEFINED) {
109            assertNotNull(config.reqInputFeatures & ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD);
110        }
111    }
112
113    // If any entries in appear in the list, sanity check them against all running applications
114    private void checkErrorListSanity(List<ActivityManager.ProcessErrorStateInfo> errList) {
115        if (errList == null) return;
116
117        Iterator<ActivityManager.ProcessErrorStateInfo> iter = errList.iterator();
118        while (iter.hasNext()) {
119            ActivityManager.ProcessErrorStateInfo info = iter.next();
120            assertNotNull(info);
121            // sanity checks
122            assertTrue((info.condition == ActivityManager.ProcessErrorStateInfo.CRASHED) ||
123                       (info.condition == ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING));
124            // TODO look at each of these and consider a stronger test
125            // TODO can we cross-check at the process name via some other API?
126            // TODO is there a better test for strings, e.g. "assertIsLegalString")
127            assertNotNull(info.processName);
128            // reasonableness test for info.pid ?
129            assertNotNull(info.longMsg);
130            assertNotNull(info.shortMsg);
131            // is there any reasonable test for the crashData?  Probably not.
132        }
133    }
134}
135
136