diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt index fbbebe4b953e..498430bd32cd 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlController.kt @@ -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() { diff --git a/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt b/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt index e5ae5cee8ac9..907a7e25883a 100644 --- a/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/home/DefaultSessionControlControllerTest.kt @@ -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 @@ -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 @@ -1255,4 +1313,35 @@ class DefaultSessionControlControllerTest { showTabTray = showTabTray, ) } + + private fun makeFakeRemoteWallpapers(size: Int, hasError: Boolean): List { + val list = mutableListOf() + 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, + ) }