Improve require error messages
Makes it easier to diagnose test failures like the one in the bug.
Bug: 179457097
Test: N/A
Change-Id: Ie1c8b104a5c1f6ec87e5083fc86eed7c8f47ac1e
diff --git a/wear/wear-watchface-editor/api/restricted_current.txt b/wear/wear-watchface-editor/api/restricted_current.txt
index bc26a15..3213be4 100644
--- a/wear/wear-watchface-editor/api/restricted_current.txt
+++ b/wear/wear-watchface-editor/api/restricted_current.txt
@@ -11,6 +11,7 @@
method public final kotlinx.coroutines.CoroutineScope getCoroutineScope();
method public suspend Object? launchComplicationProviderChooser(int p, kotlin.coroutines.Continuation<? super java.lang.Boolean> $completion);
method protected abstract void releaseResources();
+ method protected final void requireNotClosed();
method protected final void setClosed(boolean p);
property public Integer? backgroundComplicationId;
property protected final boolean closed;
diff --git a/wear/wear-watchface-editor/src/main/java/androidx/wear/watchface/editor/EditorSession.kt b/wear/wear-watchface-editor/src/main/java/androidx/wear/watchface/editor/EditorSession.kt
index 755c1e7..3d8a39b 100644
--- a/wear/wear-watchface-editor/src/main/java/androidx/wear/watchface/editor/EditorSession.kt
+++ b/wear/wear-watchface-editor/src/main/java/androidx/wear/watchface/editor/EditorSession.kt
@@ -285,7 +285,7 @@
}
override suspend fun launchComplicationProviderChooser(complicationId: Int): Boolean {
- require(!closed)
+ requireNotClosed()
require(!complicationState[complicationId]!!.fixedComplicationProvider) {
"Can't configure fixed complication ID $complicationId"
}
@@ -298,14 +298,14 @@
}
override val backgroundComplicationId: Int? by lazy {
- require(!closed)
+ requireNotClosed()
complicationState.entries.firstOrNull {
it.value.boundsType == ComplicationBoundsType.BACKGROUND
}?.key
}
override fun getComplicationIdAt(@Px x: Int, @Px y: Int): Int? {
- require(!closed)
+ requireNotClosed()
return complicationState.entries.firstOrNull {
it.value.isEnabled && when (it.value.boundsType) {
ComplicationBoundsType.ROUND_RECT -> it.value.bounds.contains(x, y)
@@ -384,7 +384,7 @@
}
override fun close() {
- require(!closed)
+ requireNotClosed()
coroutineScope.launch {
val editorState = EditorStateWireFormat(
instanceId,
@@ -403,6 +403,12 @@
}
}
+ protected fun requireNotClosed() {
+ require(!closed) {
+ "EditorSession method called after close()"
+ }
+ }
+
protected abstract fun releaseResources()
}
@@ -417,7 +423,7 @@
private lateinit var editorDelegate: WatchFace.EditorDelegate
override val userStyleSchema by lazy {
- require(!closed)
+ requireNotClosed()
editorDelegate.userStyleRepository.schema
}
@@ -425,7 +431,7 @@
override val complicationState
get() = editorDelegate.complicationsManager.complications.mapValues {
- require(!closed)
+ requireNotClosed()
ComplicationState(
it.value.computeBounds(editorDelegate.screenBounds),
it.value.boundsType,
@@ -445,14 +451,14 @@
// side effects (it would apply to the active watch face).
override var userStyle: UserStyle
get() {
- require(!closed)
+ requireNotClosed()
if (_userStyle == null) {
_userStyle = UserStyle(editorDelegate.userStyleRepository.userStyle)
}
return _userStyle!!
}
set(value) {
- require(!closed)
+ requireNotClosed()
_userStyle = value
editorDelegate.userStyleRepository.userStyle = UserStyle(value)
}
@@ -464,7 +470,7 @@
calendarTimeMillis: Long,
idToComplicationData: Map<Int, ComplicationData>?
): Bitmap {
- require(!closed)
+ requireNotClosed()
return editorDelegate.takeScreenshot(
renderParameters,
calendarTimeMillis,
@@ -519,7 +525,7 @@
calendarTimeMillis: Long,
idToComplicationData: Map<Int, ComplicationData>?
): Bitmap {
- require(!closed)
+ requireNotClosed()
return headlessWatchFaceClient.takeWatchFaceScreenshot(
renderParameters,
100,
diff --git a/wear/wear-watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt b/wear/wear-watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt
index 255ef08..7618adc 100644
--- a/wear/wear-watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt
+++ b/wear/wear-watchface/src/main/java/androidx/wear/watchface/WatchFaceService.kt
@@ -294,7 +294,9 @@
if (destroyed) {
return
}
- require(allowWatchfaceToAnimate)
+ require(allowWatchfaceToAnimate) {
+ "Choreographer doFrame called but allowWatchfaceToAnimate is false"
+ }
frameCallbackPending = false
draw()
}
@@ -759,7 +761,7 @@
suspend fun createHeadlessInstance(
params: HeadlessWatchFaceInstanceParams
): HeadlessWatchFaceImpl {
- require(!watchFaceCreated())
+ require(!watchFaceCreated()) { "WatchFace already exists!" }
setImmutableSystemState(params.deviceConfig)
// Fake SurfaceHolder with just enough methods implemented for headless rendering.
@@ -838,7 +840,7 @@
suspend fun createInteractiveInstance(
params: WallpaperInteractiveWatchFaceInstanceParams
): InteractiveWatchFaceImpl {
- require(!watchFaceCreated())
+ require(!watchFaceCreated()) { "WatchFace already exists!" }
setImmutableSystemState(params.deviceConfig)
setSystemState(params.systemState)