commit bd8f46ca365978f6483a9f98ecebf525cc36d2e2
parent 427c04b1569f4138d3c74c2f23d0c3e19e902448
Author: mcarare <48995920+mcarare@users.noreply.github.com>
Date: Fri, 5 Dec 2025 14:34:16 +0000
Bug 2004356 - Refactor SaveToPDFMiddlewareTest to use StandardTestDispatcher. r=android-reviewers,avirvara
This patch replaces the usage of `MainCoroutineRule` and `runTestOnMain` with `StandardTestDispatcher` and `runTest`.
It also updates the `SaveToPDFMiddleware` and `EngineMiddleware` instantiation to use the new test scope and adds missing mocks for `requestPdfToDownload` and `requestPrintContent`.
Differential Revision: https://phabricator.services.mozilla.com/D275208
Diffstat:
1 file changed, 23 insertions(+), 19 deletions(-)
diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/share/SaveToPDFMiddlewareTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/share/SaveToPDFMiddlewareTest.kt
@@ -9,6 +9,9 @@ import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.verify
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.runTest
import mozilla.components.browser.state.action.EngineAction
import mozilla.components.browser.state.engine.EngineMiddleware
import mozilla.components.browser.state.state.BrowserState
@@ -17,8 +20,6 @@ import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession
import mozilla.components.support.test.robolectric.testContext
-import mozilla.components.support.test.rule.MainCoroutineRule
-import mozilla.components.support.test.rule.runTestOnMain
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
@@ -44,25 +45,24 @@ class SaveToPDFMiddlewareTest {
@get:Rule
val gleanTestRule = FenixGleanTestRule(testContext)
- @get:Rule
- val mainCoroutineTestRule = MainCoroutineRule()
-
// Only ERROR_PRINT_SETTINGS_SERVICE_NOT_AVAILABLE is available for testing
class MockGeckoPrintException : GeckoSession.GeckoPrintException()
private lateinit var middleware: SaveToPDFMiddleware
+ private val testDispatcher = StandardTestDispatcher()
+ private val scope = CoroutineScope(testDispatcher)
@Before
fun setup() {
every { testContext.recordEventInNimbus(any()) } just Runs
- middleware = SaveToPDFMiddleware(context = testContext)
+ middleware = SaveToPDFMiddleware(context = testContext, scope)
appStore = mockk(relaxed = true)
every { testContext.components.appStore } returns appStore
}
@Test
fun `GIVEN a save to pdf request WHEN it fails unexpectedly THEN unknown failure telemetry is sent AND a snackbar error is shown`() =
- runTestOnMain {
+ runTest(testDispatcher) {
val exceptionToThrow = RuntimeException("reader save to pdf failed")
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
@@ -106,7 +106,7 @@ class SaveToPDFMiddlewareTest {
@Test
fun `GIVEN a save to pdf request WHEN it fails due to io THEN io failure telemetry is sent AND a snackbar error is shown`() =
- runTestOnMain {
+ runTest(testDispatcher) {
val exceptionToThrow = IOException()
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
@@ -148,7 +148,7 @@ class SaveToPDFMiddlewareTest {
@Test
fun `GIVEN a save to pdf request WHEN it fails due to print exception THEN print exception failure telemetry is sent AND a snackbar error is shown`() =
- runTestOnMain {
+ runTest(testDispatcher) {
val exceptionToThrow = MockGeckoPrintException()
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
@@ -190,7 +190,7 @@ class SaveToPDFMiddlewareTest {
@Test
fun `GIVEN a save to pdf request WHEN it completes THEN completed telemetry is sent`() =
- runTestOnMain {
+ runTest(testDispatcher) {
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
checkForPdfViewer(any(), any())
@@ -220,17 +220,19 @@ class SaveToPDFMiddlewareTest {
@Test
fun `GIVEN a save to pdf request WHEN it the action begins THEN tapped telemetry is sent`() =
- runTestOnMain {
+ runTest(testDispatcher) {
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
checkForPdfViewer(any(), any())
} answers {
firstArg<(Boolean) -> Unit>().invoke(false)
}
+
+ every { requestPdfToDownload() } just Runs
}
val engineMiddleware = EngineMiddleware.create(
mockk<Engine>(),
- mainCoroutineTestRule.scope,
+ this,
)
val browserStore = BrowserStore(
middleware = listOf(middleware) + engineMiddleware,
@@ -253,7 +255,7 @@ class SaveToPDFMiddlewareTest {
}
@Test
- fun `GIVEN a save as pdf exception THEN should calculate the correct failure reason for telemetry`() = runTestOnMain {
+ fun `GIVEN a save as pdf exception THEN should calculate the correct failure reason for telemetry`() = runTest(testDispatcher) {
val noSettingsService = middleware.telemetryErrorReason(MockGeckoPrintException())
assertEquals("no_settings_service", noSettingsService)
val ioException = middleware.telemetryErrorReason(IOException())
@@ -263,14 +265,14 @@ class SaveToPDFMiddlewareTest {
}
@Test
- fun `GIVEN a save as pdf page type THEN should calculate the correct page source for telemetry`() = runTestOnMain {
+ fun `GIVEN a save as pdf page type THEN should calculate the correct page source for telemetry`() = runTest(testDispatcher) {
assertEquals("pdf", middleware.telemetrySource(isPdfViewer = true))
assertEquals("non-pdf", middleware.telemetrySource(isPdfViewer = false))
assertEquals("unknown", middleware.telemetrySource(isPdfViewer = null))
}
@Test
- fun `GIVEN a print request WHEN it fails unexpectedly THEN unknown failure telemetry is sent AND a snackbar error is shown`() = runTestOnMain {
+ fun `GIVEN a print request WHEN it fails unexpectedly THEN unknown failure telemetry is sent AND a snackbar error is shown`() = runTest(testDispatcher) {
val exceptionToThrow = RuntimeException("No Print Spooler")
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
@@ -313,7 +315,7 @@ class SaveToPDFMiddlewareTest {
}
@Test
- fun `GIVEN a print request WHEN it fails due to print exception THEN print exception failure telemetry is sent AND a snackbar error is shown`() = runTestOnMain {
+ fun `GIVEN a print request WHEN it fails due to print exception THEN print exception failure telemetry is sent AND a snackbar error is shown`() = runTest(testDispatcher) {
val exceptionToThrow = MockGeckoPrintException()
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
@@ -354,7 +356,7 @@ class SaveToPDFMiddlewareTest {
}
@Test
- fun `GIVEN a print request WHEN it completes THEN completed telemetry is sent`() = runTestOnMain {
+ fun `GIVEN a print request WHEN it completes THEN completed telemetry is sent`() = runTest(testDispatcher) {
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
checkForPdfViewer(any(), any())
@@ -383,17 +385,19 @@ class SaveToPDFMiddlewareTest {
}
@Test
- fun `GIVEN a print request WHEN it the action begins THEN tapped telemetry is sent`() = runTestOnMain {
+ fun `GIVEN a print request WHEN it the action begins THEN tapped telemetry is sent`() = runTest(testDispatcher) {
val mockEngineSession: EngineSession = mockk<EngineSession>().apply {
every {
checkForPdfViewer(any(), any())
} answers {
firstArg<(Boolean) -> Unit>().invoke(false)
}
+
+ every { requestPrintContent() } just Runs
}
val engineMiddleware = EngineMiddleware.create(
mockk<Engine>(),
- mainCoroutineTestRule.scope,
+ this,
)
val browserStore = BrowserStore(
middleware = listOf(middleware) + engineMiddleware,