Skip to content

Commit 7a2ab4b

Browse files
EmilioBejasaclaude
andauthored
Add childCountTest and childCountScreenshotTest to investigate readiness indicator (#111)
Explores using childCount > 0 as the signal that Fabric has mounted children, followed by a re-layout pass before snapping the screenshot. Also investigates OnPreDrawListener as an alternative, which does not work with WindowAttachment.dispatchAttach (no real draw pipeline). Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 216e773 commit 7a2ab4b

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

android/app/src/androidTest/java/com/testapp/IsolatedTest.kt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.rnstorybookautoscreenshots
22

3+
import android.graphics.Color
4+
import android.view.View
35
import androidx.test.ext.junit.runners.AndroidJUnit4
46
import androidx.test.platform.app.InstrumentationRegistry
57
import com.testapp.MainApplication
@@ -9,6 +11,7 @@ import org.junit.Test
911
import org.junit.runner.RunWith
1012
import com.facebook.testing.screenshot.ViewHelpers
1113
import com.facebook.testing.screenshot.Screenshot
14+
import com.facebook.testing.screenshot.WindowAttachment
1215
import org.junit.Assert.*;
1316
import com.facebook.react.interfaces.*
1417

@@ -46,6 +49,81 @@ class IsolatedTest {
4649
Screenshot.snap(surface.view!!)
4750
.record()
4851
}
52+
53+
@Test
54+
fun childCountTest() {
55+
val instrumentation = InstrumentationRegistry.getInstrumentation()
56+
val context = instrumentation.targetContext
57+
val app = context.applicationContext as MainApplication
58+
val surface = app.reactHost.createSurface(context, "SimpleTestComponent", null)
59+
60+
val view = surface.view!!
61+
var detacher: WindowAttachment.Detacher? = null
62+
63+
try {
64+
instrumentation.runOnMainSync {
65+
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
66+
view.setBackgroundColor(Color.WHITE)
67+
detacher = WindowAttachment.dispatchAttach(view)
68+
app.reactHost.onHostResume(null)
69+
ViewHelpers.setupView(view).setExactWidthPx(1080).setExactHeightPx(1920).layout()
70+
surface.start()
71+
}
72+
73+
val deadline = System.currentTimeMillis() + 30_000
74+
var hasChildren = false
75+
while (!hasChildren && System.currentTimeMillis() < deadline) {
76+
Thread.sleep(50)
77+
instrumentation.runOnMainSync { hasChildren = view.childCount > 0 }
78+
}
79+
assertTrue("Timed out waiting for children", hasChildren)
80+
} finally {
81+
instrumentation.runOnMainSync {
82+
surface.stop()
83+
detacher?.detach()
84+
}
85+
}
86+
}
87+
88+
@Test
89+
fun childCountScreenshotTest() {
90+
val instrumentation = InstrumentationRegistry.getInstrumentation()
91+
val context = instrumentation.targetContext
92+
val app = context.applicationContext as MainApplication
93+
val surface = app.reactHost.createSurface(context, "SimpleTestComponent", null)
94+
95+
val view = surface.view!!
96+
var detacher: WindowAttachment.Detacher? = null
97+
98+
try {
99+
instrumentation.runOnMainSync {
100+
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
101+
view.setBackgroundColor(Color.WHITE)
102+
detacher = WindowAttachment.dispatchAttach(view)
103+
app.reactHost.onHostResume(null)
104+
ViewHelpers.setupView(view).setExactWidthPx(1080).setExactHeightPx(1920).layout()
105+
surface.start()
106+
}
107+
108+
val deadline = System.currentTimeMillis() + 30_000
109+
var hasChildren = false
110+
while (!hasChildren && System.currentTimeMillis() < deadline) {
111+
Thread.sleep(50)
112+
instrumentation.runOnMainSync { hasChildren = view.childCount > 0 }
113+
}
114+
assertTrue("Timed out waiting for children", hasChildren)
115+
116+
instrumentation.runOnMainSync {
117+
ViewHelpers.setupView(view).setExactWidthPx(1080).setExactHeightPx(1920).layout()
118+
Screenshot.snap(view).setName("childCountReadyTest").record()
119+
}
120+
} finally {
121+
instrumentation.runOnMainSync {
122+
surface.stop()
123+
detacher?.detach()
124+
}
125+
}
126+
}
49127
}
50128

51129
fun assertGoodTask(ti : TaskInterface<Void>) {

0 commit comments

Comments
 (0)