-
Notifications
You must be signed in to change notification settings - Fork 939
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Android version filter for experiments (#4033)
Task/Issue URL: https://app.asana.com/0/488551667048375/1206193424728741/f ### Description ### Steps to test this PR - Change `const val PRIVACY_REMOTE_CONFIG_URL` to `https://www.jsonblob.com/api/1168612786158034944` - There is a log in `VariantManagerImpl` _Line 136_ when a new variant allocation is made. This way, you can check if the variant has been allocated correctly _Android version filter_ - Add the API version of your device you are testing with in the `filters` parameter.in the JSONBlob endpoint e.g. <img width="545" alt="Screenshot 2023-12-21 at 11 57 51" src="https://github.com/duckduckgo/Android/assets/20798495/7399632e-e119-4499-9911-09fe89b888f9"> - Fresh install - [x] Check the experimental variant is allocated correctly _Android version filter doesn't match_ - Add a different API version from your device you are testing with in the `filters` parameter in the JSONBlob endpoint e.g. <img width="545" alt="Screenshot 2023-12-21 at 11 57 51" src="https://github.com/duckduckgo/Android/assets/20798495/7399632e-e119-4499-9911-09fe89b888f9"> - Fresh install - [x] Check the default variant is allocated _No filters_ - Remove filters parameter or leave it empty - Fresh install - [x] Check the experimental variant is allocated correctly _Filters combined 1_ - Add the API version of your device you are testing with in the `filters` parameter.in the JSONBlob endpoint e.g. <img width="545" alt="Screenshot 2023-12-21 at 11 57 51" src="https://github.com/duckduckgo/Android/assets/20798495/7399632e-e119-4499-9911-09fe89b888f9"> - Add a different locale filter which doesn't match your testing device. _e.g._ `"locale": ["en_US"]` - Fresh install - [ ] Check the default variant is allocated _Filters combined 2_ - Add the API version of your device you are testing with in the `filters` parameter.in the JSONBlob endpoint e.g. <img width="545" alt="Screenshot 2023-12-21 at 11 57 51" src="https://github.com/duckduckgo/Android/assets/20798495/7399632e-e119-4499-9911-09fe89b888f9"> - Add the locale which matched your testing device. - Fresh install - [x] Check the experimental variant is allocated correctly _Filters combined 3_ - Add a different API version from your device you are testing with in the `filters` parameter in the JSONBlob endpoint _e.g._ <img width="545" alt="Screenshot 2023-12-21 at 11 57 51" src="https://github.com/duckduckgo/Android/assets/20798495/7399632e-e119-4499-9911-09fe89b888f9"> - Add a different locale filter which doesn't match your testing device. _e.g._ `"locale": ["en_US"]` - Fresh install - [x] Check the default variant is allocated --------------- Improved steps in this [comment](#4033 (review)) by @marcosholgado ### No UI changes
- Loading branch information
Showing
10 changed files
with
231 additions
and
70 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
...xperiments-impl/src/main/java/com/duckduckgo/experiments/impl/ExperimentFiltersManager.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.experiments.impl | ||
|
||
import com.duckduckgo.appbuildconfig.api.AppBuildConfig | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.experiments.impl.ExperimentFiltersManagerImpl.ExperimentFilterType.ANDROID_VERSION | ||
import com.duckduckgo.experiments.impl.ExperimentFiltersManagerImpl.ExperimentFilterType.LOCALE | ||
import com.duckduckgo.experiments.impl.store.ExperimentVariantEntity | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.util.Locale | ||
import javax.inject.Inject | ||
|
||
interface ExperimentFiltersManager { | ||
fun addFilters(entity: ExperimentVariantEntity): (AppBuildConfig) -> Boolean | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class ExperimentFiltersManagerImpl @Inject constructor( | ||
private val appBuildConfig: AppBuildConfig, | ||
) : ExperimentFiltersManager { | ||
override fun addFilters(entity: ExperimentVariantEntity): (AppBuildConfig) -> Boolean { | ||
if (entity.key == "sc" || entity.key == "se") { | ||
return { isSerpRegionToggleCountry() } | ||
} | ||
|
||
val filters: MutableMap<ExperimentFilterType, Boolean> = mutableMapOf( | ||
LOCALE to true, | ||
ANDROID_VERSION to true, | ||
) | ||
|
||
if (entity.localeFilter.isNotEmpty()) { | ||
val userLocale = Locale.getDefault() | ||
filters[LOCALE] = entity.localeFilter.contains(userLocale.toString()) | ||
} | ||
if (entity.androidVersionFilter.isNotEmpty()) { | ||
val userAndroidVersion = appBuildConfig.sdkInt.toString() | ||
filters[ANDROID_VERSION] = entity.androidVersionFilter.contains(userAndroidVersion) | ||
} | ||
|
||
return { filters.filter { !it.value }.isEmpty() } | ||
} | ||
|
||
private val serpRegionToggleTargetCountries = listOf( | ||
"AU", | ||
"AT", | ||
"DK", | ||
"FI", | ||
"FR", | ||
"DE", | ||
"IT", | ||
"IE", | ||
"NZ", | ||
"NO", | ||
"ES", | ||
"SE", | ||
"GB", | ||
) | ||
|
||
private fun isSerpRegionToggleCountry(): Boolean { | ||
val locale = Locale.getDefault() | ||
return serpRegionToggleTargetCountries.contains(locale.country) | ||
} | ||
|
||
enum class ExperimentFilterType { | ||
LOCALE, | ||
ANDROID_VERSION, | ||
} | ||
} |
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
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
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
112 changes: 112 additions & 0 deletions
112
...ts-impl/src/test/java/com/duckduckgo/experiments/impl/ExperimentFiltersManagerImplTest.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.experiments.impl | ||
|
||
import com.duckduckgo.appbuildconfig.api.AppBuildConfig | ||
import com.duckduckgo.experiments.impl.store.ExperimentVariantEntity | ||
import java.util.Locale | ||
import junit.framework.TestCase.assertFalse | ||
import junit.framework.TestCase.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
class ExperimentFiltersManagerImplTest { | ||
|
||
private lateinit var testee: ExperimentFiltersManager | ||
|
||
private val mockAppBuildConfig: AppBuildConfig = mock() | ||
|
||
@Before | ||
fun setup() { | ||
testee = ExperimentFiltersManagerImpl( | ||
mockAppBuildConfig, | ||
) | ||
} | ||
|
||
@Test | ||
fun whenVariantComplyWithLocaleFilterThenAddFiltersReturnsTrue() { | ||
val locale = Locale("en", "US") | ||
Locale.setDefault(locale) | ||
val testEntity = addActiveVariant(localeFilter = listOf("en_US")) | ||
|
||
assertTrue(testee.addFilters(testEntity).invoke(mockAppBuildConfig)) | ||
} | ||
|
||
@Test | ||
fun whenVariantDoesNotComplyWithLocaleFilterThenAddFiltersReturnsFalse() { | ||
val locale = Locale("en", "US") | ||
Locale.setDefault(locale) | ||
val testEntity = addActiveVariant(localeFilter = listOf("de_DE")) | ||
|
||
assertFalse(testee.addFilters(testEntity).invoke(mockAppBuildConfig)) | ||
} | ||
|
||
@Test | ||
fun whenVariantComplyWithAndroidVersionFilterThenAddFiltersReturnsTrue() { | ||
whenever(mockAppBuildConfig.sdkInt).thenReturn(33) | ||
val testEntity = addActiveVariant(androidVersionFilter = listOf("33", "34")) | ||
|
||
assertTrue(testee.addFilters(testEntity).invoke(mockAppBuildConfig)) | ||
} | ||
|
||
@Test | ||
fun whenVariantDoesNotComplyWithAndroidVersionFilterThenAddFiltersReturnsFalse() { | ||
whenever(mockAppBuildConfig.sdkInt).thenReturn(32) | ||
val testEntity = addActiveVariant(androidVersionFilter = listOf("33", "34")) | ||
|
||
assertFalse(testee.addFilters(testEntity).invoke(mockAppBuildConfig)) | ||
} | ||
|
||
@Test | ||
fun whenVariantComplyWithBothFiltersThenAddFiltersReturnsTrue() { | ||
val locale = Locale("en", "US") | ||
Locale.setDefault(locale) | ||
whenever(mockAppBuildConfig.sdkInt).thenReturn(33) | ||
val testEntity = addActiveVariant(localeFilter = listOf("en_US"), androidVersionFilter = listOf("33", "34")) | ||
|
||
assertTrue(testee.addFilters(testEntity).invoke(mockAppBuildConfig)) | ||
} | ||
|
||
@Test | ||
fun whenVariantComplyWithLocaleFiltersAndDoesNotComplyWithAndroidVersionFilterThenAddFiltersReturnsFalse() { | ||
val locale = Locale("en", "US") | ||
Locale.setDefault(locale) | ||
whenever(mockAppBuildConfig.sdkInt).thenReturn(32) | ||
val testEntity = addActiveVariant(localeFilter = listOf("en_US"), androidVersionFilter = listOf("33", "34")) | ||
|
||
assertFalse(testee.addFilters(testEntity).invoke(mockAppBuildConfig)) | ||
} | ||
|
||
@Test | ||
fun whenVariantComplyWithAndroidVersionFiltersAndDoesNotComplyWithLocaleFilterThenAddFiltersReturnsFalse() { | ||
val locale = Locale("en", "US") | ||
Locale.setDefault(locale) | ||
whenever(mockAppBuildConfig.sdkInt).thenReturn(33) | ||
val testEntity = addActiveVariant(localeFilter = listOf("de_DE"), androidVersionFilter = listOf("33", "34")) | ||
|
||
assertFalse(testee.addFilters(testEntity).invoke(mockAppBuildConfig)) | ||
} | ||
|
||
private fun addActiveVariant( | ||
localeFilter: List<String> = listOf(), | ||
androidVersionFilter: List<String> = listOf(), | ||
): ExperimentVariantEntity { | ||
return ExperimentVariantEntity("key", 1.0, localeFilter, androidVersionFilter) | ||
} | ||
} |
Oops, something went wrong.