For #26424 - Handle special legacy wallpaper cases

This commit is contained in:
Alexandru2909 2022-09-23 17:21:40 +03:00 committed by mergify[bot]
parent f0c4660851
commit 19685170de
4 changed files with 96 additions and 9 deletions

View File

@ -8,6 +8,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.amethystName
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.beachVibeName
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.ceruleanName
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.sunriseName
import java.io.File
import java.io.IOException
@ -16,10 +20,12 @@ import java.io.IOException
*
* @property storageRootDirectory The top level app-local storage directory.
* @property settings Used to update the color of the text shown above wallpapers.
* @property downloadWallpaper Function used to download assets for legacy drawable wallpapers.
*/
class LegacyWallpaperMigration(
private val storageRootDirectory: File,
private val settings: Settings,
private val downloadWallpaper: suspend (Wallpaper) -> Wallpaper.ImageFileState,
) {
/**
* Migrate the legacy wallpaper to the new path and delete the remaining legacy files.
@ -28,7 +34,22 @@ class LegacyWallpaperMigration(
*/
suspend fun migrateLegacyWallpaper(
wallpaperName: String,
) = withContext(Dispatchers.IO) {
): String = withContext(Dispatchers.IO) {
// For the legacy wallpapers previously stored as drawables,
// attempt to download them at startup.
when (wallpaperName) {
ceruleanName, sunriseName, amethystName -> {
downloadWallpaper(
Wallpaper.Default.copy(
name = wallpaperName,
collection = Wallpaper.ClassicFirefoxCollection,
thumbnailFileState = Wallpaper.ImageFileState.Unavailable,
assetsFileState = Wallpaper.ImageFileState.Unavailable,
),
)
return@withContext wallpaperName
}
}
val legacyPortraitFile =
File(storageRootDirectory, "wallpapers/portrait/light/$wallpaperName.png")
val legacyLandscapeFile =
@ -36,10 +57,16 @@ class LegacyWallpaperMigration(
// If any of portrait or landscape files of the wallpaper are missing, then we shouldn't
// migrate it
if (!legacyLandscapeFile.exists() || !legacyPortraitFile.exists()) {
return@withContext
return@withContext wallpaperName
}
// The V2 name for the "beach-vibe" wallpaper is "beach-vibes".
val migratedWallpaperName = if (wallpaperName == beachVibeName) {
"beach-vibes"
} else {
wallpaperName
}
// Directory where the legacy wallpaper files should be migrated
val targetDirectory = "wallpapers/${wallpaperName.lowercase()}"
val targetDirectory = "wallpapers/" + migratedWallpaperName.lowercase()
try {
// Use the portrait file as thumbnail
@ -75,6 +102,8 @@ class LegacyWallpaperMigration(
// Delete the remaining legacy files
File(storageRootDirectory, "wallpapers/portrait").deleteRecursively()
File(storageRootDirectory, "wallpapers/landscape").deleteRecursively()
return@withContext migratedWallpaperName
}
companion object {

View File

@ -50,6 +50,7 @@ class WallpapersUseCases(
val migrationHelper = LegacyWallpaperMigration(
storageRootDirectory = storageRootDirectory,
settings = context.settings(),
selectWallpaper::invoke,
)
DefaultInitializeWallpaperUseCase(
store = store,
@ -242,10 +243,14 @@ class WallpapersUseCases(
Wallpaper.getCurrentWallpaperFromSettings(settings)?.let {
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(it))
}
val currentWallpaperName = withContext(Dispatchers.IO) { settings.currentWallpaperName }
if (settings.shouldMigrateLegacyWallpaper) {
migrationHelper.migrateLegacyWallpaper(currentWallpaperName)
val currentWallpaperName = if (settings.shouldMigrateLegacyWallpaper) {
val migratedWallpaperName =
migrationHelper.migrateLegacyWallpaper(settings.currentWallpaperName)
settings.currentWallpaperName = migratedWallpaperName
settings.shouldMigrateLegacyWallpaper = false
migratedWallpaperName
} else {
settings.currentWallpaperName
}
val possibleWallpapers = metadataFetcher.downloadWallpaperList().filter {
!it.isExpired() && it.isAvailableInLocale()

View File

@ -3,6 +3,7 @@ package org.mozilla.fenix.wallpapers
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
@ -21,6 +22,7 @@ class LegacyWallpaperMigrationTest {
val tempFolder = TemporaryFolder()
private lateinit var settings: Settings
private lateinit var wallpapersFolder: File
private lateinit var downloadWallpaper: (Wallpaper) -> Wallpaper.ImageFileState
private lateinit var migrationHelper: LegacyWallpaperMigration
private lateinit var portraitLightFolder: File
private lateinit var portraitDarkFolder: File
@ -31,9 +33,11 @@ class LegacyWallpaperMigrationTest {
fun setup() {
wallpapersFolder = File(tempFolder.root, "wallpapers")
settings = mockk(relaxed = true)
downloadWallpaper = mockk(relaxed = true)
migrationHelper = LegacyWallpaperMigration(
storageRootDirectory = tempFolder.root,
settings = settings,
downloadWallpaper,
)
}
@ -146,6 +150,57 @@ class LegacyWallpaperMigrationTest {
}
}
@Test
fun `WHEN the beach-vibe legacy wallpaper is migrated THEN the legacy wallpapers destination is beach-vibes`() = runTest {
val wallpaperName = Wallpaper.beachVibeName
createAllLegacyFiles(wallpaperName)
val migratedWallpaperName = migrationHelper.migrateLegacyWallpaper(wallpaperName)
assertEquals("beach-vibes", migratedWallpaperName)
assertTrue(getAllFiles("beach-vibes").all { it.exists() })
}
@Test
fun `WHEN a drawable legacy wallpaper is migrated THEN the respective V2 wallpaper is downloaded`() = runTest {
var migratedWallpaperName = migrationHelper.migrateLegacyWallpaper(Wallpaper.ceruleanName)
assertEquals(Wallpaper.ceruleanName, migratedWallpaperName)
verify {
downloadWallpaper(
withArg {
assertEquals(Wallpaper.ceruleanName, it.name)
assertEquals(Wallpaper.ClassicFirefoxCollection, it.collection)
},
)
}
migratedWallpaperName = migrationHelper.migrateLegacyWallpaper(Wallpaper.sunriseName)
assertEquals(Wallpaper.sunriseName, migratedWallpaperName)
verify {
downloadWallpaper(
withArg {
assertEquals(Wallpaper.sunriseName, it.name)
assertEquals(Wallpaper.ClassicFirefoxCollection, it.collection)
},
)
}
migratedWallpaperName = migrationHelper.migrateLegacyWallpaper(Wallpaper.amethystName)
assertEquals(Wallpaper.amethystName, migratedWallpaperName)
verify {
downloadWallpaper(
withArg {
assertEquals(Wallpaper.amethystName, it.name)
assertEquals(Wallpaper.ClassicFirefoxCollection, it.collection)
},
)
}
}
private fun createAllLegacyFiles(name: String) {
if (!this::portraitLightFolder.isInitialized || !portraitLightFolder.exists()) {
portraitLightFolder = tempFolder.newFolder("wallpapers", "portrait", "light")

View File

@ -50,9 +50,7 @@ class WallpapersUseCasesTest {
private val mockLegacyFileManager = mockk<LegacyWallpaperFileManager> {
every { clean(any(), any()) } just runs
}
private val mockMigrationHelper = mockk<LegacyWallpaperMigration> {
coEvery { migrateLegacyWallpaper(any()) } just runs
}
private val mockMigrationHelper = mockk<LegacyWallpaperMigration>(relaxed = true)
private val mockMetadataFetcher = mockk<WallpaperMetadataFetcher>()
private val mockDownloader = mockk<WallpaperDownloader> {