Skip to content

Commit 9c8734c

Browse files
committed
fix tests with new cookie manager
1 parent dc35665 commit 9c8734c

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

duckchat/duckchat-impl/src/test/kotlin/com/duckduckgo/duckchat/impl/RealDuckChatTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.duckduckgo.duckchat.impl
1919
import android.annotation.SuppressLint
2020
import android.content.Context
2121
import android.content.Intent
22+
import android.webkit.CookieManager
2223
import androidx.core.net.toUri
2324
import androidx.lifecycle.Lifecycle
2425
import androidx.lifecycle.Lifecycle.State.CREATED
@@ -27,6 +28,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
2728
import com.duckduckgo.app.statistics.pixels.Pixel
2829
import com.duckduckgo.app.tabs.BrowserNav
2930
import com.duckduckgo.common.test.CoroutineTestRule
31+
import com.duckduckgo.common.utils.AppUrl
32+
import com.duckduckgo.cookies.api.CookieManagerProvider
3033
import com.duckduckgo.duckchat.api.DuckChatSettingsNoParams
3134
import com.duckduckgo.duckchat.impl.feature.AIChatImageUploadFeature
3235
import com.duckduckgo.duckchat.impl.feature.DuckChatFeature
@@ -95,6 +98,7 @@ class RealDuckChatTest {
9598
private val mockNewAddressBarOptionBottomSheetDialog: NewAddressBarOptionBottomSheetDialog = mock()
9699
private val mockDuckAiContextualOnboardingBottomSheetDialogFactory: DuckAiContextualOnboardingBottomSheetDialogFactory = mock()
97100
private val mockDeviceSyncState: DeviceSyncState = mock()
101+
private val cookiesManager: CookieManagerProvider = mock()
98102

99103
private lateinit var testee: RealDuckChat
100104

@@ -131,6 +135,7 @@ class RealDuckChatTest {
131135
mockNewAddressBarOptionBottomSheetDialogFactory,
132136
mockDuckAiContextualOnboardingBottomSheetDialogFactory,
133137
mockDeviceSyncState,
138+
cookiesManager,
134139
),
135140
)
136141
coroutineRule.testScope.advanceUntilIdle()
@@ -1234,6 +1239,7 @@ class RealDuckChatTest {
12341239
@Test
12351240
fun `when contextual mode enabled, isDuckChatContextualModeEnabled returns true`() = runTest {
12361241
duckChatFeature.contextualMode().setRawStoredState(State(enable = true))
1242+
duckChatFeature.contextualModeKillSwitch().setRawStoredState(State(enable = true))
12371243
testee.onPrivacyConfigDownloaded()
12381244

12391245
assertTrue(testee.isDuckChatContextualModeEnabled())
@@ -1242,11 +1248,50 @@ class RealDuckChatTest {
12421248
@Test
12431249
fun `when contextual mode disabled, isDuckChatContextualModeEnabled returns false`() = runTest {
12441250
duckChatFeature.contextualMode().setRawStoredState(State(enable = false))
1251+
duckChatFeature.contextualModeKillSwitch().setRawStoredState(State(enable = true))
12451252
testee.onPrivacyConfigDownloaded()
12461253

12471254
assertFalse(testee.isDuckChatContextualModeEnabled())
12481255
}
12491256

1257+
@Test
1258+
fun `when migration cookie present and kill switch enabled then isDuckChatContextualModeEnabled returns true`() = runTest {
1259+
val cookieManager = mock<CookieManager>()
1260+
whenever(cookiesManager.get()).thenReturn(cookieManager)
1261+
whenever(cookieManager.getCookie(AppUrl.Url.COOKIES)).thenReturn("migration_status_dev_01=migrated_dev_01")
1262+
duckChatFeature.contextualMode().setRawStoredState(State(enable = false))
1263+
duckChatFeature.contextualModeKillSwitch().setRawStoredState(State(enable = true))
1264+
1265+
testee.onPrivacyConfigDownloaded()
1266+
1267+
assertTrue(testee.isDuckChatContextualModeEnabled())
1268+
}
1269+
1270+
@Test
1271+
fun `when migration cookie present then isStandaloneMigrationCompleted returns true`() = runTest {
1272+
val cookieManager = mock<CookieManager>()
1273+
whenever(cookiesManager.get()).thenReturn(cookieManager)
1274+
whenever(cookieManager.getCookie(AppUrl.Url.COOKIES)).thenReturn("a=b;migration_status_dev_01=migrated_dev_01;c=d")
1275+
1276+
assertTrue(testee.isStandaloneMigrationCompleted())
1277+
}
1278+
1279+
@Test
1280+
fun `when migration cookie missing then isStandaloneMigrationCompleted returns false`() = runTest {
1281+
val cookieManager = mock<CookieManager>()
1282+
whenever(cookiesManager.get()).thenReturn(cookieManager)
1283+
whenever(cookieManager.getCookie(AppUrl.Url.COOKIES)).thenReturn("a=b; c=d")
1284+
1285+
assertFalse(testee.isStandaloneMigrationCompleted())
1286+
}
1287+
1288+
@Test
1289+
fun `when cookie manager is null then isStandaloneMigrationCompleted returns false`() = runTest {
1290+
whenever(cookiesManager.get()).thenReturn(null)
1291+
1292+
assertFalse(testee.isStandaloneMigrationCompleted())
1293+
}
1294+
12501295
companion object {
12511296
val SETTINGS_JSON = """
12521297
{

duckchat/duckchat-impl/src/test/kotlin/com/duckduckgo/duckchat/impl/contextual/DuckChatContextualViewModelTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ class DuckChatContextualViewModelTest {
10531053
) = Unit
10541054

10551055
override suspend fun isContextualOnboardingCompleted(): Boolean = true
1056+
override suspend fun isStandaloneMigrationCompleted(): Boolean = true
10561057
}
10571058

10581059
private class FakeDuckChatContextualDataStore : DuckChatContextualDataStore {

duckchat/duckchat-impl/src/test/kotlin/com/duckduckgo/duckchat/impl/messaging/fakes/FakeDuckChat.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class FakeDuckChat(
3737
private val cosmeticInputScreenUserSettingEnabled = MutableStateFlow<Boolean?>(null)
3838
private val automaticContextAttachmentUserSettingEnabled = MutableStateFlow<Boolean>(false)
3939
var contextualOnboardingCompleted: Boolean = false
40+
var standaloneMigrationCompleted: Boolean = false
4041

4142
override fun isEnabled(): Boolean = enabled
4243

@@ -100,6 +101,10 @@ class FakeDuckChat(
100101
return contextualOnboardingCompleted
101102
}
102103

104+
override suspend fun isStandaloneMigrationCompleted(): Boolean {
105+
return standaloneMigrationCompleted
106+
}
107+
103108
fun setEnabled(enabled: Boolean) {
104109
this.enabled = enabled
105110
}

duckchat/duckchat-impl/src/test/kotlin/com/duckduckgo/duckchat/impl/messaging/fakes/FakeDuckChatInternal.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class FakeDuckChatInternal(
4242
private val inputScreenUserSettingEnabled = MutableStateFlow(false)
4343
private val cosmeticInputScreenUserSettingEnabled = MutableStateFlow<Boolean?>(null)
4444
private val automaticContextAttachmentUserSettingEnabled = MutableStateFlow<Boolean>(false)
45+
private val standaloneMigrationCompleted = MutableStateFlow<Boolean>(false)
4546
var contextualOnboardingCompleted: Boolean = false
4647

4748
// DuckChat interface methods
@@ -81,6 +82,7 @@ class FakeDuckChatInternal(
8182
}
8283

8384
override suspend fun isContextualOnboardingCompleted(): Boolean = contextualOnboardingCompleted
85+
override suspend fun isStandaloneMigrationCompleted(): Boolean = standaloneMigrationCompleted.value
8486

8587
override fun isAutomaticContextAttachmentEnabled(): Boolean = automaticContextAttachmentUserSettingEnabled.value
8688

0 commit comments

Comments
 (0)