For #26644 - Apply the wallpaper provided text color to the Fenix logo

And to the private mode switcher.
This commit is contained in:
Mugurell 2022-09-15 15:02:01 +03:00 committed by mergify[bot]
parent 103a0cb250
commit 7b5ba840ab
4 changed files with 80 additions and 11 deletions

View File

@ -5,6 +5,7 @@
package org.mozilla.fenix.home
import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
@ -128,7 +129,9 @@ class HomeFragment : Fragment() {
@VisibleForTesting
internal lateinit var bundleArgs: Bundle
private var _binding: FragmentHomeBinding? = null
@VisibleForTesting
@Suppress("VariableNaming")
internal var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
private val homeViewModel: HomeScreenViewModel by activityViewModels()
@ -968,11 +971,29 @@ class HomeFragment : Fragment() {
text = resources.getString(R.string.wallpaper_select_error_snackbar_message),
)
}
// If setting a wallpaper failed reset also the contrasting text color.
requireContext().settings().currentWallpaperTextColor = 0L
lastAppliedWallpaperName = Wallpaper.defaultName
}
}
}
}
// Logo color should be updated in all cases.
applyWallpaperTextColor()
}
/**
* Apply a color better contrasting with the current wallpaper to the Fenix logo and private mode switcher.
*/
@VisibleForTesting
internal fun applyWallpaperTextColor() {
val tintColor = when (val color = requireContext().settings().currentWallpaperTextColor.toInt()) {
0 -> null // a null ColorStateList will clear the current tint
else -> ColorStateList.valueOf(color)
}
binding.wordmarkText.imageTintList = tintColor
binding.privateBrowsingButton.imageTintList = tintColor
}
private fun observeWallpaperUpdates() {

View File

@ -193,6 +193,7 @@ class Settings(private val appContext: Context) : PreferencesHolder {
/**
* A cache of the text color to use on text overlaying the current wallpaper.
* The value will be `0` if the color is unavailable.
*/
var currentWallpaperTextColor by longPreference(
appContext.getPreferenceKey(R.string.pref_key_current_wallpaper_text_color),

View File

@ -483,8 +483,10 @@ class WallpapersUseCases(
}
}
private fun selectWallpaper(wallpaper: Wallpaper) {
@VisibleForTesting
internal fun selectWallpaper(wallpaper: Wallpaper) {
settings.currentWallpaperName = wallpaper.name
settings.currentWallpaperTextColor = wallpaper.textColor ?: 0L
store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper))
}

View File

@ -25,6 +25,7 @@ import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.utils.Settings
import java.util.Calendar
import java.util.Date
import kotlin.random.Random
@RunWith(AndroidJUnit4::class)
class WallpapersUseCasesTest {
@ -515,10 +516,7 @@ class WallpapersUseCasesTest {
@Test
fun `WHEN legacy selected wallpaper usecase invoked THEN storage updated and store receives dispatch`() = runTest {
val selectedWallpaper = makeFakeRemoteWallpaper(TimeRelation.LATER, "selected")
val slot = slot<String>()
coEvery { mockFileManager.lookupExpiredWallpaper(any()) } returns null
every { mockSettings.currentWallpaperName } returns ""
every { mockSettings.currentWallpaperName = capture(slot) } just runs
every { mockSettings.currentWallpaperName = any() } just Runs
val wallpaperFileState = WallpapersUseCases.LegacySelectWallpaperUseCase(
mockSettings,
@ -526,7 +524,10 @@ class WallpapersUseCasesTest {
).invoke(selectedWallpaper)
appStore.waitUntilIdle()
assertEquals(selectedWallpaper.name, slot.captured)
verify { mockSettings.currentWallpaperName = selectedWallpaper.name }
verify { mockSettings.currentWallpaperTextColor = selectedWallpaper.textColor!! }
verify { mockSettings.currentWallpaperCardColor = selectedWallpaper.cardColor!! }
assertEquals(selectedWallpaper, appStore.state.wallpaperState.currentWallpaper)
assertEquals(wallpaperFileState, Wallpaper.ImageFileState.Downloaded)
}
@ -600,6 +601,50 @@ class WallpapersUseCasesTest {
assertEquals(wallpaperFileState, Wallpaper.ImageFileState.Error)
}
@Test
fun `GIVEN a wallpaper with no text color WHEN it is is selected THEN persist the wallpaper name and missing text color and dispatch the update`() {
every { mockSettings.currentWallpaperName = any() } just Runs
val store = mockk<AppStore>(relaxed = true)
val wallpaperFileState = WallpapersUseCases.DefaultSelectWallpaperUseCase(
settings = mockSettings,
store = store,
fileManager = mockk(),
downloader = mockk(),
)
val wallpaper: Wallpaper = mockk {
every { name } returns "Test"
every { textColor } returns null
}
wallpaperFileState.selectWallpaper(wallpaper)
verify { mockSettings.currentWallpaperName = "Test" }
verify { mockSettings.currentWallpaperTextColor = 0L }
verify { store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper)) }
}
@Test
fun `GIVEN a wallpaper with available text color WHEN it is is selected THEN persist the wallpaper name and text color and dispatch the update`() {
every { mockSettings.currentWallpaperName = any() } just Runs
val store = mockk<AppStore>(relaxed = true)
val wallpaperFileState = WallpapersUseCases.DefaultSelectWallpaperUseCase(
settings = mockSettings,
store = store,
fileManager = mockk(),
downloader = mockk(),
)
val wallpaper: Wallpaper = mockk {
every { name } returns "Test"
every { textColor } returns 321L
}
wallpaperFileState.selectWallpaper(wallpaper)
verify { mockSettings.currentWallpaperName = "Test" }
verify { mockSettings.currentWallpaperTextColor = 321L }
verify { store.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper)) }
}
private enum class TimeRelation {
BEFORE,
NOW,
@ -633,8 +678,8 @@ class WallpapersUseCasesTest {
endDate = relativeTime,
learnMoreUrl = null,
),
textColor = null,
cardColor = null,
textColor = Random.nextLong(),
cardColor = Random.nextLong(),
thumbnailFileState = Wallpaper.ImageFileState.Unavailable,
assetsFileState = Wallpaper.ImageFileState.Unavailable,
)
@ -650,8 +695,8 @@ class WallpapersUseCasesTest {
endDate = relativeTime,
learnMoreUrl = null,
),
textColor = null,
cardColor = null,
textColor = Random.nextLong(),
cardColor = Random.nextLong(),
thumbnailFileState = Wallpaper.ImageFileState.Unavailable,
assetsFileState = Wallpaper.ImageFileState.Unavailable,
)