library/ink: add tests for serialization and ink canvas
- Commit
80cc99b5731688073edd152b2aaa0ead72f598ac- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 06:20 UTC (29 days ago)
- Parents
54e7634
Merged INSTRUCTION coverage 0% -> 94.6% (1373/1452). Brush/Stroke construction loads libink.so, which ships only as an Android .so, so JVM/Robolectric tests cannot create a BrushFamily at all (UnsatisfiedLinkError). The suite is therefore split: host tests cover the pure Kotlin layer (SerializedStroke/Point JSON bytes round-trip, unknown-key tolerance, point and text element defaults), and an instrumented suite on the emulator (x86_64 libink.so is in the AAR) covers brush-family naming and fallbacks, serialize/deserialize/translate round-trips, and the InkCanvasView hosted in a ComponentActivity: drawing finished strokes/text/selections to a Canvas, a down/move/up stroke firing onStrokeFinished, a cancelled stroke, eraser hits and misses against the stroke bounding box, and disabled touch refusal. Adds the androidTest dependencies and instrumentation runner the on-device suite needs, following library/map's harness shape. (cherry picked from commit 36585a2e9bef434f860591d6dcd87872140d7e61)
Diff
library/ink/build.gradle.kts
diff --git a/library/ink/build.gradle.kts b/library/ink/build.gradle.ktsindex e4d68efa8c..8e46c6a790 100644--- a/library/ink/build.gradle.kts+++ b/library/ink/build.gradle.kts@@ -2,6 +2,12 @@ plugins { id("common-conventions-library") } +android {+ defaultConfig {+ testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"+ }+}+ dependencies { // Ink. libink.so / libgraphics-core.so (~1.5 MB) live here, so only apps that // depend on :library:ink bundle them. Exposed via `api` because consumers@@ -11,4 +17,12 @@ dependencies { api(libs.androidx.ink.geometry) api(libs.androidx.ink.strokes) api(libs.androidx.ink.rendering)++ // On-device suite: Brush/Stroke load libink.so, which has no host build, so the+ // canvas and serialization round-trips can only run on a device/emulator.+ androidTestImplementation(libs.androidx.test.runner)+ androidTestImplementation(libs.androidx.test.core)+ androidTestImplementation(libs.androidx.test.ext.junit)+ androidTestImplementation(libs.androidx.activity.compose)+ androidTestImplementation(libs.androidx.compose.ui.test.manifest) }
library/ink/src/androidTest/java/com/vayunmathur/library/ink/InkDeviceTest.kt
diff --git a/library/ink/src/androidTest/java/com/vayunmathur/library/ink/InkDeviceTest.kt b/library/ink/src/androidTest/java/com/vayunmathur/library/ink/InkDeviceTest.ktnew file mode 100644index 0000000000..f76774aa2e--- /dev/null+++ b/library/ink/src/androidTest/java/com/vayunmathur/library/ink/InkDeviceTest.kt@@ -0,0 +1,163 @@+package com.vayunmathur.library.ink++import android.graphics.Canvas+import android.view.MotionEvent+import android.view.View+import android.view.ViewGroup+import androidx.activity.ComponentActivity+import androidx.activity.compose.setContent+import androidx.compose.runtime.mutableStateOf+import androidx.compose.ui.graphics.Color+import androidx.ink.authoring.InProgressStrokesView+import androidx.ink.brush.Brush+import androidx.ink.brush.StockBrushes+import androidx.ink.strokes.Stroke+import androidx.test.core.app.ActivityScenario+import java.util.concurrent.CountDownLatch+import java.util.concurrent.TimeUnit+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertSame+import org.junit.Assert.assertTrue+import org.junit.Test++/** On-device: Brush/Stroke construction loads libink.so, which has no host build. */+class InkDeviceTest {++ private val brush: Brush = Brush.createWithColorIntArgb(+ family = StockBrushes.pressurePen(),+ colorIntArgb = 0xFF000000.toInt(),+ size = 5f,+ epsilon = 0.1f,+ )++ private fun sample(family: String = "marker") = SerializedStroke(+ brushFamily = family,+ brushSize = 4.5f,+ brushColor = 0xFF336699.toInt(),+ brushEpsilon = 0.25f,+ points = listOf(+ SerializedPoint(1f, 2f, 0L, pressure = 0.5f, tiltRadians = 0.1f, orientationRadians = 0.2f),+ SerializedPoint(11f, 22f, 16L, pressure = 0.7f, tiltRadians = 0.3f, orientationRadians = 0.4f),+ SerializedPoint(21f, 12f, 32L),+ ),+ )++ @Test fun brushFamiliesAndStrokeRoundTrip() {+ assertEquals("pressurePen", StockBrushes.pressurePen().toName())+ assertEquals("highlighter", StockBrushes.highlighter().toName())+ assertEquals("marker", StockBrushes.marker().toName())+ assertEquals(StockBrushes.marker(), "marker".toBrushFamily())+ assertEquals(StockBrushes.pressurePen(), "airbrush".toBrushFamily())++ val original = sample()+ val stroke = original.deserialize()+ assertEquals(original.brushSize, stroke.brush.size)+ assertEquals(original.brushColor, stroke.brush.colorIntArgb)+ val again = stroke.serialize()+ assertEquals("marker", again.brushFamily)+ assertEquals(original.points.size, again.points.size)+ for ((e, a) in original.points.zip(again.points)) {+ assertEquals(e.x, a.x, 0.001f)+ assertEquals(e.y, a.y, 0.001f)+ assertEquals(e.timeMillis, a.timeMillis)+ assertEquals(e.pressure, a.pressure, 0.001f)+ }+ assertTrue(sample().copy(points = emptyList()).deserialize().serialize().points.isEmpty())+ }++ @Test fun translateShiftsPoints() {+ val moved = sample().deserialize().translate(10f, -5f).serialize()+ for ((e, a) in sample().points.zip(moved.points)) {+ assertEquals(e.x + 10f, a.x, 0.001f)+ assertEquals(e.y - 5f, a.y, 0.001f)+ assertEquals(e.timeMillis, a.timeMillis)+ }+ }++ private fun findViews(root: View, simpleName: String): List<View> {+ val out = mutableListOf<View>()+ fun walk(v: View) {+ if (v.javaClass.simpleName == simpleName) out += v+ if (v is ViewGroup) for (i in 0 until v.childCount) walk(v.getChildAt(i))+ }+ walk(root)+ return out+ }++ @Test fun canvasStrokeEraserAndDisabled() {+ val existing = sample().deserialize().translate(100f, 100f)+ val finished = mutableListOf<Stroke>()+ val erased = mutableListOf<Stroke>()+ val finishLatch = CountDownLatch(1)+ val eraser = mutableStateOf(false)+ val enabled = mutableStateOf(true)+ val scenario = ActivityScenario.launch(ComponentActivity::class.java)+ lateinit var inProgress: InProgressStrokesView+ scenario.onActivity { activity ->+ activity.setContent {+ InkCanvasView(+ currentBrush = brush,+ finishedStrokes = listOf(existing),+ onStrokeFinished = { finished += it; finishLatch.countDown() },+ onStrokeErased = { erased += it },+ eraserMode = eraser.value,+ enabled = enabled.value,+ textElements = listOf(+ CanvasTextElement("plain", 0f, 0f, 0f, 0xFF000000.toInt(), 14f),+ CanvasTextElement("bi", 0f, 30f, 15f, 0xFFFF0000.toInt(), 14f, bold = true, italic = true, align = 1),+ CanvasTextElement("b", 0f, 60f, 0f, 0xFF00FF00.toInt(), 14f, bold = true, align = 2),+ CanvasTextElement("i", 0f, 90f, 0f, 0xFF0000FF.toInt(), 14f, italic = true, align = 99),+ ),+ selectedStrokeIndex = 0,+ selectedTextIndex = 1,+ selectionColor = Color.Red,+ )+ }+ }+ Thread.sleep(1_000)+ scenario.onActivity { activity ->+ val finishedView = findViews(activity.window.decorView, "FinishedStrokesView").first()+ finishedView.draw(Canvas())+ inProgress = findViews(activity.window.decorView, "InProgressStrokesView")+ .first() as InProgressStrokesView+ fun touch(action: Int, x: Float, y: Float): Boolean {+ val ev = MotionEvent.obtain(100L, 200L, action, x, y, 0)+ return try { inProgress.dispatchTouchEvent(ev) } finally { ev.recycle() }+ }+ assertTrue(touch(MotionEvent.ACTION_DOWN, 20f, 20f))+ assertTrue(touch(MotionEvent.ACTION_MOVE, 40f, 30f))+ assertTrue(touch(MotionEvent.ACTION_UP, 60f, 30f))+ }+ assertTrue("stroke never finished", finishLatch.await(10, TimeUnit.SECONDS))+ scenario.onActivity {+ fun touch(action: Int, x: Float, y: Float): Boolean {+ val ev = MotionEvent.obtain(100L, 200L, action, x, y, 0)+ return try { inProgress.dispatchTouchEvent(ev) } finally { ev.recycle() }+ }+ assertTrue(touch(MotionEvent.ACTION_DOWN, 20f, 60f))+ assertTrue(touch(MotionEvent.ACTION_CANCEL, 20f, 60f))+ eraser.value = true+ }+ Thread.sleep(500)+ scenario.onActivity {+ fun touch(action: Int, x: Float, y: Float): Boolean {+ val ev = MotionEvent.obtain(100L, 200L, action, x, y, 0)+ return try { inProgress.dispatchTouchEvent(ev) } finally { ev.recycle() }+ }+ // Existing stroke spans roughly (100,100)-(180,140) after translation.+ assertTrue(touch(MotionEvent.ACTION_DOWN, 130f, 120f))+ assertEquals(1, erased.size)+ assertSame(existing, erased.first())+ assertTrue(touch(MotionEvent.ACTION_MOVE, 1500f, 1500f))+ assertEquals(1, erased.size)+ enabled.value = false+ }+ Thread.sleep(500)+ scenario.onActivity {+ val ev = MotionEvent.obtain(100L, 200L, MotionEvent.ACTION_DOWN, 130f, 120f, 0)+ assertFalse(try { inProgress.dispatchTouchEvent(ev) } finally { ev.recycle() })+ }+ scenario.close()+ }+}
library/ink/src/test/java/com/vayunmathur/library/ink/InkSerializationJvmTest.kt
diff --git a/library/ink/src/test/java/com/vayunmathur/library/ink/InkSerializationJvmTest.kt b/library/ink/src/test/java/com/vayunmathur/library/ink/InkSerializationJvmTest.ktnew file mode 100644index 0000000000..3f15b72229--- /dev/null+++ b/library/ink/src/test/java/com/vayunmathur/library/ink/InkSerializationJvmTest.kt@@ -0,0 +1,48 @@+package com.vayunmathur.library.ink++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/** Pure-Kotlin parts of the serialization layer. Brush/Stroke need libink.so, so the+ * round-trips through them live in the instrumented suite. */+class InkSerializationJvmTest {++ @Test fun bytesRoundTripPreservesEveryField() {+ val original = SerializedStroke(+ brushFamily = "highlighter",+ brushSize = 4.5f,+ brushColor = 0xFF336699.toInt(),+ brushEpsilon = 0.25f,+ points = listOf(+ SerializedPoint(1f, 2f, 0L, pressure = 0.5f, tiltRadians = 0.1f, orientationRadians = 0.2f),+ SerializedPoint(11f, 22f, 16L),+ ),+ )+ assertEquals(original, original.toBytes().toSerializedStroke())+ }++ @Test fun unknownKeysFromNewerPayloadsAreIgnored() {+ val payload = """{"brushFamily":"marker","brushSize":2.0,"brushColor":123,"brushEpsilon":0.5,"points":[],"future":true}"""+ val parsed = payload.encodeToByteArray().toSerializedStroke()+ assertEquals("marker", parsed.brushFamily)+ assertEquals(2f, parsed.brushSize)+ assertTrue(parsed.points.isEmpty())+ }++ @Test fun pointDefaults() {+ val p = SerializedPoint(x = 3f, y = 4f, timeMillis = 9L)+ assertEquals(1f, p.pressure)+ assertEquals(0f, p.tiltRadians)+ assertEquals(0f, p.orientationRadians)+ assertEquals(p, p.copy())+ }++ @Test fun textElementDefaults() {+ val elem = CanvasTextElement(text = "hi", x = 1f, y = 2f, rotation = 0f, color = 7, fontSize = 12f)+ assertEquals("sans-serif", elem.fontFamily)+ assertEquals(false, elem.bold)+ assertEquals(false, elem.italic)+ assertEquals(0, elem.align)+ }+}