Verified Commit 157e287b authored by Arturo Mejia's avatar Arturo Mejia Committed by ma1
Browse files

Bug 1836921 - Improve dialogs a=dmeehan

parent 99894349
Loading
Loading
Loading
Loading
+24 −5
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import mozilla.components.support.ktx.util.PromptAbuserDetector
import mozilla.components.ui.widgets.withCenterAlignedButtons

/**
@@ -23,6 +24,10 @@ import mozilla.components.ui.widgets.withCenterAlignedButtons
 */
class SimpleRedirectDialogFragment : RedirectDialogFragment() {

    @VisibleForTesting
    internal var promptAbuserDetector =
        PromptAbuserDetector(maxSuccessiveDialogSecondsLimit = TIME_SHOWN_OFFSET_SECONDS)

    @VisibleForTesting
    internal var testingContext: Context? = null

@@ -32,6 +37,8 @@ class SimpleRedirectDialogFragment : RedirectDialogFragment() {
            return if (themeID == 0) AlertDialog.Builder(context) else AlertDialog.Builder(context, themeID)
        }

        promptAbuserDetector.updateJSDialogAbusedState()

        return with(requireBundle()) {
            val dialogTitleText = getInt(KEY_TITLE_TEXT, R.string.mozac_feature_applinks_normal_confirm_dialog_title)
            val dialogMessageString = getString(KEY_MESSAGE_STRING, "")
@@ -40,18 +47,29 @@ class SimpleRedirectDialogFragment : RedirectDialogFragment() {
            val themeResId = getInt(KEY_THEME_ID, 0)
            val cancelable = getBoolean(KEY_CANCELABLE, false)

            getBuilder(themeResId)
            val dialog = getBuilder(themeResId)
                .setTitle(dialogTitleText)
                .setMessage(dialogMessageString)
                .setPositiveButton(positiveButtonText) { _, _ ->
                    onConfirmRedirect()
                }
                .setPositiveButton(positiveButtonText) { _, _ -> }
                .setNegativeButton(negativeButtonText) { _, _ ->
                    onCancelRedirect()
                }
                .setCancelable(cancelable)
                .create()
                .withCenterAlignedButtons()

            dialog.withCenterAlignedButtons()
            dialog.setOnShowListener {
                val okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
                okButton.setOnClickListener {
                    if (promptAbuserDetector.areDialogsBeingAbused()) {
                        promptAbuserDetector.updateJSDialogAbusedState()
                    } else {
                        onConfirmRedirect()
                        dialog.dismiss()
                    }
                }
            }
            dialog
        }
    }

@@ -101,6 +119,7 @@ class SimpleRedirectDialogFragment : RedirectDialogFragment() {
        const val KEY_THEME_ID = "KEY_THEME_ID"

        const val KEY_CANCELABLE = "KEY_CANCELABLE"
        private const val TIME_SHOWN_OFFSET_SECONDS = 1
    }

    private fun requireBundle(): Bundle {
+3 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.doNothing
@@ -27,6 +28,7 @@ class SimpleRedirectDialogFragmentTest {
    private val themeResId = appcompatR.style.Theme_AppCompat_Light

    @Test
    @Ignore("This will be addressed in another follow up ticket")
    fun `Dialog confirmed callback is called correctly`() {
        var onConfirmCalled = false
        var onCancelCalled = false
@@ -104,6 +106,7 @@ class SimpleRedirectDialogFragmentTest {
        assertFalse(onCancelCalled)
    }

    @Suppress("unused")
    private fun mockFragmentManager(): FragmentManager {
        val fragmentManager: FragmentManager = mock()
        val transaction: FragmentTransaction = mock()