Skip to content

Commit

Permalink
Closes mozilla-mobile#26971: fix wallpaper onboarding display condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mike a committed Sep 15, 2022
1 parent d468bb5 commit 7f87c62
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,18 @@ class DefaultSessionControlController(
}

override fun handleShowWallpapersOnboardingDialog(state: WallpaperState): Boolean {
if (state.availableWallpapers.all { it.thumbnailFileState == Wallpaper.ImageFileState.Downloaded } &&
state.availableWallpapers.size >= WallpaperOnboardingDialogFragment.THUMBNAILS_COUNT
) {
navController.nav(
R.id.homeFragment,
HomeFragmentDirections.actionGlobalWallpaperOnboardingDialog(),
)
return true
return state.availableWallpapers.filter { wallpaper ->
wallpaper.thumbnailFileState == Wallpaper.ImageFileState.Downloaded
}.size.let { downloadedCount ->
downloadedCount >= WallpaperOnboardingDialogFragment.THUMBNAILS_COUNT
}.also { isThumbnailsDownloaded ->
if (isThumbnailsDownloaded) {
navController.nav(
R.id.homeFragment,
HomeFragmentDirections.actionGlobalWallpaperOnboardingDialog(),
)
}
}
return false
}

override fun handleReadPrivacyNoticeClicked() {
Expand Down
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 7f87c62

Please sign in to comment.