package com.cottondb.android.ui.photos import android.graphics.Bitmap import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import androidx.activity.ComponentActivity import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onAllNodesWithContentDescription import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTouchInput import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.ScreenEvidence import com.cottondb.android.media.LocalMediaFile import java.io.File import java.io.FileOutputStream import java.util.UUID import org.junit.After import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith /** Real multi-touch coverage for the route-local full-size viewer. */ @RunWith(AndroidJUnit4::class) class LocalPhotoViewerGestureTest { @get:Rule val compose = createAndroidComposeRule() private val root = File( androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().targetContext.cacheDir, "photo-viewer-gesture-${UUID.randomUUID()}", ) @After fun cleanUp() { root.deleteRecursively() } @Test fun checkerboardViewerAcceptsPinchAndPanThenCloses() { val file = checkerboard("gesture.png") var closes = 0 compose.setContent { LocalPhotoViewer(LocalMediaFile(file), "Checkerboard") { closes += 1 } } compose.waitUntil(5_000) { compose.onAllNodesWithContentDescription("Photo").fetchSemanticsNodes().isNotEmpty() } assertEquals("opening the viewer never closes it", 0, closes) ScreenEvidence.capture("photo-viewer-gesture-before") { compose.onRoot().captureToImage().asAndroidBitmap() } // These are two real pointers moving apart, followed by a one-finger pan. The viewer // deliberately keeps transform values internal, so its pure math is tested separately; // this test proves the live pointer-input route accepts the interaction without closing. compose.onNodeWithContentDescription("Photo").performTouchInput { val middle = center down(0, Offset(middle.x - 24f, middle.y)) down(1, Offset(middle.x + 24f, middle.y)) // The first move crosses Compose's touch slop; the second is deliberate transform // input, so the test cannot pass merely because a gesture was recognized late. updatePointerTo(0, Offset(middle.x - 52f, middle.y - 8f)) updatePointerTo(1, Offset(middle.x + 52f, middle.y + 8f)) move() updatePointerTo(0, Offset(middle.x - 104f, middle.y - 24f)) updatePointerTo(1, Offset(middle.x + 104f, middle.y + 24f)) move() up(1) up(0) } compose.onNodeWithContentDescription("Photo").performTouchInput { val middle = center down(0, middle) moveTo(0, Offset(middle.x + 20f, middle.y + 14f)) moveTo(0, Offset(middle.x + 42f, middle.y + 28f)) up(0) } compose.waitForIdle() ScreenEvidence.capture("photo-viewer-gesture-after") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Photo").assertExists() assertEquals("gesture input must not consume the close action", 0, closes) compose.onNodeWithContentDescription("Close").performClick() assertEquals("Close is one explicit action", 1, closes) } private fun checkerboard(name: String): File { root.mkdirs() val target = root.resolve(name) val bitmap = Bitmap.createBitmap(384, 384, Bitmap.Config.ARGB_8888) try { val paint = Paint() val canvas = Canvas(bitmap) val cell = bitmap.width / 12f repeat(12) { row -> repeat(12) { column -> paint.color = if ((row + column) % 2 == 0) Color.rgb(26, 61, 85) else Color.rgb(229, 203, 145) canvas.drawRect(column * cell, row * cell, (column + 1) * cell, (row + 1) * cell, paint) } } FileOutputStream(target).use { output -> check(bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)) } } finally { bitmap.recycle() } return target } }