mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix: preserve Electric contrast for filled and text buttons (#1149)
This commit is contained in:
1 parent
2457f27cdf
commit
746a850a44
4 files changed
+88
-4
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.luminance
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class PrimaryButtonContrastTest {
|
||||
@Test fun electricTextButtonsRemainReadableOnPageAndDialogSurfaces() {
|
||||
val palette = Themes.byId(Themes.ELECTRIC)
|
||||
for (background in listOf(palette.background, palette.surface)) {
|
||||
assertTrue("Electric text button: ${contrast(palette.primary, background)}",
|
||||
contrast(palette.primary, background) >= 4.5f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun normalSizedPrimaryTextMeetsContrastAcrossEveryThemeGradient() {
|
||||
for (palette in Themes.all) {
|
||||
for (step in 0..100) {
|
||||
val fraction = step / 100f
|
||||
val start = palette.primary
|
||||
val end = palette.primary2
|
||||
val background = Color(
|
||||
start.red + (end.red - start.red) * fraction,
|
||||
start.green + (end.green - start.green) * fraction,
|
||||
start.blue + (end.blue - start.blue) * fraction,
|
||||
)
|
||||
val ratio = contrast(palette.onPrimary, background)
|
||||
assertTrue("${palette.id} gradient $step: $ratio", ratio >= 4.5f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun contrast(a: Color, b: Color): Float {
|
||||
val light = maxOf(a.luminance(), b.luminance())
|
||||
val dark = minOf(a.luminance(), b.luminance())
|
||||
return (light + 0.05f) / (dark + 0.05f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.compose.ui.semantics.SemanticsActions
|
||||
import androidx.compose.ui.test.assertIsFocused
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.compose.ui.test.performSemanticsAction
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28])
|
||||
class PrimaryButtonFocusContrastTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
|
||||
@Test fun focusedPrimaryButtonDoesNotClaimAColorOverlayAboveItsLabel() {
|
||||
lateinit var glide: FocusGlide
|
||||
var clicks = 0
|
||||
compose.setContent {
|
||||
val view = LocalView.current
|
||||
glide = remember(view) { FocusGlide(view) }
|
||||
DroidDeckTheme(Themes.ELECTRIC) {
|
||||
CompositionLocalProvider(LocalFocusGlide provides glide) {
|
||||
PrimaryButton("Play") { clicks++ }
|
||||
}
|
||||
}
|
||||
}
|
||||
val button = compose.onNodeWithText("Play")
|
||||
button.performSemanticsAction(SemanticsActions.RequestFocus)
|
||||
button.assertIsFocused()
|
||||
compose.runOnIdle { assertNull(glide.current) }
|
||||
button.performClick()
|
||||
compose.runOnIdle {
|
||||
assertEquals(1, clicks)
|
||||
assertNull(glide.current)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user