Month widget: count Glance's real view ids per Android version, test the budget on device (#372)

This commit is contained in:
makiolaj committed 2026-10-21 18:44:14 +02:00
1 parent a87ebc33b6
commit 46ca2db63a
4 files changed
+313 -124

No files matched your search

@@ -1,7 +1,10 @@
package de.jeanlucmakiola.calendula.widget.month
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.calendula.allDayEvent
import de.jeanlucmakiola.calendula.domain.EventInstance
import de.jeanlucmakiola.calendula.timedEvent
import de.jeanlucmakiola.calendula.ui.month.MonthWeek
import de.jeanlucmakiola.calendula.ui.month.layoutMonthWeeks
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.DayOfWeek
@@ -19,60 +22,88 @@ class MonthGridTest {
private val jul26 = YearMonth(2026, Month.JULY)
private val today = LocalDate(2026, 7, 15)
/** Monday Jul 6 to Sunday Jul 12. */
private val monday = LocalDate(2026, 7, 6)
private val android16 = ViewIds(sdkInt = 36)
private val android10 = ViewIds(sdkInt = 29)
private fun weeks(events: List<EventInstance>): List<MonthWeek> =
layoutMonthWeeks(jul26, DayOfWeek.MONDAY, events, zone)
/** July 2026 with [perDay] timed events on every day of the grid. */
private fun weeks(perDay: Int): List<de.jeanlucmakiola.calendula.ui.month.MonthWeek> {
private fun busyWeeks(perDay: Int): List<MonthWeek> {
val first = LocalDate(2026, 6, 29)
var id = 0L
val events = (0 until 42).flatMap { offset ->
val day = first.plus(offset, DateTimeUnit.DAY)
(1..perDay).map { hour ->
timedEvent(day, hour, zone = zone, id = ++id, title = "E$id", color = 0xFFF44336.toInt())
}
return weeks(
(0 until 42).flatMap { offset ->
val day = first.plus(offset, DateTimeUnit.DAY)
(1..perDay).map { hour -> timedEvent(day, hour, zone = zone, id = ++id, title = "E$id") }
},
)
}
private fun List<WeekLayout>.cost(ids: ViewIds) = sumOf { it.viewIds(today, ids) }
@Test
fun `a quiet month keeps every lane it uses`() {
val layouts = layoutWeeks(busyWeeks(perDay = 1), today, android16)
assertThat(layouts.map { it.laneCap }.distinct()).containsExactly(1)
assertThat(layouts.all { it.overflow.isEmpty() }).isTrue()
}
@Test
fun `a full month folds lanes until it fits, on every Android version`() {
for (ids in listOf(android16, android10)) {
val weeks = busyWeeks(perDay = 5)
assertThat(weeks.map { WeekLayout(it, MAX_LANES) }.cost(ids)).isGreaterThan(VIEW_BUDGET - ids.fixed)
val layouts = layoutWeeks(weeks, today, ids)
assertThat(layouts.cost(ids)).isAtMost(VIEW_BUDGET - ids.fixed)
assertThat(layouts.any { it.laneCap < MAX_LANES }).isTrue()
}
return layoutMonthWeeks(jul26, DayOfWeek.MONDAY, events, zone)
}
@Test
fun `a quiet month keeps every lane`() {
val weeks = weeks(perDay = 1)
assertThat(laneCaps(weeks, today)).containsExactlyElementsIn(List(weeks.size) { MAX_LANES })
}
@Test
fun `a full month folds lanes until it fits the budget`() {
val weeks = weeks(perDay = 5)
val fullCost = weeks.sumOf { it.viewCount(MAX_LANES, today) }
assertThat(fullCost).isGreaterThan(VIEW_BUDGET - FIXED_VIEWS)
val caps = laneCaps(weeks, today)
val cost = weeks.indices.sumOf { weeks[it].viewCount(caps[it], today) }
assertThat(cost).isAtMost(VIEW_BUDGET - FIXED_VIEWS)
assertThat(caps.any { it < MAX_LANES }).isTrue()
fun `older Android folds more, since a sized view costs it two ids`() {
val weeks = busyWeeks(perDay = 3)
val newer = layoutWeeks(weeks, today, android16).sumOf { it.laneCap }
val older = layoutWeeks(weeks, today, android10).sumOf { it.laneCap }
assertThat(older).isLessThan(newer)
}
@Test
fun `folded events are counted in the overflow`() {
val week = weeks(perDay = 5)[1]
val hidden = week.hiddenPerDay(laneCap = 1)
assertThat(hidden).containsExactlyElementsIn(List(7) { 4 })
assertThat(week.usedLanes(laneCap = 1)).isEqualTo(1)
val layout = WeekLayout(busyWeeks(perDay = 5)[1], laneCap = 1)
assertThat(layout.usedLanes).isEqualTo(1)
assertThat(layout.overflow).containsExactly(GridCell(4, 1), GridCell(4, 1), GridCell(4, 1),
GridCell(4, 1), GridCell(4, 1), GridCell(4, 1), GridCell(4, 1)).inOrder()
}
@Test
fun `a lane of single-day events is one cell per day`() {
val cells = weeks(perDay = 1)[1].laneCells(lane = 0, laneCap = MAX_LANES)
assertThat(cells).hasSize(7)
assertThat(cells.all { it.event != null && it.cols == 1 }).isTrue()
fun `a multi-day bar is one cell as wide as its days`() {
val bar = allDayEvent(monday.plus(1, DateTimeUnit.DAY), monday.plus(3, DateTimeUnit.DAY), id = 1)
val layout = WeekLayout(weeks(listOf(bar))[1], MAX_LANES)
assertThat(layout.lanes).hasSize(1)
assertThat(layout.lanes[0]).containsExactly(GridCell(null, 1), GridCell(bar, 3), GridCell(null, 3)).inOrder()
}
@Test
fun `overflow gaps are merged`() {
assertThat(overflowCells(listOf(0, 0, 2, 0, 0, 0, 1))).containsExactly(
OverflowCell(0, 2),
OverflowCell(2, 1),
OverflowCell(0, 3),
OverflowCell(1, 1),
).inOrder()
assertThat(overflowCells(List(7) { 0 })).isEmpty()
fun `a bar on a folded lane moves into the overflow of each day it covers`() {
val top = allDayEvent(monday, monday.plus(6, DateTimeUnit.DAY), id = 1)
val lower = allDayEvent(monday.plus(2, DateTimeUnit.DAY), monday.plus(3, DateTimeUnit.DAY), id = 2)
val layout = WeekLayout(weeks(listOf(top, lower))[1], laneCap = 1)
assertThat(layout.lanes).hasSize(1)
assertThat(layout.lanes[0]).containsExactly(GridCell(top, 7))
assertThat(layout.overflow).containsExactly(GridCell(null, 2), GridCell(1, 1), GridCell(1, 1), GridCell(null, 3))
.inOrder()
}
@Test
fun `gaps are merged`() {
val counts = listOf(0, 0, 2, 0, 0, 0, 1)
val cells = mergeGaps(counts.size) { col -> counts[col].takeIf { it > 0 }?.let { GridCell(it, 1) } }
assertThat(cells).containsExactly(GridCell(null, 2), GridCell(2, 1), GridCell(null, 3), GridCell(1, 1))
.inOrder()
}
}