-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[telemetry] Fix crash when using
obtainCellularNetworkType
`TelemetryUtils.obtainCellularNetworkType` is annotated with @nonnull annotation meaning that this function should never return a null. However before this change there was a way to return a null when returned underlying network type is not covered in the `NETWORKS` map.
- Loading branch information
1 parent
92b695d
commit 442bd99
Showing
2 changed files
with
22 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
libtelemetry/src/androidTest/java/com/mapbox/android/telemetry/TestCellularNetworkType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
package com.mapbox.android.telemetry; | ||
|
||
import android.content.Context; | ||
import android.telephony.TelephonyManager; | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.ArgumentMatchers.any; | ||
|
||
public class TestCellularNetworkType { | ||
@Test | ||
public void testCellularNetworkType() { | ||
Context context = InstrumentationRegistry.getInstrumentation().getContext(); | ||
String cellularNetworkType = TelemetryUtils.obtainCellularNetworkType(context); | ||
Assert.assertFalse(cellularNetworkType.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testUnknownNetworkType() { | ||
Context mockedContext = mock(Context.class); | ||
TelephonyManager mockedTelephonyManager = mock(TelephonyManager.class); | ||
when(mockedContext.getSystemService(any(String.class))).thenReturn(mockedTelephonyManager); | ||
when(mockedTelephonyManager.getDataNetworkType()).thenReturn(TelephonyManager.NETWORK_TYPE_UNKNOWN); | ||
Assert.assertEquals(TelemetryUtils.obtainCellularNetworkType(mockedContext), "Unknown"); | ||
when(mockedTelephonyManager.getDataNetworkType()).thenReturn(Integer.MAX_VALUE); | ||
Assert.assertEquals(TelemetryUtils.obtainCellularNetworkType(mockedContext), "Unknown"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters