Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Jul 25, 2024
1 parent 34bca50 commit fbd4ece
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package org.wordpress.android.fluxc.network

import android.content.Context
import android.webkit.WebSettings
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.PackageUtils

@Suppress("MemberNameEqualsClassName")
class UserAgent(
class UserAgent @JvmOverloads constructor(
private val appContext: Context?,
private val appName: String
private val appName: String,
bgDispatcher: CoroutineDispatcher = Dispatchers.Default
) {
/**
* User-Agent string when making HTTP connections, for both API traffic and WebViews.
Expand All @@ -24,7 +26,7 @@ class UserAgent(
var userAgent: String = getAppNameVersion()
private set

private val coroutineScope = CoroutineScope(Dispatchers.Default)
private val coroutineScope = CoroutineScope(bgDispatcher)

init {
coroutineScope.launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.fluxc.network

import android.webkit.WebSettings
import kotlinx.coroutines.Dispatchers
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mockStatic
Expand All @@ -22,7 +23,8 @@ class UserAgentTest {
fun testUserAgent() = withMockedPackageUtils {
mockStatic(WebSettings::class.java).use {
whenever(WebSettings.getDefaultUserAgent(context)).thenReturn(USER_AGENT)
val result = UserAgent(context, APP_NAME)
// Use the Unconfined dispatcher to allow the test to run synchronously
val result = UserAgent(context, APP_NAME, bgDispatcher = Dispatchers.Unconfined)
assertEquals("$USER_AGENT $APP_NAME/$APP_VERSION", result.toString())
}
}
Expand All @@ -31,7 +33,8 @@ class UserAgentTest {
fun testDefaultUserAgentFailure() = withMockedPackageUtils {
mockStatic(WebSettings::class.java).use {
whenever(WebSettings.getDefaultUserAgent(context)).thenThrow(RuntimeException(""))
val result = UserAgent(context, APP_NAME)
// Use the Unconfined dispatcher to allow the test to run synchronously
val result = UserAgent(context, APP_NAME, bgDispatcher = Dispatchers.Unconfined)
assertEquals("$APP_NAME/$APP_VERSION", result.toString())
}
}
Expand Down

0 comments on commit fbd4ece

Please sign in to comment.