Skip to content

Commit

Permalink
For mozilla-mobile#20919 quit the after removing a study.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 authored and mergify[bot] committed Aug 20, 2021
1 parent bf5b4a5 commit d301998
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
package org.mozilla.fenix.settings.studies

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
Expand Down Expand Up @@ -117,10 +120,32 @@ class StudiesAdapter(
holder.summaryView.text = study.userFacingDescription

holder.deleteButton.setOnClickListener {
studiesDelegate.onRemoveButtonClicked(study)
showDeleteDialog(holder.titleView.context, study)
}
}

@VisibleForTesting
internal fun showDeleteDialog(context: Context, study: EnrolledExperiment): AlertDialog {
val builder = AlertDialog.Builder(context)
.setPositiveButton(
R.string.studies_restart_dialog_ok
) { dialog, _ ->
studiesDelegate.onRemoveButtonClicked(study)
dialog.dismiss()
}
.setNegativeButton(
R.string.studies_restart_dialog_cancel
) { dialog: DialogInterface, _ ->
dialog.dismiss()
}
.setTitle(R.string.preference_experiments_2)
.setMessage(R.string.studies_restart_app)
.setCancelable(false)
val alertDialog: AlertDialog = builder.create()
alertDialog.show()
return alertDialog
}

internal fun createListWithSections(studies: List<EnrolledExperiment>): List<Any> {
val itemsWithSections = ArrayList<Any>()
val activeStudies = ArrayList<EnrolledExperiment>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

package org.mozilla.fenix.settings.studies

import androidx.annotation.VisibleForTesting
import mozilla.components.service.nimbus.NimbusApi
import org.mozilla.experiments.nimbus.internal.EnrolledExperiment
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import kotlin.system.exitProcess

interface StudiesInteractor {
/**
Expand Down Expand Up @@ -35,5 +37,11 @@ class DefaultStudiesInteractor(

override fun removeStudy(experiment: EnrolledExperiment) {
experiments.optOut(experiment.slug)
killApplication()
}

@VisibleForTesting
internal fun killApplication() {
exitProcess(0)
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@
<string name="studies_description">Firefox may install and run studies from time to time.</string>
<!-- Learn more link for studies, links to an article for more information about studies. -->
<string name="studies_learn_more">Learn more</string>
<!-- Dialog message shown after removing a study -->
<string name="studies_restart_app">The application will quit to apply changes</string>
<!-- Dialog button to confirm the removing a study. -->
<string name="studies_restart_dialog_ok">OK</string>
<!-- Dialog button text for canceling removing a study. -->
<string name="studies_restart_dialog_cancel">Cancel</string>

<!-- Sessions -->
<!-- Title for the list of tabs -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package org.mozilla.fenix.settings.studies
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.RelaxedMockK
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.components.service.nimbus.NimbusApi
Expand All @@ -30,7 +33,7 @@ class DefaultStudiesInteractorTest {
@Before
fun setup() {
MockKAnnotations.init(this)
interactor = DefaultStudiesInteractor(activity, experiments)
interactor = spyk(DefaultStudiesInteractor(activity, experiments))
}

@Test
Expand All @@ -48,6 +51,7 @@ class DefaultStudiesInteractorTest {
val experiment = mockk<EnrolledExperiment>(relaxed = true)

every { experiment.slug } returns "slug"
every { interactor.killApplication() } just runs

interactor.removeStudy(experiment)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import io.mockk.mockk
import io.mockk.runs
import io.mockk.spyk
import io.mockk.verify
import junit.framework.TestCase.assertTrue
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.components.support.test.robolectric.testContext
import org.junit.Before
Expand Down Expand Up @@ -69,7 +69,7 @@ class StudiesAdapterTest {
fun `WHEN bindStudy THEN bind the study information`() {
val holder = mockk<StudyViewHolder>()
val study = mockk<EnrolledExperiment>()
val titleView = mockk<TextView>(relaxed = true)
val titleView = spyk(TextView(testContext))
val summaryView = mockk<TextView>(relaxed = true)
val deleteButton = spyk(MaterialButton(testContext))

Expand All @@ -82,6 +82,8 @@ class StudiesAdapterTest {

adapter = spyk(StudiesAdapter(delegate, listOf(study), false))

every { adapter.showDeleteDialog(any(), any()) } returns mockk()

adapter.bindStudy(holder, study)

verify {
Expand All @@ -92,7 +94,7 @@ class StudiesAdapterTest {
deleteButton.performClick()

verify {
delegate.onRemoveButtonClicked(study)
adapter.showDeleteDialog(any(), any())
}
}

Expand Down

0 comments on commit d301998

Please sign in to comment.