Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.am; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | |
| 21 | import android.app.ActivityManagerInternal; |
| 22 | import android.support.test.filters.SmallTest; |
| 23 | import android.support.test.runner.AndroidJUnit4; |
| 24 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 28 | import org.mockito.Mock; |
| 29 | import org.mockito.MockitoAnnotations; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Test class for {@link ActivityManagerInternal}. |
| 33 | * |
| 34 | * To run the tests, use |
| 35 | * |
| 36 | * runtest -c com.android.server.am.ActivityManagerInternalTest frameworks-services |
| 37 | * |
| 38 | * or the following steps: |
| 39 | * |
| 40 | * Build: m FrameworksServicesTests |
| 41 | * Install: adb install -r \ |
| 42 | * ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk |
| 43 | * Run: adb shell am instrument -e class com.android.server.am.ActivityManagerInternalTest -w \ |
| 44 | * com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
| 45 | */ |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidJUnit4.class) |
| 48 | public class ActivityManagerInternalTest { |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 49 | @Mock private ActivityManagerService.Injector mMockInjector; |
| 50 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 51 | private ActivityManagerService mAms; |
| 52 | private ActivityManagerInternal mAmi; |
| 53 | @Before |
| 54 | public void setUp() { |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 55 | MockitoAnnotations.initMocks(this); |
| 56 | |
| 57 | mAms = new ActivityManagerService(mMockInjector); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 58 | mAmi = mAms.new LocalService(); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testNotifyNetworkPolicyRulesUpdated() { |
| 63 | // For checking there is no crash when there are no active uid records. |
| 64 | mAmi.notifyNetworkPolicyRulesUpdated(111, 11); |
| 65 | |
| 66 | // Insert active uid records. |
| 67 | final UidRecord record1 = addActiveUidRecord(222, 22); |
| 68 | final UidRecord record2 = addActiveUidRecord(333, 33); |
| 69 | // Notify that network policy rules are updated for uid 222. |
| 70 | mAmi.notifyNetworkPolicyRulesUpdated(222, 44); |
| 71 | assertEquals("UidRecord for uid 222 should be updated", |
| 72 | 44L, record1.lastNetworkUpdatedProcStateSeq); |
| 73 | assertEquals("UidRecord for uid 333 should not be updated", |
| 74 | 33L, record2.lastNetworkUpdatedProcStateSeq); |
| 75 | } |
| 76 | |
| 77 | private UidRecord addActiveUidRecord(int uid, long lastNetworkUpdatedProcStateSeq) { |
| 78 | final UidRecord record = new UidRecord(uid); |
| 79 | record.lastNetworkUpdatedProcStateSeq = lastNetworkUpdatedProcStateSeq; |
| 80 | mAms.mActiveUids.put(uid, record); |
| 81 | return record; |
| 82 | } |
| 83 | } |