forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#16133: Simplify OpenInAppOnboardingObserver condit…
…ion (mozilla-mobile#16851)
- Loading branch information
1 parent
d4ef63f
commit 3c3ddb4
Showing
2 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
app/src/test/java/org/mozilla/fenix/browser/OpenInAppOnboardingObserverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.browser | ||
|
||
import android.content.Context | ||
import io.mockk.MockKAnnotations | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import mozilla.components.browser.session.Session | ||
import mozilla.components.feature.app.links.AppLinkRedirect | ||
import mozilla.components.feature.app.links.AppLinksUseCases | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
@ExperimentalCoroutinesApi | ||
@RunWith(FenixRobolectricTestRunner::class) | ||
class OpenInAppOnboardingObserverTest { | ||
|
||
@MockK(relaxed = true) private lateinit var context: Context | ||
@MockK(relaxed = true) private lateinit var settings: Settings | ||
@MockK(relaxed = true) private lateinit var session: Session | ||
@MockK(relaxed = true) private lateinit var appLinksUseCases: AppLinksUseCases | ||
@MockK(relaxed = true) private lateinit var applinksRedirect: AppLinkRedirect | ||
@MockK(relaxed = true) private lateinit var getAppLinkRedirect: AppLinksUseCases.GetAppLinkRedirect | ||
@MockK(relaxed = true) private lateinit var infoBanner: InfoBanner | ||
|
||
@Before | ||
fun setup() { | ||
MockKAnnotations.init(this) | ||
} | ||
|
||
@Test | ||
fun `do not show banner when openLinksInExternalApp is set to true`() { | ||
every { settings.openLinksInExternalApp } returns true | ||
every { settings.shouldShowOpenInAppCfr } returns true | ||
|
||
val observer = OpenInAppOnboardingObserver(context, mockk(), settings, appLinksUseCases, mockk()) | ||
observer.onLoadingStateChanged(session, false) | ||
|
||
verify(exactly = 0) { appLinksUseCases.appLinkRedirect } | ||
|
||
every { settings.openLinksInExternalApp } returns false | ||
observer.onLoadingStateChanged(session, false) | ||
|
||
verify(exactly = 1) { appLinksUseCases.appLinkRedirect } | ||
} | ||
|
||
@Test | ||
fun `do not show banner when shouldShowOpenInAppCfr is set to false`() { | ||
every { settings.openLinksInExternalApp } returns false | ||
every { settings.shouldShowOpenInAppCfr } returns false | ||
|
||
val observer = OpenInAppOnboardingObserver(context, mockk(), settings, appLinksUseCases, mockk()) | ||
observer.onLoadingStateChanged(session, false) | ||
|
||
verify(exactly = 0) { appLinksUseCases.appLinkRedirect } | ||
|
||
every { settings.shouldShowOpenInAppCfr } returns true | ||
observer.onLoadingStateChanged(session, false) | ||
|
||
verify(exactly = 1) { appLinksUseCases.appLinkRedirect } | ||
} | ||
|
||
@Test | ||
fun `do not show banner when URL is loading`() { | ||
every { settings.openLinksInExternalApp } returns false | ||
every { settings.shouldShowOpenInAppCfr } returns true | ||
|
||
val observer = OpenInAppOnboardingObserver(context, mockk(), settings, appLinksUseCases, mockk()) | ||
|
||
observer.onLoadingStateChanged(session, true) | ||
|
||
verify(exactly = 0) { appLinksUseCases.appLinkRedirect } | ||
|
||
observer.onLoadingStateChanged(session, false) | ||
|
||
verify(exactly = 1) { appLinksUseCases.appLinkRedirect } | ||
} | ||
|
||
@Test | ||
fun `do not show banner when external app is not found`() { | ||
every { settings.openLinksInExternalApp } returns false | ||
every { settings.shouldShowOpenInAppCfr } returns true | ||
every { appLinksUseCases.appLinkRedirect } returns getAppLinkRedirect | ||
every { getAppLinkRedirect.invoke(any()) } returns applinksRedirect | ||
|
||
val observer = OpenInAppOnboardingObserver(context, mockk(), settings, appLinksUseCases, mockk()) | ||
observer.onLoadingStateChanged(session, false) | ||
|
||
verify(exactly = 0) { settings.shouldShowOpenInAppBanner } | ||
} | ||
|
||
@Test | ||
fun `do not dismiss banner when URL is the same`() { | ||
val observer = OpenInAppOnboardingObserver(context, mockk(), settings, appLinksUseCases, mockk()) | ||
observer.infoBanner = infoBanner | ||
observer.sessionDomainForDisplayedBanner = "mozilla.com" | ||
observer.onUrlChanged(session, "https://mozilla.com") | ||
|
||
verify(exactly = 0) { infoBanner.dismiss() } | ||
|
||
observer.onUrlChanged(session, "https://abc.com") | ||
verify(exactly = 1) { infoBanner.dismiss() } | ||
} | ||
} |