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

For #6687: Add crash reporting information for ClassCastException in PromptFeature #6995

Merged
merged 1 commit into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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