From 1193f08d69db3b3f1180aa892d098926cb973b60 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Wed, 16 Oct 2024 14:08:57 +0800 Subject: [PATCH] Introduce dependencies object for EntitlementManager This is a no-op refactoring which only introduce dependencies object injection to replace the wrapper class. Test: atest TetheringTests:EntitlementManagerTest Fix: 363972541 Change-Id: I24b0bf5f8951c453af1e9ae9278285d1a7b86849 --- .../tethering/EntitlementManager.java | 201 ++++++++++-------- .../tethering/EntitlementManagerTest.java | 160 +++++++------- 2 files changed, 194 insertions(+), 167 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java b/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java index e42a516f9b..cd57c8d298 100644 --- a/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java +++ b/Tethering/src/com/android/networkstack/tethering/EntitlementManager.java @@ -33,9 +33,11 @@ import static android.net.TetheringManager.TETHER_ERROR_ENTITLEMENT_UNKNOWN; import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR; import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED; +import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; import static com.android.networkstack.apishim.ConstantsShim.ACTION_TETHER_UNSUPPORTED_CARRIER_UI; import static com.android.networkstack.apishim.ConstantsShim.RECEIVER_NOT_EXPORTED; +import android.annotation.NonNull; import android.app.ActivityManager; import android.app.AlarmManager; import android.app.PendingIntent; @@ -90,7 +92,6 @@ public class EntitlementManager { // Indicate tethering is not supported by carrier. private static final int TETHERING_PROVISIONING_CARRIER_UNSUPPORT = 1002; - private final ComponentName mSilentProvisioningService; private static final int MS_PER_HOUR = 60 * 60 * 1000; private static final int DUMP_TIMEOUT = 10_000; @@ -114,9 +115,115 @@ public class EntitlementManager { private boolean mNeedReRunProvisioningUi = false; private OnTetherProvisioningFailedListener mListener; private TetheringConfigurationFetcher mFetcher; + private final Dependencies mDeps; + + @VisibleForTesting(visibility = PRIVATE) + static class Dependencies { + @NonNull + private final Context mContext; + @NonNull + private final SharedLog mLog; + private final ComponentName mSilentProvisioningService; + + Dependencies(@NonNull Context context, @NonNull SharedLog log) { + mContext = context; + mLog = log; + mSilentProvisioningService = ComponentName.unflattenFromString( + mContext.getResources().getString(R.string.config_wifi_tether_enable)); + } + + /** + * Run the UI-enabled tethering provisioning check. + * @param type tethering type from TetheringManager.TETHERING_{@code *} + * @param receiver to receive entitlement check result. + * + * @return the broadcast intent, or null if the current user is not allowed to + * perform entitlement check. + */ + @Nullable + protected Intent runUiTetherProvisioning(int type, final TetheringConfiguration config, + ResultReceiver receiver) { + if (DBG) mLog.i("runUiTetherProvisioning: " + type); + + Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING_UI); + intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); + intent.putExtra(EXTRA_TETHER_UI_PROVISIONING_APP_NAME, config.provisioningApp); + intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); + intent.putExtra(EXTRA_TETHER_SUBID, config.activeDataSubId); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + // Only launch entitlement UI for the current user if it is allowed to + // change tethering. This usually means the system user or the admin users in HSUM. + if (SdkLevel.isAtLeastT()) { + // Create a user context for the current foreground user as UserManager#isAdmin() + // operates on the context user. + final int currentUserId = getCurrentUser(); + final UserHandle currentUser = UserHandle.of(currentUserId); + final Context userContext = mContext.createContextAsUser(currentUser, 0); + final UserManager userManager = userContext.getSystemService(UserManager.class); + + if (userManager.isAdminUser()) { + mContext.startActivityAsUser(intent, currentUser); + } else { + mLog.e("Current user (" + currentUserId + + ") is not allowed to perform entitlement check."); + return null; + } + } else { + // For T- devices, there is no other admin user other than the system user. + mContext.startActivity(intent); + } + return intent; + } + + /** + * Run no UI tethering provisioning check. + * @param type tethering type from TetheringManager.TETHERING_{@code *} + */ + protected Intent runSilentTetherProvisioning( + int type, final TetheringConfiguration config, ResultReceiver receiver) { + if (DBG) mLog.i("runSilentTetherProvisioning: " + type); + + Intent intent = new Intent(); + intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); + intent.putExtra(EXTRA_RUN_PROVISION, true); + intent.putExtra(EXTRA_TETHER_SILENT_PROVISIONING_ACTION, config.provisioningAppNoUi); + intent.putExtra(EXTRA_TETHER_PROVISIONING_RESPONSE, config.provisioningResponse); + intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); + intent.putExtra(EXTRA_TETHER_SUBID, config.activeDataSubId); + intent.setComponent(mSilentProvisioningService); + // Only admin user can change tethering and SilentTetherProvisioning don't need to + // show UI, it is fine to always start setting's background service as system user. + mContext.startService(intent); + return intent; + } + + /** + * Create a PendingIntent for the provisioning recheck alarm. + * @param pkgName the package name of the PendingIntent. + */ + PendingIntent createRecheckAlarmIntent(final String pkgName) { + final Intent intent = new Intent(ACTION_PROVISIONING_ALARM); + intent.setPackage(pkgName); + return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE); + } + + /** + * Get the current user id. + */ + int getCurrentUser() { + return ActivityManager.getCurrentUser(); + } + } public EntitlementManager(Context ctx, Handler h, SharedLog log, Runnable callback) { + this(ctx, h, log, callback, new Dependencies(ctx, log)); + } + + @VisibleForTesting(visibility = PRIVATE) + EntitlementManager(Context ctx, Handler h, SharedLog log, + Runnable callback, @NonNull Dependencies deps) { mContext = ctx; mLog = log.forSubComponent(TAG); mCurrentDownstreams = new BitSet(); @@ -125,6 +232,7 @@ public class EntitlementManager { mEntitlementCacheValue = new SparseIntArray(); mPermissionChangeCallback = callback; mHandler = h; + mDeps = deps; if (SdkLevel.isAtLeastU()) { mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISIONING_ALARM), null, mHandler, RECEIVER_NOT_EXPORTED); @@ -132,8 +240,6 @@ public class EntitlementManager { mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISIONING_ALARM), null, mHandler); } - mSilentProvisioningService = ComponentName.unflattenFromString( - mContext.getResources().getString(R.string.config_wifi_tether_enable)); } public void setOnTetherProvisioningFailedListener( @@ -387,80 +493,6 @@ public class EntitlementManager { } } - /** - * Run no UI tethering provisioning check. - * @param type tethering type from TetheringManager.TETHERING_{@code *} - * @param subId default data subscription ID. - */ - @VisibleForTesting - protected Intent runSilentTetherProvisioning( - int type, final TetheringConfiguration config, ResultReceiver receiver) { - if (DBG) mLog.i("runSilentTetherProvisioning: " + type); - - Intent intent = new Intent(); - intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); - intent.putExtra(EXTRA_RUN_PROVISION, true); - intent.putExtra(EXTRA_TETHER_SILENT_PROVISIONING_ACTION, config.provisioningAppNoUi); - intent.putExtra(EXTRA_TETHER_PROVISIONING_RESPONSE, config.provisioningResponse); - intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); - intent.putExtra(EXTRA_TETHER_SUBID, config.activeDataSubId); - intent.setComponent(mSilentProvisioningService); - // Only admin user can change tethering and SilentTetherProvisioning don't need to - // show UI, it is fine to always start setting's background service as system user. - mContext.startService(intent); - return intent; - } - - @VisibleForTesting - int getCurrentUser() { - return ActivityManager.getCurrentUser(); - } - - /** - * Run the UI-enabled tethering provisioning check. - * @param type tethering type from TetheringManager.TETHERING_{@code *} - * @param receiver to receive entitlement check result. - * - * @return the broadcast intent, or null if the current user is not allowed to - * perform entitlement check. - */ - @Nullable - @VisibleForTesting - protected Intent runUiTetherProvisioning(int type, final TetheringConfiguration config, - ResultReceiver receiver) { - if (DBG) mLog.i("runUiTetherProvisioning: " + type); - - Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING_UI); - intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); - intent.putExtra(EXTRA_TETHER_UI_PROVISIONING_APP_NAME, config.provisioningApp); - intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); - intent.putExtra(EXTRA_TETHER_SUBID, config.activeDataSubId); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - - // Only launch entitlement UI for the current user if it is allowed to - // change tethering. This usually means the system user or the admin users in HSUM. - if (SdkLevel.isAtLeastT()) { - // Create a user context for the current foreground user as UserManager#isAdmin() - // operates on the context user. - final int currentUserId = getCurrentUser(); - final UserHandle currentUser = UserHandle.of(currentUserId); - final Context userContext = mContext.createContextAsUser(currentUser, 0); - final UserManager userManager = userContext.getSystemService(UserManager.class); - - if (userManager.isAdminUser()) { - mContext.startActivityAsUser(intent, currentUser); - } else { - mLog.e("Current user (" + currentUserId - + ") is not allowed to perform entitlement check."); - return null; - } - } else { - // For T- devices, there is no other admin user other than the system user. - mContext.startActivity(intent); - } - return intent; - } - private void runTetheringProvisioning( boolean showProvisioningUi, int downstreamType, final TetheringConfiguration config) { if (!config.isCarrierSupportTethering) { @@ -474,9 +506,9 @@ public class EntitlementManager { ResultReceiver receiver = buildProxyReceiver(downstreamType, showProvisioningUi/* notifyFail */, null); if (showProvisioningUi) { - runUiTetherProvisioning(downstreamType, config, receiver); + mDeps.runUiTetherProvisioning(downstreamType, config, receiver); } else { - runSilentTetherProvisioning(downstreamType, config, receiver); + mDeps.runSilentTetherProvisioning(downstreamType, config, receiver); } } @@ -490,20 +522,13 @@ public class EntitlementManager { mContext.startActivity(intent); } - @VisibleForTesting - PendingIntent createRecheckAlarmIntent(final String pkgName) { - final Intent intent = new Intent(ACTION_PROVISIONING_ALARM); - intent.setPackage(pkgName); - return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE); - } - // Not needed to check if this don't run on the handler thread because it's private. private void scheduleProvisioningRecheck(final TetheringConfiguration config) { if (mProvisioningRecheckAlarm == null) { final int period = config.provisioningCheckPeriod; if (period <= 0) return; - mProvisioningRecheckAlarm = createRecheckAlarmIntent(mContext.getPackageName()); + mProvisioningRecheckAlarm = mDeps.createRecheckAlarmIntent(mContext.getPackageName()); AlarmManager alarmManager = (AlarmManager) mContext.getSystemService( Context.ALARM_SERVICE); long triggerAtMillis = SystemClock.elapsedRealtime() + (period * MS_PER_HOUR); @@ -729,7 +754,7 @@ public class EntitlementManager { receiver.send(cacheValue, null); } else { ResultReceiver proxy = buildProxyReceiver(downstream, false/* notifyFail */, receiver); - runUiTetherProvisioning(downstream, config, proxy); + mDeps.runUiTetherProvisioning(downstream, config, proxy); } } } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/EntitlementManagerTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/EntitlementManagerTest.java index 9fa7004298..8626b18b57 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/EntitlementManagerTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/EntitlementManagerTest.java @@ -78,6 +78,7 @@ import android.provider.DeviceConfig; import android.provider.Settings; import android.telephony.CarrierConfigManager; +import androidx.annotation.NonNull; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; @@ -129,9 +130,10 @@ public final class EntitlementManagerTest { private MockContext mMockContext; private Runnable mPermissionChangeCallback; - private WrappedEntitlementManager mEnMgr; + private EntitlementManager mEnMgr; private TetheringConfiguration mConfig; private MockitoSession mMockingSession; + private TestDependencies mDeps; private class MockContext extends BroadcastInterceptingContext { MockContext(Context base) { @@ -163,14 +165,13 @@ public final class EntitlementManagerTest { } } - public class WrappedEntitlementManager extends EntitlementManager { + class TestDependencies extends EntitlementManager.Dependencies { public int fakeEntitlementResult = TETHER_ERROR_ENTITLEMENT_UNKNOWN; public int uiProvisionCount = 0; public int silentProvisionCount = 0; - - public WrappedEntitlementManager(Context ctx, Handler h, SharedLog log, - Runnable callback) { - super(ctx, h, log, callback); + TestDependencies(@NonNull Context context, + @NonNull SharedLog log) { + super(context, log); } public void reset() { @@ -212,7 +213,7 @@ public final class EntitlementManagerTest { Intent intent = super.runSilentTetherProvisioning(type, config, receiver); assertSilentTetherProvisioning(type, config, intent); silentProvisionCount++; - addDownstreamMapping(type, fakeEntitlementResult); + mEnMgr.addDownstreamMapping(type, fakeEntitlementResult); return intent; } @@ -280,9 +281,10 @@ public final class EntitlementManagerTest { doReturn(true).when(mUserManager).isAdminUser(); mMockContext = new MockContext(mContext); + mDeps = new TestDependencies(mMockContext, mLog); mPermissionChangeCallback = spy(() -> { }); - mEnMgr = new WrappedEntitlementManager(mMockContext, new Handler(mLooper.getLooper()), mLog, - mPermissionChangeCallback); + mEnMgr = new EntitlementManager(mMockContext, new Handler(mLooper.getLooper()), mLog, + mPermissionChangeCallback, mDeps); mEnMgr.setOnTetherProvisioningFailedListener(mTetherProvisioningFailedListener); mConfig = new FakeTetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); mEnMgr.setTetheringConfigurationFetcher(() -> { @@ -345,7 +347,7 @@ public final class EntitlementManagerTest { @Test public void testRequestLastEntitlementCacheValue() throws Exception { // 1. Entitlement check is not required. - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; ResultReceiver receiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { @@ -354,8 +356,8 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(0, mDeps.uiProvisionCount); + mDeps.reset(); setupForRequiredProvisioning(); // 2. No cache value and don't need to run entitlement check. @@ -367,10 +369,10 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(0, mDeps.uiProvisionCount); + mDeps.reset(); // 3. No cache value and ui entitlement check is needed. - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; receiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { @@ -379,11 +381,11 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); mLooper.dispatchAll(); - assertEquals(1, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(1, mDeps.uiProvisionCount); + mDeps.reset(); // 4. Cache value is TETHER_ERROR_PROVISIONING_FAILED and don't need to run entitlement // check. - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; receiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { @@ -392,10 +394,10 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(0, mDeps.uiProvisionCount); + mDeps.reset(); // 5. Cache value is TETHER_ERROR_PROVISIONING_FAILED and ui entitlement check is needed. - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; receiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { @@ -404,10 +406,10 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); mLooper.dispatchAll(); - assertEquals(1, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(1, mDeps.uiProvisionCount); + mDeps.reset(); // 6. Cache value is TETHER_ERROR_NO_ERROR. - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; receiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { @@ -416,8 +418,8 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, true); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(0, mDeps.uiProvisionCount); + mDeps.reset(); // 7. Test get value for other downstream type. receiver = new ResultReceiver(null) { @Override @@ -427,10 +429,10 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_USB, receiver, false); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(0, mDeps.uiProvisionCount); + mDeps.reset(); // 8. Test get value for invalid downstream type. - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; receiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { @@ -439,8 +441,8 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI_P2P, receiver, true); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(0, mDeps.uiProvisionCount); + mDeps.reset(); } private void assertPermissionChangeCallback(InOrder inOrder) { @@ -456,7 +458,7 @@ public final class EntitlementManagerTest { final InOrder inOrder = inOrder(mPermissionChangeCallback); setupForRequiredProvisioning(); mEnMgr.notifyUpstream(true); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI, true); mLooper.dispatchAll(); // Permitted: true -> false @@ -468,7 +470,7 @@ public final class EntitlementManagerTest { // Permitted: false -> false assertNoPermissionChange(inOrder); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI, true); mLooper.dispatchAll(); // Permitted: false -> true @@ -481,21 +483,21 @@ public final class EntitlementManagerTest { final InOrder inOrder = inOrder(mPermissionChangeCallback); setupForRequiredProvisioning(); mEnMgr.notifyUpstream(true); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI, true); mLooper.dispatchAll(); // Permitted: true -> false assertPermissionChangeCallback(inOrder); assertFalse(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.startProvisioningIfNeeded(TETHERING_USB, true); mLooper.dispatchAll(); // Permitted: false -> false assertNoPermissionChange(inOrder); assertFalse(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.startProvisioningIfNeeded(TETHERING_BLUETOOTH, true); mLooper.dispatchAll(); // Permitted: false -> false @@ -508,14 +510,14 @@ public final class EntitlementManagerTest { final InOrder inOrder = inOrder(mPermissionChangeCallback); setupForRequiredProvisioning(); mEnMgr.notifyUpstream(true); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI, true); mLooper.dispatchAll(); // Permitted: true -> true assertNoPermissionChange(inOrder); assertTrue(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.startProvisioningIfNeeded(TETHERING_USB, true); mLooper.dispatchAll(); // Permitted: true -> true @@ -544,89 +546,89 @@ public final class EntitlementManagerTest { final InOrder inOrder = inOrder(mPermissionChangeCallback); setupForRequiredProvisioning(); // 1. start ui provisioning, upstream is mobile - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.notifyUpstream(true); mLooper.dispatchAll(); mEnMgr.startProvisioningIfNeeded(TETHERING_USB, true); mLooper.dispatchAll(); - assertEquals(1, mEnMgr.uiProvisionCount); - assertEquals(0, mEnMgr.silentProvisionCount); + assertEquals(1, mDeps.uiProvisionCount); + assertEquals(0, mDeps.silentProvisionCount); // Permitted: true -> true assertNoPermissionChange(inOrder); assertTrue(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.reset(); + mDeps.reset(); // 2. start no-ui provisioning - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI, false); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - assertEquals(1, mEnMgr.silentProvisionCount); + assertEquals(0, mDeps.uiProvisionCount); + assertEquals(1, mDeps.silentProvisionCount); // Permitted: true -> true assertNoPermissionChange(inOrder); assertTrue(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.reset(); + mDeps.reset(); // 3. tear down mobile, then start ui provisioning mEnMgr.notifyUpstream(false); mLooper.dispatchAll(); mEnMgr.startProvisioningIfNeeded(TETHERING_BLUETOOTH, true); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - assertEquals(0, mEnMgr.silentProvisionCount); + assertEquals(0, mDeps.uiProvisionCount); + assertEquals(0, mDeps.silentProvisionCount); assertNoPermissionChange(inOrder); - mEnMgr.reset(); + mDeps.reset(); // 4. switch upstream back to mobile - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.notifyUpstream(true); mLooper.dispatchAll(); - assertEquals(1, mEnMgr.uiProvisionCount); - assertEquals(0, mEnMgr.silentProvisionCount); + assertEquals(1, mDeps.uiProvisionCount); + assertEquals(0, mDeps.silentProvisionCount); // Permitted: true -> true assertNoPermissionChange(inOrder); assertTrue(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.reset(); + mDeps.reset(); // 5. tear down mobile, then switch SIM mEnMgr.notifyUpstream(false); mLooper.dispatchAll(); mEnMgr.reevaluateSimCardProvisioning(mConfig); - assertEquals(0, mEnMgr.uiProvisionCount); - assertEquals(0, mEnMgr.silentProvisionCount); + assertEquals(0, mDeps.uiProvisionCount); + assertEquals(0, mDeps.silentProvisionCount); assertNoPermissionChange(inOrder); - mEnMgr.reset(); + mDeps.reset(); // 6. switch upstream back to mobile again - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.notifyUpstream(true); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - assertEquals(3, mEnMgr.silentProvisionCount); + assertEquals(0, mDeps.uiProvisionCount); + assertEquals(3, mDeps.silentProvisionCount); // Permitted: true -> false assertPermissionChangeCallback(inOrder); assertFalse(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.reset(); + mDeps.reset(); // 7. start ui provisioning, upstream is mobile, downstream is ethernet - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.startProvisioningIfNeeded(TETHERING_ETHERNET, true); mLooper.dispatchAll(); - assertEquals(1, mEnMgr.uiProvisionCount); - assertEquals(0, mEnMgr.silentProvisionCount); + assertEquals(1, mDeps.uiProvisionCount); + assertEquals(0, mDeps.silentProvisionCount); // Permitted: false -> true assertPermissionChangeCallback(inOrder); assertTrue(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.reset(); + mDeps.reset(); // 8. downstream is invalid - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI_P2P, true); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - assertEquals(0, mEnMgr.silentProvisionCount); + assertEquals(0, mDeps.uiProvisionCount); + assertEquals(0, mDeps.silentProvisionCount); assertNoPermissionChange(inOrder); - mEnMgr.reset(); + mDeps.reset(); } @Test @@ -634,12 +636,12 @@ public final class EntitlementManagerTest { setupForRequiredProvisioning(); verify(mTetherProvisioningFailedListener, times(0)) .onTetherProvisioningFailed(TETHERING_WIFI, FAILED_TETHERING_REASON); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.notifyUpstream(true); mLooper.dispatchAll(); mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI, true); mLooper.dispatchAll(); - assertEquals(1, mEnMgr.uiProvisionCount); + assertEquals(1, mDeps.uiProvisionCount); verify(mTetherProvisioningFailedListener, times(1)) .onTetherProvisioningFailed(TETHERING_WIFI, FAILED_TETHERING_REASON); } @@ -662,13 +664,13 @@ public final class EntitlementManagerTest { setupForRequiredProvisioning(); doReturn(isAdminUser).when(mUserManager).isAdminUser(); - mEnMgr.reset(); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.reset(); + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.notifyUpstream(true); mLooper.dispatchAll(); mEnMgr.startProvisioningIfNeeded(TETHERING_USB, true); mLooper.dispatchAll(); - assertEquals(expectedUiProvisionCount, mEnMgr.uiProvisionCount); + assertEquals(expectedUiProvisionCount, mDeps.uiProvisionCount); } @Test @@ -683,7 +685,7 @@ public final class EntitlementManagerTest { assertTrue(mEnMgr.isCellularUpstreamPermitted()); // If second downstream run entitlement check fail, cellular upstream is not permitted. - mEnMgr.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; + mDeps.fakeEntitlementResult = TETHER_ERROR_PROVISIONING_FAILED; mEnMgr.notifyUpstream(true); mLooper.dispatchAll(); mEnMgr.startProvisioningIfNeeded(TETHERING_USB, true); @@ -691,7 +693,7 @@ public final class EntitlementManagerTest { assertFalse(mEnMgr.isCellularUpstreamPermitted()); // When second downstream is down, exempted downstream can use cellular upstream. - assertEquals(1, mEnMgr.uiProvisionCount); + assertEquals(1, mDeps.uiProvisionCount); verify(mTetherProvisioningFailedListener).onTetherProvisioningFailed(TETHERING_USB, FAILED_TETHERING_REASON); mEnMgr.stopProvisioningIfNeeded(TETHERING_USB); @@ -712,7 +714,7 @@ public final class EntitlementManagerTest { setupForRequiredProvisioning(); assertFalse(mEnMgr.isCellularUpstreamPermitted()); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; mEnMgr.notifyUpstream(true); mLooper.dispatchAll(); mEnMgr.startProvisioningIfNeeded(TETHERING_WIFI, true); @@ -734,7 +736,7 @@ public final class EntitlementManagerTest { throws Exception { setupCarrierConfig(false); setupForRequiredProvisioning(); - mEnMgr.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; + mDeps.fakeEntitlementResult = TETHER_ERROR_NO_ERROR; ResultReceiver receiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { @@ -743,8 +745,8 @@ public final class EntitlementManagerTest { }; mEnMgr.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, false); mLooper.dispatchAll(); - assertEquals(0, mEnMgr.uiProvisionCount); - mEnMgr.reset(); + assertEquals(0, mDeps.uiProvisionCount); + mDeps.reset(); } @Test