Commit f0bfeb3e authored by Tiger Oakes's avatar Tiger Oakes
Browse files

Use interface for PromptDialogFragment

parent 04ffc01c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ internal class ConfirmDialogFragment : PromptDialogFragment() {
    }

    private fun onPositiveClickAction() {
        feature?.onConfirm(sessionId)
        feature?.onConfirm(sessionId, null)
    }

    companion object {
+29 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ internal const val KEY_MESSAGE = "KEY_MESSAGE"
 */
internal abstract class PromptDialogFragment : DialogFragment() {

    var feature: PromptFeature? = null
    var feature: Prompter? = null

    internal val sessionId: String by lazy { requireNotNull(arguments).getString(KEY_SESSION_ID)!! }

@@ -25,3 +25,31 @@ internal abstract class PromptDialogFragment : DialogFragment() {

    val safeArguments get() = requireNotNull(arguments)
}

internal interface Prompter {

    /**
     * Invoked when a dialog is dismissed. This consumes the [PromptFeature]
     * value from the session indicated by [sessionId].
     *
     * @param sessionId this is the id of the session which requested the prompt.
     */
    fun onCancel(sessionId: String)

    /**
     * Invoked when the user confirms the action on the dialog. This consumes
     * the [PromptFeature] value from the session indicated by [sessionId].
     *
     * @param sessionId that requested to show the dialog.
     * @param value an optional value provided by the dialog as a result of confirming the action.
     */
    fun onConfirm(sessionId: String, value: Any?)

    /**
     * Invoked when the user is requesting to clear the selected value from the dialog.
     * This consumes the [PromptFeature] value from the session indicated by [sessionId].
     *
     * @param sessionId that requested to show the dialog.
     */
    fun onClear(sessionId: String)
}
+5 −5
Original line number Diff line number Diff line
@@ -71,14 +71,14 @@ internal const val FRAGMENT_TAG = "mozac_feature_prompt_dialog"
 * need to be requested before a prompt (e.g. a file picker) can be displayed.
 * Once the request is completed, [onPermissionsResult] needs to be invoked.
 */
@Suppress("TooManyFunctions")
@Suppress("TooManyFunctions", "LargeClass")
class PromptFeature internal constructor(
    private val container: PromptContainer,
    private val store: BrowserStore,
    private var customTabId: String? = null,
    private val fragmentManager: FragmentManager,
    onNeedToRequestPermissions: OnNeedToRequestPermissions
) : LifecycleAwareFeature, PermissionsFeature {
) : LifecycleAwareFeature, PermissionsFeature, Prompter {
    private var scope: CoroutineScope? = null
    private var activePromptRequest: PromptRequest? = null

@@ -212,7 +212,7 @@ class PromptFeature internal constructor(
     *
     * @param sessionId this is the id of the session which requested the prompt.
     */
    internal fun onCancel(sessionId: String) {
    override fun onCancel(sessionId: String) {
        store.consumePromptFrom(sessionId) {
            when (it) {
                is PromptRequest.Dismissible -> it.onDismiss()
@@ -229,7 +229,7 @@ class PromptFeature internal constructor(
     * @param value an optional value provided by the dialog as a result of confirming the action.
     */
    @Suppress("UNCHECKED_CAST", "ComplexMethod")
    internal fun onConfirm(sessionId: String, value: Any? = null) {
    override fun onConfirm(sessionId: String, value: Any?) {
        store.consumePromptFrom(sessionId) {
            when (it) {
                is TimeSelection -> it.onConfirm(value as Date)
@@ -278,7 +278,7 @@ class PromptFeature internal constructor(
     *
     * @param sessionId that requested to show the dialog.
     */
    internal fun onClear(sessionId: String) {
    override fun onClear(sessionId: String) {
        store.consumePromptFrom(sessionId) {
            when (it) {
                is TimeSelection -> it.onClear()
+11 −9
Original line number Diff line number Diff line
@@ -9,24 +9,33 @@ import android.widget.CheckBox
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.ext.appCompatContext
import mozilla.components.feature.prompts.R.id
import mozilla.components.support.test.mock
import mozilla.ext.appCompatContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations.initMocks

@RunWith(AndroidJUnit4::class)
class AlertDialogFragmentTest {

    @Mock private lateinit var mockFeature: Prompter

    @Before
    fun setup() {
        initMocks(this)
    }

    @Test
    fun `build dialog`() {

        val fragment = spy(
            AlertDialogFragment.newInstance("sessionId", "title", "message", true)
        )
@@ -53,7 +62,6 @@ class AlertDialogFragmentTest {

    @Test
    fun `Alert with hasShownManyDialogs equals false should not have a checkbox`() {

        val fragment = spy(
            AlertDialogFragment.newInstance("sessionId", "title", "message", false)
        )
@@ -93,9 +101,6 @@ class AlertDialogFragmentTest {

    @Test
    fun `After checking no more dialogs checkbox feature onNoMoreDialogsChecked must be called`() {

        val mockFeature: PromptFeature = mock()

        val fragment = spy(
            AlertDialogFragment.newInstance("sessionId", "title", "message", true)
        )
@@ -119,9 +124,6 @@ class AlertDialogFragmentTest {

    @Test
    fun `touching outside of the dialog must notify the feature onCancel`() {

        val mockFeature: PromptFeature = mock()

        val fragment = spy(
            AlertDialogFragment.newInstance("sessionId", "title", "message", true)
        )
+13 −9
Original line number Diff line number Diff line
@@ -10,19 +10,29 @@ import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.ext.appCompatContext
import mozilla.components.feature.prompts.R.id
import mozilla.components.support.test.mock
import mozilla.ext.appCompatContext
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations.initMocks

@RunWith(AndroidJUnit4::class)
class AuthenticationDialogFragmentTest {

    @Mock private lateinit var mockFeature: Prompter

    @Before
    fun setup() {
        initMocks(this)
    }

    @Test
    fun `build dialog`() {

@@ -33,7 +43,7 @@ class AuthenticationDialogFragmentTest {
                "message",
                "username",
                "password",
                false
                onlyShowPassword = false
            )
        )

@@ -120,9 +130,6 @@ class AuthenticationDialogFragmentTest {

    @Test
    fun `Clicking on positive button notifies the feature`() {

        val mockFeature: PromptFeature = mock()

        val fragment = spy(
            AuthenticationDialogFragment.newInstance(
                "sessionId",
@@ -149,9 +156,6 @@ class AuthenticationDialogFragmentTest {

    @Test
    fun `touching outside of the dialog must notify the feature onCancel`() {

        val mockFeature: PromptFeature = mock()

        val fragment = spy(
            AuthenticationDialogFragment.newInstance(
                "sessionId",
Loading