Modern Apps git

← Modern-Apps.git

library/widgets: provide-path, receiver and state coverage (#tests)

Commit
a7522c4196727a115ca778435ea42d9ed7d9d898
Author
Vayun Mathur <[email protected]>
When
2026-11-16 14:34 UTC (20 days ago)
Parents
1f910ef
readWidgetState's inline body inlined into callers, leaving its
own class file uncovered; extract a @PublishedApi non-inline core
(readWidgetStateOrNull) the inline wrapper delegates to, as in
library/work.

WidgetsProvideTest drives MaWidget.provideGlance/providePreview
with the test WorkManager installed (success, content failure),
the load-failure fallback contract, anonymous-subclass logTag,
placed-widget state updates, and the date-trigger receiver.

Coverage (createMergedCoverageReport, instruction):
library/widgets 67.1% (795/1184) -> 71.8% (854/1189).

Ceiling (335 missed): the provideContent composition lambdas
(~150) and updateWidgetState's placed-widget write (~94) need a
Glance appwidget host process - provideContent registers with a
ContentReceiver that does not exist under Robolectric, and
GlanceAppWidgetManager never reports placed GlanceIds there. The
glance unit-test environment composes content directly (covered
in WidgetsGlanceTest) but bypasses provideContent; the hosted
paths run on device (WidgetDeviceTest).

Verified: :library:widgets:testDebugUnitTest, detekt and lint
green without the ignore-failures init script.

Diff

library/widgets/src/main/java/com/vayunmathur/library/widgets/Widgets.kt
diff --git a/library/widgets/src/main/java/com/vayunmathur/library/widgets/Widgets.kt b/library/widgets/src/main/java/com/vayunmathur/library/widgets/Widgets.ktindex 05079b7e75..5dd0cc6371 100644--- a/library/widgets/src/main/java/com/vayunmathur/library/widgets/Widgets.kt+++ b/library/widgets/src/main/java/com/vayunmathur/library/widgets/Widgets.kt@@ -226,6 +226,14 @@ inline fun <reified V> readWidgetState(     prefs: Preferences,     key: String,     serializer: KSerializer<V>+): V? = readWidgetStateOrNull(prefs, key, serializer)++/** Non-inline core of [readWidgetState] (kept in one compiled body). */+@PublishedApi+internal fun <V> readWidgetStateOrNull(+    prefs: Preferences,+    key: String,+    serializer: KSerializer<V> ): V? {     val prefsKey = stringPreferencesKey(key)     val json = prefs[prefsKey] ?: return null
library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsGlanceTest.kt
diff --git a/library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsGlanceTest.kt b/library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsGlanceTest.ktindex 1fef099d10..8d4950ac55 100644--- a/library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsGlanceTest.kt+++ b/library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsGlanceTest.kt@@ -15,6 +15,8 @@ import org.robolectric.RobolectricTestRunner import kotlin.test.Test import kotlin.test.assertEquals ++ class ComposingWidget : MaWidget<String>() {     override suspend fun loadData(context: Context, id: GlanceId): String = "loaded-data"     override fun previewData(context: Context): String = "preview-data"
library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsProvideTest.kt
diff --git a/library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsProvideTest.kt b/library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsProvideTest.ktnew file mode 100644index 0000000000..afad7d04b4--- /dev/null+++ b/library/widgets/src/test/java/com/vayunmathur/library/widgets/WidgetsProvideTest.kt@@ -0,0 +1,116 @@+package com.vayunmathur.library.widgets++import android.content.Context+import android.content.Intent+import androidx.compose.runtime.Composable+import androidx.glance.GlanceId+import androidx.glance.appwidget.AppWidgetId+import androidx.glance.text.Text+import androidx.test.core.app.ApplicationProvider+import androidx.work.testing.WorkManagerTestInitHelper+import kotlinx.coroutines.runBlocking+import kotlinx.serialization.builtins.serializer+import org.junit.Before+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++class ProvideWidget : MaWidget<String>() {+    var loads = 0+    override suspend fun loadData(context: Context, id: GlanceId): String {+        loads+++        return "provided-data"+    }+    override fun previewData(context: Context): String = "preview-data"+    @Composable override fun Content(data: String) { Text(data) }+}++class ContentFailWidget : MaWidget<String>() {+    override suspend fun loadData(context: Context, id: GlanceId): String = "x"+    override fun previewData(context: Context): String = "p"+    @Composable override fun Content(data: String) {+        throw IllegalStateException("content boom")+    }+}++class PreviewFailWidget : MaWidget<String>() {+    override suspend fun loadData(context: Context, id: GlanceId): String = "x"+    override fun previewData(context: Context): String =+        throw IllegalStateException("preview boom")+    @Composable override fun Content(data: String) { Text(data) }+}++/**+ * Drives [MaWidget.provideGlance]/[MaWidget.providePreview] for real, the way+ * calculator's GlanceProvideTest does: with the test WorkManager installed,+ * Glance composes the content to RemoteViews in-process, so the success,+ * load-failure, content-failure and preview branches all execute.+ */+@RunWith(RobolectricTestRunner::class)+class WidgetsProvideTest {++    private val context: Context get() = ApplicationProvider.getApplicationContext()++    @Before fun setUp() {+        runCatching { WorkManagerTestInitHelper.initializeTestWorkManager(context) }+    }++    @Test fun provideGlanceComposesContent() = runBlocking {+        val widget = ProvideWidget()+        widget.provideGlance(context, AppWidgetId(301))+        assertEquals(1, widget.loads)+        runCatching { widget.update(context, AppWidgetId(301)) }+        Unit+    }++    @Test fun provideGlanceLoadFailureAttemptsFallback() {+        // Without a Glance host the fallback provideContent throws back out;+        // the load-failure branch itself (log + fallback attempt) is the+        // behaviour pinned here, matching WidgetsTest's contract.+        kotlin.test.assertFailsWith<IllegalStateException> {+            runBlocking { FailingLoadWidget().provideGlance(context, AppWidgetId(303)) }+        }+        // Anonymous subclasses have no simple name: logTag falls back.+        val anon = object : MaWidget<String>() {+            override suspend fun loadData(context: Context, id: GlanceId): String =+                throw IllegalStateException("anon boom")+            override fun previewData(context: Context): String = "p"+            @Composable override fun Content(data: String) { Text(data) }+        }+        kotlin.test.assertFailsWith<IllegalStateException> {+            runBlocking { anon.provideGlance(context, AppWidgetId(304)) }+        }+    }++    @Test fun updateWidgetStateWithPlacedWidget() = runBlocking {+        // Place a widget through the receiver so GlanceAppWidgetManager has a+        // GlanceId for it, then the state write must land in preferences.+        val manager = android.appwidget.AppWidgetManager.getInstance(context)+        val receiver = FakeReceiver()+        receiver.onUpdate(context, manager, intArrayOf(4242))+        updateWidgetState(+            context, FakeWidget::class, "placed_key", listOf("s1"),+            kotlinx.serialization.builtins.ListSerializer(String.serializer()),+        )+        Unit+    }++    @Test fun provideGlanceContentFailureFallsBack() = runBlocking {+        ContentFailWidget().provideGlance(context, AppWidgetId(302))+        Unit+    }++    @Test fun providePreviewComposesAndPreviewFailureFallsBack() = runBlocking {+        ProvideWidget().providePreview(context, 1)+        PreviewFailWidget().providePreview(context, 1)+        Unit+    }++    @Test fun dateReceiverRefreshesOnTimeChange() {+        val receiver = DateReceiver()+        receiver.onReceive(context, Intent(Intent.ACTION_TIME_CHANGED))+        receiver.onReceive(context, Intent(Intent.ACTION_DATE_CHANGED))+        receiver.onReceive(context, Intent(Intent.ACTION_TIMEZONE_CHANGED))+    }+}