Skip to content

Commit

Permalink
For mozilla-mobile#11670: Use TestCoroutineDispatcher for runBlocking…
Browse files Browse the repository at this point in the history
… tests.

This avoids tests timing out.
  • Loading branch information
mcarare committed Mar 18, 2022
1 parent cda0ee7 commit e95ea6e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ import android.support.customtabs.ICustomTabsCallback
import android.support.customtabs.ICustomTabsService
import androidx.browser.customtabs.CustomTabsService
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import mozilla.components.concept.engine.Engine
import mozilla.components.feature.customtabs.store.CustomTabsServiceStore
import mozilla.components.service.digitalassetlinks.RelationChecker
import mozilla.components.support.test.mock
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.doReturn
Expand All @@ -29,6 +36,22 @@ import org.mockito.Mockito.verify
@RunWith(AndroidJUnit4::class)
class AbstractCustomTabsServiceTest {

@ExperimentalCoroutinesApi
private val testDispatcher = TestCoroutineDispatcher()

@ExperimentalCoroutinesApi
@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
}

@ExperimentalCoroutinesApi
@After
fun tearDown() {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}

@Test
fun customTabService() {
val customTabsService = object : MockCustomTabsService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
package mozilla.components.support.migration

import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.webextension.DisabledFlags
import mozilla.components.concept.engine.webextension.Metadata
Expand All @@ -18,9 +23,11 @@ import mozilla.components.support.test.argumentCaptor
import mozilla.components.support.test.eq
import mozilla.components.support.test.mock
import mozilla.components.support.test.whenever
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyBoolean
Expand All @@ -31,6 +38,22 @@ import java.lang.IllegalArgumentException
@RunWith(AndroidJUnit4::class)
class AddonMigrationTest {

@ExperimentalCoroutinesApi
private val testDispatcher = TestCoroutineDispatcher()

@ExperimentalCoroutinesApi
@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
}

@ExperimentalCoroutinesApi
@After
fun tearDown() {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}

@Test
fun `No addons installed`() = runBlocking {
val engine: Engine = mock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ package mozilla.components.support.migration

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import mozilla.appservices.places.BookmarkRoot
import mozilla.appservices.places.uniffi.PlacesException
import mozilla.components.browser.state.action.BrowserAction
Expand Down Expand Up @@ -35,6 +40,7 @@ import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.whenever
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
Expand All @@ -50,17 +56,26 @@ import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.verifyNoInteractions
import java.io.File
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException

@RunWith(AndroidJUnit4::class)
class FennecMigratorTest {
private lateinit var securePrefs: SecureAbove22Preferences
@ExperimentalCoroutinesApi
private val testDispatcher = TestCoroutineDispatcher()

@Before
@ExperimentalCoroutinesApi
fun setup() {
// forceInsecure is set in the tests because a keystore wouldn't be configured in the test environment.
securePrefs = SecureAbove22Preferences(testContext, "test_prefs", forceInsecure = true)
Dispatchers.setMain(testDispatcher)
}

@ExperimentalCoroutinesApi
@After
fun tearDown() {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}

@Test
Expand Down

0 comments on commit e95ea6e

Please sign in to comment.