11package com.rnstorybookautoscreenshots
22
3+ import android.graphics.Color
4+ import android.view.View
35import androidx.test.ext.junit.runners.AndroidJUnit4
46import androidx.test.platform.app.InstrumentationRegistry
57import com.testapp.MainApplication
@@ -9,6 +11,7 @@ import org.junit.Test
911import org.junit.runner.RunWith
1012import com.facebook.testing.screenshot.ViewHelpers
1113import com.facebook.testing.screenshot.Screenshot
14+ import com.facebook.testing.screenshot.WindowAttachment
1215import org.junit.Assert.*;
1316import 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
51129fun assertGoodTask (ti : TaskInterface <Void >) {
0 commit comments