diff options
author | Googler <noreply@google.com> | 2024-04-22 10:18:36 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-04-22 10:19:14 -0700 |
commit | 12d31cc0b137d71355adb07a560294ce6c02374e (patch) | |
tree | e87b5e65a9d365a985857e699b23726f5ae55a06 | |
parent | cf26305eee97a511071b71b5693e81a8924981ab (diff) | |
download | robolectric-12d31cc0b137d71355adb07a560294ce6c02374e.tar.gz |
Add carrier restriction rules API to ShadowTelephonyManager
Implement a shadow of Telephony.getCarrierRestrictionRules() and a setter to configure the network lock state for test.
PiperOrigin-RevId: 627078587
-rw-r--r-- | robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java | 18 | ||||
-rw-r--r-- | shadows/framework/src/main/java/org/robolectric/shadows/ShadowTelephonyManager.java | 22 |
2 files changed, 40 insertions, 0 deletions
diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java index 6da61d037..c3535c043 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java @@ -53,6 +53,7 @@ import android.net.Uri; import android.os.Build; import android.os.PersistableBundle; import android.telecom.PhoneAccountHandle; +import android.telephony.CarrierRestrictionRules; import android.telephony.CellInfo; import android.telephony.CellLocation; import android.telephony.PhoneCapability; @@ -1512,4 +1513,21 @@ public class ShadowTelephonyManagerTest { shadowOf(telephonyManager).setDataRoamingEnabled(true); assertThat(telephonyManager.isDataRoamingEnabled()).isTrue(); } + + @Test + @Config(minSdk = Q) + public void setCarrierRestrictionRules_changesCarrierRestrictionRules() { + CarrierRestrictionRules carrierRestrictionRules = CarrierRestrictionRules.newBuilder().build(); + shadowOf(telephonyManager).setCarrierRestrictionRules(carrierRestrictionRules); + + assertThat(telephonyManager.getCarrierRestrictionRules()).isEqualTo(carrierRestrictionRules); + } + + @Test() + @Config(minSdk = Q) + public void setCarrierRestrictionRules_throwsIllegalStateException() { + assertThrows( + IllegalStateException.class, + () -> shadowTelephonyManager.setCarrierRestrictionRules(new Object())); + } } diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTelephonyManager.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTelephonyManager.java index bdf1d08bd..249d53483 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTelephonyManager.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTelephonyManager.java @@ -33,6 +33,7 @@ import android.os.SystemProperties; import android.telecom.PhoneAccountHandle; import android.telephony.Annotation.NetworkType; import android.telephony.Annotation.OverrideNetworkType; +import android.telephony.CarrierRestrictionRules; import android.telephony.CellInfo; import android.telephony.CellLocation; import android.telephony.PhoneStateListener; @@ -179,6 +180,7 @@ public class ShadowTelephonyManager { private static volatile boolean emergencyCallbackMode; private static Map<Integer, List<EmergencyNumber>> emergencyNumbersList; private static volatile boolean isDataRoamingEnabled; + private /*CarrierRestrictionRules*/ Object carrierRestrictionRules; /** * Should be {@link TelephonyManager.BootstrapAuthenticationCallback} but this object was @@ -1662,4 +1664,24 @@ public class ShadowTelephonyManager { protected void setDataRoamingEnabled(boolean isDataRoamingEnabled) { ShadowTelephonyManager.isDataRoamingEnabled = isDataRoamingEnabled; } + + /** + * Sets the value to be returned by {@link #getCarrierRestrictionRules()}. Marked as public in + * order to allow it to be used as a test API. + * + * @param carrierRestrictionRules An object of type {@link CarrierRestrictionRules} + */ + public void setCarrierRestrictionRules(Object carrierRestrictionRules) { + Preconditions.checkState(carrierRestrictionRules instanceof CarrierRestrictionRules); + this.carrierRestrictionRules = carrierRestrictionRules; + } + + /** + * Implementation for {@link TelephonyManager#getCarrierRestrictionRules} that is set for tests by + * {@link TelephonyManager#setCarrierRestrictionRules}. + */ + @Implementation(minSdk = Build.VERSION_CODES.Q) + protected /*CarrierRestrictionRules*/ Object getCarrierRestrictionRules() { + return carrierRestrictionRules; + } } |