Skip to content

Commit

Permalink
For mozilla-mobile#6687: Add crash reporting information for ClassCas…
Browse files Browse the repository at this point in the history
…tException in PromptFeature
  • Loading branch information
rocketsroger committed May 25, 2020
1 parent d760644 commit 9fd251c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,47 +312,52 @@ class PromptFeature private constructor(
@Suppress("UNCHECKED_CAST", "ComplexMethod")
override fun onConfirm(sessionId: String, value: Any?) {
store.consumePromptFrom(sessionId, activePrompt) {
when (it) {
is TimeSelection -> it.onConfirm(value as Date)
is Color -> it.onConfirm(value as String)
is Alert -> {
val shouldNotShowMoreDialogs = value as Boolean
promptAbuserDetector.userWantsMoreDialogs(!shouldNotShowMoreDialogs)
it.onConfirm(!shouldNotShowMoreDialogs)
}
is SingleChoice -> it.onConfirm(value as Choice)
is MenuChoice -> it.onConfirm(value as Choice)
is PromptRequest.Popup -> it.onAllow()
is MultipleChoice -> it.onConfirm(value as Array<Choice>)

is Authentication -> {
val (user, password) = value as Pair<String, String>
it.onConfirm(user, password)
}

is TextPrompt -> {
val (shouldNotShowMoreDialogs, text) = value as Pair<Boolean, String>

promptAbuserDetector.userWantsMoreDialogs(!shouldNotShowMoreDialogs)
it.onConfirm(!shouldNotShowMoreDialogs, text)
}
try {
when (it) {
is TimeSelection -> it.onConfirm(value as Date)
is Color -> it.onConfirm(value as String)
is Alert -> {
val shouldNotShowMoreDialogs = value as Boolean
promptAbuserDetector.userWantsMoreDialogs(!shouldNotShowMoreDialogs)
it.onConfirm(!shouldNotShowMoreDialogs)
}
is SingleChoice -> it.onConfirm(value as Choice)
is MenuChoice -> it.onConfirm(value as Choice)
is PromptRequest.Popup -> it.onAllow()
is MultipleChoice -> it.onConfirm(value as Array<Choice>)

is Authentication -> {
val (user, password) = value as Pair<String, String>
it.onConfirm(user, password)
}

is Share -> it.onSuccess()
is TextPrompt -> {
val (shouldNotShowMoreDialogs, text) = value as Pair<Boolean, String>

is LoginPrompt -> it.onConfirm(value as Login)
promptAbuserDetector.userWantsMoreDialogs(!shouldNotShowMoreDialogs)
it.onConfirm(!shouldNotShowMoreDialogs, text)
}

is PromptRequest.Confirm -> {
val (isCheckBoxChecked, buttonType) = value as Pair<Boolean, MultiButtonDialogFragment.ButtonType>
promptAbuserDetector.userWantsMoreDialogs(!isCheckBoxChecked)
when (buttonType) {
MultiButtonDialogFragment.ButtonType.POSITIVE ->
it.onConfirmPositiveButton(!isCheckBoxChecked)
MultiButtonDialogFragment.ButtonType.NEGATIVE ->
it.onConfirmNegativeButton(!isCheckBoxChecked)
MultiButtonDialogFragment.ButtonType.NEUTRAL ->
it.onConfirmNeutralButton(!isCheckBoxChecked)
is Share -> it.onSuccess()

is LoginPrompt -> it.onConfirm(value as Login)

is PromptRequest.Confirm -> {
val (isCheckBoxChecked, buttonType) =
value as Pair<Boolean, MultiButtonDialogFragment.ButtonType>
promptAbuserDetector.userWantsMoreDialogs(!isCheckBoxChecked)
when (buttonType) {
MultiButtonDialogFragment.ButtonType.POSITIVE ->
it.onConfirmPositiveButton(!isCheckBoxChecked)
MultiButtonDialogFragment.ButtonType.NEGATIVE ->
it.onConfirmNegativeButton(!isCheckBoxChecked)
MultiButtonDialogFragment.ButtonType.NEUTRAL ->
it.onConfirmNeutralButton(!isCheckBoxChecked)
}
}
}
} catch (e: ClassCastException) {
throw IllegalArgumentException("PromptFeature onConsume cast failed with ${it.javaClass}", e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,30 @@ class PromptFeatureTest {
assertFalse(prompt!!.shouldDismissOnLoad())
}

@Test
fun `PromptFeature throws IllegalArgumentException when ClassCastException is triggered`() {
val feature = PromptFeature(
activity = mock(),
store = store,
fragmentManager = fragmentManager
) { }
feature.start()

val singleChoiceRequest = SingleChoice(arrayOf()) {}
var illegalArgumentExceptionThrown = false
store.dispatch(ContentAction.UpdatePromptRequestAction(tabId, singleChoiceRequest)).joinBlocking()

try {
feature.onConfirm(tabId, "wrong")
} catch (e: IllegalArgumentException) {
illegalArgumentExceptionThrown = true
assertEquals("PromptFeature onConsume cast failed with class mozilla.components.concept.engine.prompt.PromptRequest\$SingleChoice", e.message)
}

store.waitUntilIdle()
assert(illegalArgumentExceptionThrown)
}

private fun mockFragmentManager(): FragmentManager {
val fragmentManager: FragmentManager = mock()
val transaction: FragmentTransaction = mock()
Expand Down

0 comments on commit 9fd251c

Please sign in to comment.