Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

For #26424 - Handle special legacy wallpaper cases (backport #27137) #27203

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -28,18 +34,39 @@ 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 =
File(storageRootDirectory, "wallpapers/landscape/light/$wallpaperName.png")
// 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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WallpapersUseCases(
val migrationHelper = LegacyWallpaperMigration(
storageRootDirectory = storageRootDirectory,
settings = context.settings(),
selectWallpaper::invoke,
)
DefaultInitializeWallpaperUseCase(
store = store,
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
)
}

Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down