Merge "Added new testNotifyCellLocationForSubscriberByUserSwitched" am: 93cc1d5336 am: 062a26cd3e

Original change: https://android-review.googlesource.com/c/platform/frameworks/opt/telephony/+/1552066

Change-Id: Id8df3686eb73baaa2538c5f998e38c7888cb71d2
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java b/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
index 068deef..b34807d 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
@@ -55,6 +55,7 @@
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.MatrixCursor;
+import android.location.LocationManager;
 import android.net.ConnectivityManager;
 import android.net.Network;
 import android.net.Uri;
@@ -275,6 +276,8 @@
                     return TestApplication.getAppContext().getSystemService(name);
                 case Context.POWER_WHITELIST_MANAGER:
                     return mPowerWhitelistManager;
+                case Context.LOCATION_SERVICE:
+                    return mLocationManager;
                 default:
                     return null;
             }
@@ -306,6 +309,8 @@
                 return Context.KEYGUARD_SERVICE;
             } else if (serviceClass == VcnManager.class) {
                 return Context.VCN_MANAGEMENT_SERVICE;
+            } else if (serviceClass == LocationManager.class) {
+                return Context.LOCATION_SERVICE;
             }
             return super.getSystemServiceName(serviceClass);
         }
@@ -654,6 +659,7 @@
     private final PowerWhitelistManager mPowerWhitelistManager = mock(PowerWhitelistManager.class);
     private final KeyguardManager mKeyguardManager = mock(KeyguardManager.class);
     private final VcnManager mVcnManager = mock(VcnManager.class);
+    private final LocationManager mLocationManager = mock(LocationManager.class);
 
     private final ContentProvider mContentProvider = spy(new FakeContentProvider());
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java b/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
index 6f455ed..44dc4c7 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
@@ -27,16 +27,24 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.anyString;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import android.content.Intent;
+import android.content.pm.UserInfo;
 import android.net.LinkProperties;
+import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.UserHandle;
 import android.telephony.AccessNetworkConstants;
 import android.telephony.Annotation;
+import android.telephony.CellIdentity;
+import android.telephony.CellIdentityGsm;
+import android.telephony.CellLocation;
 import android.telephony.LinkCapacityEstimate;
 import android.telephony.PhoneCapability;
 import android.telephony.PhysicalChannelConfig;
@@ -63,6 +71,7 @@
 import org.mockito.Mock;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -83,6 +92,7 @@
     private int mSrvccState = -1;
     private int mRadioPowerState = RADIO_POWER_UNAVAILABLE;
     private List<PhysicalChannelConfig> mPhysicalChannelConfigs;
+    private CellLocation mCellLocation;
 
     // All events contribute to TelephonyRegistry#isPhoneStatePermissionRequired
     private static final Set<Integer> READ_PHONE_STATE_EVENTS;
@@ -149,7 +159,8 @@
             TelephonyCallback.PreciseDataConnectionStateListener,
             TelephonyCallback.DisplayInfoListener,
             TelephonyCallback.LinkCapacityEstimateChangedListener,
-            TelephonyCallback.PhysicalChannelConfigListener {
+            TelephonyCallback.PhysicalChannelConfigListener,
+            TelephonyCallback.CellLocationListener {
         // This class isn't mockable to get invocation counts because the IBinder is null and
         // crashes the TelephonyRegistry. Make a cheesy verify(times()) alternative.
         public AtomicInteger invocationCount = new AtomicInteger(0);
@@ -194,6 +205,11 @@
         public void onPhysicalChannelConfigChanged(@NonNull List<PhysicalChannelConfig> configs) {
             mPhysicalChannelConfigs = configs;
         }
+
+        @Override
+        public void onCellLocationChanged(CellLocation location) {
+            mCellLocation = location;
+        }
     }
 
     private void addTelephonyRegistryService() {
@@ -585,6 +601,51 @@
         assertEquals(displayInfo, mTelephonyDisplayInfo);
     }
 
+    @Test
+    public void testNotifyCellLocationForSubscriberByUserSwitched() throws RemoteException {
+        final int phoneId = 0;
+        final int subId = 1;
+
+        // Return a slotIndex / phoneId of 0 for subId 1.
+        doReturn(new int[] {subId}).when(mSubscriptionController).getSubId(phoneId);
+        doReturn(mMockSubInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(subId);
+        doReturn(phoneId).when(mMockSubInfo).getSimSlotIndex();
+        mServiceManagerMockedServices.put("isub", mSubscriptionController);
+        doReturn(mSubscriptionController).when(mSubscriptionController)
+                .queryLocalInterface(anyString());
+
+        UserInfo userInfo = new UserInfo(UserHandle.myUserId(), "" /* name */, 0 /* flags */);
+        doReturn(userInfo).when(mIActivityManager).getCurrentUser();
+
+        doReturn(true).when(mLocationManager).isLocationEnabledForUser(any(UserHandle.class));
+
+        CellIdentity cellIdentity = new CellIdentityGsm(-1, -1, -1, -1, null, null, null, null,
+                Collections.emptyList());
+        mTelephonyRegistry.notifyCellLocationForSubscriber(subId, cellIdentity);
+        processAllMessages();
+
+        // Listen to EVENT_CELL_LOCATION_CHANGED for the current user Id.
+        int[] events = {TelephonyCallback.EVENT_CELL_LOCATION_CHANGED};
+        mTelephonyRegistry.listenWithEventList(subId, mContext.getOpPackageName(),
+                mContext.getAttributionTag(), mTelephonyCallback.callback, events, false);
+
+        // Broadcast ACTION_USER_SWITCHED for USER_SYSTEM. Callback should be triggered.
+        mCellLocation = null;
+        mContext.sendBroadcast(new Intent(Intent.ACTION_USER_SWITCHED));
+
+        processAllMessages();
+        assertEquals(cellIdentity.asCellLocation(), mCellLocation);
+
+        // Broadcast ACTION_USER_SWITCHED for the current user Id + 1. Callback shouldn't be
+        // triggered.
+        userInfo.id++;
+        mCellLocation = null;
+        mContext.sendBroadcast(new Intent(Intent.ACTION_USER_SWITCHED));
+
+        processAllMessages();
+        assertEquals(null, mCellLocation);
+    }
+
     private void assertSecurityExceptionThrown(int[] event) {
         try {
             mTelephonyRegistry.listenWithEventList(
diff --git a/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java b/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java
index b102613..ab076d8 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java
@@ -41,6 +41,7 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
+import android.location.LocationManager;
 import android.net.ConnectivityManager;
 import android.net.NetworkCapabilities;
 import android.net.vcn.VcnManager;
@@ -320,6 +321,8 @@
     protected ImsStats mImsStats;
     @Mock
     protected PinStorage mPinStorage;
+    @Mock
+    protected LocationManager mLocationManager;
 
     protected ActivityManager mActivityManager;
     protected ImsCallProfile mImsCallProfile;
@@ -478,6 +481,7 @@
         mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
         mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
         mVcnManager = mContext.getSystemService(VcnManager.class);
+        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
 
         //mTelephonyComponentFactory
         doReturn(mTelephonyComponentFactory).when(mTelephonyComponentFactory).inject(anyString());