This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fenix] For mozilla-mobile/fenix#26424 - Create wallpaper file migrat…
…ion helper
- Loading branch information
1 parent
481fbb8
commit 7d26178
Showing
6 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
fenix/app/src/main/java/org/mozilla/fenix/wallpapers/LegacyWallpaperMigration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
fenix/app/src/test/java/org/mozilla/fenix/wallpapers/LegacyWallpaperMigrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters