Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Feb 26, 2025
1 parent 3466a87 commit d4ce231
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5161,15 +5161,15 @@ class BrowserTabViewModelTest {
@Test
fun whenMaliciousSiteActionLeaveSiteAndCustomTabThenClose() {
val url = "http://example.com".toUri()
testee.onMaliciousSiteUserAction(LeaveSite, url, MALWARE, true)
testee.onMaliciousSiteUserAction(LeaveSite, url, url, MALWARE, true)
verify(mockCommandObserver, atLeastOnce()).onChanged(commandCaptor.capture())
assertTrue(commandCaptor.allValues.any { it is Command.CloseCustomTab })
}

@Test
fun whenMaliciousSiteActionLeaveSiteAndCustomTabFalseThenHideSSLError() {
val url = "http://example.com".toUri()
testee.onMaliciousSiteUserAction(LeaveSite, url, MALWARE, false)
testee.onMaliciousSiteUserAction(LeaveSite, url, url, MALWARE, false)
verify(mockCommandObserver, atLeastOnce()).onChanged(commandCaptor.capture())
assertTrue(commandCaptor.allValues.any { it is Command.EscapeMaliciousSite })
}
Expand Down Expand Up @@ -5265,15 +5265,27 @@ class BrowserTabViewModelTest {
}

@Test
fun whenTrackersBlockedCtaShownThenPrivacyShieldIsHighlighted() = runTest {
fun whenTrackersBlockedCtaShownWithBrowserShowingThenPrivacyShieldIsHighlighted() = runTest {
val cta = DaxTrackersBlockedCta(mockOnboardingStore, mockAppInstallStore, emptyList(), mockSettingsDataStore)
testee.ctaViewState.value = ctaViewState().copy(cta = cta)
testee.browserViewState.value = browserViewState().copy(browserShowing = true, maliciousSiteBlocked = false)

testee.onOnboardingDaxTypingAnimationFinished()

assertTrue(browserViewState().showPrivacyShield.isHighlighted())
}

@Test
fun whenTrackersBlockedCtaShownWithMaliciousSiteBlockedThenPrivacyShieldIsNotHighlighted() = runTest {
val cta = DaxTrackersBlockedCta(mockOnboardingStore, mockAppInstallStore, emptyList(), mockSettingsDataStore)
testee.ctaViewState.value = ctaViewState().copy(cta = cta)
testee.browserViewState.value = browserViewState().copy(browserShowing = false, maliciousSiteBlocked = true)

testee.onOnboardingDaxTypingAnimationFinished()

assertFalse(browserViewState().showPrivacyShield.isHighlighted())
}

@Test
fun givenTrackersBlockedCtaShownWhenLaunchingTabSwitcherThenCtaIsDismissed() = runTest {
val cta = DaxTrackersBlockedCta(mockOnboardingStore, mockAppInstallStore, emptyList(), mockSettingsDataStore)
Expand Down Expand Up @@ -5933,7 +5945,7 @@ class BrowserTabViewModelTest {
}

@Test
fun whenVisitSiteThenUpdateBrowserViewStateLoadingViewStateAndOmnibarViewState() {
fun whenVisitSiteThenUpdateLoadingViewStateAndOmnibarViewState() {
testee.browserViewState.value = browserViewState().copy(
browserShowing = false,
maliciousSiteBlocked = true,
Expand All @@ -5943,17 +5955,8 @@ class BrowserTabViewModelTest {
testee.loadingViewState.value = loadingViewState().copy(isLoading = false)
testee.omnibarViewState.value = omnibarViewState().copy(isEditing = true)

testee.onMaliciousSiteUserAction(VisitSite, "http://example.com".toUri(), Feed.PHISHING, false)
testee.onMaliciousSiteUserAction(VisitSite, "http://example.com".toUri(), "http://example.com".toUri(), Feed.PHISHING, false)

assertEquals(
browserViewState(),
browserViewState().copy(
browserShowing = true,
showPrivacyShield = HighlightableButton.Visible(enabled = true),
fireButton = HighlightableButton.Visible(enabled = true),
maliciousSiteBlocked = false,
),
)
assertEquals(
loadingViewState(),
loadingViewState().copy(
Expand All @@ -5975,7 +5978,7 @@ class BrowserTabViewModelTest {

@Test
fun whenLeaveSiteAndCustomTabThenEmitCloseCustomTab() {
testee.onMaliciousSiteUserAction(LeaveSite, "http://example.com".toUri(), Feed.PHISHING, true)
testee.onMaliciousSiteUserAction(LeaveSite, "http://example.com".toUri(), "http://example.com".toUri(), Feed.PHISHING, true)
assertCommandIssued<CloseCustomTab>()
}

Expand All @@ -5985,7 +5988,7 @@ class BrowserTabViewModelTest {
whenever(mockLiveSelectedTab.value).thenReturn(TabEntity("ID"))
whenever(mockTabRepository.liveSelectedTab).thenReturn(mockLiveSelectedTab)

testee.onMaliciousSiteUserAction(LeaveSite, "http://example.com".toUri(), Feed.PHISHING, false)
testee.onMaliciousSiteUserAction(LeaveSite, "http://example.com".toUri(), "http://example.com".toUri(), Feed.PHISHING, false)

assertCommandIssued<EscapeMaliciousSite>()
assertCommandIssued<LaunchNewTab>()
Expand All @@ -5994,13 +5997,13 @@ class BrowserTabViewModelTest {

@Test
fun whenLearnMoreThenEmitOpenBrokenSiteLearnMore() {
testee.onMaliciousSiteUserAction(LearnMore, "http://example.com".toUri(), Feed.PHISHING, false)
testee.onMaliciousSiteUserAction(LearnMore, "http://example.com".toUri(), "http://example.com".toUri(), Feed.PHISHING, false)
assertCommandIssued<OpenBrokenSiteLearnMore>()
}

@Test
fun whenReportErrorThenEmitOpenBrokenSiteLearnMore() {
testee.onMaliciousSiteUserAction(ReportError, "http://example.com".toUri(), Feed.PHISHING, false)
testee.onMaliciousSiteUserAction(ReportError, "http://example.com".toUri(), "http://example.com".toUri(), Feed.PHISHING, false)
assertCommandIssued<ReportBrokenSiteError>()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class FakeMaliciousSiteBlockerWebViewIntegration : MaliciousSiteBlockerWebViewIn
documentUri: Uri?,
confirmationCallback: (maliciousStatus: MaliciousStatus) -> Unit,
): IsMaliciousViewData {
return Safe
return Safe(request.isForMainFrame)
}

override fun shouldOverrideUrlLoading(
url: Uri,
isForMainFrame: Boolean,
confirmationCallback: (maliciousStatus: MaliciousStatus) -> Unit,
): IsMaliciousViewData {
return Safe
return Safe(isForMainFrame)
}

override fun onPageLoadStarted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.webkit.WebResourceRequest
import androidx.core.net.toUri
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.duckduckgo.app.browser.webview.ExemptedUrlsHolder.ExemptedUrl
import com.duckduckgo.app.browser.webview.RealMaliciousSiteBlockerWebViewIntegration.IsMaliciousViewData.Ignored
import com.duckduckgo.app.browser.webview.RealMaliciousSiteBlockerWebViewIntegration.IsMaliciousViewData.MaliciousSite
import com.duckduckgo.app.browser.webview.RealMaliciousSiteBlockerWebViewIntegration.IsMaliciousViewData.Safe
import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature
Expand Down Expand Up @@ -66,43 +67,45 @@ class RealMaliciousSiteBlockerWebViewIntegrationTest {
updateFeatureEnabled(false)

val result = testee.shouldOverrideUrlLoading(exampleUri, true) {}
assertEquals(Safe, result)
assertEquals(Safe(true), result)
}

@Test
fun `shouldOverrideUrlLoading returns safe when setting is disabled by user`() = runTest {
whenever(mockSettingsDataStore.maliciousSiteProtectionEnabled).thenReturn(false)

val result = testee.shouldOverrideUrlLoading(exampleUri, true) {}
assertEquals(Safe, result)
assertEquals(Safe(true), result)
}

@Test
fun `shouldInterceptRequest returns safe when feature is disabled`() = runTest {
val request = mock(WebResourceRequest::class.java)
whenever(request.url).thenReturn(exampleUri)
whenever(request.isForMainFrame).thenReturn(false)
updateFeatureEnabled(false)

val result = testee.shouldIntercept(request, null) {}
assertEquals(Safe, result)
assertEquals(Safe(false), result)
}

@Test
fun `shouldInterceptRequest returns safe when setting is disabled by user`() = runTest {
val request = mock(WebResourceRequest::class.java)
whenever(request.url).thenReturn(exampleUri)
whenever(mockSettingsDataStore.maliciousSiteProtectionEnabled).thenReturn(false)
whenever(request.isForMainFrame).thenReturn(false)

val result = testee.shouldIntercept(request, null) {}
assertEquals(Safe, result)
assertEquals(Safe(false), result)
}

@Test
fun `shouldOverrideUrlLoading returns safe when url is already processed`() = runTest {
testee.processedUrls.add(exampleUri.toString())

val result = testee.shouldOverrideUrlLoading(exampleUri, true) {}
assertEquals(Safe, result)
assertEquals(Safe(true), result)
}

@Test
Expand All @@ -129,22 +132,22 @@ class RealMaliciousSiteBlockerWebViewIntegrationTest {
}

@Test
fun `shouldInterceptRequest returns safe when feature is enabled, setting is enabled, is malicious, and is not mainframe nor iframe`() = runTest {
fun `shouldInterceptRequest returns ignored when feature is enabled, setting is enabled, is malicious, and is not mainframe nor iframe`() = runTest {
val request = mock(WebResourceRequest::class.java)
whenever(request.url).thenReturn(maliciousUri)
whenever(request.isForMainFrame).thenReturn(false)
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(ConfirmedResult(Malicious(MALWARE)))

val result = testee.shouldIntercept(request, maliciousUri) {}
assertEquals(Safe, result)
assertEquals(Ignored, result)
}

@Test
fun `shouldOverride returns safe when feature is enabled, setting is enabled, is malicious, and is not mainframe`() = runTest {
fun `shouldOverride returns ignored when feature is enabled, setting is enabled, is malicious, and is not mainframe`() = runTest {
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(ConfirmedResult(Malicious(MALWARE)))

val result = testee.shouldOverrideUrlLoading(maliciousUri, false) {}
assertEquals(Safe, result)
assertEquals(Ignored, result)
}

@Test
Expand All @@ -156,22 +159,22 @@ class RealMaliciousSiteBlockerWebViewIntegrationTest {
}

@Test
fun `shouldOverride returns safe when feature is enabled, setting is enabled, is malicious, and not mainframe nor iframe`() = runTest {
fun `shouldOverride returns ignored when feature is enabled, setting is enabled, is malicious, and not mainframe nor iframe`() = runTest {
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(ConfirmedResult(Malicious(MALWARE)))

val result = testee.shouldOverrideUrlLoading(maliciousUri, false) {}
assertEquals(Safe, result)
assertEquals(Ignored, result)
}

@Test
fun `shouldIntercept returns safe when feature and setting enabled, is malicious, and is mainframe but webView has different host`() = runTest {
fun `shouldIntercept returns safe when feature and setting enabled, is malicious, and is not mainframe but webView has different host`() = runTest {
whenever(maliciousSiteProtection.isMalicious(any(), any())).thenReturn(ConfirmedResult(Malicious(MALWARE)))
val request = mock(WebResourceRequest::class.java)
whenever(request.url).thenReturn(maliciousUri)
whenever(request.isForMainFrame).thenReturn(false)

val result = testee.shouldIntercept(request, maliciousUri) {}
assertEquals(Safe, result)
assertEquals(Ignored, result)
}

@Test
Expand Down

0 comments on commit d4ce231

Please sign in to comment.