From 4d1e64b65b5c4d7458fc49af73ef677c1061183e Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 15 Mar 2022 12:11:47 -0700 Subject: [PATCH] Ensure getNetworkCountryIso & getSimCountryIso return lowercase only, to match the actual APIs. PiperOrigin-RevId: 434820755 --- .../shadows/ShadowTelephonyManagerTest.java | 6 +++--- .../shadows/ShadowTelephonyManager.java | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java index 92fce2262d5..d4be0ed8db0 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java @@ -242,9 +242,9 @@ public void onCellInfo(List list) { } @Test - public void shouldGiveNetworkCountryIso() { + public void shouldGiveNetworkCountryIsoInLowercase() { shadowOf(telephonyManager).setNetworkCountryIso("SomeIso"); - assertEquals("SomeIso", telephonyManager.getNetworkCountryIso()); + assertEquals("someiso", telephonyManager.getNetworkCountryIso()); } @Test @@ -506,7 +506,7 @@ public void shouldGetSimIso() { @Test @Config(minSdk = N, maxSdk = Q) - public void shouldGetSimIosWhenSetUsingSlotNumber() { + public void shouldGetSimIsoWhenSetUsingSlotNumber() { String expectedSimIso = "usa"; int subId = 2; shadowOf(telephonyManager).setSimCountryIso(subId, expectedSimIso); 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 95da7e5832a..6ffe593e228 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTelephonyManager.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTelephonyManager.java @@ -42,6 +42,7 @@ import android.text.TextUtils; import android.util.SparseArray; import android.util.SparseIntArray; +import com.google.common.base.Ascii; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableList; @@ -319,9 +320,13 @@ public void setNetworkCountryIso(String networkCountryIso) { this.networkCountryIso = networkCountryIso; } + /** + * Returns the SIM country lowercase. This matches the API this shadows: + * https://developer.android.com/reference/android/telephony/TelephonyManager#getNetworkCountryIso(). + */ @Implementation protected String getNetworkCountryIso() { - return networkCountryIso; + return networkCountryIso == null ? null : Ascii.toLowerCase(networkCountryIso); } /** Sets the sim locale returned by {@link #getSimLocale()}. */ @@ -374,9 +379,14 @@ public void setSimSerialNumber(String simSerialNumber) { this.simSerialNumber = simSerialNumber; } + /** + * Returns the SIM country lowercase. This matches the API it shadows: + * https://developer.android.com/reference/android/telephony/TelephonyManager#getSimCountryIso(). + */ @Implementation protected String getSimCountryIso() { - return simCountryIsoMap.get(/* subId= */ 0); + String simCountryIso = simCountryIsoMap.get(/* subId= */ 0); + return simCountryIso == null ? simCountryIso : Ascii.toLowerCase(simCountryIso); } @Implementation(minSdk = N, maxSdk = Q)