Skip to content

Commit

Permalink
Closes mozilla-mobile#26971: add SessionControlController tests for h…
Browse files Browse the repository at this point in the history
…andleShowWallpapersOnboardingDialog method
  • Loading branch information
mike a committed Sep 15, 2022
1 parent f3fe48a commit e5e7c5b
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.recentbookmarks.RecentBookmark
import org.mozilla.fenix.home.recenttabs.RecentTab
import org.mozilla.fenix.home.sessioncontrol.DefaultSessionControlController
import org.mozilla.fenix.onboarding.WallpaperOnboardingDialogFragment
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.wallpapers.Wallpaper
import org.mozilla.fenix.wallpapers.WallpaperState
import mozilla.components.feature.tab.collections.Tab as ComponentTab

@RunWith(FenixRobolectricTestRunner::class) // For gleanTestRule
Expand Down Expand Up @@ -878,6 +881,61 @@ class DefaultSessionControlControllerTest {
assertNull(TopSites.remove.testGetValue()!!.single().extra)
}

@Test
fun `GIVEN exactly the required amount of downloaded thumbnails WHEN has no errors THEN handleShowWallpapersOnboardingDialog calls the dialog`() {
val wallpaperState = WallpaperState.default.copy(
availableWallpapers = makeFakeRemoteWallpapers(
WallpaperOnboardingDialogFragment.THUMBNAILS_COUNT,
false,
),
)
assert(createController().handleShowWallpapersOnboardingDialog(wallpaperState))
}

@Test
fun `GIVEN more than required amount of downloaded thumbnails WHEN has no errors THEN handleShowWallpapersOnboardingDialog calls the dialog`() {
val wallpaperState = WallpaperState.default.copy(
availableWallpapers = makeFakeRemoteWallpapers(
WallpaperOnboardingDialogFragment.THUMBNAILS_COUNT,
false,
),
)
assert(createController().handleShowWallpapersOnboardingDialog(wallpaperState))
}

@Test
fun `GIVEN more than required amount of downloaded thumbnails WHEN also has some errors THEN the dialog is called`() {
val wallpaperState = WallpaperState.default.copy(
availableWallpapers = makeFakeRemoteWallpapers(
WallpaperOnboardingDialogFragment.THUMBNAILS_COUNT + 2,
true,
),
)
assert(createController().handleShowWallpapersOnboardingDialog(wallpaperState))
}

@Test
fun `GIVEN less than required amount of downloaded thumbnails THEN the dialog is not called`() {
val wallpaperState = WallpaperState.default.copy(
availableWallpapers = makeFakeRemoteWallpapers(
WallpaperOnboardingDialogFragment.THUMBNAILS_COUNT - 1,
false,
),
)
assert(!createController().handleShowWallpapersOnboardingDialog(wallpaperState))
}

@Test
fun `GIVEN exactly the required amount of downloaded thumbnails WHEN has errors THEN the dialog is not called`() {
val wallpaperState = WallpaperState.default.copy(
availableWallpapers = makeFakeRemoteWallpapers(
WallpaperOnboardingDialogFragment.THUMBNAILS_COUNT,
true,
),
)
assert(!createController().handleShowWallpapersOnboardingDialog(wallpaperState))
}

@Test
fun handleStartBrowsingClicked() {
var hideOnboardingInvoked = false
Expand Down Expand Up @@ -1255,4 +1313,35 @@ class DefaultSessionControlControllerTest {
showTabTray = showTabTray,
)
}

private fun makeFakeRemoteWallpapers(size: Int, hasError: Boolean): List<Wallpaper> {
val list = mutableListOf<Wallpaper>()
for (i in 0 until size) {
if (hasError && i == 0) {
list.add(makeFakeRemoteWallpaper(Wallpaper.ImageFileState.Error))
} else {
list.add(makeFakeRemoteWallpaper(Wallpaper.ImageFileState.Downloaded))
}
}
return list
}

private fun makeFakeRemoteWallpaper(
thumbnailFileState: Wallpaper.ImageFileState = Wallpaper.ImageFileState.Unavailable,
) = Wallpaper(
name = "name",
collection = Wallpaper.Collection(
name = Wallpaper.firefoxCollectionName,
heading = null,
description = null,
availableLocales = null,
startDate = null,
endDate = null,
learnMoreUrl = null,
),
textColor = null,
cardColor = null,
thumbnailFileState = thumbnailFileState,
assetsFileState = Wallpaper.ImageFileState.Unavailable,
)
}

0 comments on commit e5e7c5b

Please sign in to comment.