|
| 1 | +package fixture.composenative |
| 2 | + |
| 3 | +import androidx.compose.ui.ImageComposeScene |
| 4 | +import androidx.compose.ui.unit.Density |
| 5 | +import org.jetbrains.skia.Bitmap |
| 6 | +import python.multiplatform.ffi.Python3 |
| 7 | +import python.multiplatform.ffi.exceptions.PyException |
| 8 | +import python.multiplatform.ffi.pythonx.PythonxAdapter |
| 9 | +import python.multiplatform.ffi.upcall.UpcallBootstrap |
| 10 | +import python.multiplatform.generated.artifacts.ArtifactTable |
| 11 | +import python.multiplatform.reflection.UpcallTable |
| 12 | +import kotlin.test.AfterTest |
| 13 | +import kotlin.test.BeforeTest |
| 14 | +import kotlin.test.Test |
| 15 | +import kotlin.test.assertEquals |
| 16 | +import kotlin.test.assertTrue |
| 17 | + |
| 18 | +/** |
| 19 | + * #191 on Kotlin/Native desktop: Python draws material3 composables whose bindings come from the |
| 20 | + * walked klibs. `Text` (about 15 defaulted parameters) and `Button` (8) are past B1's limit of 6, so |
| 21 | + * each call is the B3 compiler plugin's rewrite; if the plugin did not run, the marker function in |
| 22 | + * the generated fragment throws on the first call and these tests fail with its message. |
| 23 | + */ |
| 24 | +class NativeComposableRenderTest { |
| 25 | + |
| 26 | + @BeforeTest |
| 27 | + fun install() { |
| 28 | + Python3.initialize(silent = true) |
| 29 | + UpcallTable.clear() |
| 30 | + UpcallTable.install(ArtifactTable.fragments) |
| 31 | + check(UpcallBootstrap.publishToGlobals()) { "UpcallBootstrap.publishToGlobals() failed" } |
| 32 | + PythonxAdapter.install(setOf("androidx.compose.ui.unit.Dp")) |
| 33 | + } |
| 34 | + |
| 35 | + @AfterTest |
| 36 | + fun cleanup() { |
| 37 | + UpcallTable.clear() |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun pythonDrawsAMaterial3TextThroughTheCompilerPlugin() { |
| 42 | + val drawn = inkOf("from androidx.compose.material3 import Text\nText('hi')") |
| 43 | + val blank = inkOf("pass") |
| 44 | + println("compose-native render: Text('hi') -> $drawn px, empty body -> $blank px") |
| 45 | + assertEquals(0, blank, "an empty composition must draw nothing, or the measurement is not measuring") |
| 46 | + assertTrue(drawn > 0, "Text('hi') drew nothing") |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun moreTextIsMoreInk() { |
| 51 | + val short = inkOf("from androidx.compose.material3 import Text\nText('hi')") |
| 52 | + val long = inkOf("from androidx.compose.material3 import Text\nText('hi hi hi hi hi')") |
| 53 | + assertTrue(long > short, "expected more ink for more text: $short vs $long") |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun aButtonWithAPythonContentLambdaDraws() { |
| 58 | + val drawn = inkOf( |
| 59 | + "from androidx.compose.material3 import Button, Text\n" + |
| 60 | + "Button(onClick=lambda: None, content=lambda: Text('ok'))", |
| 61 | + ) |
| 62 | + assertTrue(drawn > 0, "Button(...) drew nothing") |
| 63 | + } |
| 64 | + |
| 65 | + private fun inkOf(body: String): Int { |
| 66 | + val scene = ImageComposeScene(width = 200, height = 60, density = Density(1f)) { |
| 67 | + PythonComposition(body) |
| 68 | + } |
| 69 | + try { |
| 70 | + val image = try { |
| 71 | + scene.render() |
| 72 | + } catch (e: PyException) { |
| 73 | + throw AssertionError("Python raised ${e.type?.name}: ${e.message}, running:\n$body", e) |
| 74 | + } |
| 75 | + val bitmap = Bitmap.makeFromImage(image) |
| 76 | + var ink = 0 |
| 77 | + for (y in 0 until bitmap.height) { |
| 78 | + for (x in 0 until bitmap.width) { |
| 79 | + if (bitmap.getColor(x, y) != 0) ink++ |
| 80 | + } |
| 81 | + } |
| 82 | + return ink |
| 83 | + } finally { |
| 84 | + scene.close() |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments