[fenix] For https://github.com/mozilla-mobile/fenix/issues/26424 - Create wallpaper file migration helper
parent
bb43506a47
commit
bfddfa7177
@ -0,0 +1,71 @@
|
|||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
package org.mozilla.fenix.wallpapers
|
||||||
|
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import mozilla.components.support.base.log.logger.Logger
|
||||||
|
import java.io.File
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages the migration of legacy wallpapers to the new paths
|
||||||
|
*
|
||||||
|
* @property storageRootDirectory The top level app-local storage directory.
|
||||||
|
*/
|
||||||
|
class LegacyWallpaperMigration(
|
||||||
|
private val storageRootDirectory: File,
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* Migrate the legacy wallpaper to the new path and delete the remaining legacy files.
|
||||||
|
*
|
||||||
|
* @param wallpaperName Name of the wallpaper to be migrated.
|
||||||
|
*/
|
||||||
|
suspend fun migrateLegacyWallpaper(
|
||||||
|
wallpaperName: String,
|
||||||
|
) = withContext(Dispatchers.IO) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
// Directory where the legacy wallpaper files should be migrated
|
||||||
|
val targetDirectory = "wallpapers/${wallpaperName.lowercase()}"
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Use the portrait file as thumbnail
|
||||||
|
legacyPortraitFile.copyTo(
|
||||||
|
File(
|
||||||
|
storageRootDirectory,
|
||||||
|
"$targetDirectory/thumbnail.png",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
// Copy the portrait file
|
||||||
|
legacyPortraitFile.copyTo(
|
||||||
|
File(
|
||||||
|
storageRootDirectory,
|
||||||
|
"$targetDirectory/portrait.png",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
// Copy the landscape file
|
||||||
|
legacyLandscapeFile.copyTo(
|
||||||
|
File(
|
||||||
|
storageRootDirectory,
|
||||||
|
"$targetDirectory/landscape.png",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Logger.error("Failed to migrate legacy wallpaper", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the remaining legacy files
|
||||||
|
File(storageRootDirectory, "wallpapers/portrait").deleteRecursively()
|
||||||
|
File(storageRootDirectory, "wallpapers/landscape").deleteRecursively()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,117 @@
|
|||||||
|
package org.mozilla.fenix.wallpapers
|
||||||
|
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.rules.TemporaryFolder
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class LegacyWallpaperMigrationTest {
|
||||||
|
@Rule
|
||||||
|
@JvmField
|
||||||
|
val tempFolder = TemporaryFolder()
|
||||||
|
private lateinit var wallpapersFolder: File
|
||||||
|
private lateinit var migrationHelper: LegacyWallpaperMigration
|
||||||
|
private lateinit var portraitLightFolder: File
|
||||||
|
private lateinit var portraitDarkFolder: File
|
||||||
|
private lateinit var landscapeLightFolder: File
|
||||||
|
private lateinit var landscapeDarkFolder: File
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
wallpapersFolder = File(tempFolder.root, "wallpapers")
|
||||||
|
migrationHelper = LegacyWallpaperMigration(
|
||||||
|
storageRootDirectory = tempFolder.root,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN the legacy wallpaper is migrated THEN the legacy wallpapers are deleted`() = runTest {
|
||||||
|
val wallpaperName = "wallpaper1"
|
||||||
|
|
||||||
|
createAllLegacyFiles(wallpaperName)
|
||||||
|
|
||||||
|
migrationHelper.migrateLegacyWallpaper(wallpaperName)
|
||||||
|
|
||||||
|
assertTrue(getAllFiles(wallpaperName).all { it.exists() })
|
||||||
|
assertFalse(File(portraitLightFolder, "$wallpaperName.png").exists())
|
||||||
|
assertFalse(File(portraitDarkFolder, "$wallpaperName.png").exists())
|
||||||
|
assertFalse(File(landscapeLightFolder, "$wallpaperName.png").exists())
|
||||||
|
assertFalse(File(landscapeDarkFolder, "$wallpaperName.png").exists())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN landscape legacy wallpaper is missing WHEN the wallpapers are migrated THEN the wallpaper is not migrated`() =
|
||||||
|
runTest {
|
||||||
|
val portraitOnlyWallpaperName = "portraitOnly"
|
||||||
|
val completeWallpaperName = "legacy"
|
||||||
|
createAllLegacyFiles(completeWallpaperName)
|
||||||
|
File(landscapeLightFolder, "$portraitOnlyWallpaperName.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
File(landscapeDarkFolder, "$portraitOnlyWallpaperName.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
migrationHelper.migrateLegacyWallpaper(portraitOnlyWallpaperName)
|
||||||
|
migrationHelper.migrateLegacyWallpaper(completeWallpaperName)
|
||||||
|
|
||||||
|
assertTrue(getAllFiles(completeWallpaperName).all { it.exists() })
|
||||||
|
assertFalse(getAllFiles(portraitOnlyWallpaperName).any { it.exists() })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN portrait legacy wallpaper is missing WHEN the wallpapers are migrated THEN the wallpaper is not migrated`() =
|
||||||
|
runTest {
|
||||||
|
val landscapeOnlyWallpaperName = "portraitOnly"
|
||||||
|
val completeWallpaperName = "legacy"
|
||||||
|
createAllLegacyFiles(completeWallpaperName)
|
||||||
|
File(portraitLightFolder, "$landscapeOnlyWallpaperName.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
File(portraitDarkFolder, "$landscapeOnlyWallpaperName.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
migrationHelper.migrateLegacyWallpaper(landscapeOnlyWallpaperName)
|
||||||
|
migrationHelper.migrateLegacyWallpaper(completeWallpaperName)
|
||||||
|
|
||||||
|
assertTrue(getAllFiles(completeWallpaperName).all { it.exists() })
|
||||||
|
assertFalse(getAllFiles(landscapeOnlyWallpaperName).any { it.exists() })
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createAllLegacyFiles(name: String) {
|
||||||
|
if (!this::portraitLightFolder.isInitialized) {
|
||||||
|
portraitLightFolder = tempFolder.newFolder("wallpapers", "portrait", "light")
|
||||||
|
portraitDarkFolder = tempFolder.newFolder("wallpapers", "portrait", "dark")
|
||||||
|
landscapeLightFolder = tempFolder.newFolder("wallpapers", "landscape", "light")
|
||||||
|
landscapeDarkFolder = tempFolder.newFolder("wallpapers", "landscape", "dark")
|
||||||
|
}
|
||||||
|
|
||||||
|
File(portraitLightFolder, "$name.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
File(landscapeLightFolder, "$name.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
File(portraitDarkFolder, "$name.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
File(landscapeDarkFolder, "$name.png").apply {
|
||||||
|
createNewFile()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getAllFiles(name: String): List<File> {
|
||||||
|
val folder = File(wallpapersFolder, name)
|
||||||
|
return listOf(
|
||||||
|
folder,
|
||||||
|
File(folder, "portrait.png"),
|
||||||
|
File(folder, "landscape.png"),
|
||||||
|
File(folder, "thumbnail.png"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue