Commit 1f54b728 authored by travis79's avatar travis79
Browse files

Ensure experiments record enrollment/unenrollment in Glean

- Add missing call to record unenrollment
- Add test coverage for recording enrollment/unenrollment in Glean
- Clarify test with comments
parent d1a5aacc
......@@ -187,6 +187,10 @@ open class ExperimentsInternalAPI internal constructor() {
private fun stopActiveExperiment() {
assert(activeExperiment != null) { "Should have an active experiment" }
activeExperiment?.let {
Glean.setExperimentInactive(it.experiment.id)
}
ActiveExperiment.clear(context)
activeExperiment = null
}
......
......@@ -13,6 +13,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.testing.WorkManagerTestInitHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import mozilla.components.service.glean.Glean
import mozilla.components.support.test.any
import mozilla.components.support.test.eq
import mozilla.components.support.test.mock
......@@ -79,6 +80,7 @@ class ExperimentsTest {
`when`(prefsEditor.putString(anyString(), anyString())).thenReturn(prefsEditor)
`when`(sharedPrefs.edit()).thenReturn(prefsEditor)
`when`(sharedPrefs.getBoolean(anyString(), anyBoolean())).thenAnswer { invocation -> invocation.arguments[1] as Boolean }
`when`(prefsEditor.clear()).thenReturn(prefsEditor)
`when`(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs)
val packageInfo: PackageInfo = mock()
......@@ -322,6 +324,32 @@ class ExperimentsTest {
assertEquals("second-id", experiments.activeExperiment!!.experiment.id)
}
@Test
fun `enrollment and unenrollment recorded in Glean`() {
resetExperiments()
experiments.initialize(mockContext, configuration)
// Update the experiments with an empty list to unenroll from any experiments
experiments.onExperimentsUpdated(ExperimentsSnapshot(listOf(), null))
// Make sure that we aren't already enrolled
assertFalse(Glean.testIsExperimentActive("second-id"))
// Simulate update from server that should enroll us in 'second-id' experiment
experiments.onExperimentsUpdated(ExperimentsSnapshot(experimentsList, null))
assertNotNull(experiments.activeExperiment)
assertEquals("second-id", experiments.activeExperiment!!.experiment.id)
// Verify that we are now enrolled in the experiment
assertTrue(Glean.testIsExperimentActive("second-id"))
// Update the experiments with an empty list to unenroll from any experiments
experiments.onExperimentsUpdated(ExperimentsSnapshot(listOf(), null))
// Verify we are now unenrolled
assertFalse(Glean.testIsExperimentActive("second-id"))
}
@Test
fun isInExperiment() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment