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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.squareup.workflow1.ui

import android.content.res.Resources.NotFoundException
import android.view.View
import android.view.View.NO_ID
import androidx.lifecycle.ViewTreeLifecycleOwner
Expand Down Expand Up @@ -95,8 +96,13 @@ private fun View.ensureAttachedScope(): AttachedScope {
val coroutineName = buildString {
append("${view::class.java.name}@${view.hashCode()}")
if (view.id != NO_ID) {
append('-')
append(resources.getResourceEntryName(view.id))
try {
val name = resources.getResourceEntryName(view.id)
append('-')
append(name)
} catch (e: NotFoundException) {
// Ignore. It's just a debugging name, who cares.
}
}
}.let(::CoroutineName)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.squareup.workflow1.ui

import android.content.res.Resources.NotFoundException
import android.view.View
import android.view.View.OnAttachStateChangeListener
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY
Expand Down Expand Up @@ -177,6 +178,34 @@ internal class ViewLaunchWhenAttachedTest {
assertThat(coroutineName).contains("${view.hashCode()}")
}

@Test fun `launchWhenAttached includes view id name in coroutine name`() {
var coroutineName: String? = null
mockAttachedToWindow(view, true)
whenever(view.resources.getResourceEntryName(anyInt())).thenReturn("fnord")

// Action: launch coroutine!
view.launchWhenAttached {
coroutineName = coroutineContext[CoroutineName]?.name
}

assertThat(coroutineName).contains("fnord")
}

@Test fun `launchWhenAttached tolerates garbage ids`() {
var coroutineName: String? = null
mockAttachedToWindow(view, true)
whenever(view.resources.getResourceEntryName(anyInt())).thenThrow(NotFoundException())

// Action: launch coroutine!
view.launchWhenAttached {
coroutineName = coroutineContext[CoroutineName]?.name
}

assertThat(coroutineName).isNotNull()
assertThat(coroutineName).contains("android.view.View")
assertThat(coroutineName).contains("${view.hashCode()}")
}

private fun performViewAttach() {
mockAttachedToWindow(view, true)
verify(view).addOnAttachStateChangeListener(onAttachStateChangeListener.capture())
Expand Down