Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## Unreleased

### Fixed

- Support for monochrome system color palette (#199).

## 2.1.0

### New
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright (c) 2026 Tomasz Świstak <tomasz@swistak.codes>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
package codes.swistak.batterymonitor.testsupport

import androidx.compose.material3.darkColorScheme
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.graphics.luminance

internal val monochromaticDarkColorScheme = darkColorScheme(
primary = Color.White,
onPrimary = Color(0xFF1B1B1B),
primaryContainer = Color(0xFFD4D4D4),
onPrimaryContainer = Color.Black
)

internal fun contrast(foreground: Color, background: Color): Float {
val fg = foreground.compositeOver(background).luminance()
val bg = background.luminance()
return (maxOf(fg, bg) + 0.05f) / (minOf(fg, bg) + 0.05f)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright (c) 2026 Tomasz Świstak <tomasz@swistak.codes>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
package codes.swistak.batterymonitor.ui.components

import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.semantics.getOrNull
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.test.core.app.ActivityScenario
import codes.swistak.batterymonitor.testsupport.ComposeTestActivity
import codes.swistak.batterymonitor.testsupport.awaitCondition
import codes.swistak.batterymonitor.testsupport.composeNodes
import codes.swistak.batterymonitor.testsupport.contrast
import codes.swistak.batterymonitor.testsupport.monochromaticDarkColorScheme
import codes.swistak.batterymonitor.testsupport.runOnMain
import org.junit.Assert.assertTrue
import org.junit.Test
import kotlin.math.roundToInt

class BatteryCellHeroContrastInstrumentedTest {
@Test
fun fill_and_markings_are_visible_on_both_sides_of_the_fill_boundary() {
ActivityScenario.launch(ComposeTestActivity::class.java).use { scenario ->
val showTarget = mutableStateOf(false)
lateinit var decor: View
scenario.onActivity { activity ->
decor = activity.window.decorView
activity.setContent {
CompositionLocalProvider(
LocalDensity provides Density(PIXELS_PER_DP.toFloat()),
LocalLayoutDirection provides LayoutDirection.Ltr
) {
MaterialTheme(colorScheme = monochromaticDarkColorScheme) {
BatteryCellHero(
title = "Battery",
level = 50,
status = "",
detail = "",
spokenSummary = if (showTarget.value) TARGET else CHARGING,
modifier = Modifier.width(320.dp),
targetPercent = if (showTarget.value) 50 else null,
charging = !showTarget.value
)
}
}
}
}

withCellImage(decor, CHARGING) { image ->
val empty = image.colorAt(30, 18)
val filled = image.colorAt(30, 102)
val separation = contrast(filled, empty)
assertTrue("Fill against empty cell had $separation:1 contrast", separation >= 3f)
image.assertFeature("Upper tick", Rect(5f, 29f, 12f, 31f), empty)
image.assertFeature("Lower tick", Rect(5f, 89f, 12f, 91f), filled)
image.assertFeature("Upper bolt", Rect(20f, 47f, 40f, 58f), empty)
image.assertFeature("Lower bolt", Rect(20f, 62f, 40f, 73f), filled)
}

runOnMain { showTarget.value = true }
withCellImage(decor, TARGET) { image ->
val empty = image.colorAt(30, 18)
val filled = image.colorAt(30, 102)
image.assertFeature("Target line above fill", Rect(22f, 58.5f, 38f, 60f), empty)
image.assertFeature("Target line over fill", Rect(22f, 60f, 38f, 61.5f), filled)
image.assertFeature("Target ring above fill", Rect(54f, 56f, 55.5f, 57f), empty)
image.assertFeature("Target ring over fill", Rect(54f, 63f, 55.5f, 64f), filled)
}
}
}

private fun withCellImage(decor: View, summary: String, inspect: (Bitmap) -> Unit) {
fun cardBounds(): Rect? = composeNodes(decor).singleOrNull {
it.config.getOrNull(SemanticsProperties.ContentDescription) == listOf(summary)
}?.boundsInWindow?.takeIf { it.width > 0f }

awaitCondition({ "$summary did not render" }) { runOnMain { cardBounds() != null } }
runOnMain {
val card = checkNotNull(cardBounds())
val image = Bitmap.createBitmap(
60 * PIXELS_PER_DP, 120 * PIXELS_PER_DP, Bitmap.Config.ARGB_8888
)
try {
// Crop to the non-expanded cell interior, excluding the terminal, border and inset.
val canvas = Canvas(image)
canvas.translate(
90f * PIXELS_PER_DP - card.right,
56f * PIXELS_PER_DP - card.center.y.roundToInt()
)
decor.draw(canvas)
inspect(image)
} finally {
image.recycle()
}
}
}

private fun Bitmap.colorAt(xDp: Int, yDp: Int): Color =
Color(getPixel(xDp * PIXELS_PER_DP, yDp * PIXELS_PER_DP))

private fun Bitmap.assertFeature(name: String, boundsDp: Rect, background: Color) {
var best = 1f
for (y in (boundsDp.top * PIXELS_PER_DP).toInt() until (boundsDp.bottom * PIXELS_PER_DP).toInt()) {
for (x in (boundsDp.left * PIXELS_PER_DP).toInt() until (boundsDp.right * PIXELS_PER_DP).toInt()) {
best = maxOf(best, contrast(Color(getPixel(x, y)), background))
}
}
assertTrue("$name had $best:1 contrast", best >= 3f)
}

private companion object {
const val PIXELS_PER_DP = 2
const val CHARGING = "Battery charging at half capacity"
const val TARGET = "Battery at the charging target"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
Copyright (c) 2026 Tomasz Świstak <tomasz@swistak.codes>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
package codes.swistak.batterymonitor.ui.navigation

import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.semantics.getAllSemanticsNodes
import androidx.compose.ui.semantics.getOrNull
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.test.core.app.ActivityScenario
import codes.swistak.batterymonitor.testsupport.ComposeTestActivity
import codes.swistak.batterymonitor.testsupport.awaitCondition
import codes.swistak.batterymonitor.testsupport.composeNodes
import codes.swistak.batterymonitor.testsupport.contrast
import codes.swistak.batterymonitor.testsupport.monochromaticDarkColorScheme
import codes.swistak.batterymonitor.testsupport.runOnMain
import codes.swistak.batterymonitor.ui.theme.Brightness
import codes.swistak.batterymonitor.ui.theme.ColorSource
import codes.swistak.batterymonitor.ui.theme.batteryColorScheme
import org.junit.Assert.assertTrue
import org.junit.Test
import kotlin.math.abs

class SideMenuContrastInstrumentedTest {
@Test
fun selected_drawer_text_and_icon_use_the_role_paired_with_the_rendered_container() {
ActivityScenario.launch(ComposeTestActivity::class.java).use { scenario ->
lateinit var activity: ComposeTestActivity
scenario.onActivity { activity = it }

val dark = darkColorScheme()
val batteryBlue =
listOf(Brightness.Light, Brightness.Dark, Brightness.TrueBlack).map { brightness ->
"Battery Blue $brightness" to batteryColorScheme(
activity, ColorSource.BatteryBlue, brightness
)
}
val cases = listOf(
"Material You monochromatic dark" to monochromaticDarkColorScheme,
"Material You monochromatic light" to lightColorScheme(
primary = Color.Black,
onPrimary = Color.White,
primaryContainer = Color(0xFF343434),
onPrimaryContainer = Color.White
),
"Material You chromatic light" to lightColorScheme(),
"Material You chromatic dark" to dark,
"Material You True Black" to dark.copy(
background = Color.Black, surface = Color(0xFF10151B)
)
) + batteryBlue

val palette = mutableStateOf(cases.first())
runOnMain {
activity.setContent {
val (name, colors) = palette.value
// Use a fixed density so strokes and glyphs contain solid foreground pixels.
CompositionLocalProvider(
LocalDensity provides Density(2f),
LocalLayoutDirection provides LayoutDirection.Ltr
) {
MaterialTheme(colorScheme = colors) {
Surface {
SideMenuContent(
appName = "Test app",
closeLabel = "Close",
batteryGroupLabel = "Battery",
toolsGroupLabel = "Tools",
batterySections = listOf(MenuSection("current", MENU_LABEL)),
toolSections = emptyList(),
selectedId = "current",
onSelect = {},
onClose = {},
modifier = Modifier.testTag(name),
showCloseButton = false
)
}
}
}
}
}

val decor = activity.window.decorView
for ((name, colors) in cases) {
runOnMain { palette.value = name to colors }
awaitCondition({ "$name drawer did not render" }) {
runOnMain {
val nodes = composeNodes(decor)
nodes.any { it.config.getOrNull(SemanticsProperties.TestTag) == name } && nodes.any {
it.config.getOrNull(SemanticsProperties.Selected) == true && it.boundsInWindow.width > 0f
}
}
}
val (background, label, icon) = runOnMain { captureSelectedColors(decor) }
assertColor(
"$name selected background",
colors.primaryContainer.compositeOver(colors.surface),
background
)
val foreground = colors.onPrimaryContainer.compositeOver(background)
assertColor("$name selected text", foreground, label)
assertColor("$name selected icon", foreground, icon)
assertTrue(
"$name text contrast must be at least 4.5:1",
contrast(label, background) >= 4.5f
)
assertTrue(
"$name icon contrast must be at least 3:1", contrast(icon, background) >= 3f
)
}
}
}

private fun captureSelectedColors(decor: View): Triple<Color, Color, Color> {
val row = composeNodes(decor).single {
it.config.getOrNull(SemanticsProperties.Selected) == true
}
val rowBounds = row.boundsInWindow
val labelBounds =
requireNotNull(row.root).semanticsOwner.getAllSemanticsNodes(mergingEnabled = false)
.single { node ->
node.config.getOrNull(SemanticsProperties.Text)
?.any { it.text == MENU_LABEL } == true
}.boundsInWindow

// The icon precedes the label and has no semantics of its own. Sample the middle of that
// region to keep the antialiased ends of the selection pill out of the comparison.
val iconBounds = Rect(
rowBounds.left + rowBounds.height / 8f,
rowBounds.center.y - rowBounds.height / 6f,
labelBounds.left,
rowBounds.center.y + rowBounds.height / 6f
)
val image = Bitmap.createBitmap(decor.width, decor.height, Bitmap.Config.ARGB_8888)
try {
decor.draw(Canvas(image))
val backgroundX = (labelBounds.right + rowBounds.right) / 2f
val background = Color(image.getPixel(backgroundX.toInt(), rowBounds.center.y.toInt()))
return Triple(
background,
image.strongestForeground(labelBounds, background),
image.strongestForeground(iconBounds, background)
)
} finally {
image.recycle()
}
}

private fun Bitmap.strongestForeground(bounds: Rect, background: Color): Color {
var foreground = background
var bestContrast = 1f
for (y in bounds.top.toInt() until bounds.bottom.toInt()) {
for (x in bounds.left.toInt() until bounds.right.toInt()) {
val pixel = Color(getPixel(x, y))
val pixelContrast = contrast(pixel, background)
if (pixelContrast > bestContrast) {
foreground = pixel
bestContrast = pixelContrast
}
}
}
return foreground
}

private fun assertColor(message: String, expected: Color, actual: Color) {
val difference = maxOf(
abs(expected.red - actual.red),
abs(expected.green - actual.green),
abs(expected.blue - actual.blue)
)
assertTrue("$message expected $expected, rendered $actual", difference <= 2f / 255f)
}

private companion object {
const val MENU_LABEL = "Current"
}
}
Loading
Loading