Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Get browser-domains tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoryk committed Feb 15, 2022
1 parent 58b1186 commit 928d658
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions components/browser/domains/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {

testImplementation Dependencies.androidx_test_junit
testImplementation Dependencies.testing_robolectric
testImplementation Dependencies.testing_coroutines
}

apply from: '../../../publish.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
package mozilla.components.browser.domains.autocomplete

import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import mozilla.components.browser.domains.CustomDomains
import mozilla.components.browser.domains.Domain
import mozilla.components.browser.domains.Domains
Expand Down Expand Up @@ -47,17 +44,16 @@ private fun CustomDomains.asLoader(): DomainsLoader = { context: Context -> load
*/
open class BaseDomainAutocompleteProvider(
private val list: DomainList,
private val domainsLoader: DomainsLoader
) : DomainAutocompleteProvider, CoroutineScope by CoroutineScope(Dispatchers.IO) {
private val domainsLoader: DomainsLoader,
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO)
) : DomainAutocompleteProvider {

// We compute 'domains' on the worker thread; make sure it's immediately visible on the UI thread.
@Volatile
var domains: List<Domain> = emptyList()

fun initialize(context: Context) {
launch {
domains = async { domainsLoader(context) }.await()
}
suspend fun initialize(context: Context) = withContext(scope.coroutineContext) {
domains = domainsLoader(context)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package mozilla.components.browser.domains

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import mozilla.components.browser.domains.autocomplete.BaseDomainAutocompleteProvider
import mozilla.components.browser.domains.autocomplete.DomainAutocompleteProvider
import mozilla.components.browser.domains.autocomplete.DomainList
Expand All @@ -18,8 +21,11 @@ import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class BaseDomainAutocompleteProviderTest {
private val dispatcher = StandardTestDispatcher()
private val scope = TestScope(dispatcher)

@Test
fun `empty provider with DEFAULT list returns nothing`() {
Expand Down Expand Up @@ -98,6 +104,12 @@ class BaseDomainAutocompleteProviderTest {
assertNoCompletion(provider, "wwww")
assertNoCompletion(provider, "yahoo")
}

private fun createAndInitProvider(context: Context, list: DomainList, loader: DomainsLoader): DomainAutocompleteProvider {
return BaseDomainAutocompleteProvider(list, loader, scope).apply {
runTest(dispatcher) { initialize(context) }
}
}
}

private fun assertCompletion(
Expand Down Expand Up @@ -125,8 +137,3 @@ private fun assertNoCompletion(provider: DomainAutocompleteProvider, input: Stri

assertNull("Result should be null", result)
}

private fun createAndInitProvider(context: Context, list: DomainList, loader: DomainsLoader): DomainAutocompleteProvider =
object : BaseDomainAutocompleteProvider(list, loader) {
override val coroutineContext = super.coroutineContext + Dispatchers.Main
}.apply { initialize(context) }

0 comments on commit 928d658

Please sign in to comment.