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

Commit

Permalink
WIP cleanup of couroutine stuff in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoryk committed Jan 15, 2022
1 parent ee81486 commit 2be3f2e
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import mozilla.components.browser.menu2.R
import mozilla.components.concept.menu.Side
import mozilla.components.concept.menu.candidate.AsyncDrawableMenuIcon
Expand All @@ -22,11 +26,10 @@ import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.test.any
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.clearInvocations
Expand All @@ -37,18 +40,15 @@ import org.mockito.Mockito.verify
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class DrawableMenuIconViewHoldersTest {

@get:Rule
val coroutinesTestRule = MainCoroutineRule()
private val testDispatcher = coroutinesTestRule.testDispatcher

private lateinit var parent: ConstraintLayout
private lateinit var layoutInflater: LayoutInflater
private lateinit var imageView: ImageView
private lateinit var imageButton: ImageButton

@Before
fun setup() {
Dispatchers.setMain(UnconfinedTestDispatcher())

parent = mock()
layoutInflater = mock()
imageView = mock()
Expand All @@ -63,6 +63,11 @@ class DrawableMenuIconViewHoldersTest {
doReturn(imageButton).`when`(imageButton).findViewById<TextView>(R.id.icon)
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun `icon view holder sets icon on view`() {
val holder = DrawableMenuIconViewHolder(parent, layoutInflater, Side.END)
Expand All @@ -85,7 +90,7 @@ class DrawableMenuIconViewHoldersTest {
}

@Test
fun `async view holder sets icon on view`() = testDispatcher.runBlockingTest {
fun `async view holder sets icon on view`() = runTest(UnconfinedTestDispatcher()) {
val holder = AsyncDrawableMenuIconViewHolder(parent, layoutInflater, Side.END)

val drawable = mock<Drawable>()
Expand All @@ -95,7 +100,7 @@ class DrawableMenuIconViewHoldersTest {
}

@Test
fun `async view holder uses loading icon and fallback icon`() = testDispatcher.runBlockingTest {
fun `async view holder uses loading icon and fallback icon`() = runTest(UnconfinedTestDispatcher()) {
val logger = mock<Logger>()
val holder = AsyncDrawableMenuIconViewHolder(parent, layoutInflater, Side.END, logger)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package mozilla.components.browser.thumbnails
import android.graphics.Bitmap
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import mozilla.components.browser.state.action.ContentAction
Expand All @@ -32,17 +32,14 @@ import org.mockito.Mockito.verifyNoMoreInteractions

@RunWith(AndroidJUnit4::class)
class BrowserThumbnailsTest {

private val testDispatcher = TestCoroutineDispatcher()

private lateinit var store: BrowserStore
private lateinit var engineView: EngineView
private lateinit var thumbnails: BrowserThumbnails
private val tabId = "test-tab"

@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
Dispatchers.setMain(UnconfinedTestDispatcher())
store = spy(
BrowserStore(
BrowserState(
Expand All @@ -60,7 +57,6 @@ class BrowserThumbnailsTest {
@After
fun tearDown() {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package mozilla.components.browser.thumbnails.storage
import android.graphics.Bitmap
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import mozilla.components.concept.base.images.ImageLoadRequest
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.mock
Expand All @@ -26,7 +26,7 @@ import org.mockito.Mockito.spy
@RunWith(AndroidJUnit4::class)
class ThumbnailStorageTest {

private val testDispatcher = TestCoroutineDispatcher()
private val testDispatcher = UnconfinedTestDispatcher()

@Before
@After
Expand All @@ -35,7 +35,7 @@ class ThumbnailStorageTest {
}

@Test
fun `clearThumbnails`() = runBlocking {
fun `clearThumbnails`() = runTest(testDispatcher) {
val bitmap: Bitmap = mock()
val thumbnailStorage = spy(ThumbnailStorage(testContext, testDispatcher))

Expand All @@ -54,7 +54,7 @@ class ThumbnailStorageTest {
}

@Test
fun `deleteThumbnail`() = runBlocking {
fun `deleteThumbnail`() = runTest(testDispatcher) {
val request = "test-tab1"
val bitmap: Bitmap = mock()
val thumbnailStorage = spy(ThumbnailStorage(testContext, testDispatcher))
Expand All @@ -69,7 +69,7 @@ class ThumbnailStorageTest {
}

@Test
fun `saveThumbnail`() = runBlocking {
fun `saveThumbnail`() = runTest(testDispatcher) {
val request = ImageLoadRequest("test-tab1", 100)
val bitmap: Bitmap = mock()
val thumbnailStorage = spy(ThumbnailStorage(testContext))
Expand All @@ -83,7 +83,7 @@ class ThumbnailStorageTest {
}

@Test
fun `loadThumbnail`() = runBlocking {
fun `loadThumbnail`() = runTest(testDispatcher) {
val request = ImageLoadRequest("test-tab1", 100)
val bitmap: Bitmap = mock()
val thumbnailStorage = spy(ThumbnailStorage(testContext, testDispatcher))
Expand Down
6 changes: 0 additions & 6 deletions components/feature/addons/src/test/java/AddonManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import mozilla.components.support.test.any
import mozilla.components.support.test.argumentCaptor
import mozilla.components.support.test.eq
import mozilla.components.support.test.mock
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.whenever
import mozilla.components.support.webextensions.WebExtensionSupport
import org.junit.After
Expand All @@ -40,7 +39,6 @@ import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyBoolean
Expand All @@ -53,10 +51,6 @@ import org.mockito.Mockito.verify
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class AddonManagerTest {

@get:Rule
val coroutinesTestRule = MainCoroutineRule()

@Before
fun setup() {
WebExtensionSupport.installedExtensions.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package mozilla.components.feature.addons.menu
import android.graphics.Color
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import mozilla.components.concept.engine.webextension.Action
import mozilla.components.concept.menu.candidate.AsyncDrawableMenuIcon
import mozilla.components.concept.menu.candidate.TextMenuIcon
Expand Down Expand Up @@ -97,7 +99,7 @@ class WebExtensionActionMenuCandidateTest {
}

@Test
fun `create menu candidate with icon`() = runBlockingTest {
fun `create menu candidate with icon`() = runTest(UnconfinedTestDispatcher()) {
var calledWith: Int = -1
val candidate = baseAction
.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.ListenableWorker
import androidx.work.await
import androidx.work.testing.TestListenableWorkerBuilder
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import mozilla.components.concept.engine.webextension.EnableSource
import mozilla.components.feature.addons.Addon
Expand All @@ -27,15 +26,13 @@ import mozilla.components.support.test.argumentCaptor
import mozilla.components.support.test.eq
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.whenever
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.verify
Expand All @@ -44,11 +41,6 @@ import java.lang.Exception

@RunWith(AndroidJUnit4::class)
class SupportedAddonsWorkerTest {

@ExperimentalCoroutinesApi
@get:Rule
val coroutinesTestRule = MainCoroutineRule()

@Before
fun setUp() {
GlobalAddonDependencyProvider.addonManager = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ import androidx.appcompat.widget.AppCompatCheckBox
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.test.resetMain
import mozilla.components.feature.addons.Addon
import mozilla.components.feature.addons.R
import mozilla.components.feature.addons.amo.AddonCollectionProvider
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.whenever
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
Expand All @@ -43,10 +47,18 @@ import java.io.IOException

@RunWith(AndroidJUnit4::class)
class AddonInstallationDialogFragmentTest {
private val testDispatcher = UnconfinedTestDispatcher()
private val scope = TestScope(testDispatcher)

@get:Rule
val coroutinesTestRule = MainCoroutineRule()
private val scope = TestCoroutineScope(coroutinesTestRule.testDispatcher)
@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun `build dialog`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.test.resetMain
import mozilla.components.feature.addons.Addon
import mozilla.components.feature.addons.R
import mozilla.components.feature.addons.amo.AddonCollectionProvider
Expand All @@ -26,14 +30,14 @@ import mozilla.components.feature.addons.ui.AddonsManagerAdapter.Section
import mozilla.components.support.test.argumentCaptor
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.whenever
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
Expand All @@ -48,9 +52,18 @@ import java.util.Locale
@RunWith(AndroidJUnit4::class)
class AddonsManagerAdapterTest {

@get:Rule
val coroutinesTestRule = MainCoroutineRule()
private val scope = TestCoroutineScope(coroutinesTestRule.testDispatcher)
private val testDispatcher = UnconfinedTestDispatcher()
private val scope = TestScope(testDispatcher)

@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun `createListWithSections`() {
Expand Down
Loading

0 comments on commit 2be3f2e

Please sign in to comment.