For #27575: Add String.toHexColor extension

This commit is contained in:
kl3jvi 2022-11-09 23:28:10 +01:00 committed by mergify[bot]
parent 1a5443167d
commit c87589bef2
5 changed files with 34 additions and 19 deletions

View File

@ -0,0 +1,10 @@
/* 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.utils
/**
* Converts a string of hexadecimal characters to a Long color value.
*/
fun String.toHexColor() = toLong(radix = 16)

View File

@ -8,6 +8,7 @@ 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.utils.toHexColor
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.amethystName
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.beachVibeName
import org.mozilla.fenix.wallpapers.Wallpaper.Companion.ceruleanName
@ -93,7 +94,7 @@ class LegacyWallpaperMigration(
// If an expired Turning Red wallpaper is successfully migrated
if (wallpaperName == TURNING_RED_MEI_WALLPAPER_NAME || wallpaperName == TURNING_RED_PANDA_WALLPAPER_NAME) {
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toLong(radix = 16)
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toHexColor()
}
} catch (e: IOException) {
Logger.error("Failed to migrate legacy wallpaper", e)
@ -119,15 +120,15 @@ class LegacyWallpaperMigration(
when (settings.currentWallpaperName) {
TURNING_RED_MEI_WALLPAPER_NAME -> {
settings.currentWallpaperCardColorLight =
TURNING_RED_MEI_WALLPAPER_CARD_COLOR_LIGHT.toLong(radix = 16)
TURNING_RED_MEI_WALLPAPER_CARD_COLOR_LIGHT.toHexColor()
settings.currentWallpaperCardColorDark =
TURNING_RED_MEI_WALLPAPER_CARD_COLOR_DARK.toLong(radix = 16)
TURNING_RED_MEI_WALLPAPER_CARD_COLOR_DARK.toHexColor()
}
TURNING_RED_PANDA_WALLPAPER_NAME -> {
settings.currentWallpaperCardColorLight =
TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_LIGHT.toLong(radix = 16)
TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_LIGHT.toHexColor()
settings.currentWallpaperCardColorDark =
TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_DARK.toLong(radix = 16)
TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_DARK.toHexColor()
}
}
}

View File

@ -11,6 +11,7 @@ import mozilla.components.concept.fetch.Request
import org.json.JSONArray
import org.json.JSONObject
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.utils.toHexColor
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
@ -40,11 +41,12 @@ class WallpaperMetadataFetcher(
}.getOrElse { listOf() }
}
private fun JSONObject.parseAsWallpapers(): List<Wallpaper> = with(getJSONArray("collections")) {
(0 until length()).map { index ->
getJSONObject(index).toCollectionOfWallpapers()
}.flatten()
}
private fun JSONObject.parseAsWallpapers(): List<Wallpaper> =
with(getJSONArray("collections")) {
(0 until length()).map { index ->
getJSONObject(index).toCollectionOfWallpapers()
}.flatten()
}
private fun JSONObject.toCollectionOfWallpapers(): List<Wallpaper> {
val collectionId = getString("id")
@ -105,7 +107,7 @@ class WallpaperMetadataFetcher(
* listed in the metadata.
*/
private fun JSONObject.getArgbValueAsLong(propName: String): Long = "FF${getString(propName)}"
.toLong(radix = 16)
.toHexColor()
companion object {
internal const val currentJsonVersion = 1

View File

@ -11,6 +11,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.utils.toHexColor
import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_MEI_WALLPAPER_NAME
import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_PANDA_WALLPAPER_NAME
import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_WALLPAPER_TEXT_COLOR
@ -103,14 +104,14 @@ class LegacyWallpaperMigrationTest {
migrationHelper.migrateLegacyWallpaper(TURNING_RED_MEI_WALLPAPER_NAME)
assertTrue(getAllFiles(TURNING_RED_MEI_WALLPAPER_NAME).all { it.exists() })
verify(exactly = 1) {
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toLong(radix = 16)
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toHexColor()
}
createAllLegacyFiles(TURNING_RED_PANDA_WALLPAPER_NAME)
migrationHelper.migrateLegacyWallpaper(TURNING_RED_PANDA_WALLPAPER_NAME)
assertTrue(getAllFiles(TURNING_RED_PANDA_WALLPAPER_NAME).all { it.exists() })
verify(exactly = 2) {
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toLong(radix = 16)
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toHexColor()
}
}
}
@ -124,7 +125,7 @@ class LegacyWallpaperMigrationTest {
assertFalse(getAllFiles(TURNING_RED_MEI_WALLPAPER_NAME).all { it.exists() })
assertFalse(getAllFiles(TURNING_RED_PANDA_WALLPAPER_NAME).all { it.exists() })
verify(exactly = 0) {
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toLong(radix = 16)
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toHexColor()
}
}
}
@ -138,14 +139,14 @@ class LegacyWallpaperMigrationTest {
migrationHelper.migrateLegacyWallpaper(wallpaper1)
assertFalse(getAllFiles(wallpaper1).all { it.exists() })
verify(exactly = 0) {
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toLong(radix = 16)
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toHexColor()
}
createAllLegacyFiles(wallpaper2)
migrationHelper.migrateLegacyWallpaper(wallpaper2)
assertTrue(getAllFiles(wallpaper2).all { it.exists() })
verify(exactly = 0) {
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toLong(radix = 16)
settings.currentWallpaperTextColor = TURNING_RED_WALLPAPER_TEXT_COLOR.toHexColor()
}
}
}

View File

@ -25,6 +25,7 @@ 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 org.mozilla.fenix.utils.toHexColor
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
@ -382,7 +383,7 @@ class WallpapersUseCasesTest {
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)
every { mockSettings.currentWallpaperTextColor } returns TURNING_RED_WALLPAPER_TEXT_COLOR.toHexColor()
coEvery { mockFileManager.lookupExpiredWallpaper(any()) } returns expiredWallpaper
coEvery { mockMetadataFetcher.downloadWallpaperList() } returns allWallpapers
coEvery { mockDownloader.downloadThumbnail(any()) } returns Wallpaper.ImageFileState.Downloaded
@ -400,8 +401,8 @@ class WallpapersUseCasesTest {
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) }
verify { mockSettings.currentWallpaperCardColorLight = TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_LIGHT.toHexColor() }
verify { mockSettings.currentWallpaperCardColorDark = TURNING_RED_PANDA_WALLPAPER_CARD_COLOR_DARK.toHexColor() }
}
@Test