From c12a5b98ba162b8c734add12b99e9c0feed3439d Mon Sep 17 00:00:00 2001 From: Jonathan Almeida Date: Mon, 12 Jul 2021 17:27:57 -0400 Subject: [PATCH] [fenix] Close https://github.com/mozilla-mobile/fenix/issues/20320: Use scope from testDispatcher in RecentBookmarksFeatureTest --- .../recentbookmarks/RecentBookmarksFeature.kt | 7 ++-- .../RecentBookmarksFeatureTest.kt | 34 +++---------------- 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeature.kt b/app/src/main/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeature.kt index 05ff15360..26c45c708 100644 --- a/app/src/main/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeature.kt +++ b/app/src/main/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeature.kt @@ -4,6 +4,7 @@ package org.mozilla.fenix.home.recentbookmarks +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -22,16 +23,18 @@ import org.mozilla.fenix.home.HomeFragmentStore * @param bookmarksUseCase the [BookmarksUseCase] for retrieving the list of recently saved * bookmarks from storage. * @param scope the [CoroutineScope] used to fetch the bookmarks list + * @param ioDispatcher the [CoroutineDispatcher] for performing read/write operations. */ class RecentBookmarksFeature( private val homeStore: HomeFragmentStore, private val bookmarksUseCase: BookmarksUseCase, - private val scope: CoroutineScope + private val scope: CoroutineScope, + private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO ) : LifecycleAwareFeature { internal var job: Job? = null override fun start() { - job = scope.launch(Dispatchers.IO) { + job = scope.launch(ioDispatcher) { val bookmarks = bookmarksUseCase.retrieveRecentBookmarks() homeStore.dispatch(HomeFragmentAction.RecentBookmarksChange(bookmarks)) diff --git a/app/src/test/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeatureTest.kt b/app/src/test/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeatureTest.kt index 92a507081..fee1c1431 100644 --- a/app/src/test/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeatureTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/recentbookmarks/RecentBookmarksFeatureTest.kt @@ -7,11 +7,9 @@ package org.mozilla.fenix.home.recentbookmarks import io.mockk.coEvery import io.mockk.coVerify import io.mockk.mockk -import io.mockk.spyk -import io.mockk.verify +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestCoroutineDispatcher -import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.runBlockingTest import mozilla.components.concept.storage.BookmarkNode import mozilla.components.concept.storage.BookmarkNodeType @@ -20,8 +18,6 @@ import mozilla.components.support.test.middleware.CaptureActionsMiddleware import mozilla.components.support.test.rule.MainCoroutineRule import org.junit.After import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertNull import org.junit.Before import org.junit.Rule import org.junit.Test @@ -36,7 +32,6 @@ class RecentBookmarksFeatureTest { private val middleware = CaptureActionsMiddleware() private val homeStore = HomeFragmentStore(middlewares = listOf(middleware)) private val bookmarksUseCases: BookmarksUseCase = mockk(relaxed = true) - private val scope = TestCoroutineScope() private val testDispatcher = TestCoroutineDispatcher() private val bookmark = BookmarkNode( type = BookmarkNodeType.ITEM, @@ -58,7 +53,6 @@ class RecentBookmarksFeatureTest { @After fun cleanUp() { - scope.cleanupTestCoroutines() testDispatcher.cleanupTestCoroutines() } @@ -68,13 +62,14 @@ class RecentBookmarksFeatureTest { val feature = RecentBookmarksFeature( homeStore, bookmarksUseCases, - scope + CoroutineScope(testDispatcher), + testDispatcher ) - feature.start() - assertEquals(emptyList(), homeStore.state.recentBookmarks) + feature.start() + testDispatcher.advanceUntilIdle() homeStore.waitUntilIdle() @@ -86,23 +81,4 @@ class RecentBookmarksFeatureTest { assertEquals(listOf(bookmark), it.recentBookmarks) } } - - @Test - fun `WHEN the feature is destroyed THEN the job is cancelled`() { - val feature = spyk(RecentBookmarksFeature( - homeStore, - bookmarksUseCases, - scope - )) - - assertNull(feature.job) - - feature.start() - - assertNotNull(feature.job) - - feature.stop() - - verify(exactly = 1) { feature.stop() } - } }