Skip to content

Commit

Permalink
wip: add logging to figure out why dialog is not getting focus
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrjr committed Apr 3, 2024
1 parent 6339d63 commit 677a736
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.Window.Callback
import androidx.test.core.app.ActivityScenario
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Test
import java.util.concurrent.atomic.AtomicReference
import radiography.Radiography
Expand Down Expand Up @@ -68,8 +69,8 @@ class RadiographyUiTest {

val hierarchy = scenario.runOnActivity {
Radiography.scan(
scanScope = FocusedWindowScope,
viewStateRenderers = DefaultsIncludingPii
scanScope = FocusedWindowScope,
viewStateRenderers = DefaultsIncludingPii
)
}

Expand Down Expand Up @@ -113,19 +114,27 @@ class RadiographyUiTest {
/**
* Waits for the activity to lose focus and the dialog to gain focus.
*/
private fun waitForFocus(dialog: Dialog, activity: Activity) {
private fun waitForFocus(
dialog: Dialog,
activity: Activity
) {
val dialogFocused = CountDownLatch(2)
val stringBuffer = StringBuffer()

getInstrumentation().runOnMainSync {
val activityHasWindowFocus = activity.hasWindowFocus()
val dialogHasWindowFocus = dialog.window!!.peekDecorView()?.hasWindowFocus() ?: false

if (!activityHasWindowFocus) {
stringBuffer.appendLine("activity not focused")
dialogFocused.countDown()
} else {
stringBuffer.appendLine("activity focused")
val activityWindow = activity.window
val delegateCallback = activityWindow.callback
activityWindow.callback = object : Callback by delegateCallback {
override fun onWindowFocusChanged(hasFocus: Boolean) {
stringBuffer.appendLine("activity focus change: $hasFocus")
delegateCallback.onWindowFocusChanged(hasFocus)
if (!hasFocus) {
dialogFocused.countDown()
Expand All @@ -135,11 +144,14 @@ class RadiographyUiTest {
}

if (dialogHasWindowFocus) {
stringBuffer.appendLine("dialog focused")
dialogFocused.countDown()
} else {
stringBuffer.appendLine("dialog not focused")
val delegateCallback = dialog.window!!.callback
dialog.window!!.callback = object : Callback by delegateCallback {
override fun onWindowFocusChanged(hasFocus: Boolean) {
stringBuffer.appendLine("dialog focus change: $hasFocus")
delegateCallback.onWindowFocusChanged(hasFocus)
if (hasFocus) {
dialogFocused.countDown()
Expand All @@ -148,6 +160,8 @@ class RadiographyUiTest {
}
}
}
assertThat(dialogFocused.await(10, SECONDS)).isTrue()

assertWithMessage("Dialog $dialog is focused: $stringBuffer")
.that(dialogFocused.await(10, SECONDS)).isTrue()
}
}

0 comments on commit 677a736

Please sign in to comment.