-
Notifications
You must be signed in to change notification settings - Fork 1
test: screenshot plain Android view in IsolatedTest #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EmilioBejasa
wants to merge
4
commits into
main
Choose a base branch
from
screenshot-isolated-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
772f046
test: screenshot plain Android view in IsolatedTest
EmilioBejasa 3704bbb
fix: measure and layout plain view before screenshotting
EmilioBejasa 550baf6
test: screenshot SimpleTestComponent React Native surface in Isolated…
EmilioBejasa 0bebcca
fix: use proper Fabric wait and recursive software layer in screensho…
EmilioBejasa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,34 @@ | ||
| package com.rnstorybookautoscreenshots | ||
|
|
||
| import android.Manifest | ||
| import android.graphics.PixelFormat | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.view.Choreographer | ||
| import android.view.ContextThemeWrapper | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.view.WindowManager | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import androidx.test.rule.GrantPermissionRule | ||
| import com.facebook.testing.screenshot.Screenshot | ||
| import com.testapp.MainApplication | ||
| import junit.framework.TestCase.assertNotNull | ||
| import junit.framework.TestCase.assertTrue | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import java.util.concurrent.CountDownLatch | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class IsolatedTest { | ||
|
|
||
| @get:Rule | ||
| val permissionRule: GrantPermissionRule = GrantPermissionRule.grant( | ||
| Manifest.permission.SYSTEM_ALERT_WINDOW | ||
| ) | ||
|
|
||
| @Test | ||
| fun simpleTest() { | ||
| assertTrue(true) | ||
|
|
@@ -23,4 +42,71 @@ class IsolatedTest { | |
| surface.start() | ||
| assertNotNull(surface.view) | ||
| } | ||
|
|
||
| @Test | ||
| fun screenshotSimpleTestComponent() { | ||
| val instrumentation = InstrumentationRegistry.getInstrumentation() | ||
| val context = instrumentation.targetContext | ||
| val app = context.applicationContext as MainApplication | ||
|
|
||
| val themedContext = ContextThemeWrapper(context, context.applicationInfo.theme) | ||
| val surface = app.reactHost.createSurface(themedContext, "SimpleTestComponent", null) | ||
| val view = surface.view | ||
| ?: throw IllegalStateException("ReactSurface returned a null view") | ||
|
|
||
| val wm = context.getSystemService(android.content.Context.WINDOW_SERVICE) as WindowManager | ||
| val params = WindowManager.LayoutParams( | ||
| 1080, | ||
| 1920, | ||
| WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, | ||
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, | ||
| PixelFormat.TRANSLUCENT | ||
| ).apply { alpha = 0f } | ||
|
|
||
| instrumentation.runOnMainSync { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we also specifically said we wanted to do everything without inter-thread synchronization. There can be one runOnMainSync, but all the code needs to be in it. |
||
| view.setLayerType(View.LAYER_TYPE_SOFTWARE, null) | ||
| wm.addView(view, params) | ||
| surface.start() | ||
| } | ||
|
|
||
| // Wait for JS to load and Fabric to mount children (up to 30s for cold start) | ||
| val deadline = System.currentTimeMillis() + 30_000L | ||
| while (System.currentTimeMillis() < deadline) { | ||
| waitTwoFrames() | ||
| var childCount = 0 | ||
| instrumentation.runOnMainSync { childCount = view.childCount } | ||
| if (childCount > 0) break | ||
| } | ||
|
|
||
| instrumentation.runOnMainSync { | ||
| // Child views added by Fabric default to hardware acceleration; | ||
| // set software layer recursively so Screenshot.snap() can capture them. | ||
| setLayerTypeSoftwareRecursively(view) | ||
| Screenshot.snap(view).setName("simple_test_component").record() | ||
| } | ||
|
|
||
| instrumentation.runOnMainSync { | ||
| surface.stop() | ||
| wm.removeView(view) | ||
| } | ||
| } | ||
|
|
||
| private fun setLayerTypeSoftwareRecursively(view: View) { | ||
| view.setLayerType(View.LAYER_TYPE_SOFTWARE, null) | ||
| if (view is ViewGroup) { | ||
| for (i in 0 until view.childCount) { | ||
| setLayerTypeSoftwareRecursively(view.getChildAt(i)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun waitTwoFrames() { | ||
| repeat(2) { | ||
| val latch = CountDownLatch(1) | ||
| Handler(Looper.getMainLooper()).post { | ||
| Choreographer.getInstance().postFrameCallback { latch.countDown() } | ||
| } | ||
| latch.await() | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're going in circles, I think we specifically said we didn't want to do it using WindowManager.