-
Notifications
You must be signed in to change notification settings - Fork 38
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
Fix alert dialog support #555
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2c626c4
Add alert dialog test
takahirom 17ef64c
Fix preview dialog problem(not working yet)
takahirom f93fa2a
Merge branch 'main' into takahirom/add-alertdialog-test/2024-11-15
takahirom 434cc8e
Merge branch 'takahirom/refactor-test/2024-12-12' into takahirom/add-…
takahirom 968b5e9
Merge branch 'takahirom/refactor-test/2024-12-12' into takahirom/add-…
takahirom b648545
Merge branch 'main' into takahirom/add-alertdialog-test/2024-11-15
takahirom c6e8413
Use captureScreenRoboImage for dialog
takahirom 0b22865
Add PreviewDialogSurface
takahirom 3bb4ce4
Add captureScreenIfMultipleWindows
takahirom d766c55
Remove Espresso.onIdle for now (it seems no effect now)
takahirom 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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -144,18 +144,22 @@ fun captureScreenRoboImage( | |
// Views needs to be laid out before we can capture them | ||
Espresso.onIdle() | ||
|
||
val rootsOracle = RootsOracle_Factory({ Looper.getMainLooper() }) | ||
.get() | ||
// Invoke rootOracle.listActiveRoots() via reflection | ||
val listActiveRoots = rootsOracle.javaClass.getMethod("listActiveRoots") | ||
listActiveRoots.isAccessible = true | ||
@Suppress("UNCHECKED_CAST") val roots: List<Root> = listActiveRoots.invoke(rootsOracle) as List<Root> | ||
val roots: List<Root> = fetchRobolectricWindowRoots() | ||
debugLog { | ||
"captureScreenRoboImage roots: ${roots.joinToString("\n") { it.toString() }}" | ||
} | ||
captureRootsInternal(roots, roborazziOptions, file) | ||
} | ||
|
||
@InternalRoborazziApi | ||
fun captureRootsInternal( | ||
roots: List<Root>, | ||
roborazziOptions: RoborazziOptions, | ||
file: File | ||
) { | ||
capture( | ||
rootComponent = RoboComponent.Screen( | ||
rootsOrderByDepth = roots.sortedBy { it.windowLayoutParams.get()?.type }, | ||
rootsOrderByDepth = roots, | ||
roborazziOptions = roborazziOptions | ||
), | ||
roborazziOptions = roborazziOptions, | ||
|
@@ -169,6 +173,43 @@ fun captureScreenRoboImage( | |
} | ||
} | ||
|
||
@InternalRoborazziApi | ||
fun captureScreenIfMultipleWindows( | ||
file: File, | ||
roborazziOptions: RoborazziOptions, | ||
captureSingleComponent: () -> Unit | ||
) { | ||
if (fetchRobolectricWindowRoots().size > 1) { | ||
roborazziReportLog( | ||
"It seems that there are multiple windows." + | ||
"We capture all windows using captureScreenRoboImage(). " + | ||
"We can add a flag to disable this behavior so please let us know if you need it." | ||
) | ||
captureScreenRoboImage(file, roborazziOptions) | ||
} else { | ||
captureSingleComponent() | ||
} | ||
} | ||
|
||
@InternalRoborazziApi | ||
fun fetchRobolectricWindowRoots(): List<Root> { | ||
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. This is the method we use for |
||
try { | ||
@Suppress("INACCESSIBLE_TYPE") val rootsOracle = RootsOracle_Factory({ Looper.getMainLooper() }) | ||
.get() | ||
// Invoke rootOracle.listActiveRoots() via reflection | ||
@Suppress("INACCESSIBLE_TYPE") val listActiveRoots = | ||
rootsOracle.javaClass.getMethod("listActiveRoots") | ||
listActiveRoots.isAccessible = true | ||
@Suppress("UNCHECKED_CAST", "INACCESSIBLE_TYPE") val roots: List<Root> = | ||
(listActiveRoots.invoke(rootsOracle) as List<Root> | ||
).sortedBy { it.windowLayoutParams.get()?.type } | ||
return roots | ||
} catch (e: Throwable) { | ||
e.printStackTrace() | ||
return emptyList() | ||
} | ||
} | ||
|
||
fun Bitmap.captureRoboImage( | ||
filePath: String = DefaultFileNameGenerator.generateFilePath(), | ||
roborazziOptions: RoborazziOptions = provideRoborazziContext().options, | ||
|
@@ -278,20 +319,26 @@ fun SemanticsNodeInteraction.captureRoboImage( | |
roborazziOptions: RoborazziOptions = provideRoborazziContext().options, | ||
) { | ||
if (!roborazziOptions.taskType.isEnabled()) return | ||
capture( | ||
rootComponent = RoboComponent.Compose( | ||
node = this.fetchSemanticsNode("fail to captureRoboImage"), | ||
roborazziOptions = roborazziOptions | ||
), | ||
captureScreenIfMultipleWindows( | ||
file = file, | ||
roborazziOptions = roborazziOptions, | ||
) { canvas -> | ||
processOutputImageAndReportWithDefaults( | ||
canvas = canvas, | ||
goldenFile = file, | ||
roborazziOptions = roborazziOptions | ||
) | ||
canvas.release() | ||
} | ||
captureSingleComponent = { | ||
capture( | ||
rootComponent = RoboComponent.Compose( | ||
node = this.fetchSemanticsNode("fail to captureRoboImage"), | ||
roborazziOptions = roborazziOptions | ||
), | ||
roborazziOptions = roborazziOptions, | ||
) { canvas -> | ||
processOutputImageAndReportWithDefaults( | ||
canvas = canvas, | ||
goldenFile = file, | ||
roborazziOptions = roborazziOptions | ||
) | ||
canvas.release() | ||
} | ||
} | ||
) | ||
} | ||
|
||
fun SemanticsNodeInteraction.captureRoboGif( | ||
|
This file contains 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
5 changes: 2 additions & 3 deletions
5
...ithub/takahirom/sample/ExampleUnitTest.kt → ...a/com/github/takahirom/ExampleUnitTest.kt
This file contains 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
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.
@sergio-sastre
Thanks as always. I'm thinking about adding support for the
AlertDialog()
Composable.Currently, it throws an
IllegalStateException
, and I would like to fix that behavior.The basic strategy here is to use
captureScreenRoboImage()
(which merges all windows into one screenshot) if there are multiple windows.Do you have any thought on this?
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.
@takahirom
Oh sorry, I‘ve just seen this and also saw that it was merged.
I think it is fine as first solution.
Getting it right with the windows is a tricky one.
Have you tried by converting the corresponding ComposeView to bitmap and capturing a bitmap from it? That would likely avoid the IllegalStateException. However, I am not sure whether we could use PixelCopy to draw the bitmap with elevation.
what I mean is to use sth like this
composeView.drawToBitmap().captureRoboImage()
maybe worth giving it a try?