Merge "[MS42.2] Adopt TelephonyDisplayInfo in unit test" am: 6a360b5a71

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1938693

Change-Id: I72c3175fe3dce0800051d854928a1227d6f5c225
This commit is contained in:
Junyu Lai
2022-01-10 12:06:47 +00:00
committed by Automerger Merge Worker

View File

@@ -16,6 +16,9 @@
package com.android.server.net; package com.android.server.net;
import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE;
import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@@ -36,10 +39,9 @@ import android.content.Context;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.os.Build; import android.os.Build;
import android.os.Looper; import android.os.Looper;
import android.telephony.NetworkRegistrationInfo; import android.os.Parcel;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyDisplayInfo;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.SparseArray; import android.util.SparseArray;
@@ -121,14 +123,29 @@ public final class NetworkStatsSubscriptionsMonitorTest {
return list; return list;
} }
private TelephonyDisplayInfo makeTelephonyDisplayInfo(
int networkType, int overrideNetworkType) {
// Create from parcel since final classes cannot be mocked and there is no exposed public
// constructors.
Parcel p = Parcel.obtain();
p.writeInt(networkType);
p.writeInt(overrideNetworkType);
p.setDataPosition(0);
return TelephonyDisplayInfo.CREATOR.createFromParcel(p);
}
private void setRatTypeForSub(int subId, int type) { private void setRatTypeForSub(int subId, int type) {
final ServiceState serviceState = mock(ServiceState.class); setRatTypeForSub(subId, type, OVERRIDE_NETWORK_TYPE_NONE);
when(serviceState.getDataNetworkType()).thenReturn(type); }
private void setRatTypeForSub(int subId, int type, int overrideType) {
final TelephonyDisplayInfo displayInfo = makeTelephonyDisplayInfo(type, overrideType);
final RatTypeListener match = mRatTypeListenerOfSub.get(subId); final RatTypeListener match = mRatTypeListenerOfSub.get(subId);
if (match == null) { if (match == null) {
fail("Could not find listener with subId: " + subId); fail("Could not find listener with subId: " + subId);
} }
match.onServiceStateChanged(serviceState); match.onDisplayInfoChanged(displayInfo);
} }
private void addTestSub(int subId, String subscriberId) { private void addTestSub(int subId, String subscriberId) {
@@ -155,11 +172,11 @@ public final class NetworkStatsSubscriptionsMonitorTest {
mMonitor.onSubscriptionsChanged(); mMonitor.onSubscriptionsChanged();
} }
private void assertAndCaptureRatTypeListenerRegistrationWith(int subId, int listenedState) { private void assertAndCaptureRatTypeListenerRegistration(int subId) {
final ArgumentCaptor<RatTypeListener> ratTypeListenerCaptor = final ArgumentCaptor<RatTypeListener> ratTypeListenerCaptor =
ArgumentCaptor.forClass(RatTypeListener.class); ArgumentCaptor.forClass(RatTypeListener.class);
verify(mTelephonyManagerOfSub.get(subId)).listen(ratTypeListenerCaptor.capture(), verify(mTelephonyManagerOfSub.get(subId))
eq(listenedState)); .registerTelephonyCallback(any(), ratTypeListenerCaptor.capture());
final RatTypeListener listener = CollectionUtils final RatTypeListener listener = CollectionUtils
.find(ratTypeListenerCaptor.getAllValues(), it -> it.getSubId() == subId); .find(ratTypeListenerCaptor.getAllValues(), it -> it.getSubId() == subId);
assertNotNull(listener); assertNotNull(listener);
@@ -172,8 +189,14 @@ public final class NetworkStatsSubscriptionsMonitorTest {
final int[] subList = convertArrayListToIntArray(mTestSubList); final int[] subList = convertArrayListToIntArray(mTestSubList);
when(mSubscriptionManager.getCompleteActiveSubscriptionIdList()).thenReturn(subList); when(mSubscriptionManager.getCompleteActiveSubscriptionIdList()).thenReturn(subList);
mMonitor.onSubscriptionsChanged(); mMonitor.onSubscriptionsChanged();
assertRatTypeListenerDeregistration(subId);
mRatTypeListenerOfSub.delete(subId); mRatTypeListenerOfSub.delete(subId);
// Keep TelephonyManagerOfSubs so the test could verify de-registration. mTelephonyManagerOfSub.delete(subId);
}
private void assertRatTypeListenerDeregistration(int subId) {
verify(mTelephonyManagerOfSub.get(subId))
.unregisterTelephonyCallback(eq(mRatTypeListenerOfSub.get(subId)));
} }
private void assertRatTypeChangedForSub(String subscriberId, int ratType) { private void assertRatTypeChangedForSub(String subscriberId, int ratType) {
@@ -203,10 +226,8 @@ public final class NetworkStatsSubscriptionsMonitorTest {
// Insert sim2. // Insert sim2.
addTestSub(TEST_SUBID2, TEST_IMSI2); addTestSub(TEST_SUBID2, TEST_IMSI2);
assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertAndCaptureRatTypeListenerRegistration(TEST_SUBID1);
PhoneStateListener.LISTEN_SERVICE_STATE); assertAndCaptureRatTypeListenerRegistration(TEST_SUBID2);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID2,
PhoneStateListener.LISTEN_SERVICE_STATE);
reset(mDelegate); reset(mDelegate);
// Set RAT type of sim1 to UMTS. // Set RAT type of sim1 to UMTS.
@@ -230,8 +251,6 @@ public final class NetworkStatsSubscriptionsMonitorTest {
// Remove sim2 and verify that callbacks are fired and RAT type is correct for sim2. // Remove sim2 and verify that callbacks are fired and RAT type is correct for sim2.
// while the other two remain untouched. // while the other two remain untouched.
removeTestSub(TEST_SUBID2); removeTestSub(TEST_SUBID2);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID2,
PhoneStateListener.LISTEN_NONE);
assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UMTS); assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UMTS);
assertRatTypeChangedForSub(TEST_IMSI2, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeChangedForSub(TEST_IMSI2, TelephonyManager.NETWORK_TYPE_UNKNOWN);
assertRatTypeNotChangedForSub(TEST_IMSI3, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeNotChangedForSub(TEST_IMSI3, TelephonyManager.NETWORK_TYPE_UNKNOWN);
@@ -244,10 +263,7 @@ public final class NetworkStatsSubscriptionsMonitorTest {
reset(mDelegate); reset(mDelegate);
mMonitor.stop(); mMonitor.stop();
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertRatTypeListenerDeregistration(TEST_SUBID1);
PhoneStateListener.LISTEN_NONE);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID2,
PhoneStateListener.LISTEN_NONE);
assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
} }
@@ -259,29 +275,26 @@ public final class NetworkStatsSubscriptionsMonitorTest {
// before changing RAT type. Also capture listener for later use. // before changing RAT type. Also capture listener for later use.
addTestSub(TEST_SUBID1, TEST_IMSI1); addTestSub(TEST_SUBID1, TEST_IMSI1);
assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertAndCaptureRatTypeListenerRegistration(TEST_SUBID1);
PhoneStateListener.LISTEN_SERVICE_STATE);
final RatTypeListener listener = mRatTypeListenerOfSub.get(TEST_SUBID1); final RatTypeListener listener = mRatTypeListenerOfSub.get(TEST_SUBID1);
// Set RAT type to 5G NSA (non-standalone) mode, verify the monitor outputs // Set RAT type to 5G NSA (non-standalone) mode, verify the monitor outputs
// NETWORK_TYPE_5G_NSA. // NETWORK_TYPE_5G_NSA.
final ServiceState serviceState = mock(ServiceState.class); setRatTypeForSub(TEST_SUBID1, TelephonyManager.NETWORK_TYPE_LTE,
when(serviceState.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_LTE); OVERRIDE_NETWORK_TYPE_NR_NSA);
when(serviceState.getNrState()).thenReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED);
listener.onServiceStateChanged(serviceState);
assertRatTypeChangedForSub(TEST_IMSI1, NetworkTemplate.NETWORK_TYPE_5G_NSA); assertRatTypeChangedForSub(TEST_IMSI1, NetworkTemplate.NETWORK_TYPE_5G_NSA);
reset(mDelegate); reset(mDelegate);
// Set RAT type to LTE without NR connected, the RAT type should be downgraded to LTE. // Set RAT type to LTE without NR connected, the RAT type should be downgraded to LTE.
when(serviceState.getNrState()).thenReturn(NetworkRegistrationInfo.NR_STATE_NONE); setRatTypeForSub(TEST_SUBID1, TelephonyManager.NETWORK_TYPE_LTE,
listener.onServiceStateChanged(serviceState); OVERRIDE_NETWORK_TYPE_NONE);
assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_LTE); assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_LTE);
reset(mDelegate); reset(mDelegate);
// Verify NR connected with other RAT type does not take effect. // Verify NR connected with other RAT type does not take effect.
when(serviceState.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_UMTS); // This should not be happened in practice.
when(serviceState.getNrState()).thenReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED); setRatTypeForSub(TEST_SUBID1, TelephonyManager.NETWORK_TYPE_UMTS,
listener.onServiceStateChanged(serviceState); OVERRIDE_NETWORK_TYPE_NR_NSA);
assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UMTS); assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UMTS);
reset(mDelegate); reset(mDelegate);
@@ -291,9 +304,7 @@ public final class NetworkStatsSubscriptionsMonitorTest {
reset(mDelegate); reset(mDelegate);
// Set NR state to none in standalone mode does not change anything. // Set NR state to none in standalone mode does not change anything.
when(serviceState.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_NR); setRatTypeForSub(TEST_SUBID1, TelephonyManager.NETWORK_TYPE_NR, OVERRIDE_NETWORK_TYPE_NONE);
when(serviceState.getNrState()).thenReturn(NetworkRegistrationInfo.NR_STATE_NONE);
listener.onServiceStateChanged(serviceState);
assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_NR); assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_NR);
} }
@@ -309,8 +320,7 @@ public final class NetworkStatsSubscriptionsMonitorTest {
// Set IMSI for sim1, verify the listener will be registered. // Set IMSI for sim1, verify the listener will be registered.
updateSubscriberIdForTestSub(TEST_SUBID1, TEST_IMSI1); updateSubscriberIdForTestSub(TEST_SUBID1, TEST_IMSI1);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertAndCaptureRatTypeListenerRegistration(TEST_SUBID1);
PhoneStateListener.LISTEN_SERVICE_STATE);
reset(mTelephonyManager); reset(mTelephonyManager);
// Set RAT type of sim1 to UMTS. Verify RAT type of sim1 is changed. // Set RAT type of sim1 to UMTS. Verify RAT type of sim1 is changed.
@@ -321,8 +331,7 @@ public final class NetworkStatsSubscriptionsMonitorTest {
// Set IMSI to null again to simulate somehow IMSI is not available, such as // Set IMSI to null again to simulate somehow IMSI is not available, such as
// modem crash. Verify service should unregister listener. // modem crash. Verify service should unregister listener.
updateSubscriberIdForTestSub(TEST_SUBID1, null); updateSubscriberIdForTestSub(TEST_SUBID1, null);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertRatTypeListenerDeregistration(TEST_SUBID1);
PhoneStateListener.LISTEN_NONE);
assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
reset(mDelegate); reset(mDelegate);
clearInvocations(mTelephonyManagerOfSub.get(TEST_SUBID1)); clearInvocations(mTelephonyManagerOfSub.get(TEST_SUBID1));
@@ -332,8 +341,7 @@ public final class NetworkStatsSubscriptionsMonitorTest {
final ArgumentCaptor<RatTypeListener> ratTypeListenerCaptor2 = final ArgumentCaptor<RatTypeListener> ratTypeListenerCaptor2 =
ArgumentCaptor.forClass(RatTypeListener.class); ArgumentCaptor.forClass(RatTypeListener.class);
updateSubscriberIdForTestSub(TEST_SUBID1, TEST_IMSI1); updateSubscriberIdForTestSub(TEST_SUBID1, TEST_IMSI1);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertAndCaptureRatTypeListenerRegistration(TEST_SUBID1);
PhoneStateListener.LISTEN_SERVICE_STATE);
assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
reset(mDelegate); reset(mDelegate);
clearInvocations(mTelephonyManagerOfSub.get(TEST_SUBID1)); clearInvocations(mTelephonyManagerOfSub.get(TEST_SUBID1));
@@ -344,8 +352,7 @@ public final class NetworkStatsSubscriptionsMonitorTest {
reset(mDelegate); reset(mDelegate);
mMonitor.stop(); mMonitor.stop();
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertRatTypeListenerDeregistration(TEST_SUBID1);
PhoneStateListener.LISTEN_NONE);
assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
} }
@@ -361,8 +368,7 @@ public final class NetworkStatsSubscriptionsMonitorTest {
// Insert sim1, verify RAT type is NETWORK_TYPE_UNKNOWN, and never get any callback // Insert sim1, verify RAT type is NETWORK_TYPE_UNKNOWN, and never get any callback
// before changing RAT type. // before changing RAT type.
addTestSub(TEST_SUBID1, TEST_IMSI1); addTestSub(TEST_SUBID1, TEST_IMSI1);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertAndCaptureRatTypeListenerRegistration(TEST_SUBID1);
PhoneStateListener.LISTEN_SERVICE_STATE);
assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeNotChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
// Set RAT type of sim1 to UMTS. // Set RAT type of sim1 to UMTS.
@@ -377,10 +383,9 @@ public final class NetworkStatsSubscriptionsMonitorTest {
// NETWORK_TYPE_UNKNOWN until received initial callback from telephony. // NETWORK_TYPE_UNKNOWN until received initial callback from telephony.
updateSubscriberIdForTestSub(TEST_SUBID1, TEST_IMSI2); updateSubscriberIdForTestSub(TEST_SUBID1, TEST_IMSI2);
final RatTypeListener oldListener = mRatTypeListenerOfSub.get(TEST_SUBID1); final RatTypeListener oldListener = mRatTypeListenerOfSub.get(TEST_SUBID1);
assertAndCaptureRatTypeListenerRegistrationWith(TEST_SUBID1, assertAndCaptureRatTypeListenerRegistration(TEST_SUBID1);
PhoneStateListener.LISTEN_SERVICE_STATE);
verify(mTelephonyManagerOfSub.get(TEST_SUBID1), times(1)) verify(mTelephonyManagerOfSub.get(TEST_SUBID1), times(1))
.listen(eq(oldListener), eq(PhoneStateListener.LISTEN_NONE)); .unregisterTelephonyCallback(eq(oldListener));
assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeChangedForSub(TEST_IMSI1, TelephonyManager.NETWORK_TYPE_UNKNOWN);
assertRatTypeNotChangedForSub(TEST_IMSI2, TelephonyManager.NETWORK_TYPE_UNKNOWN); assertRatTypeNotChangedForSub(TEST_IMSI2, TelephonyManager.NETWORK_TYPE_UNKNOWN);
reset(mDelegate); reset(mDelegate);