Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge #8284
Browse files Browse the repository at this point in the history
8284: Issue #8009: Handle all throwables in hasCamera check r=Amejia481 a=csadilek

We're seeing some devices that throw `AssertionErrors` here! 💯 🤦 

Let's not crash in those cases either and return false:
mozilla-mobile/fenix#13956

Co-authored-by: Christian Sadilek <christian.sadilek@gmail.com>
  • Loading branch information
MozLando and csadilek committed Aug 28, 2020
2 parents b117c19 + 762c27c commit 622ca64
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fun Context.hasCamera(): Boolean {
return try {
val cameraManager: CameraManager? = getSystemService()
cameraManager?.cameraIdList?.isNotEmpty() ?: false
} catch (e: Exception) {
} catch (_: Throwable) {
false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.robolectric.Shadows.shadowOf
import org.robolectric.shadows.ShadowApplication
import org.robolectric.shadows.ShadowCameraCharacteristics
import org.robolectric.shadows.ShadowProcess
import java.lang.AssertionError
import java.lang.IllegalStateException

@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -157,7 +158,18 @@ class ContextTest {
val context = spy(testContext)
val cameraManager: CameraManager = mock()
whenever(context.getSystemService(Context.CAMERA_SERVICE)).thenReturn(cameraManager)

whenever(cameraManager.cameraIdList).thenThrow(IllegalStateException("Test"))
assertFalse(context.hasCamera())
}

@Test
fun `hasCamera returns false if assertion is thrown`() {
val context = spy(testContext)
val cameraManager: CameraManager = mock()
whenever(context.getSystemService(Context.CAMERA_SERVICE)).thenReturn(cameraManager)

whenever(cameraManager.cameraIdList).thenThrow(AssertionError("Test"))
assertFalse(context.hasCamera())
}
}

0 comments on commit 622ca64

Please sign in to comment.