Skip to content

Commit

Permalink
Fix preview dialog problem(not working yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Nov 15, 2024
1 parent 2c626c4 commit 17ef64c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ class CustomPreviewTester : ComposePreviewTester<AndroidPreviewInfo> by AndroidC
composeTestRule.setContent {
preview()
}
composeTestRule.onRoot().captureRoboImage("${roborazziSystemPropertyOutputDirectory()}/${preview.methodName}.png")
composeTestRule.onRoot().captureRoboImage("${roborazziSystemPropertyOutputDirectory()}/${preview.methodName}.${provideRoborazziContext().imageExtension}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class AndroidComposePreviewTester : ComposePreviewTester<AndroidPreviewInfo> {
preview.declaringClass,
createScreenshotIdFor(preview)
)
val filePath = "$pathPrefix$name.png"
val filePath = "$pathPrefix$name.${provideRoborazziContext().imageExtension}"
preview.captureRoboImage(filePath)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewRootForTest
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso
import org.robolectric.Shadows
import java.io.File

Expand All @@ -31,15 +32,26 @@ fun captureRoboImage(
) {
if (!roborazziOptions.taskType.isEnabled()) return
registerRoborazziActivityToRobolectricIfNeeded()
// Views needs to be laid out before we can capture them
Espresso.onIdle()

val activityScenario = ActivityScenario.launch(RoborazziTransparentActivity::class.java)
activityScenario.use {
activityScenario.onActivity { activity ->
activity.setContent(content = content)
val composeView = activity.window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.getChildAt(0) as ComposeView
val viewRootForTest = composeView.getChildAt(0) as ViewRootForTest
viewRootForTest.view.captureRoboImage(file, roborazziOptions)
val windowRoots = fetchRobolectricWindowRoots()
if (windowRoots.size <= 1) {
val composeView = activity.window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.getChildAt(0) as ComposeView
val viewRootForTest = composeView.getChildAt(0) as ViewRootForTest
viewRootForTest.view.captureRoboImage(file, roborazziOptions)
} else {
// For dialog
// windowRoots[1].decorView.rootView.captureRoboImage(file, roborazziOptions)
// captureScreenRoboImage()
captureRootsInternal(windowRoots.drop(1), roborazziOptions, file)
}
}

// Closing the activity is necessary to prevent memory leaks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -169,6 +173,24 @@ fun captureScreenRoboImage(
}
}

@InternalRoborazziApi
fun fetchRobolectricWindowRoots(): List<Root> {
try {
@Suppress("INACCESSIBLE_TYPE") 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>
).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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.takahirom.sample
package com.github.takahirom

import org.junit.Assert.assertEquals
import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
Expand Down

0 comments on commit 17ef64c

Please sign in to comment.