Suprabh Shukla | 0d51a8b | 2019-10-30 18:56:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 20 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; |
| 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
| 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
Jing Ji | 7b82cbe | 2020-01-30 15:03:05 -0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.mock; |
Suprabh Shukla | 0d51a8b | 2019-10-30 18:56:44 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.app.ActivityManager; |
| 30 | import android.app.ActivityManagerInternal; |
| 31 | import android.app.AppGlobals; |
| 32 | import android.app.PendingIntent; |
| 33 | import android.content.Intent; |
| 34 | import android.content.pm.IPackageManager; |
| 35 | import android.os.Looper; |
| 36 | |
| 37 | import androidx.test.runner.AndroidJUnit4; |
| 38 | |
| 39 | import com.android.server.AlarmManagerInternal; |
| 40 | import com.android.server.LocalServices; |
| 41 | |
| 42 | import org.junit.After; |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | import org.mockito.ArgumentCaptor; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoSession; |
| 49 | import org.mockito.quality.Strictness; |
| 50 | |
| 51 | @RunWith(AndroidJUnit4.class) |
| 52 | public class PendingIntentControllerTest { |
| 53 | private static final String TEST_PACKAGE_NAME = "test-package-1"; |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame] | 54 | private static final String TEST_FEATURE_ID = "test-feature-1"; |
Suprabh Shukla | 0d51a8b | 2019-10-30 18:56:44 -0700 | [diff] [blame] | 55 | private static final int TEST_CALLING_UID = android.os.Process.myUid(); |
| 56 | private static final Intent[] TEST_INTENTS = new Intent[]{new Intent("com.test.intent")}; |
| 57 | |
| 58 | @Mock |
| 59 | private UserController mUserController; |
| 60 | @Mock |
| 61 | private AlarmManagerInternal mAlarmManagerInternal; |
| 62 | @Mock |
| 63 | private ActivityManagerInternal mActivityManagerInternal; |
| 64 | @Mock |
| 65 | private IPackageManager mIPackageManager; |
| 66 | |
| 67 | private MockitoSession mMockingSession; |
| 68 | private PendingIntentController mPendingIntentController; |
| 69 | |
| 70 | @Before |
| 71 | public void setUp() throws Exception { |
| 72 | mMockingSession = mockitoSession() |
| 73 | .initMocks(this) |
| 74 | .mockStatic(LocalServices.class) |
| 75 | .mockStatic(AppGlobals.class) |
| 76 | .strictness(Strictness.LENIENT) // Needed to stub LocalServices.getService twice |
| 77 | .startMocking(); |
| 78 | doReturn(mAlarmManagerInternal).when( |
| 79 | () -> LocalServices.getService(AlarmManagerInternal.class)); |
| 80 | doReturn(mActivityManagerInternal).when( |
| 81 | () -> LocalServices.getService(ActivityManagerInternal.class)); |
| 82 | doReturn(mIPackageManager).when(() -> AppGlobals.getPackageManager()); |
| 83 | when(mIPackageManager.getPackageUid(eq(TEST_PACKAGE_NAME), anyInt(), anyInt())).thenReturn( |
| 84 | TEST_CALLING_UID); |
Jing Ji | 7b82cbe | 2020-01-30 15:03:05 -0800 | [diff] [blame] | 85 | ActivityManagerConstants constants = mock(ActivityManagerConstants.class); |
| 86 | constants.PENDINGINTENT_WARNING_THRESHOLD = 2000; |
Suprabh Shukla | 0d51a8b | 2019-10-30 18:56:44 -0700 | [diff] [blame] | 87 | mPendingIntentController = new PendingIntentController(Looper.getMainLooper(), |
Jing Ji | 7b82cbe | 2020-01-30 15:03:05 -0800 | [diff] [blame] | 88 | mUserController, constants); |
Suprabh Shukla | 0d51a8b | 2019-10-30 18:56:44 -0700 | [diff] [blame] | 89 | mPendingIntentController.onActivityManagerInternalAdded(); |
| 90 | } |
| 91 | |
| 92 | private PendingIntentRecord createPendingIntentRecord(int flags) { |
| 93 | return mPendingIntentController.getIntentSender(ActivityManager.INTENT_SENDER_BROADCAST, |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame] | 94 | TEST_PACKAGE_NAME, TEST_FEATURE_ID, TEST_CALLING_UID, 0, null, null, 0, |
| 95 | TEST_INTENTS, null, flags, null); |
Suprabh Shukla | 0d51a8b | 2019-10-30 18:56:44 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void alarmsRemovedOnCancel() { |
| 100 | final PendingIntentRecord pir = createPendingIntentRecord(0); |
| 101 | mPendingIntentController.cancelIntentSender(pir); |
| 102 | final ArgumentCaptor<PendingIntent> piCaptor = ArgumentCaptor.forClass(PendingIntent.class); |
| 103 | verify(mAlarmManagerInternal).remove(piCaptor.capture()); |
| 104 | assertEquals("Wrong target for pending intent passed to alarm manager", pir, |
| 105 | piCaptor.getValue().getTarget()); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void alarmsRemovedOnRecreateWithCancelCurrent() { |
| 110 | final PendingIntentRecord pir = createPendingIntentRecord(0); |
| 111 | createPendingIntentRecord(PendingIntent.FLAG_CANCEL_CURRENT); |
| 112 | final ArgumentCaptor<PendingIntent> piCaptor = ArgumentCaptor.forClass(PendingIntent.class); |
| 113 | verify(mAlarmManagerInternal).remove(piCaptor.capture()); |
| 114 | assertEquals("Wrong target for pending intent passed to alarm manager", pir, |
| 115 | piCaptor.getValue().getTarget()); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void alarmsRemovedOnSendingOneShot() { |
| 120 | final PendingIntentRecord pir = createPendingIntentRecord(PendingIntent.FLAG_ONE_SHOT); |
| 121 | pir.send(0, null, null, null, null, null, null); |
| 122 | final ArgumentCaptor<PendingIntent> piCaptor = ArgumentCaptor.forClass(PendingIntent.class); |
| 123 | verify(mAlarmManagerInternal).remove(piCaptor.capture()); |
| 124 | assertEquals("Wrong target for pending intent passed to alarm manager", pir, |
| 125 | piCaptor.getValue().getTarget()); |
| 126 | } |
| 127 | |
| 128 | @After |
| 129 | public void tearDown() { |
| 130 | if (mMockingSession != null) { |
| 131 | mMockingSession.finishMocking(); |
| 132 | } |
| 133 | } |
| 134 | } |