|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.browsermode.impl.profile |
| 18 | + |
| 19 | +import android.webkit.CookieManager |
| 20 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 21 | +import androidx.webkit.Profile |
| 22 | +import com.duckduckgo.app.fire.FireproofRepository |
| 23 | +import com.duckduckgo.common.test.CoroutineTestRule |
| 24 | +import com.duckduckgo.common.utils.AppUrl |
| 25 | +import com.duckduckgo.duckchat.api.DuckAiHostProvider |
| 26 | +import kotlinx.coroutines.test.runTest |
| 27 | +import org.junit.Before |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | +import org.mockito.kotlin.any |
| 32 | +import org.mockito.kotlin.eq |
| 33 | +import org.mockito.kotlin.mock |
| 34 | +import org.mockito.kotlin.never |
| 35 | +import org.mockito.kotlin.stub |
| 36 | +import org.mockito.kotlin.verify |
| 37 | +import org.mockito.kotlin.whenever |
| 38 | + |
| 39 | +@RunWith(AndroidJUnit4::class) |
| 40 | +class RealWebViewProfileMigrationManagerTest { |
| 41 | + |
| 42 | + @get:Rule val coroutineRule = CoroutineTestRule() |
| 43 | + |
| 44 | + private val fireproofRepository: FireproofRepository = mock() |
| 45 | + private val duckAiHostProvider: DuckAiHostProvider = mock() |
| 46 | + private val oldCookies: CookieManager = mock() |
| 47 | + private val newCookies: CookieManager = mock() |
| 48 | + private val old: Profile = mock() |
| 49 | + private val new: Profile = mock() |
| 50 | + |
| 51 | + private val testee = RealWebViewProfileMigrationManager( |
| 52 | + fireproofRepository, |
| 53 | + duckAiHostProvider, |
| 54 | + coroutineRule.testDispatcherProvider, |
| 55 | + ) |
| 56 | + |
| 57 | + @Before |
| 58 | + fun setUp() { |
| 59 | + whenever(old.cookieManager).thenReturn(oldCookies) |
| 60 | + whenever(new.cookieManager).thenReturn(newCookies) |
| 61 | + whenever(duckAiHostProvider.getHost()).thenReturn("duck.ai") |
| 62 | + fireproofRepository.stub { onBlocking { fireproofWebsites() }.thenReturn(emptyList()) } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `migrates fireproofed cookies prepending https for bare domains`() = runTest { |
| 67 | + fireproofRepository.stub { onBlocking { fireproofWebsites() }.thenReturn(listOf("example.com")) } |
| 68 | + whenever(oldCookies.getCookie("https://example.com")).thenReturn("a=1") |
| 69 | + |
| 70 | + testee.migrate(old, new) |
| 71 | + |
| 72 | + verify(newCookies).setCookie("https://example.com", "a=1") |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `migrates ddg and duck ai cookies using the injected duck ai host`() = runTest { |
| 77 | + whenever(duckAiHostProvider.getHost()).thenReturn("custom.duck.ai") |
| 78 | + whenever(oldCookies.getCookie(AppUrl.Url.COOKIES)).thenReturn("b=2") |
| 79 | + whenever(oldCookies.getCookie(AppUrl.Url.SURVEY_COOKIES)).thenReturn("c=3") |
| 80 | + whenever(oldCookies.getCookie("https://custom.duck.ai")).thenReturn("d=4") |
| 81 | + |
| 82 | + testee.migrate(old, new) |
| 83 | + |
| 84 | + verify(newCookies).setCookie(AppUrl.Url.COOKIES, "b=2") |
| 85 | + verify(newCookies).setCookie(AppUrl.Url.SURVEY_COOKIES, "c=3") |
| 86 | + verify(newCookies).setCookie("https://custom.duck.ai", "d=4") |
| 87 | + verify(newCookies, never()).setCookie(eq("https://duck.ai"), any()) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `splits multi-cookie strings and skips empty entries`() = runTest { |
| 92 | + fireproofRepository.stub { onBlocking { fireproofWebsites() }.thenReturn(listOf("example.com")) } |
| 93 | + whenever(oldCookies.getCookie("https://example.com")).thenReturn("a=1; b=2; ; c=3") |
| 94 | + |
| 95 | + testee.migrate(old, new) |
| 96 | + |
| 97 | + verify(newCookies).setCookie("https://example.com", "a=1") |
| 98 | + verify(newCookies).setCookie("https://example.com", "b=2") |
| 99 | + verify(newCookies).setCookie("https://example.com", "c=3") |
| 100 | + verify(newCookies, never()).setCookie("https://example.com", "") |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `does nothing when source profile has no cookies for any domain`() = runTest { |
| 105 | + fireproofRepository.stub { onBlocking { fireproofWebsites() }.thenReturn(listOf("example.com")) } |
| 106 | + whenever(oldCookies.getCookie(any())).thenReturn(null) |
| 107 | + |
| 108 | + testee.migrate(old, new) |
| 109 | + |
| 110 | + verify(newCookies, never()).setCookie(any(), any()) |
| 111 | + } |
| 112 | +} |
0 commit comments