ActivityManagerServiceTest.java revision 88c4d1ce97f5fa3859dd1854d4a82305e68a2b0e
1d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala/*
2d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * Copyright (C) 2017 The Android Open Source Project
3d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala *
4d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
5d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * you may not use this file except in compliance with the License.
6d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * You may obtain a copy of the License at
7d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala *
8d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
9d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala *
10d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
11d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
12d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * See the License for the specific language governing permissions and
14d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala * limitations under the License
15d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala */
16d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala
17e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanpackage com.android.server.am;
18e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatraman
19d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport static android.app.ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
20e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static android.app.ActivityManager.PROCESS_STATE_CACHED_ACTIVITY;
21e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static android.app.ActivityManager.PROCESS_STATE_CACHED_EMPTY;
22d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
23e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static android.app.ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND;
24e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static android.app.ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
25d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport static android.app.ActivityManager.PROCESS_STATE_LAST_ACTIVITY;
26d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
27d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport static android.app.ActivityManager.PROCESS_STATE_RECEIVER;
28e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static android.app.ActivityManager.PROCESS_STATE_SERVICE;
29e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static android.app.ActivityManager.PROCESS_STATE_TOP;
30d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport static android.util.DebugUtils.valueToString;
31e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatraman
32d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport static org.junit.Assert.assertEquals;
33e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static org.junit.Assert.assertFalse;
34e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport static org.junit.Assert.assertTrue;
35d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala
36e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport android.app.ActivityManager;
37e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport android.support.test.filters.SmallTest;
38e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport android.support.test.runner.AndroidJUnit4;
39e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatraman
40e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport org.junit.Test;
41d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvalaimport org.junit.runner.RunWith;
42d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala
43e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatramanimport java.util.function.Function;
44d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala
45e2b43843fd12783188edd2c54188ea8d26864788Vijay Venkatraman/**
46 * Test class for {@link ActivityManagerService}.
47 *
48 * To run the tests, use
49 *
50 * runtest -c com.android.server.am.ActivityManagerServiceTest frameworks-services
51 *
52 * or the following steps:
53 *
54 * Build: m FrameworksServicesTests
55 * Install: adb install -r \
56 *     ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
57 * Run: adb shell am instrument -e class com.android.server.am.ActivityManagerServiceTest -w \
58 *     com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
59 */
60@SmallTest
61@RunWith(AndroidJUnit4.class)
62public class ActivityManagerServiceTest {
63    private static final int TEST_UID = 111;
64
65    @Test
66    public void testIncrementProcStateSeqIfNeeded() {
67        final ActivityManagerService ams = new ActivityManagerService();
68        final UidRecord uidRec = new UidRecord(TEST_UID);
69
70        assertEquals("Initially global seq counter should be 0", 0, ams.mProcStateSeqCounter);
71        assertEquals("Initially seq counter in uidRecord should be 0", 0, uidRec.curProcStateSeq);
72
73        // Uid state is not moving from background to foreground or vice versa.
74        uidRec.setProcState = PROCESS_STATE_TOP;
75        uidRec.curProcState = PROCESS_STATE_TOP;
76        ams.incrementProcStateSeqIfNeeded(uidRec);
77        assertEquals(0, ams.mProcStateSeqCounter);
78        assertEquals(0, uidRec.curProcStateSeq);
79
80        // Uid state is moving from foreground to background.
81        uidRec.curProcState = PROCESS_STATE_FOREGROUND_SERVICE;
82        uidRec.setProcState = PROCESS_STATE_SERVICE;
83        ams.incrementProcStateSeqIfNeeded(uidRec);
84        assertEquals(1, ams.mProcStateSeqCounter);
85        assertEquals(1, uidRec.curProcStateSeq);
86
87        // Explicitly setting the seq counter for more verification.
88        ams.mProcStateSeqCounter = 42;
89
90        // Uid state is not moving from background to foreground or vice versa.
91        uidRec.setProcState = PROCESS_STATE_IMPORTANT_BACKGROUND;
92        uidRec.curProcState = PROCESS_STATE_IMPORTANT_FOREGROUND;
93        ams.incrementProcStateSeqIfNeeded(uidRec);
94        assertEquals(42, ams.mProcStateSeqCounter);
95        assertEquals(1, uidRec.curProcStateSeq);
96
97        // Uid state is moving from background to foreground.
98        uidRec.setProcState = PROCESS_STATE_LAST_ACTIVITY;
99        uidRec.curProcState = PROCESS_STATE_TOP;
100        ams.incrementProcStateSeqIfNeeded(uidRec);
101        assertEquals(43, ams.mProcStateSeqCounter);
102        assertEquals(43, uidRec.curProcStateSeq);
103    }
104
105    @Test
106    public void testShouldIncrementProcStateSeq() {
107        final ActivityManagerService ams = new ActivityManagerService();
108        final UidRecord uidRec = new UidRecord(TEST_UID);
109
110        final String error1 = "Seq should be incremented: prevState: %s, curState: %s";
111        final String error2 = "Seq should not be incremented: prevState: %s, curState: %s";
112        Function<String, String> errorMsg = errorTemplate -> {
113            return String.format(errorTemplate,
114                    valueToString(ActivityManager.class, "PROCESS_STATE_", uidRec.setProcState),
115                    valueToString(ActivityManager.class, "PROCESS_STATE_", uidRec.curProcState));
116        };
117
118        // No change in uid state
119        uidRec.setProcState = PROCESS_STATE_RECEIVER;
120        uidRec.curProcState = PROCESS_STATE_RECEIVER;
121        assertFalse(errorMsg.apply(error2), ams.shouldIncrementProcStateSeq(uidRec));
122
123        // Foreground to foreground
124        uidRec.setProcState = PROCESS_STATE_FOREGROUND_SERVICE;
125        uidRec.curProcState = PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
126        assertFalse(errorMsg.apply(error2), ams.shouldIncrementProcStateSeq(uidRec));
127
128        // Background to background
129        uidRec.setProcState = PROCESS_STATE_CACHED_ACTIVITY;
130        uidRec.curProcState = PROCESS_STATE_CACHED_EMPTY;
131        assertFalse(errorMsg.apply(error2), ams.shouldIncrementProcStateSeq(uidRec));
132
133        // Background to background
134        uidRec.setProcState = PROCESS_STATE_NONEXISTENT;
135        uidRec.curProcState = PROCESS_STATE_CACHED_ACTIVITY;
136        assertFalse(errorMsg.apply(error2), ams.shouldIncrementProcStateSeq(uidRec));
137
138        // Background to foreground
139        uidRec.setProcState = PROCESS_STATE_SERVICE;
140        uidRec.curProcState = PROCESS_STATE_FOREGROUND_SERVICE;
141        assertTrue(errorMsg.apply(error1), ams.shouldIncrementProcStateSeq(uidRec));
142
143        // Foreground to background
144        uidRec.setProcState = PROCESS_STATE_TOP;
145        uidRec.curProcState = PROCESS_STATE_LAST_ACTIVITY;
146        assertTrue(errorMsg.apply(error1), ams.shouldIncrementProcStateSeq(uidRec));
147    }
148}