Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit fbd4ece

Browse files
Fix tests
1 parent 34bca50 commit fbd4ece

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

fluxc/src/main/java/org/wordpress/android/fluxc/network/UserAgent.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package org.wordpress.android.fluxc.network
22

33
import android.content.Context
44
import android.webkit.WebSettings
5+
import kotlinx.coroutines.CoroutineDispatcher
56
import kotlinx.coroutines.CoroutineScope
67
import kotlinx.coroutines.Dispatchers
78
import kotlinx.coroutines.launch
89
import org.wordpress.android.util.AppLog
910
import org.wordpress.android.util.PackageUtils
1011

1112
@Suppress("MemberNameEqualsClassName")
12-
class UserAgent(
13+
class UserAgent @JvmOverloads constructor(
1314
private val appContext: Context?,
14-
private val appName: String
15+
private val appName: String,
16+
bgDispatcher: CoroutineDispatcher = Dispatchers.Default
1517
) {
1618
/**
1719
* User-Agent string when making HTTP connections, for both API traffic and WebViews.
@@ -24,7 +26,7 @@ class UserAgent(
2426
var userAgent: String = getAppNameVersion()
2527
private set
2628

27-
private val coroutineScope = CoroutineScope(Dispatchers.Default)
29+
private val coroutineScope = CoroutineScope(bgDispatcher)
2830

2931
init {
3032
coroutineScope.launch {

fluxc/src/test/java/org/wordpress/android/fluxc/network/UserAgentTest.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.wordpress.android.fluxc.network
22

33
import android.webkit.WebSettings
4+
import kotlinx.coroutines.Dispatchers
45
import org.junit.Test
56
import org.junit.runner.RunWith
67
import org.mockito.Mockito.mockStatic
@@ -22,7 +23,8 @@ class UserAgentTest {
2223
fun testUserAgent() = withMockedPackageUtils {
2324
mockStatic(WebSettings::class.java).use {
2425
whenever(WebSettings.getDefaultUserAgent(context)).thenReturn(USER_AGENT)
25-
val result = UserAgent(context, APP_NAME)
26+
// Use the Unconfined dispatcher to allow the test to run synchronously
27+
val result = UserAgent(context, APP_NAME, bgDispatcher = Dispatchers.Unconfined)
2628
assertEquals("$USER_AGENT $APP_NAME/$APP_VERSION", result.toString())
2729
}
2830
}
@@ -31,7 +33,8 @@ class UserAgentTest {
3133
fun testDefaultUserAgentFailure() = withMockedPackageUtils {
3234
mockStatic(WebSettings::class.java).use {
3335
whenever(WebSettings.getDefaultUserAgent(context)).thenThrow(RuntimeException(""))
34-
val result = UserAgent(context, APP_NAME)
36+
// Use the Unconfined dispatcher to allow the test to run synchronously
37+
val result = UserAgent(context, APP_NAME, bgDispatcher = Dispatchers.Unconfined)
3538
assertEquals("$APP_NAME/$APP_VERSION", result.toString())
3639
}
3740
}

0 commit comments

Comments
 (0)