Skip to content

Commit

Permalink
For mozilla-mobile#12562 - Handle onPromptDismiss in ChoicePromptDele…
Browse files Browse the repository at this point in the history
…gate
  • Loading branch information
Alexandru2909 committed Jul 28, 2022
1 parent 0ed5dfb commit 2f555e3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ import org.mozilla.geckoview.GeckoSession.PromptDelegate.PromptInstanceDelegate
* with the onPromptUpdate callback.
* @param previousPrompt [PromptRequest] to be updated.
*/
internal class ChoicePromptUpdateDelegate(
internal class ChoicePromptDelegate(
private val geckoSession: GeckoEngineSession,
private var previousPrompt: PromptRequest,
) : PromptInstanceDelegate {

override fun onPromptDismiss(prompt: BasePrompt) {
geckoSession.notifyObservers {
onPromptDismissed(previousPrompt)
}
}

override fun onPromptUpdate(prompt: BasePrompt) {
if (prompt is ChoicePrompt) {
val promptRequest = updatePromptChoices(prompt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ internal class GeckoPromptDelegate(private val geckoEngineSession: GeckoEngineSe
else -> throw InvalidParameterException("${geckoPrompt.type} is not a valid Gecko @Choice.ChoiceType")
}

geckoPrompt.delegate = ChoicePromptUpdateDelegate(
geckoPrompt.delegate = ChoicePromptDelegate(
geckoEngineSession,
promptRequest
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.junit.runner.RunWith
import org.mozilla.geckoview.GeckoSession

@RunWith(AndroidJUnit4::class)
class ChoicePromptUpdateDelegateTest {
class ChoicePromptDelegateTest {

@Test
fun `WHEN onPromptUpdate is called from GeckoView THEN notifyObservers is invoked with onPromptUpdate`() {
Expand All @@ -41,7 +41,7 @@ class ChoicePromptUpdateDelegateTest {
{ isOnConfirmCalled = true },
{ isOnDismissCalled = true }
)
val delegate = ChoicePromptUpdateDelegate(mockSession, prompt)
val delegate = ChoicePromptDelegate(mockSession, prompt)
val updatedPrompt = mock<GeckoSession.PromptDelegate.ChoicePrompt>()
ReflectionUtils.setField(updatedPrompt, "choices", arrayOf<GeckoChoice>())

Expand All @@ -55,4 +55,23 @@ class ChoicePromptUpdateDelegateTest {
assertTrue(isOnDismissCalled)
assertTrue(isOnConfirmCalled)
}

@Test
fun `WHEN onPromptDismiss is called from GeckoView THEN notifyObservers is invoked with onPromptDismissed`() {
val mockSession = GeckoEngineSession(mock())
var isOnDismissCalled = false
mockSession.register(object : EngineSession.Observer {
override fun onPromptDismissed(promptRequest: PromptRequest) {
super.onPromptDismissed(promptRequest)
isOnDismissCalled = true
}
})
val basePrompt: GeckoSession.PromptDelegate.ChoicePrompt = mock()
val prompt: PromptRequest = mock()
val delegate = ChoicePromptDelegate(mockSession, prompt)

delegate.onPromptDismiss(basePrompt)

assertTrue(isOnDismissCalled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ class PromptFeature private constructor(
activePromptRequest as SelectAddress
)
}
is SingleChoice,
is MultipleChoice,
is MenuChoice -> {
(activePrompt?.get() as? ChoiceDialogFragment)?.dismissAllowingStateLoss()
}
else -> {
// no-op
}
Expand Down

0 comments on commit 2f555e3

Please sign in to comment.