Fix uiservicestests

They were hitting a limit of registered broadcast recievers,
but they don't actually need to register real receivers with AM.

Test: atest FrameworksUiServicesTests
Fixes: 274705714
Change-Id: Ic60fc42d159f3c9df0f041db6f827ac82c733060
diff --git a/services/tests/uiservicestests/AndroidManifest.xml b/services/tests/uiservicestests/AndroidManifest.xml
index e8e3a8f..09ee598 100644
--- a/services/tests/uiservicestests/AndroidManifest.xml
+++ b/services/tests/uiservicestests/AndroidManifest.xml
@@ -32,6 +32,7 @@
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.OBSERVE_ROLE_HOLDERS" />
     <uses-permission android:name="android.permission.GET_INTENT_SENDER_INTENT"/>
+    <uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />
     <uses-permission android:name="android.permission.WRITE_DEVICE_CONFIG" />
     <uses-permission android:name="android.permission.ACCESS_KEYGUARD_SECURE_STORAGE" />
 
diff --git a/services/tests/uiservicestests/src/com/android/server/UiServiceTestCase.java b/services/tests/uiservicestests/src/com/android/server/UiServiceTestCase.java
index 182bf94..82bc6f6 100644
--- a/services/tests/uiservicestests/src/com/android/server/UiServiceTestCase.java
+++ b/services/tests/uiservicestests/src/com/android/server/UiServiceTestCase.java
@@ -16,8 +16,10 @@
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
+import android.content.Intent;
 import android.content.pm.PackageManagerInternal;
 import android.net.Uri;
 import android.os.Build;
@@ -44,8 +46,8 @@
     protected static final String PKG_R = "com.example.r";
 
     @Rule
-    public final TestableContext mContext =
-            new TestableContext(InstrumentationRegistry.getContext(), null);
+    public TestableContext mContext =
+            spy(new TestableContext(InstrumentationRegistry.getContext(), null));
 
     protected TestableContext getContext() {
         return mContext;
@@ -81,6 +83,11 @@
         LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
         when(mUgmInternal.checkGrantUriPermission(
                 anyInt(), anyString(), any(Uri.class), anyInt(), anyInt())).thenReturn(-1);
+
+        Mockito.doReturn(new Intent()).when(mContext).registerReceiverAsUser(
+                any(), any(), any(), any(), any());
+        Mockito.doReturn(new Intent()).when(mContext).registerReceiver(any(), any());
+        Mockito.doNothing().when(mContext).unregisterReceiver(any());
     }
 
     @After
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java
index ce07621..8fcbf2f 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java
@@ -130,13 +130,18 @@
     private ArrayMap<Integer, ArrayMap<Integer, String>> mExpectedPrimary;
     private ArrayMap<Integer, ArrayMap<Integer, String>> mExpectedSecondary;
 
+    private UserHandle mUser;
+    private String mPkg;
+
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
 
-        getContext().setMockPackageManager(mPm);
-        getContext().addMockSystemService(Context.USER_SERVICE, mUm);
-        getContext().addMockSystemService(DEVICE_POLICY_SERVICE, mDpm);
+        mContext.setMockPackageManager(mPm);
+        mContext.addMockSystemService(Context.USER_SERVICE, mUm);
+        mContext.addMockSystemService(DEVICE_POLICY_SERVICE, mDpm);
+        mUser = mContext.getUser();
+        mPkg = mContext.getPackageName();
 
         List<UserInfo> users = new ArrayList<>();
         users.add(mZero);
@@ -861,8 +866,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -891,8 +896,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -921,8 +926,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -951,8 +956,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -981,8 +986,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1011,8 +1016,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1437,8 +1442,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1464,8 +1469,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1492,8 +1497,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1522,8 +1527,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1552,8 +1557,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1791,8 +1796,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1837,8 +1842,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
@@ -1880,8 +1885,8 @@
         ApplicationInfo ai = new ApplicationInfo();
         ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
 
-        when(context.getPackageName()).thenReturn(mContext.getPackageName());
-        when(context.getUserId()).thenReturn(mContext.getUserId());
+        when(context.getPackageName()).thenReturn(mPkg);
+        when(context.getUserId()).thenReturn(mUser.getIdentifier());
         when(context.getPackageManager()).thenReturn(pm);
         when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai);
 
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationComparatorTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationComparatorTest.java
index d73a3b8..95fae07 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationComparatorTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationComparatorTest.java
@@ -33,9 +33,11 @@
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Person;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.graphics.Color;
 import android.os.Build;
 import android.os.UserHandle;
@@ -63,7 +65,7 @@
 @SmallTest
 @RunWith(Parameterized.class)
 public class NotificationComparatorTest extends UiServiceTestCase {
-    @Mock Context mContext;
+    @Mock Context mMockContext;
     @Mock TelecomManager mTm;
     @Mock RankingHandler handler;
     @Mock PackageManager mPm;
@@ -115,32 +117,35 @@
 
         int userId = UserHandle.myUserId();
 
-        when(mContext.getResources()).thenReturn(getContext().getResources());
-        when(mContext.getTheme()).thenReturn(getContext().getTheme());
-        when(mContext.getContentResolver()).thenReturn(getContext().getContentResolver());
-        when(mContext.getPackageManager()).thenReturn(mPm);
-        when(mContext.getSystemService(eq(Context.TELECOM_SERVICE))).thenReturn(mTm);
-        when(mContext.getSystemService(Vibrator.class)).thenReturn(mVibrator);
-        when(mContext.getString(anyInt())).thenCallRealMethod();
-        when(mContext.getColor(anyInt())).thenCallRealMethod();
+        final Resources res = mContext.getResources();
+        when(mMockContext.getResources()).thenReturn(res);
+        final Resources.Theme theme = mContext.getTheme();
+        when(mMockContext.getTheme()).thenReturn(theme);
+        final ContentResolver cr = mContext.getContentResolver();
+        when(mMockContext.getContentResolver()).thenReturn(cr);
+        when(mMockContext.getPackageManager()).thenReturn(mPm);
+        when(mMockContext.getSystemService(eq(mMockContext.TELECOM_SERVICE))).thenReturn(mTm);
+        when(mMockContext.getSystemService(Vibrator.class)).thenReturn(mVibrator);
+        when(mMockContext.getString(anyInt())).thenCallRealMethod();
+        when(mMockContext.getColor(anyInt())).thenCallRealMethod();
         when(mTm.getDefaultDialerPackage()).thenReturn(callPkg);
         final ApplicationInfo legacy = new ApplicationInfo();
         legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
         try {
             when(mPm.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(legacy);
-            when(mContext.getApplicationInfo()).thenReturn(legacy);
+            when(mMockContext.getApplicationInfo()).thenReturn(legacy);
         } catch (PackageManager.NameNotFoundException e) {
             // let's hope not
         }
 
-        smsPkg = Settings.Secure.getString(mContext.getContentResolver(),
+        smsPkg = Settings.Secure.getString(mMockContext.getContentResolver(),
                 Settings.Secure.SMS_DEFAULT_APPLICATION);
 
-        Notification nonInterruptiveNotif = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification nonInterruptiveNotif = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setCategory(Notification.CATEGORY_CALL)
                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                 .build();
-        mRecordMinCallNonInterruptive = new NotificationRecord(mContext,
+        mRecordMinCallNonInterruptive = new NotificationRecord(mMockContext,
                 new StatusBarNotification(callPkg,
                         callPkg, 1, "mRecordMinCallNonInterruptive", callUid, callUid,
                         nonInterruptiveNotif,
@@ -148,134 +153,134 @@
         mRecordMinCallNonInterruptive.setSystemImportance(NotificationManager.IMPORTANCE_MIN);
         mRecordMinCallNonInterruptive.setInterruptive(false);
 
-        Notification n1 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n1 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setCategory(Notification.CATEGORY_CALL)
                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                 .build();
-        mRecordMinCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
+        mRecordMinCall = new NotificationRecord(mMockContext, new StatusBarNotification(callPkg,
                 callPkg, 1, "minCall", callUid, callUid, n1,
                 new UserHandle(userId), "", 2000), getDefaultChannel());
         mRecordMinCall.setSystemImportance(NotificationManager.IMPORTANCE_MIN);
         mRecordMinCall.setInterruptive(true);
 
-        Notification n2 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n2 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setCategory(Notification.CATEGORY_CALL)
                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                 .build();
-        mRecordHighCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
+        mRecordHighCall = new NotificationRecord(mMockContext, new StatusBarNotification(callPkg,
                 callPkg, 1, "highcall", callUid, callUid, n2,
                 new UserHandle(userId), "", 1999), getDefaultChannel());
         mRecordHighCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
 
-        Notification nHighCallStyle = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification nHighCallStyle = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setStyle(Notification.CallStyle.forOngoingCall(
                         new Person.Builder().setName("caller").build(),
                         mock(PendingIntent.class)
                 ))
                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                 .build();
-        mRecordHighCallStyle = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
-                callPkg, 1, "highCallStyle", callUid, callUid, nHighCallStyle,
+        mRecordHighCallStyle = new NotificationRecord(mMockContext, new StatusBarNotification(
+                callPkg, callPkg, 1, "highCallStyle", callUid, callUid, nHighCallStyle,
                 new UserHandle(userId), "", 2000), getDefaultChannel());
         mRecordHighCallStyle.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
         mRecordHighCallStyle.setInterruptive(true);
 
-        Notification n4 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n4 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setStyle(new Notification.MessagingStyle("sender!")).build();
-        mRecordInlineReply = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
+        mRecordInlineReply = new NotificationRecord(mMockContext, new StatusBarNotification(pkg2,
                 pkg2, 1, "inlinereply", uid2, uid2, n4, new UserHandle(userId),
                 "", 1599), getDefaultChannel());
         mRecordInlineReply.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
         mRecordInlineReply.setPackagePriority(Notification.PRIORITY_MAX);
 
         if (smsPkg != null) {
-            Notification n5 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+            Notification n5 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                     .setCategory(Notification.CATEGORY_MESSAGE).build();
-            mRecordSms = new NotificationRecord(mContext, new StatusBarNotification(smsPkg,
+            mRecordSms = new NotificationRecord(mMockContext, new StatusBarNotification(smsPkg,
                     smsPkg, 1, "sms", smsUid, smsUid, n5, new UserHandle(userId),
                     "", 1299), getDefaultChannel());
             mRecordSms.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
         }
 
-        Notification n6 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
-        mRecordStarredContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
+        Notification n6 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID).build();
+        mRecordStarredContact = new NotificationRecord(mMockContext, new StatusBarNotification(pkg2,
                 pkg2, 1, "starred", uid2, uid2, n6, new UserHandle(userId),
                 "", 1259), getDefaultChannel());
         mRecordStarredContact.setContactAffinity(ValidateNotificationPeople.STARRED_CONTACT);
         mRecordStarredContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
 
-        Notification n7 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
-        mRecordContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
+        Notification n7 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID).build();
+        mRecordContact = new NotificationRecord(mMockContext, new StatusBarNotification(pkg2,
                 pkg2, 1, "contact", uid2, uid2, n7, new UserHandle(userId),
                 "", 1259), getDefaultChannel());
         mRecordContact.setContactAffinity(ValidateNotificationPeople.VALID_CONTACT);
         mRecordContact.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
 
-        Notification nSystemMax = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
-        mRecordSystemMax = new NotificationRecord(mContext, new StatusBarNotification(sysPkg,
+        Notification nSystemMax = new Notification.Builder(mMockContext, TEST_CHANNEL_ID).build();
+        mRecordSystemMax = new NotificationRecord(mMockContext, new StatusBarNotification(sysPkg,
                 sysPkg, 1, "systemmax", uid2, uid2, nSystemMax, new UserHandle(userId),
                 "", 1244), getDefaultChannel());
         mRecordSystemMax.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
 
-        Notification n8 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build();
-        mRecordUrgent = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
+        Notification n8 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID).build();
+        mRecordUrgent = new NotificationRecord(mMockContext, new StatusBarNotification(pkg2,
                 pkg2, 1, "urgent", uid2, uid2, n8, new UserHandle(userId),
                 "", 1258), getDefaultChannel());
         mRecordUrgent.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
 
-        Notification n9 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n9 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setCategory(Notification.CATEGORY_MESSAGE)
                 .setFlag(Notification.FLAG_ONGOING_EVENT
                         |Notification.FLAG_FOREGROUND_SERVICE, true)
                 .build();
-        mRecordCheater = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
+        mRecordCheater = new NotificationRecord(mMockContext, new StatusBarNotification(pkg2,
                 pkg2, 1, "cheater", uid2, uid2, n9, new UserHandle(userId),
                 "", 9258), getDefaultChannel());
         mRecordCheater.setSystemImportance(NotificationManager.IMPORTANCE_LOW);
         mRecordCheater.setPackagePriority(Notification.PRIORITY_MAX);
 
-        Notification n10 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n10 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setStyle(new Notification.InboxStyle().setSummaryText("message!")).build();
-        mRecordEmail = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
+        mRecordEmail = new NotificationRecord(mMockContext, new StatusBarNotification(pkg2,
                 pkg2, 1, "email", uid2, uid2, n10, new UserHandle(userId),
                 "", 1599), getDefaultChannel());
         mRecordEmail.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
 
-        Notification n11 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n11 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setCategory(Notification.CATEGORY_MESSAGE)
                 .setColorized(true).setColor(Color.WHITE)
                 .build();
-        mRecordCheaterColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
-                pkg2, 1, "cheaterColorized", uid2, uid2, n11, new UserHandle(userId),
-                "", 9258), getDefaultChannel());
+        mRecordCheaterColorized = new NotificationRecord(mMockContext,
+                new StatusBarNotification(pkg2,pkg2, 1, "cheaterColorized", uid2, uid2, n11,
+                new UserHandle(userId), "", 9258), getDefaultChannel());
         mRecordCheaterColorized.setSystemImportance(NotificationManager.IMPORTANCE_LOW);
 
-        Notification n12 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n12 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setCategory(Notification.CATEGORY_MESSAGE)
                 .setColorized(true).setColor(Color.WHITE)
                 .setStyle(new Notification.MediaStyle())
                 .build();
-        mNoMediaSessionMedia = new NotificationRecord(mContext, new StatusBarNotification(
+        mNoMediaSessionMedia = new NotificationRecord(mMockContext, new StatusBarNotification(
                 pkg2, pkg2, 1, "media", uid2, uid2, n12, new UserHandle(userId),
                 "", 9258), getDefaultChannel());
         mNoMediaSessionMedia.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
 
-        Notification n13 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n13 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                 .setColorized(true).setColor(Color.WHITE)
                 .build();
-        mRecordColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2,
+        mRecordColorized = new NotificationRecord(mMockContext, new StatusBarNotification(pkg2,
                 pkg2, 1, "colorized", uid2, uid2, n13,
                 new UserHandle(userId), "", 1999), getDefaultChannel());
         mRecordColorized.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
 
-        Notification n14 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
+        Notification n14 = new Notification.Builder(mMockContext, TEST_CHANNEL_ID)
                 .setCategory(Notification.CATEGORY_CALL)
                 .setColorized(true).setColor(Color.WHITE)
                 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                 .build();
-        mRecordColorizedCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg,
-                callPkg, 1, "colorizedCall", callUid, callUid, n14,
+        mRecordColorizedCall = new NotificationRecord(mMockContext, new StatusBarNotification(
+                callPkg, callPkg, 1, "colorizedCall", callUid, callUid, n14,
                 new UserHandle(userId), "", 1999), getDefaultChannel());
         mRecordColorizedCall.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
     }
@@ -316,14 +321,14 @@
         actual.addAll(expected);
         Collections.shuffle(actual);
 
-        Collections.sort(actual, new NotificationComparator(mContext));
+        Collections.sort(actual, new NotificationComparator(mMockContext));
 
         assertThat(actual).containsExactlyElementsIn(expected).inOrder();
     }
 
     @Test
     public void testRankingScoreOverrides() {
-        NotificationComparator comp = new NotificationComparator(mContext);
+        NotificationComparator comp = new NotificationComparator(mMockContext);
         NotificationRecord recordMinCallNonInterruptive = spy(mRecordMinCallNonInterruptive);
         if (mSortByInterruptiveness) {
             assertTrue(comp.compare(mRecordMinCall, recordMinCallNonInterruptive) < 0);
@@ -339,7 +344,7 @@
 
     @Test
     public void testMessaging() {
-        NotificationComparator comp = new NotificationComparator(mContext);
+        NotificationComparator comp = new NotificationComparator(mMockContext);
         assertTrue(comp.isImportantMessaging(mRecordInlineReply));
         if (mRecordSms != null) {
             assertTrue(comp.isImportantMessaging(mRecordSms));
@@ -350,7 +355,7 @@
 
     @Test
     public void testPeople() {
-        NotificationComparator comp = new NotificationComparator(mContext);
+        NotificationComparator comp = new NotificationComparator(mMockContext);
         assertTrue(comp.isImportantPeople(mRecordStarredContact));
         assertTrue(comp.isImportantPeople(mRecordContact));
     }
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java
index 1b42fd3..60f1e66 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryDatabaseTest.java
@@ -30,6 +30,7 @@
 import android.content.Context;
 import android.graphics.drawable.Icon;
 import android.os.Handler;
+import android.os.UserHandle;
 import android.util.AtomicFile;
 
 import androidx.test.InstrumentationRegistry;
@@ -56,8 +57,6 @@
     File mRootDir;
     @Mock
     Handler mFileWriteHandler;
-    @Mock
-    Context mContext;
 
     NotificationHistoryDatabase mDataBase;
 
@@ -92,10 +91,8 @@
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        when(mContext.getUser()).thenReturn(getContext().getUser());
-        when(mContext.getPackageName()).thenReturn(getContext().getPackageName());
-
-        mRootDir = new File(mContext.getFilesDir(), "NotificationHistoryDatabaseTest");
+        final File fileDir = mContext.getFilesDir();
+        mRootDir = new File(fileDir, "NotificationHistoryDatabaseTest");
 
         mDataBase = new NotificationHistoryDatabase(mFileWriteHandler, mRootDir);
         mDataBase.init();
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
index 9ca8d84..ae8fb88 100755
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
@@ -307,7 +307,6 @@
     @Mock
     private PermissionHelper mPermissionHelper;
     private NotificationChannelLoggerFake mLogger = new NotificationChannelLoggerFake();
-    private TestableContext mContext = spy(getContext());
     private final String PKG = mContext.getPackageName();
     private TestableLooper mTestableLooper;
     @Mock
@@ -578,9 +577,6 @@
         ArgumentCaptor<IntentFilter> intentFilterCaptor =
                 ArgumentCaptor.forClass(IntentFilter.class);
 
-        Mockito.doReturn(new Intent()).when(mContext).registerReceiverAsUser(
-                any(), any(), any(), any(), any());
-        Mockito.doReturn(new Intent()).when(mContext).registerReceiver(any(), any());
         verify(mContext, atLeastOnce()).registerReceiverAsUser(broadcastReceiverCaptor.capture(),
                 any(), intentFilterCaptor.capture(), any(), any());
         verify(mContext, atLeastOnce()).registerReceiver(broadcastReceiverCaptor.capture(),
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java
index 25e74bf..fae92d9 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java
@@ -55,6 +55,7 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ShortcutInfo;
+import android.content.res.Resources;
 import android.graphics.Color;
 import android.graphics.drawable.Icon;
 import android.media.AudioAttributes;
@@ -126,7 +127,8 @@
         MockitoAnnotations.initMocks(this);
 
         when(mMockContext.getSystemService(eq(Vibrator.class))).thenReturn(mVibrator);
-        when(mMockContext.getResources()).thenReturn(getContext().getResources());
+        final Resources res = mContext.getResources();
+        when(mMockContext.getResources()).thenReturn(res);
         when(mMockContext.getPackageManager()).thenReturn(mPm);
         when(mMockContext.getContentResolver()).thenReturn(mContentResolver);
         ApplicationInfo appInfo = new ApplicationInfo();
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationShellCmdTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationShellCmdTest.java
index fcff228..0222bfbf 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationShellCmdTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationShellCmdTest.java
@@ -67,7 +67,6 @@
 public class NotificationShellCmdTest extends UiServiceTestCase {
     private final Binder mBinder = new Binder();
     private final ShellCallback mCallback = new ShellCallback();
-    private final TestableContext mTestableContext = spy(getContext());
     @Mock
     NotificationManagerService mMockService;
     @Mock
@@ -82,7 +81,7 @@
         mTestableLooper = TestableLooper.get(this);
         mResultReceiver = new ResultReceiver(new Handler(mTestableLooper.getLooper()));
 
-        when(mMockService.getContext()).thenReturn(mTestableContext);
+        when(mMockService.getContext()).thenReturn(mContext);
         when(mMockService.getBinderService()).thenReturn(mMockBinderService);
     }
 
@@ -116,9 +115,10 @@
     Notification captureNotification(String aTag) throws Exception {
         ArgumentCaptor<Notification> notificationCaptor =
                 ArgumentCaptor.forClass(Notification.class);
+        final String pkg = getContext().getPackageName();
         verify(mMockBinderService).enqueueNotificationWithTag(
-                eq(getContext().getPackageName()),
-                eq(getContext().getPackageName()),
+                eq(pkg),
+                eq(pkg),
                 eq(aTag),
                 eq(NotificationShellCmd.NOTIFICATION_ID),
                 notificationCaptor.capture(),
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
index bf836ae..6f9798e 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
@@ -153,7 +153,6 @@
     private Resources mResources;
     private TestableLooper mTestableLooper;
     private ZenModeHelper mZenModeHelperSpy;
-    private Context mContext;
     private ContentResolver mContentResolver;
     @Mock AppOpsManager mAppOps;
     private WrappedSysUiStatsEvent.WrappedBuilderFactory mStatsEventBuilderFactory;
@@ -163,9 +162,9 @@
         MockitoAnnotations.initMocks(this);
 
         mTestableLooper = TestableLooper.get(this);
-        mContext = spy(getContext());
         mContentResolver = mContext.getContentResolver();
         mResources = spy(mContext.getResources());
+        String pkg = mContext.getPackageName();
         try {
             when(mResources.getXml(R.xml.default_zen_mode_config)).thenReturn(
                     getDefaultConfigParser());
@@ -190,7 +189,7 @@
         when(mPackageManager.getPackageUidAsUser(eq(CUSTOM_PKG_NAME), anyInt()))
                 .thenReturn(CUSTOM_PKG_UID);
         when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(
-                new String[] {getContext().getPackageName()});
+                new String[] {pkg});
         mZenModeHelperSpy.mPm = mPackageManager;
     }