diff options
author | Po-Chun Lee <pochunlee@google.com> | 2024-02-20 08:20:36 +0000 |
---|---|---|
committer | Po-Chun Lee <pochunlee@google.com> | 2024-02-21 01:51:47 +0000 |
commit | 792a63cc312aebbbb05d0dec01d71b0e003ecf7f (patch) | |
tree | 1bc4b29252594d3dcc121c226fd85349ff0a66f1 | |
parent | 115c31c494e035f91957bf78d0e954b671f8a5a3 (diff) | |
download | Iwlan-792a63cc312aebbbb05d0dec01d71b0e003ecf7f.tar.gz |
Add IwlanCarrierConfig#putTestConfig*() utilities
To accommodate limitations in the current Mockito version for Android,
which lacks support for spying on static classes, this update introduces
test stub functions within IwlanCarrierConfig. This approach circumvents
the need for static mocking.
Test: atest
Bug: 326011118
Change-Id: I0cbf1753e263474295ff108b68e95f1052438cc7
-rw-r--r-- | Android.bp | 7 | ||||
-rw-r--r-- | src/com/google/android/iwlan/IwlanCarrierConfig.java | 78 | ||||
-rw-r--r-- | src/com/google/android/iwlan/IwlanDataService.java | 14 | ||||
-rw-r--r-- | test/com/google/android/iwlan/IwlanDataServiceTest.java | 95 |
4 files changed, 113 insertions, 81 deletions
@@ -5,8 +5,8 @@ package { genrule { name: "statslog-Iwlan-java-gen", tools: ["stats-log-api-gen"], - cmd: "$(location stats-log-api-gen) --java $(out) --module iwlan --javaPackage com.google.android.iwlan" - + " --javaClass IwlanStatsLog", + cmd: "$(location stats-log-api-gen) --java $(out) --module iwlan --javaPackage com.google.android.iwlan" + + " --javaClass IwlanStatsLog", out: ["com/google/android/iwlan/IwlanStatsLog.java"], } @@ -29,11 +29,12 @@ android_app { libs: [ "android.net.ipsec.ike.stubs.system", + "androidx.annotation_annotation", "auto_value_annotations", "framework-annotations-lib", "framework-connectivity", "framework-wifi", - "modules-utils-handlerexecutor" + "modules-utils-handlerexecutor", ], plugins: ["auto_value_plugin"], diff --git a/src/com/google/android/iwlan/IwlanCarrierConfig.java b/src/com/google/android/iwlan/IwlanCarrierConfig.java index fcaff90..e68341a 100644 --- a/src/com/google/android/iwlan/IwlanCarrierConfig.java +++ b/src/com/google/android/iwlan/IwlanCarrierConfig.java @@ -21,6 +21,8 @@ import android.os.PersistableBundle; import android.support.annotation.NonNull; import android.telephony.CarrierConfigManager; +import androidx.annotation.VisibleForTesting; + /** Class for handling IWLAN carrier configuration. */ public class IwlanCarrierConfig { static final String PREFIX = "iwlan."; @@ -66,10 +68,12 @@ public class IwlanCarrierConfig { */ public static final boolean DEFAULT_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL = true; - private static PersistableBundle mHiddenBundle = new PersistableBundle(); + private static PersistableBundle sTestBundle = new PersistableBundle(); + + private static PersistableBundle sHiddenBundle = new PersistableBundle(); static { - mHiddenBundle = createHiddenDefaultConfig(); + sHiddenBundle = createHiddenDefaultConfig(); } /** @@ -103,13 +107,17 @@ public class IwlanCarrierConfig { } private static PersistableBundle getDefaultConfig(String key) { + if (sTestBundle.containsKey(key)) { + return sTestBundle; + } + PersistableBundle bundle = CarrierConfigManager.getDefaultConfig(); if (bundle.containsKey(key)) { return bundle; } - if (mHiddenBundle.containsKey(key)) { - return mHiddenBundle; + if (sHiddenBundle.containsKey(key)) { + return sHiddenBundle; } throw new IllegalArgumentException("Default config not found for key: " + key); @@ -289,6 +297,7 @@ public class IwlanCarrierConfig { public static boolean getDefaultConfigBoolean(String key) { return getDefaultConfig(key).getBoolean(key); } + /** * Gets the default configuration int[] value for a given key. * @@ -310,6 +319,7 @@ public class IwlanCarrierConfig { public static long[] getDefaultConfigLongArray(String key) { return getDefaultConfig(key).getLongArray(key); } + /** * Gets the default configuration double[] value for a given key. * @@ -342,4 +352,64 @@ public class IwlanCarrierConfig { public static boolean[] getDefaultConfigBooleanArray(String key) { return getDefaultConfig(key).getBooleanArray(key); } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigBundle(PersistableBundle bundle) { + sTestBundle.putAll(bundle); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigInt(@NonNull String key, int value) { + sTestBundle.putInt(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigLong(@NonNull String key, long value) { + sTestBundle.putLong(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigDouble(@NonNull String key, double value) { + sTestBundle.putDouble(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigBoolean(@NonNull String key, boolean value) { + sTestBundle.putBoolean(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigString(@NonNull String key, String value) { + sTestBundle.putString(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigIntArray(@NonNull String key, @NonNull int[] value) { + sTestBundle.putIntArray(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigLongArray(@NonNull String key, @NonNull long[] value) { + sTestBundle.putLongArray(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigDoubleArray(@NonNull String key, @NonNull double[] value) { + sTestBundle.putDoubleArray(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigBooleanArray(@NonNull String key, @NonNull boolean[] value) { + sTestBundle.putBooleanArray(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void putTestConfigStringArray(@NonNull String key, @NonNull String[] value) { + sTestBundle.putStringArray(key, value); + } + + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + static void resetTestConfig() { + sTestBundle.clear(); + } } diff --git a/src/com/google/android/iwlan/IwlanDataService.java b/src/com/google/android/iwlan/IwlanDataService.java index c1c8bc3..7c680ea 100644 --- a/src/com/google/android/iwlan/IwlanDataService.java +++ b/src/com/google/android/iwlan/IwlanDataService.java @@ -1257,10 +1257,10 @@ public class IwlanDataService extends DataService { protected boolean isN1ModeSupported() { int[] nrAvailabilities = - IwlanHelper.getConfig( - CarrierConfigManager.KEY_CARRIER_NR_AVAILABILITIES_INT_ARRAY, + IwlanCarrierConfig.getConfigIntArray( mContext, - getSlotIndex()); + getSlotIndex(), + CarrierConfigManager.KEY_CARRIER_NR_AVAILABILITIES_INT_ARRAY); Log.d( TAG, "KEY_CARRIER_NR_AVAILABILITIES_INT_ARRAY : " @@ -1518,11 +1518,11 @@ public class IwlanDataService extends DataService { if (cellInfolist != null && iwlanDataServiceProvider.isRegisteredCellInfoChanged(cellInfolist)) { int[] addrResolutionMethods = - IwlanHelper.getConfig( - CarrierConfigManager.Iwlan - .KEY_EPDG_ADDRESS_PRIORITY_INT_ARRAY, + IwlanCarrierConfig.getConfigIntArray( mContext, - iwlanDataServiceProvider.getSlotIndex()); + iwlanDataServiceProvider.getSlotIndex(), + CarrierConfigManager.Iwlan + .KEY_EPDG_ADDRESS_PRIORITY_INT_ARRAY); for (int addrResolutionMethod : addrResolutionMethods) { if (addrResolutionMethod == CarrierConfigManager.Iwlan.EPDG_ADDRESS_CELLULAR_LOC) { diff --git a/test/com/google/android/iwlan/IwlanDataServiceTest.java b/test/com/google/android/iwlan/IwlanDataServiceTest.java index bb53780..6168b82 100644 --- a/test/com/google/android/iwlan/IwlanDataServiceTest.java +++ b/test/com/google/android/iwlan/IwlanDataServiceTest.java @@ -145,7 +145,6 @@ public class IwlanDataServiceTest { @Mock private LinkAddress mMockIPv6LinkAddress; @Mock private Inet4Address mMockInet4Address; @Mock private Inet6Address mMockInet6Address; - @Mock private CarrierConfigManager mMockCarrierConfigManager; @Mock private FeatureFlags mFakeFeatureFlags; MockitoSession mStaticMockSession; @@ -216,7 +215,6 @@ public class IwlanDataServiceTest { .mockStatic(ErrorPolicyManager.class) .mockStatic(IwlanBroadcastReceiver.class) .mockStatic(SubscriptionManager.class) - .mockStatic(IwlanCarrierConfig.class) .strictness(Strictness.LENIENT) .startMocking(); @@ -265,8 +263,6 @@ public class IwlanDataServiceTest { mIwlanDataService = spy(new IwlanDataService(mFakeFeatureFlags)); - when(mMockContext.getSystemService(eq(CarrierConfigManager.class))) - .thenReturn(mMockCarrierConfigManager); // Injects the test looper into the IwlanDataServiceHandler doReturn(mTestLooper.getLooper()).when(mIwlanDataService).getLooper(); mIwlanDataService.setAppContext(mMockContext); @@ -284,8 +280,9 @@ public class IwlanDataServiceTest { when(mMockConnectivityManager.getLinkProperties(eq(mMockNetwork))) .thenReturn(mLinkProperties); - when(mMockTunnelLinkProperties.ifaceName()).thenReturn("mockipsec0"); + + mockCarrierConfigForN1Mode(true); } private void moveTimeForwardAndDispatch(long milliSeconds) { @@ -296,6 +293,7 @@ public class IwlanDataServiceTest { @After public void cleanUp() throws Exception { mStaticMockSession.finishMocking(); + IwlanCarrierConfig.resetTestConfig(); mSpyIwlanDataServiceProvider.close(); mTestLooper.dispatchAll(); if (mIwlanDataService != null) { @@ -928,11 +926,8 @@ public class IwlanDataServiceTest { public void testDeactivateDataCall_DelayedReleaseAfterHandover() { DataProfile dp = buildImsDataProfile(); - when(IwlanCarrierConfig.getConfigInt( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT)) - .thenReturn(3); + IwlanCarrierConfig.putTestConfigInt( + IwlanCarrierConfig.KEY_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT, 3); onSystemDefaultNetworkConnected(TRANSPORT_WIFI); mSpyIwlanDataServiceProvider.setTunnelState( @@ -984,11 +979,8 @@ public class IwlanDataServiceTest { public void testDeactivateDataCall_DelayedReleaseAfterHandover_NetworkReleaseBeforeDelay() { DataProfile dp = buildImsDataProfile(); - when(IwlanCarrierConfig.getConfigInt( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT)) - .thenReturn(3); + IwlanCarrierConfig.putTestConfigInt( + IwlanCarrierConfig.KEY_HANDOVER_TO_WWAN_RELEASE_DELAY_SECOND_INT, 3); when(ErrorPolicyManager.getInstance(eq(mMockContext), eq(DEFAULT_SLOT_INDEX))) .thenReturn(mMockErrorPolicyManager); when(mMockErrorPolicyManager.getDataFailCause(eq(TEST_APN_NAME))) @@ -2089,15 +2081,6 @@ public class IwlanDataServiceTest { .build(); } - private void setupMockForGetConfig(PersistableBundle bundle) { - if (bundle == null) { - bundle = new PersistableBundle(); - } - when(mMockContext.getSystemService(eq(CarrierConfigManager.class))) - .thenReturn(mMockCarrierConfigManager); - when(mMockCarrierConfigManager.getConfigForSubId(DEFAULT_SLOT_INDEX)).thenReturn(bundle); - } - private void mockCarrierConfigForN1Mode(boolean supportN1Mode) { PersistableBundle bundle = new PersistableBundle(); if (supportN1Mode) { @@ -2112,7 +2095,7 @@ public class IwlanDataServiceTest { CarrierConfigManager.KEY_CARRIER_NR_AVAILABILITIES_INT_ARRAY, new int[] {CarrierConfigManager.CARRIER_NR_AVAILABILITY_NSA}); } - setupMockForGetConfig(bundle); + IwlanCarrierConfig.putTestConfigBundle(bundle); } private void mockCallState(int callState) { @@ -2149,7 +2132,7 @@ public class IwlanDataServiceTest { CarrierConfigManager.CARRIER_NR_AVAILABILITY_NSA, CarrierConfigManager.CARRIER_NR_AVAILABILITY_SA }); - setupMockForGetConfig(bundle); + IwlanCarrierConfig.putTestConfigBundle(bundle); assertTrue(mSpyIwlanDataServiceProvider.isN1ModeSupported()); bundle.putIntArray( @@ -2157,18 +2140,15 @@ public class IwlanDataServiceTest { new int[] { CarrierConfigManager.CARRIER_NR_AVAILABILITY_NSA, }); - setupMockForGetConfig(bundle); + IwlanCarrierConfig.putTestConfigBundle(bundle); assertFalse(mSpyIwlanDataServiceProvider.isN1ModeSupported()); } @Test public void testMultipleAllowedNetworkTypeChangeInIdle_updateN1Mode() throws Exception { mockCarrierConfigForN1Mode(true); - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL)) - .thenReturn(true); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, true); mockCallState(CALL_STATE_IDLE); mockSetupDataCallWithPduSessionId(0); updatePreferredNetworkType(NETWORK_TYPE_BITMASK_NR); @@ -2197,11 +2177,8 @@ public class IwlanDataServiceTest { public void testMultipleAllowedNetworkTypeChangeInCall_preferenceChanged_updateAfterCallEnds() throws Exception { mockCarrierConfigForN1Mode(true); - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL)) - .thenReturn(true); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, true); mockCallState(CALL_STATE_RINGING); mockSetupDataCallWithPduSessionId(0); @@ -2235,11 +2212,8 @@ public class IwlanDataServiceTest { public void testMultipleAllowedNetworkTypeChangeInCall_preferenceNotChanged_noUpdate() throws Exception { mockCarrierConfigForN1Mode(true); - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL)) - .thenReturn(true); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, true); mockCallState(CALL_STATE_RINGING); mockSetupDataCallWithPduSessionId(0); @@ -2266,11 +2240,8 @@ public class IwlanDataServiceTest { @Test public void testOnAllowedNetworkTypeChange_flagDisabled_noTunnelClose() { mockCarrierConfigForN1Mode(true); - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL)) - .thenReturn(false); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, false); mockCallState(CALL_STATE_IDLE); mockSetupDataCallWithPduSessionId(0); @@ -2283,11 +2254,8 @@ public class IwlanDataServiceTest { @Test public void testOnAllowedNetworkTypeChange_n1ModeNotSupported_noTunnelClose() { mockCarrierConfigForN1Mode(false); - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL)) - .thenReturn(true); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, true); mockCallState(CALL_STATE_IDLE); mockSetupDataCallWithPduSessionId(0); @@ -2300,11 +2268,8 @@ public class IwlanDataServiceTest { @Test public void testN1ModeNotSupported_tunnelBringupWithNoN1ModeCapability() { mockCarrierConfigForN1Mode(false); - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL)) - .thenReturn(true); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, true); mockSetupDataCallWithPduSessionId(1); @@ -2319,11 +2284,8 @@ public class IwlanDataServiceTest { @Test public void testNoN1ModeCapabilityInOngoingDataCall_newTunnelBringup_doNotIncludeN1() { mockCarrierConfigForN1Mode(true); - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL)) - .thenReturn(true); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_UPDATE_N1_MODE_ON_UI_CHANGE_BOOL, true); mockSetupDataCallWithPduSessionId(0); @@ -2376,6 +2338,7 @@ public class IwlanDataServiceTest { @Test public void testN1ModeForEmergencySession() { int pduSessionId = 5; + updatePreferredNetworkType(NETWORK_TYPE_BITMASK_NR); DataProfile dp = buildDataProfile(ApnSetting.TYPE_EMERGENCY); verifySetupDataCallRequestHandled(pduSessionId, dp); @@ -2389,11 +2352,9 @@ public class IwlanDataServiceTest { @Test public void testN1ModeExclusionForEmergencySession() { - when(IwlanCarrierConfig.getConfigBoolean( - mMockContext, - DEFAULT_SLOT_INDEX, - IwlanCarrierConfig.KEY_N1_MODE_EXCLUSION_FOR_EMERGENCY_SESSION_BOOL)) - .thenReturn(true); + IwlanCarrierConfig.putTestConfigBoolean( + IwlanCarrierConfig.KEY_N1_MODE_EXCLUSION_FOR_EMERGENCY_SESSION_BOOL, true); + updatePreferredNetworkType(NETWORK_TYPE_BITMASK_NR); DataProfile dp = buildDataProfile(ApnSetting.TYPE_EMERGENCY); verifySetupDataCallRequestHandled(5 /* pduSessionId */, dp); |