Skip to content

Commit

Permalink
For mozilla-mobile#8800 - add tests for dismissing highlighted items
Browse files Browse the repository at this point in the history
  • Loading branch information
BranescuMihai committed Mar 5, 2020
1 parent 1b85850 commit f7818ec
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ class BrowserInteractorTest {

verify { browserToolbarController.handleToolbarItemInteraction(item) }
}

@Test
fun onBrowserMenuDismissed() {
val itemList: List<ToolbarMenu.Item> = listOf()

interactor.onBrowserMenuDismissed(itemList)

verify { browserToolbarController.handleBrowserMenuDismissed(itemList) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ import mozilla.components.feature.session.SessionUseCases
import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.feature.tabs.TabsUseCases
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.TestApplication
import org.mozilla.fenix.browser.BrowserAnimator
import org.mozilla.fenix.browser.BrowserFragment
import org.mozilla.fenix.browser.BrowserFragmentDirections
Expand All @@ -52,12 +55,17 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.toTab
import org.mozilla.fenix.home.Tab
import org.mozilla.fenix.settings.deletebrowsingdata.deleteAndQuit
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@ExperimentalCoroutinesApi
@UseExperimental(ObsoleteCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
@Config(application = TestApplication::class)
class DefaultBrowserToolbarControllerTest {

private val mainThreadSurrogate = newSingleThreadContext("UI thread")
Expand Down Expand Up @@ -127,6 +135,12 @@ class DefaultBrowserToolbarControllerTest {
every { browserAnimator.captureEngineViewAndDrawStatically(capture(onComplete)) } answers { onComplete.captured.invoke() }
}

@After
fun tearDown() {
Dispatchers.resetMain() // reset main dispatcher to the original Main dispatcher
mainThreadSurrogate.close()
}

@Test
fun handleBrowserToolbarPaste() {
every { currentSession.id } returns "1"
Expand Down Expand Up @@ -165,10 +179,41 @@ class DefaultBrowserToolbarControllerTest {
}
}

@After
fun tearDown() {
Dispatchers.resetMain() // reset main dispatcher to the original Main dispatcher
mainThreadSurrogate.close()
@Test
fun `handle BrowserMenu dismissed with all options available`() = runBlockingTest {
val itemList: List<ToolbarMenu.Item> = listOf(
ToolbarMenu.Item.AddToHomeScreen,
ToolbarMenu.Item.ReaderMode(true),
ToolbarMenu.Item.OpenInApp
)

val activity = HomeActivity()

controller = DefaultBrowserToolbarController(
activity = activity,
navController = navController,
browsingModeManager = browsingModeManager,
findInPageLauncher = findInPageLauncher,
engineView = engineView,
browserAnimator = browserAnimator,
customTabSession = null,
getSupportUrl = getSupportUrl,
openInFenixIntent = openInFenixIntent,
scope = this,
swipeRefresh = swipeRefreshLayout,
tabCollectionStorage = tabCollectionStorage,
topSiteStorage = topSiteStorage,
bookmarkTapped = mockk(),
readerModeController = mockk(),
sessionManager = mockk(),
store = mockk()
)

controller.handleBrowserMenuDismissed(itemList)

assertEquals(true, activity.settings().installPwaOpened)
assertEquals(true, activity.settings().readerModeOpened)
assertEquals(true, activity.settings().openInAppOpened)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.mozilla.fenix.components.toolbar

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.spyk
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.storage.BookmarksStorage
import mozilla.components.feature.app.links.AppLinkRedirect
import mozilla.components.feature.app.links.AppLinksUseCases
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.utils.Settings

class DefaultToolbarMenuTest {

private lateinit var defaultToolbarMenu: DefaultToolbarMenu

private val context: Context = mockk(relaxed = true)
private val sessionManager: SessionManager = mockk(relaxed = true)
private val onItemTapped: (ToolbarMenu.Item) -> Unit = {}
private val lifecycleOwner: LifecycleOwner = mockk(relaxed = true)
private val bookmarksStorage: BookmarksStorage = mockk(relaxed = true)

@Before
fun setUp() {
defaultToolbarMenu = spyk(
DefaultToolbarMenu(
context,
sessionManager,
hasAccountProblem = false,
shouldReverseItems = false,
onItemTapped = onItemTapped,
lifecycleOwner = lifecycleOwner,
bookmarksStorage = bookmarksStorage
)
)

val settings = Settings.getInstance(context, true)
mockkStatic("org.mozilla.fenix.ext.ContextKt")
every { context.settings() } returns settings
}

@Test
fun `get all low prio highlight items`() {
every { context.components.useCases.webAppUseCases.isPinningSupported() } returns true
every { context.components.useCases.webAppUseCases.isInstallable() } returns true

val getAppLinkRedirect: AppLinksUseCases.GetAppLinkRedirect = mockk(relaxed = true)
every { context.components.useCases.appLinksUseCases.appLinkRedirect } returns getAppLinkRedirect
val appLinkRedirect: AppLinkRedirect = mockk(relaxed = true)
every { appLinkRedirect.hasExternalApp() } returns true
every { getAppLinkRedirect(any()) } returns appLinkRedirect

val session: Session = mockk(relaxed = true)
every { session.readerable } returns true
every { sessionManager.selectedSession } returns session

val list = defaultToolbarMenu.getLowPrioHighlightItems()

assertEquals(ToolbarMenu.Item.AddToHomeScreen, list[0])
assertEquals(ToolbarMenu.Item.ReaderMode(false), list[1])
assertEquals(ToolbarMenu.Item.OpenInApp, list[2])
}

@Test
fun `get all low prio highlight items without AddToHomeScreen`() {
val settings = Settings.getInstance(context, true)
mockkStatic("org.mozilla.fenix.ext.ContextKt")
every { context.settings() } returns settings

every { context.components.useCases.webAppUseCases.isPinningSupported() } returns false

val getAppLinkRedirect: AppLinksUseCases.GetAppLinkRedirect = mockk(relaxed = true)
every { context.components.useCases.appLinksUseCases.appLinkRedirect } returns getAppLinkRedirect
val appLinkRedirect: AppLinkRedirect = mockk(relaxed = true)
every { appLinkRedirect.hasExternalApp() } returns true
every { getAppLinkRedirect(any()) } returns appLinkRedirect

val session: Session = mockk(relaxed = true)
every { session.readerable } returns true
every { sessionManager.selectedSession } returns session

val list = defaultToolbarMenu.getLowPrioHighlightItems()

assertEquals(ToolbarMenu.Item.ReaderMode(false), list[0])
assertEquals(ToolbarMenu.Item.OpenInApp, list[1])
}

@Test
fun `get all low prio highlight items without ReaderMode`() {
every { context.components.useCases.webAppUseCases.isPinningSupported() } returns true
every { context.components.useCases.webAppUseCases.isInstallable() } returns true

val getAppLinkRedirect: AppLinksUseCases.GetAppLinkRedirect = mockk(relaxed = true)
every { context.components.useCases.appLinksUseCases.appLinkRedirect } returns getAppLinkRedirect
every { getAppLinkRedirect(any()).hasExternalApp() } returns true

val list = defaultToolbarMenu.getLowPrioHighlightItems()

assertEquals(ToolbarMenu.Item.AddToHomeScreen, list[0])
assertEquals(ToolbarMenu.Item.OpenInApp, list[1])
}

@Test
fun `get all low prio highlight items without OpenInApp`() {
every { context.components.useCases.webAppUseCases.isPinningSupported() } returns true
every { context.components.useCases.webAppUseCases.isInstallable() } returns true

val session: Session = mockk(relaxed = true)
every { session.readerable } returns true
every { sessionManager.selectedSession } returns session

val getAppLinkRedirect: AppLinksUseCases.GetAppLinkRedirect = mockk(relaxed = true)
every { context.components.useCases.appLinksUseCases.appLinkRedirect } returns getAppLinkRedirect
every { getAppLinkRedirect(any()).hasExternalApp() } returns false

val list = defaultToolbarMenu.getLowPrioHighlightItems()

assertEquals(ToolbarMenu.Item.AddToHomeScreen, list[0])
assertEquals(ToolbarMenu.Item.ReaderMode(false), list[1])
}
}
39 changes: 39 additions & 0 deletions app/src/test/java/org/mozilla/fenix/utils/SettingsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,45 @@ class SettingsTest {
assertFalse(settings.shouldUseTrackingProtection)
}

@Test
fun shouldSetReaderModeOpened() {
// When
// Then
assertFalse(settings.readerModeOpened)

// When
settings.readerModeOpened = true

// Then
assertTrue(settings.readerModeOpened)
}

@Test
fun shouldSetOpenInAppOpened() {
// When
// Then
assertFalse(settings.openInAppOpened)

// When
settings.openInAppOpened = true

// Then
assertTrue(settings.openInAppOpened)
}

@Test
fun shouldSetInstallPwaOpened() {
// When
// Then
assertFalse(settings.installPwaOpened)

// When
settings.installPwaOpened = true

// Then
assertTrue(settings.installPwaOpened)
}

@Test
fun shouldUseTrackingProtectionStrict() {
// When
Expand Down

0 comments on commit f7818ec

Please sign in to comment.