Skip to content
Permalink
Browse files

Issue #487: Fretboard: Add helper method to get active experiments

Closes #487: Fretboard: Add helper method to get active experiments
  • Loading branch information...
fercarcedo authored and pocmo committed Jul 24, 2018
1 parent c4005e7 commit ed825a67c1f54724aa8577bdb4c5874e91968e36
@@ -56,10 +56,10 @@ internal class ExperimentEvaluator(private val valuesProvider: ValuesProvider =
}

private fun isInBucket(userBucket: Int, experiment: Experiment): Boolean {
return !(experiment.bucket?.min == null ||
userBucket < experiment.bucket.min ||
experiment.bucket.max == null ||
userBucket >= experiment.bucket.max)
return (experiment.bucket?.min == null ||
userBucket >= experiment.bucket.min) &&
(experiment.bucket?.max == null ||
userBucket < experiment.bucket.max)
}

private fun getUserBucket(context: Context): Int {
@@ -89,6 +89,17 @@ class Fretboard(
return evaluator.getExperiment(descriptor, experimentsResult.experiments)
}

/**
* Provides the list of active experiments
*
* @param context context
*
* @return active experiments
*/
fun getActiveExperiments(context: Context): List<Experiment> {
return experiments.filter { isInExperiment(context, ExperimentDescriptor(it.id)) }.toList()
}

/**
* Overrides a specified experiment
*
@@ -4,10 +4,15 @@

package mozilla.components.service.fretboard

import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
@@ -90,4 +95,46 @@ class FretboardTest {
val returnedExperiments = fretboard.experiments
assertEquals(0, returnedExperiments.size)
}

@Test
fun testGetActiveExperiments() {
val experimentSource = mock(ExperimentSource::class.java)
val experimentStorage = mock(ExperimentStorage::class.java)
val experiments = listOf(
Experiment("first-id", match = Experiment.Matcher(
manufacturer = "manufacturer-1"
)),
Experiment("second-id", match = Experiment.Matcher(
manufacturer = "unknown",
appId = "test.appId"
)),
Experiment("third-id", match = Experiment.Matcher(
manufacturer = "unknown",
version = "version.name"
))
)
`when`(experimentStorage.retrieve()).thenReturn(ExperimentsSnapshot(experiments, null))
val fretboard = Fretboard(experimentSource, experimentStorage)
fretboard.loadExperiments()

val context = mock(Context::class.java)
`when`(context.packageName).thenReturn("test.appId")
val sharedPrefs = mock(SharedPreferences::class.java)
val prefsEditor = mock(SharedPreferences.Editor::class.java)
`when`(prefsEditor.putString(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(prefsEditor)
`when`(sharedPrefs.edit()).thenReturn(prefsEditor)
`when`(sharedPrefs.getBoolean(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean())).thenAnswer { invocation -> invocation.arguments[1] as Boolean }
`when`(context.getSharedPreferences(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt())).thenReturn(sharedPrefs)

val packageInfo = mock(PackageInfo::class.java)
packageInfo.versionName = "version.name"
val packageManager = mock(PackageManager::class.java)
`when`(packageManager.getPackageInfo(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt())).thenReturn(packageInfo)
`when`(context.packageManager).thenReturn(packageManager)

val activeExperiments = fretboard.getActiveExperiments(context)
assertEquals(2, activeExperiments.size)
assertTrue(activeExperiments.any { it.id == "second-id" })
assertTrue(activeExperiments.any { it.id == "third-id" })
}
}

0 comments on commit ed825a6

Please sign in to comment.
You can’t perform that action at this time.