Skip to content
Permalink
Browse files

Add RegionProvider

  • Loading branch information...
fercarcedo authored and pocmo committed Jun 14, 2018
1 parent b905767 commit af80d363dd7a522f43990550a7096aa5a68c9b3e
@@ -10,7 +10,7 @@ import android.text.TextUtils
import java.util.Locale
import java.util.zip.CRC32

internal class ExperimentEvaluator {
internal class ExperimentEvaluator(private val regionProvider: RegionProvider? = null) {
fun evaluate(
context: Context,
experimentDescriptor: ExperimentDescriptor,
@@ -23,8 +23,9 @@ internal class ExperimentEvaluator {

private fun matches(context: Context, experiment: Experiment): Boolean {
if (experiment.match != null) {
val region = Locale.getDefault().isO3Country
val matchesRegion = !(experiment.match.regions != null &&
val region = regionProvider?.getRegion()
val matchesRegion = !(region != null &&
experiment.match.regions != null &&
experiment.match.regions.isNotEmpty() &&
experiment.match.regions.none { it == region })
val appVersion = context.packageManager.getPackageInfo(context.packageName, 0).versionName
@@ -16,7 +16,6 @@ class Fretboard(
) {
private var experiments: List<Experiment> = listOf()
private var experimentsLoaded: Boolean = false
private val evaluator = ExperimentEvaluator()

/**
* Loads experiments from local storage
@@ -0,0 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.fretboard

/**
* Interface used to provide
* the user's region for evaluating
* experiments
*/
interface RegionProvider {
/**
* Provides the user's region
*
* @return user's region
*/
fun getRegion(): String
}
@@ -371,27 +371,19 @@ class ExperimentEvaluatorTest {
`when`(packageManager.getPackageInfo(anyString(), anyInt())).thenReturn(packageInfo)
`when`(context.packageManager).thenReturn(packageManager)

val evaluator = ExperimentEvaluator()
var evaluator = ExperimentEvaluator(object : RegionProvider {
override fun getRegion(): String {
return "USA"
}
})

assertTrue(evaluator.evaluate(context, ExperimentDescriptor("testid"), listOf(experiment), 20))

experiment = Experiment(
"testid",
"testexperiment",
"testdesc",
Experiment.Matcher(
"eng",
"test.appId",
listOf("GBR"),
"test.version",
"unknown",
"robolectric",
"USA"
),
Experiment.Bucket(
70,
20
),
1528916183)
evaluator = ExperimentEvaluator(object : RegionProvider {
override fun getRegion(): String {
return "ESP"
}
})

assertFalse(evaluator.evaluate(context, ExperimentDescriptor("testid"), listOf(experiment), 20))
}

0 comments on commit af80d36

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