Merge "Added new testNotifyCellLocationForSubscriberByUserSwitched" into sc-dev
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java b/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
index f2cb869..b96056a 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/ContextFixture.java
@@ -279,6 +279,8 @@
                     return TestApplication.getAppContext().getSystemService(name);
                 case Context.POWER_WHITELIST_MANAGER:
                     return mPowerWhitelistManager;
+                case Context.LOCATION_SERVICE:
+                    return mLocationManager;
                 default:
                     return null;
             }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java b/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
index 18390e6..1362b33 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/TelephonyRegistryTest.java
@@ -35,10 +35,16 @@
 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;
@@ -64,6 +70,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;
@@ -85,6 +92,7 @@
     private int mRadioPowerState = RADIO_POWER_UNAVAILABLE;
     private List<PhysicalChannelConfig> mPhysicalChannelConfigs;
     private TelephonyRegistry.ConfigurationProvider mMockConfigurationProvider;
+    private CellLocation mCellLocation;
 
     // All events contribute to TelephonyRegistry#isPhoneStatePermissionRequired
     private static final Set<Integer> READ_PHONE_STATE_EVENTS;
@@ -151,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);
@@ -189,7 +198,12 @@
         @Override
         public void onLinkCapacityEstimateChanged(
                 List<LinkCapacityEstimate> linkCapacityEstimateList) {
-            mLinkCapacityEstimateList =  linkCapacityEstimateList;
+            mLinkCapacityEstimateList = linkCapacityEstimateList;
+        }
+
+        @Override
+        public void onCellLocationChanged(CellLocation location) {
+            mCellLocation = location;
         }
 
         @Override
@@ -610,6 +624,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 035bcdf..b1f0e2e 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;
@@ -326,6 +327,8 @@
     protected LinkBandwidthEstimator mLinkBandwidthEstimator;
     @Mock
     protected PinStorage mPinStorage;
+    @Mock
+    protected LocationManager mLocationManager;
 
     protected ActivityManager mActivityManager;
     protected ImsCallProfile mImsCallProfile;
@@ -484,6 +487,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());