This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #6: Implement code for evaluating experiment configuration and …
…bucketing users Closes #6: Implment code for evaluating experiment configuration and bucketing users
- Loading branch information
1 parent
fcf13f7
commit b4d9e41
Showing
8 changed files
with
575 additions
and
2 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
fretboard/src/main/java/mozilla/components/service/fretboard/DeviceUuidFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,29 @@ | |||
/* 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 | |||
|
|||
import android.content.Context | |||
import java.util.UUID | |||
|
|||
internal class DeviceUuidFactory(context: Context) { | |||
val uuid by lazy { | |||
val preferences = context | |||
.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE) | |||
val prefUuid = preferences.getString(PREF_UUID_KEY, null) | |||
|
|||
if (prefUuid != null) { | |||
UUID.fromString(prefUuid) | |||
} else { | |||
val uuid = UUID.randomUUID() | |||
preferences.edit().putString(PREF_UUID_KEY, uuid.toString()).apply() | |||
uuid | |||
} | |||
} | |||
|
|||
companion object { | |||
private const val PREFS_FILE = "mozilla.components.service.fretboard" | |||
private const val PREF_UUID_KEY = "device_uuid" | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
fretboard/src/main/java/mozilla/components/service/fretboard/ExperimentDescriptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
/* 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 | |||
|
|||
data class ExperimentDescriptor(val id: String) |
66 changes: 66 additions & 0 deletions
66
fretboard/src/main/java/mozilla/components/service/fretboard/ExperimentEvaluator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,66 @@ | |||
/* 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 | |||
|
|||
import android.content.Context | |||
import android.os.Build | |||
import android.text.TextUtils | |||
import java.util.Locale | |||
import java.util.zip.CRC32 | |||
|
|||
internal class ExperimentEvaluator { | |||
fun evaluate( | |||
context: Context, | |||
experimentDescriptor: ExperimentDescriptor, | |||
experiments: List<Experiment>, | |||
userBucket: Int = getUserBucket(context) | |||
): Boolean { | |||
val experiment = experiments.firstOrNull { it.id == experimentDescriptor.id } ?: return false | |||
return isInBucket(userBucket, experiment) && matches(context, experiment) | |||
} | |||
|
|||
private fun matches(context: Context, experiment: Experiment): Boolean { | |||
if (experiment.match != null) { | |||
val region = Locale.getDefault().isO3Country | |||
val matchesRegion = !(experiment.match.regions != null && | |||
experiment.match.regions.isNotEmpty() && | |||
experiment.match.regions.none { it == region }) | |||
val appVersion = context.packageManager.getPackageInfo(context.packageName, 0).versionName | |||
return matchesRegion && | |||
matchesExperiment(experiment.match.appId, context.packageName) && | |||
matchesExperiment(experiment.match.language, Locale.getDefault().isO3Language) && | |||
matchesExperiment(experiment.match.country, Locale.getDefault().isO3Country) && | |||
matchesExperiment(experiment.match.version, appVersion) && | |||
matchesExperiment(experiment.match.manufacturer, Build.MANUFACTURER) && | |||
matchesExperiment(experiment.match.device, Build.DEVICE) | |||
} | |||
return true | |||
} | |||
|
|||
private fun matchesExperiment(experimentValue: String?, deviceValue: String): Boolean { | |||
return !(experimentValue != null && | |||
!TextUtils.isEmpty(experimentValue) && | |||
!deviceValue.matches(experimentValue.toRegex())) | |||
} | |||
|
|||
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) | |||
} | |||
|
|||
private fun getUserBucket(context: Context): Int { | |||
val uuid = DeviceUuidFactory(context).uuid.toString() | |||
val crc = CRC32() | |||
crc.update(uuid.toByteArray()) | |||
val checksum = crc.value | |||
return (checksum % MAX_BUCKET).toInt() | |||
} | |||
|
|||
companion object { | |||
private const val MAX_BUCKET = 100L | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
fretboard/src/test/java/mozilla/components/service/fretboard/DeviceUuidFactoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
/* 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 | |||
|
|||
import android.content.Context | |||
import android.content.SharedPreferences | |||
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.ArgumentMatchers.any | |||
import org.mockito.ArgumentMatchers.anyInt | |||
import org.mockito.ArgumentMatchers.anyString | |||
import org.mockito.ArgumentMatchers.eq | |||
import org.mockito.Mockito.`when` | |||
import org.mockito.Mockito.mock | |||
import org.mockito.Mockito.verify | |||
import org.robolectric.RobolectricTestRunner | |||
import java.util.UUID | |||
|
|||
@RunWith(RobolectricTestRunner::class) | |||
class DeviceUuidFactoryTest { | |||
@Test | |||
fun testUuidNoPreference() { | |||
val context = mock(Context::class.java) | |||
val sharedPreferences = mock(SharedPreferences::class.java) | |||
val editor = mock(SharedPreferences.Editor::class.java) | |||
`when`(editor.putString(anyString(), any())).thenReturn(editor) | |||
`when`(sharedPreferences.edit()).thenReturn(editor) | |||
`when`(sharedPreferences.getString(eq("device_uuid"), ArgumentMatchers.any())).thenReturn(null) | |||
`when`(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPreferences) | |||
val uuid = DeviceUuidFactory(context).uuid | |||
assertTrue(uuid is UUID) | |||
verify(editor).putString("device_uuid", uuid.toString()) | |||
} | |||
|
|||
@Test | |||
fun testUuidSavedInPreferences() { | |||
val savedUuid = "99111a0f-ca5d-4de1-913a-daba905c53b2" | |||
val context = mock(Context::class.java) | |||
val sharedPreferences = mock(SharedPreferences::class.java) | |||
`when`(sharedPreferences.getString(eq("device_uuid"), any())).thenReturn(savedUuid) | |||
`when`(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPreferences) | |||
assertEquals(savedUuid, DeviceUuidFactory(context).uuid.toString()) | |||
} | |||
} |
Oops, something went wrong.