For #27331 - Add migration for Turning Red wallpaper card colors

pull/543/head
Noah Bond 2 years ago committed by mergify[bot]
parent a44d9ff8d9
commit 79f0ce4cd3

@ -225,6 +225,14 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = true,
)
/**
* Indicates if the current legacy wallpaper card colors should be migrated.
*/
var shouldMigrateLegacyWallpaperCardColors by booleanPreference(
key = appContext.getPreferenceKey(R.string.pref_key_should_migrate_wallpaper_card_colors),
default = true,
)
/**
* Indicates if the wallpaper onboarding dialog should be shown.
*/

@ -97,6 +97,7 @@ class LegacyWallpaperMigration(
}
} catch (e: IOException) {
Logger.error("Failed to migrate legacy wallpaper", e)
settings.shouldMigrateLegacyWallpaperCardColors = false
}
// Delete the remaining legacy files
@ -106,9 +107,41 @@ class LegacyWallpaperMigration(
return@withContext migratedWallpaperName
}
/**
* Helper function used to migrate a legacy wallpaper's card colors that previously did not exist.
*/
fun migrateExpiredWallpaperCardColors() {
// The card colors should NOT be migrated if the file migration was ran prior to these
// changes and it failed. We can verify this by checking [settings.currentWallpaperTextColor],
// since this is only initialized for legacy wallpapers in
// [LegacyWallpaperMigration.migrateLegacyWallpaper].
if (settings.currentWallpaperTextColor != 0L) {
when (settings.currentWallpaperName) {
TURNING_RED_MEI_WALLPAPER_NAME -> {
settings.currentWallpaperCardColorLight =
TURNING_RED_MEI_WALLPAPER_CARD_COLOR_LIGHT.toLong(radix = 16)
settings.currentWallpaperCardColorDark =
TURNING_RED_MEI_WALLPAPER_CARD_COLOR_DARK.toLong(radix = 16)
}
TURNING_RED_PANDA_WALLPAPER_NAME -> {
settings.currentWallpaperCardColorLight =
TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_LIGHT.toLong(radix = 16)
settings.currentWallpaperCardColorDark =
TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_DARK.toLong(radix = 16)
}
}
}
settings.shouldMigrateLegacyWallpaperCardColors = false
}
companion object {
const val TURNING_RED_MEI_WALLPAPER_NAME = "mei"
const val TURNING_RED_PANDA_WALLPAPER_NAME = "panda"
const val TURNING_RED_WALLPAPER_TEXT_COLOR = "FFFBFBFE"
const val TURNING_RED_MEI_WALLPAPER_CARD_COLOR_LIGHT = "FFFDE9C3"
const val TURNING_RED_MEI_WALLPAPER_CARD_COLOR_DARK = "FF532906"
const val TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_LIGHT = "FFFFEDF1"
const val TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_DARK = "FF611B28"
}
}

@ -249,6 +249,7 @@ class WallpapersUseCases(
Wallpaper.getCurrentWallpaperFromSettings(settings)?.let {
appStore.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(it))
}
val currentWallpaperName = if (settings.shouldMigrateLegacyWallpaper) {
val migratedWallpaperName =
migrationHelper.migrateLegacyWallpaper(settings.currentWallpaperName)
@ -258,6 +259,11 @@ class WallpapersUseCases(
} else {
settings.currentWallpaperName
}
if (settings.shouldMigrateLegacyWallpaperCardColors) {
migrationHelper.migrateExpiredWallpaperCardColors()
}
val possibleWallpapers = metadataFetcher.downloadWallpaperList().filter {
!it.isExpired() && it.isAvailableInLocale()
}

@ -209,6 +209,7 @@
<string name="pref_key_current_wallpaper_card_color_dark" translatable="false">pref_key_current_wallpaper_card_color_dark</string>
<string name="pref_key_wallpapers_onboarding" translatable="false">pref_key_wallpapers_onboarding</string>
<string name="pref_key_should_migrate_wallpaper" translatable="false">pref_key_should_migrate_wallpaper</string>
<string name="pref_key_should_migrate_wallpaper_card_colors" translatable="false">pref_key_should_migrate_wallpaper_card_colors</string>
<string name="pref_key_encryption_key_generated" translatable="false">pref_key_encryption_key_generated</string>

@ -9,6 +9,7 @@ import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.slot
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import mozilla.components.service.glean.testing.GleanTestRule
@ -17,14 +18,19 @@ import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
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.runner.RunWith
import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.utils.Settings
import java.util.Calendar
import java.util.Date
import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_DARK
import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_LIGHT
import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_PANDA_WALLPAPER_NAME
import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_WALLPAPER_TEXT_COLOR
import java.io.File
import java.util.*
import kotlin.random.Random
@RunWith(AndroidJUnit4::class)
@ -47,12 +53,14 @@ class WallpapersUseCasesTest {
every { currentWallpaperCardColorDark = any() } just Runs
every { shouldMigrateLegacyWallpaper } returns false
every { shouldMigrateLegacyWallpaper = any() } just Runs
every { shouldMigrateLegacyWallpaperCardColors } returns false
every { shouldMigrateLegacyWallpaperCardColors = any() } just Runs
}
private val mockLegacyDownloader = mockk<LegacyWallpaperDownloader>(relaxed = true)
private val mockLegacyFileManager = mockk<LegacyWallpaperFileManager> {
every { clean(any(), any()) } just runs
}
private val mockMigrationHelper = mockk<LegacyWallpaperMigration>(relaxed = true)
private lateinit var mockMigrationHelper: LegacyWallpaperMigration
private val mockMetadataFetcher = mockk<WallpaperMetadataFetcher>()
private val mockDownloader = mockk<WallpaperDownloader> {
@ -62,6 +70,20 @@ class WallpapersUseCasesTest {
coEvery { clean(any(), any()) } returns mockk()
}
private val mockFolder: File = mockk()
private val downloadWallpaper: (Wallpaper) -> Wallpaper.ImageFileState = mockk(relaxed = true)
@Before
fun setup() {
mockMigrationHelper = spyk(
LegacyWallpaperMigration(
storageRootDirectory = mockFolder,
settings = mockSettings,
downloadWallpaper,
),
)
}
@Test
fun `GIVEN legacy use case WHEN initializing THEN the default wallpaper is not downloaded`() = runTest {
val fakeRemoteWallpapers = listOf("first", "second", "third").map { name ->
@ -132,7 +154,7 @@ class WallpapersUseCasesTest {
}
@Test
fun `GIVEN leagacy use case and wallpapers that expired and an expired one is selected WHEN invoking initialize use case THEN selected wallpaper is not filtered out`() = runTest {
fun `GIVEN legacy use case and wallpapers that expired and an expired one is selected WHEN invoking initialize use case THEN selected wallpaper is not filtered out`() = runTest {
val fakeRemoteWallpapers = listOf("first", "second", "third").map { name ->
makeFakeRemoteWallpaper(TimeRelation.LATER, name)
}
@ -351,6 +373,37 @@ class WallpapersUseCasesTest {
assertEquals(expiredWallpaper, appStore.state.wallpaperState.currentWallpaper)
}
@Test
fun `GIVEN wallpapers that expired and an expired one is selected and card colors have not been migrated WHEN invoking initialize use case THEN migrate card colors`() = runTest {
val fakeRemoteWallpapers = listOf("first", "second", "third").map { name ->
makeFakeRemoteWallpaper(TimeRelation.LATER, name)
}
val expiredWallpaper = makeFakeRemoteWallpaper(TimeRelation.BEFORE, TURNING_RED_PANDA_WALLPAPER_NAME)
val allWallpapers = listOf(expiredWallpaper) + fakeRemoteWallpapers
every { mockSettings.currentWallpaperName } returns TURNING_RED_PANDA_WALLPAPER_NAME
every { mockSettings.shouldMigrateLegacyWallpaperCardColors } returns true
every { mockSettings.currentWallpaperTextColor } returns TURNING_RED_WALLPAPER_TEXT_COLOR.toLong(radix = 16)
coEvery { mockFileManager.lookupExpiredWallpaper(any()) } returns expiredWallpaper
coEvery { mockMetadataFetcher.downloadWallpaperList() } returns allWallpapers
coEvery { mockDownloader.downloadThumbnail(any()) } returns Wallpaper.ImageFileState.Downloaded
WallpapersUseCases.DefaultInitializeWallpaperUseCase(
appStore,
mockDownloader,
mockFileManager,
mockMetadataFetcher,
mockMigrationHelper,
mockSettings,
"en-US",
).invoke()
appStore.waitUntilIdle()
verify { mockMigrationHelper.migrateExpiredWallpaperCardColors() }
verify { mockSettings.currentWallpaperCardColorLight = TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_LIGHT.toLong(radix = 16) }
verify { mockSettings.currentWallpaperCardColorDark = TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_DARK.toLong(radix = 16) }
}
@Test
fun `GIVEN wallpapers that are in promotions outside of locale WHEN invoking initialize use case THEN promotional wallpapers are filtered out`() = runTest {
val fakeRemoteWallpapers = listOf("first", "second", "third").map { name ->

Loading…
Cancel
Save