26 lines · 1.1 KB · KotlinRaw
package com.cottondb.androidimport android.graphics.Bitmapimport androidx.test.platform.app.InstrumentationRegistryimport java.io.File/** Opt-in review artifacts, never image comparisons or snapshot assertions. */object ScreenEvidence { fun capture(name: String, render: () -> Bitmap) { if (InstrumentationRegistry.getArguments().getString("cotton.screenshots") != "true") return require(name.matches(Regex("[a-z0-9-]+"))) val instrumentation = InstrumentationRegistry.getInstrumentation() // Capture the rendered Compose root. The emulator's system screenshot can // return an older SurfaceFlinger buffer even after semantics and frames settle. val image = render() try { val directory = File(instrumentation.targetContext.filesDir, "screen-evidence") check(directory.mkdirs() || directory.isDirectory) File(directory, "$name.png").outputStream().use { check(image.compress(Bitmap.CompressFormat.PNG, 100, it)) } } finally { image.recycle() } }}