Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 9c33aab

Browse files
rocketsrogermergify[bot]
authored andcommitted
Bug 1830780 - Add support for adding distribution based default search engines list
1 parent 5401af1 commit 9c33aab

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

android-components/components/browser/state/src/main/java/mozilla/components/browser/state/action/BrowserAction.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,8 +1398,9 @@ sealed class SearchAction : BrowserAction() {
13981398

13991399
/**
14001400
* Sets the [RegionState] (region of the user).
1401+
* distribution is a [String] that specifies a set of default search engines if available
14011402
*/
1402-
data class SetRegionAction(val regionState: RegionState) : SearchAction()
1403+
data class SetRegionAction(val regionState: RegionState, val distribution: String? = null) : SearchAction()
14031404

14041405
/**
14051406
* Sets the list of search engines and default search engine IDs.

android-components/components/feature/search/src/main/java/mozilla/components/feature/search/middleware/SearchMiddleware.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SearchMiddleware(
5757
action: BrowserAction,
5858
) {
5959
when (action) {
60-
is SearchAction.SetRegionAction -> loadSearchEngines(context.store, action.regionState)
60+
is SearchAction.SetRegionAction -> loadSearchEngines(context.store, action.regionState, action.distribution)
6161
is SearchAction.UpdateCustomSearchEngineAction -> saveCustomSearchEngine(action)
6262
is SearchAction.RemoveCustomSearchEngineAction -> removeCustomSearchEngine(action)
6363
is SearchAction.SelectSearchEngineAction -> updateSearchEngineSelection(action)
@@ -82,11 +82,14 @@ class SearchMiddleware(
8282
private fun loadSearchEngines(
8383
store: Store<BrowserState, BrowserAction>,
8484
region: RegionState,
85+
distribution: String? = null,
8586
) = scope.launch {
8687
val migrationValues = migration?.getValuesToMigrate()
8788
performCustomSearchEnginesMigration(migrationValues)
8889

89-
val regionBundle = async(ioDispatcher) { bundleStorage.load(region, coroutineContext = ioDispatcher) }
90+
val regionBundle = async(ioDispatcher) {
91+
bundleStorage.load(region, distribution = distribution, coroutineContext = ioDispatcher)
92+
}
9093
val customSearchEngines = async(ioDispatcher) { customStorage.loadSearchEngineList() }
9194
val hiddenSearchEngineIds = async(ioDispatcher) { metadataStorage.getHiddenSearchEngines() }
9295
val additionalSearchEngineIds = async(ioDispatcher) { metadataStorage.getAdditionalSearchEngines() }
@@ -240,10 +243,14 @@ class SearchMiddleware(
240243
interface BundleStorage {
241244
/**
242245
* Loads the bundled search engines for the given [locale] and [region].
246+
*
247+
* If [distribution] is not null then attempt to load the bundled search engine for the
248+
* [distribution] in the specified [locale] and [region] if available.
243249
*/
244250
suspend fun load(
245251
region: RegionState,
246252
locale: Locale = Locale.getDefault(),
253+
distribution: String? = null,
247254
coroutineContext: CoroutineContext = Dispatchers.IO,
248255
): Bundle
249256

android-components/components/feature/search/src/main/java/mozilla/components/feature/search/storage/BundledSearchEnginesStorage.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ internal class BundledSearchEnginesStorage(
3737
override suspend fun load(
3838
region: RegionState,
3939
locale: Locale,
40+
distribution: String?,
4041
coroutineContext: CoroutineContext,
4142
): SearchMiddleware.BundleStorage.Bundle = withContext(coroutineContext) {
42-
val localizedConfiguration = loadAndFilterConfiguration(context, region, locale)
43-
val searchEngineIdentifiers = localizedConfiguration.visibleDefaultEngines
43+
val localizedConfiguration = loadAndFilterConfiguration(context, region, locale, distribution)
44+
val searchEngineIdentifiers = localizedConfiguration.visibleSearchEngines
4445

4546
val searchEngines = loadSearchEnginesFromList(
4647
context,
@@ -92,7 +93,7 @@ internal class BundledSearchEnginesStorage(
9293
}
9394

9495
private data class SearchEngineListConfiguration(
95-
val visibleDefaultEngines: List<String>,
96+
val visibleSearchEngines: List<String>,
9697
val searchOrder: List<String>,
9798
val searchDefault: String?,
9899
)
@@ -101,11 +102,13 @@ private fun loadAndFilterConfiguration(
101102
context: Context,
102103
region: RegionState,
103104
locale: Locale,
105+
distribution: String?,
104106
): SearchEngineListConfiguration {
105107
val config = context.assets.readJSONObject("search/list.json")
106108

107109
val configBlocks = pickConfigurationBlocks(locale, config)
108-
val jsonSearchEngineIdentifiers = getSearchEngineIdentifiersFromBlock(region, locale, configBlocks)
110+
val jsonSearchEngineIdentifiers =
111+
getSearchEngineIdentifiersFromBlock(region, locale, distribution, configBlocks)
109112

110113
val searchOrder = getSearchOrderFromBlock(region, configBlocks)
111114
val searchDefault = getSearchDefaultFromBlock(region, configBlocks)
@@ -144,10 +147,12 @@ private fun pickConfigurationBlocks(
144147
private fun getSearchEngineIdentifiersFromBlock(
145148
region: RegionState,
146149
locale: Locale,
150+
distribution: String?,
147151
configBlocks: Array<JSONObject>,
148152
): JSONArray {
149-
// Now test if there's an override for the region (if it's set)
150-
return getArrayFromBlock(region, "visibleDefaultEngines", configBlocks)
153+
// Now test if there's an override for the distribution or region (if it's set)
154+
return distribution?.let { getArrayFromBlock(region, distribution, configBlocks) }
155+
?: getArrayFromBlock(region, "visibleDefaultEngines", configBlocks)
151156
?: throw IllegalStateException("No visibleDefaultEngines using region $region and locale $locale")
152157
}
153158

android-components/components/feature/search/src/test/java/mozilla/components/feature/search/middleware/SearchMiddlewareTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,37 @@ class SearchMiddlewareTest {
8888
assertNull(store.state.search.regionSearchEngines.find { it.name == "Yandex" })
8989
}
9090

91+
@Test
92+
fun `WHEN distribution doesn't exist THEN Loads default search engines`() {
93+
val searchMiddleware = SearchMiddleware(
94+
testContext,
95+
ioDispatcher = dispatcher,
96+
customStorage = CustomSearchEngineStorage(testContext, dispatcher),
97+
)
98+
99+
val store = BrowserStore(
100+
middleware = listOf(searchMiddleware),
101+
)
102+
103+
assertTrue(store.state.search.regionSearchEngines.isEmpty())
104+
105+
store.dispatch(
106+
SearchAction.SetRegionAction(
107+
RegionState("US", "US"),
108+
"test",
109+
),
110+
).joinBlocking()
111+
112+
wait(store, dispatcher)
113+
114+
assertTrue(store.state.search.regionSearchEngines.isNotEmpty())
115+
assertTrue(store.state.search.additionalAvailableSearchEngines.isEmpty())
116+
assertTrue(store.state.search.additionalSearchEngines.isEmpty())
117+
118+
assertNotNull(store.state.search.regionSearchEngines.find { it.name == "Google" })
119+
assertNull(store.state.search.regionSearchEngines.find { it.name == "Yandex" })
120+
}
121+
91122
@Test
92123
fun `Loads search engines for locale (en-AU)`() {
93124
Locale.setDefault(Locale("en", "AU"))

0 commit comments

Comments
 (0)