photos: add tests for editing slice (blend, selection, layers, adjustments, video state, panels)
- Commit
30c7e21187c5ac595ccb8e3f45f6122c9d54241a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-06 17:31 UTC (29 days ago)
- Parents
ffcf80f
Slice editing coverage 10.0% -> 14.6% merged instructions (10342/103597 -> 15144/103597). 6 new test files, 74 tests, full suite green (135/135). Pure-JVM blend/selection/video-state/model tests plus Robolectric matrix, layer, and Compose panel tests. (cherry picked from commit d5590f77dd287f9d7c9b001169c514b2ab3258b6)
Diff
photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2AdjustmentsTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2AdjustmentsTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2AdjustmentsTest.ktnew file mode 100644index 0000000000..a5c6a92566--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2AdjustmentsTest.kt@@ -0,0 +1,210 @@+package com.vayunmathur.photos.photoslice2++import android.graphics.Bitmap+import android.graphics.ColorMatrix+import androidx.core.graphics.createBitmap+import com.vayunmathur.photos.data.ImageAdjustments+import com.vayunmathur.photos.data.applyPixelEffects+import com.vayunmathur.photos.data.applyToBitmap+import com.vayunmathur.photos.data.hasPixelEffects+import com.vayunmathur.photos.data.mapPixels+import com.vayunmathur.photos.data.toColorMatrix+import kotlin.math.abs+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Slice-2 tests for [ImageAdjustments]: color-matrix composition per slider+ * (asserted on the matrix arrays — pure Java), bitmap application for the+ * paths Robolectric executes faithfully (identity fast-path, pixel effects+ * via getPixels/setPixels), and the [mapPixels] helper.+ *+ * NOTE: `Canvas.drawBitmap` with a `ColorMatrixColorFilter` is a native+ * pipeline Robolectric shadows as a no-op (verified: rendered pixels come+ * back unfiltered), so matrix-driven rendering is asserted on+ * [toColorMatrix] arrays instead. The matrix math IS the behavior under+ * test; the Canvas path is covered on the emulator.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class PhotoSlice2AdjustmentsTest {++ private fun solidBitmap(color: Int, w: Int = 8, h: Int = 8): Bitmap {+ val bmp = createBitmap(w, h)+ bmp.eraseColor(color)+ return bmp+ }++ private fun channelOf(px: Int, shift: Int): Int = (px shr shift) and 0xFF++ private fun identityArray(): List<Float> = ColorMatrix().array.toList()++ @Test+ fun identityAdjustmentsAreNoOps() {+ val adj = ImageAdjustments()+ assertFalse(adj.hasPixelEffects())+ assertEquals(identityArray(), adj.toColorMatrix().array.toList())+ // Bitmap unchanged via the identity fast path (no Canvas involved).+ val bmp = solidBitmap(0xFF445566.toInt())+ val out = adj.applyToBitmap(bmp)+ assertEquals(bmp.getPixel(0, 0), out.getPixel(0, 0))+ }++ @Test+ fun brightnessMatrixOffsetsChannels() {+ // brightness=100 -> v = 128 added to R/G/B rows.+ val m = ImageAdjustments(brightness = 100f).toColorMatrix().array+ assertEquals(128f, m[4], "R offset")+ assertEquals(128f, m[9], "G offset")+ assertEquals(128f, m[14], "B offset")+ assertEquals(1f, m[0])+ // Zero brightness is the identity.+ assertEquals(identityArray(), ImageAdjustments(brightness = 0f).toColorMatrix().array.toList())+ }++ @Test+ fun contrastMatrixScalesAroundMid() {+ // contrast=100 -> s=2, t=(-0.5*2+0.5)*255 = -127.5.+ val m = ImageAdjustments(contrast = 100f).toColorMatrix().array+ assertEquals(2f, m[0])+ assertEquals(-127.5f, m[4])+ assertEquals(identityArray(), ImageAdjustments(contrast = 0f).toColorMatrix().array.toList())+ }++ @Test+ fun saturationMatrixDesaturatesToGray() {+ // saturation=-100 -> setSaturation(0): all rows equal the luminance weights.+ val m = ImageAdjustments(saturation = -100f).toColorMatrix().array+ // R row ~= (0.213, 0.715, 0.072) — Android's setSaturation(0) weights.+ assertTrue(abs(m[0] - 0.213f) < 0.01f, "R weight ${m[0]}")+ assertTrue(abs(m[1] - 0.715f) < 0.01f, "G weight ${m[1]}")+ assertTrue(abs(m[2] - 0.072f) < 0.01f, "B weight ${m[2]}")+ }++ @Test+ fun warmthMatrixPushesRedPullsBlue() {+ // warmth=100 -> w=30: R+30, B-30.+ val m = ImageAdjustments(warmth = 100f).toColorMatrix().array+ assertEquals(30f, m[4])+ assertEquals(-30f, m[14])+ assertEquals(0f, m[9])+ }++ @Test+ fun exposureMatrixScalesGain() {+ // exposure=100 -> 2^1 = 2x diagonal.+ val m = ImageAdjustments(exposure = 100f).toColorMatrix().array+ assertEquals(2f, m[0])+ assertEquals(2f, m[6])+ assertEquals(2f, m[12])+ assertEquals(1f, m[18])+ }++ @Test+ fun combinedMatricesCompose() {+ val both = ImageAdjustments(brightness = 20f, contrast = 20f).toColorMatrix()+ val justBright = ImageAdjustments(brightness = 20f).toColorMatrix()+ assertFalse(both.array.toList() == justBright.array.toList())+ // Highlights/shadows/fade/tint each contribute.+ for (adj in listOf(+ ImageAdjustments(highlights = 30f),+ ImageAdjustments(shadows = 30f),+ ImageAdjustments(fade = 30f),+ ImageAdjustments(tint = 30f),+ )) {+ assertFalse(+ adj.toColorMatrix().array.toList() == identityArray(),+ "matrix for $adj",+ )+ }+ }++ @Test+ fun highlightsShadowsFadeTintValues() {+ // highlights=100: h=0.3, offset=12.+ val h = ImageAdjustments(highlights = 100f).toColorMatrix().array+ assertEquals(1.3f, h[0])+ assertEquals(12f, h[4])+ // shadows=100: +40 offset.+ val s = ImageAdjustments(shadows = 100f).toColorMatrix().array+ assertEquals(40f, s[4])+ // fade=100: scale 0.6, offset 51.2.+ val f = ImageAdjustments(fade = 100f).toColorMatrix().array+ assertTrue(abs(f[0] - 0.6f) < 0.001f)+ assertTrue(abs(f[4] - 51.2f) < 0.01f)+ // tint=100: t=25, R/B -12.5, G +25.+ val t = ImageAdjustments(tint = 100f).toColorMatrix().array+ assertEquals(-12.5f, t[4])+ assertEquals(25f, t[9])+ assertEquals(-12.5f, t[14])+ }++ @Test+ fun pixelEffectsFlags() {+ assertFalse(ImageAdjustments().hasPixelEffects())+ assertTrue(ImageAdjustments(sharpness = 50f).hasPixelEffects())+ assertTrue(ImageAdjustments(vignette = 50f).hasPixelEffects())+ assertTrue(ImageAdjustments(grain = 50f).hasPixelEffects())+ // Negative sharpness still flags (guard path in applyPixelEffects).+ assertTrue(ImageAdjustments(sharpness = -10f).hasPixelEffects())+ }++ @Test+ fun grainIsDeterministicAndBounded() {+ val bmp = solidBitmap(0xFF808080.toInt(), 16, 16)+ val a = ImageAdjustments(grain = 100f).applyPixelEffects(bmp.copy(Bitmap.Config.ARGB_8888, true))+ val b = ImageAdjustments(grain = 100f).applyPixelEffects(bmp.copy(Bitmap.Config.ARGB_8888, true))+ // Seeded RNG (42): identical outputs.+ var same = 0+ for (y in 0 until 16) for (x in 0 until 16) {+ if (a.getPixel(x, y) == b.getPixel(x, y)) same+++ }+ assertEquals(256, same)+ // Grain actually perturbs most pixels.+ var changed = 0+ for (y in 0 until 16) for (x in 0 until 16) {+ if (a.getPixel(x, y) != bmp.getPixel(x, y)) changed+++ }+ assertTrue(changed > 200, "grain changed=$changed")+ }++ @Test+ fun vignetteDarkensCorners() {+ val bmp = solidBitmap(0xFFFFFFFF.toInt(), 32, 32)+ val out = ImageAdjustments(vignette = 100f).applyPixelEffects(bmp.copy(Bitmap.Config.ARGB_8888, true))+ val center = channelOf(out.getPixel(16, 16), 16)+ val corner = channelOf(out.getPixel(0, 0), 16)+ assertTrue(corner < center, "corner=$corner center=$center")+ }++ @Test+ fun sharpenUniformImageIsStable() {+ // Uniform image: sharpen kernel sums to ~1, output ~= input.+ val bmp = solidBitmap(0xFF808080.toInt(), 8, 8)+ val out = ImageAdjustments(sharpness = 100f).applyPixelEffects(bmp.copy(Bitmap.Config.ARGB_8888, true))+ val r = channelOf(out.getPixel(4, 4), 16)+ assertTrue(abs(r - 0x80) <= 4, "sharpened uniform=$r")+ }++ @Test+ fun mapPixelsTransformsInPlace() {+ val bmp = solidBitmap(0xFF010203.toInt(), 2, 2)+ bmp.mapPixels { it or 0x00F00000 }+ assertEquals(0xFFF10203.toInt(), bmp.getPixel(0, 0))+ assertEquals(0xFFF10203.toInt(), bmp.getPixel(1, 1))+ }++ @Test+ fun applyToBitmapWithPixelEffects() {+ // Combined path: matrix (Canvas no-op under Robolectric) + grain (real).+ val bmp = solidBitmap(0xFF707070.toInt(), 8, 8)+ val out = ImageAdjustments(brightness = 20f, grain = 40f).applyToBitmap(bmp)+ assertEquals(8, out.width)+ assertEquals(8, out.height)+ }+}
photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2BlendTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2BlendTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2BlendTest.ktnew file mode 100644index 0000000000..6514e178fa--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2BlendTest.kt@@ -0,0 +1,196 @@+package com.vayunmathur.photos.photoslice2++import com.vayunmathur.photos.data.LayerBlendMode+import kotlin.math.abs+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * Slice-2 tests for [LayerBlendMode.blendPixel]: pure per-pixel math, no+ * Android framework needed. Covers identity/edge behavior plus each mode's+ * distinguishing arithmetic against hand-computed expectations.+ */+class PhotoSlice2BlendTest {++ private fun opaque(rgb: Int): Int = (0xFF shl 24) or rgb++ @Test+ fun transparentSourceReturnsBackdrop() {+ val dst = opaque(0x112233)+ val src = 0x00112233 // alpha 0+ for (mode in LayerBlendMode.entries) {+ assertEquals(dst, mode.blendPixel(dst, src, 1f), "mode $mode with alpha-0 src")+ }+ }++ @Test+ fun zeroExtraAlphaReturnsBackdrop() {+ val dst = opaque(0x112233)+ val src = opaque(0xAABBCC)+ for (mode in LayerBlendMode.entries) {+ assertEquals(dst, mode.blendPixel(dst, src, 0f), "mode $mode with alpha-0 extra")+ }+ }++ @Test+ fun normalOpaqueSourceReplacesBackdrop() {+ val src = opaque(0xDEAD42)+ val out = LayerBlendMode.Normal.blendPixel(opaque(0x112233), src, 1f)+ assertEquals(src, out)+ }++ @Test+ fun normalHalfOpacityAverages() {+ val dst = opaque(0x000000)+ val src = opaque(0xFFFFFF)+ val out = LayerBlendMode.Normal.blendPixel(dst, src, 0.5f)+ val r = (out shr 16) and 0xFF+ // 50% white over black ~= 127/128.+ assertTrue(abs(r - 127.5f) <= 1.5f, "half-blend red=$r")+ assertEquals(0xFF, (out ushr 24) and 0xFF)+ }++ @Test+ fun multiplyDarkens() {+ // 0.5 gray * 0.5 gray = 0.25 gray-ish.+ val g = opaque(0x808080)+ val out = LayerBlendMode.Multiply.blendPixel(g, g, 1f)+ val r = (out shr 16) and 0xFF+ assertTrue(r in 60..70, "multiply gray=$r")+ }++ @Test+ fun screenLightens() {+ val g = opaque(0x808080)+ val out = LayerBlendMode.Screen.blendPixel(g, g, 1f)+ val r = (out shr 16) and 0xFF+ // screen(0.5,0.5) = 0.75 -> ~191.+ assertTrue(r in 186..196, "screen gray=$r")+ }++ @Test+ fun darkenTakesMinLightenTakesMax() {+ val dark = opaque(0x102030)+ val light = opaque(0xA0B0C0)+ val d = LayerBlendMode.Darken.blendPixel(light, dark, 1f)+ assertEquals(dark, d)+ val l = LayerBlendMode.Lighten.blendPixel(dark, light, 1f)+ assertEquals(light, l)+ }++ @Test+ fun differenceOfEqualIsBlack() {+ val c = opaque(0x445566)+ val out = LayerBlendMode.Difference.blendPixel(c, c, 1f)+ assertEquals(opaque(0x000000), out)+ }++ @Test+ fun differenceIsSymmetric() {+ val a = opaque(0x102030)+ val b = opaque(0xA0B0C0)+ assertEquals(+ LayerBlendMode.Difference.blendPixel(a, b, 1f),+ LayerBlendMode.Difference.blendPixel(b, a, 1f),+ )+ }++ @Test+ fun exclusionOfEqualIsNotBlack() {+ // exclusion(c,c) = 2*c*(1-c) > 0 for mid tones.+ val c = opaque(0x808080)+ val out = LayerBlendMode.Exclusion.blendPixel(c, c, 1f)+ val r = (out shr 16) and 0xFF+ assertTrue(r in 120..136, "exclusion gray=$r")+ }++ @Test+ fun colorDodgeBrightensTowardWhite() {+ val dst = opaque(0x808080)+ val out = LayerBlendMode.ColorDodge.blendPixel(dst, opaque(0xFFFFFF), 1f)+ assertEquals(opaque(0xFFFFFF), out)+ }++ @Test+ fun colorBurnDarkensTowardBlack() {+ val dst = opaque(0x808080)+ val out = LayerBlendMode.ColorBurn.blendPixel(dst, opaque(0x000000), 1f)+ assertEquals(opaque(0x000000), out)+ }++ @Test+ fun overlayHardLightAgreeOnMid() {+ // Overlay(b,s) == HardLight(s,b) by construction.+ val b = opaque(0x336699)+ val s = opaque(0xCC6633)+ assertEquals(+ LayerBlendMode.Overlay.blendPixel(b, s, 1f),+ LayerBlendMode.HardLight.blendPixel(s, b, 1f),+ )+ }++ @Test+ fun softLightNeutralSourceIsNearIdentity() {+ // 50% gray source is the soft-light neutral: output ~= backdrop.+ val dst = opaque(0x336699)+ val out = LayerBlendMode.SoftLight.blendPixel(dst, opaque(0x808080), 1f)+ for (shift in listOf(16, 8, 0)) {+ val want = (dst shr shift) and 0xFF+ val got = (out shr shift) and 0xFF+ assertTrue(abs(got - want) <= 3, "channel shift=$shift want=$want got=$got")+ }+ }++ @Test+ fun nonSeparableModesPreserveLuminanceRelationships() {+ // Luminosity(src over dst): output luminance ~= src luminance.+ val dst = opaque(0xFF0000)+ val src = opaque(0x0000FF)+ val out = LayerBlendMode.Luminosity.blendPixel(dst, src, 1f)+ fun lum(px: Int): Float {+ val r = ((px shr 16) and 0xFF) / 255f+ val g = ((px shr 8) and 0xFF) / 255f+ val b = (px and 0xFF) / 255f+ return 0.3f * r + 0.59f * g + 0.11f * b+ }+ assertTrue(abs(lum(out) - lum(src)) < 0.08f, "lum out=${lum(out)} src=${lum(src)}")+ }++ @Test+ fun hueModeKeepsBackdropLuminance() {+ val dst = opaque(0x808080) // mid gray+ val src = opaque(0xFF0000) // red hue+ val out = LayerBlendMode.Hue.blendPixel(dst, src, 1f)+ fun lum(px: Int): Float {+ val r = ((px shr 16) and 0xFF) / 255f+ val g = ((px shr 8) and 0xFF) / 255f+ val b = (px and 0xFF) / 255f+ return 0.3f * r + 0.59f * g + 0.11f * b+ }+ assertTrue(abs(lum(out) - lum(dst)) < 0.08f, "lum out=${lum(out)} dst=${lum(dst)}")+ }++ @Test+ fun allModesReturnOpaqueForOpaqueInputs() {+ val dst = opaque(0x112233)+ val src = opaque(0xAABBCC)+ for (mode in LayerBlendMode.entries) {+ val out = mode.blendPixel(dst, src, 1f)+ assertEquals(0xFF, (out ushr 24) and 0xFF, "mode $mode alpha")+ }+ }++ @Test+ fun extraAlphaClamped() {+ val dst = opaque(0x000000)+ val src = opaque(0xFFFFFF)+ // Over-range alpha behaves like 1.+ assertEquals(+ LayerBlendMode.Normal.blendPixel(dst, src, 1f),+ LayerBlendMode.Normal.blendPixel(dst, src, 5f),+ )+ // Negative behaves like 0.+ assertEquals(dst, LayerBlendMode.Normal.blendPixel(dst, src, -2f))+ }+}
photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.ktnew file mode 100644index 0000000000..2f2d568fad--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.kt@@ -0,0 +1,230 @@+package com.vayunmathur.photos.photoslice2++import com.vayunmathur.photos.data.AdjustmentLayer+import com.vayunmathur.photos.data.BasicAdjustment+import com.vayunmathur.photos.data.DrawingLayer+import com.vayunmathur.photos.data.EditDocument+import com.vayunmathur.photos.data.GroupInfo+import com.vayunmathur.photos.data.ImageAdjustments+import com.vayunmathur.photos.data.LayerBlendMode+import com.vayunmathur.photos.data.LayerMask+import com.vayunmathur.photos.data.LayerStyle+import com.vayunmathur.photos.data.PixelLayer+import com.vayunmathur.photos.data.BitmapReference+import com.vayunmathur.photos.data.TextElement+import com.vayunmathur.photos.data.TextLayer+import com.vayunmathur.photos.ui.AdjustmentType+import com.vayunmathur.photos.ui.EditorMode+import com.vayunmathur.photos.ui.SelectionTool+import com.vayunmathur.photos.ui.ToolCategory+import com.vayunmathur.photos.ui.categoryTools+import com.vayunmathur.photos.ui.descriptionRes+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNotNull+import kotlin.test.assertNull+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Slice-2 tests for the layer-stack model ([EditDocument] mutations, layer+ * [copyBase], adjustment identity) and the editor tool model+ * ([AdjustmentType] get/set, [categoryTools], [EditorMode.descriptionRes]).+ *+ * Bitmap-backed layers use tiny Robolectric bitmaps; everything else is pure.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class PhotoSlice2LayersTest {++ private fun pixelLayer(name: String = "Layer"): PixelLayer {+ val bmp = android.graphics.Bitmap.createBitmap(4, 4, android.graphics.Bitmap.Config.ARGB_8888)+ bmp.eraseColor(0xFF112233.toInt())+ return PixelLayer(BitmapReference(bmp), name = name)+ }++ @Test+ fun addLayerSelectsIt() {+ var doc = EditDocument(canvasWidth = 8, canvasHeight = 8)+ assertNull(doc.activeLayer)+ doc = doc.addLayer(pixelLayer("A"))+ assertEquals("A", doc.activeLayer?.name)+ assertEquals(0, doc.activeLayerIndex)+ doc = doc.addLayer(pixelLayer("B"))+ assertEquals("B", doc.activeLayer?.name)+ assertEquals(1, doc.activeLayerIndex)+ }++ @Test+ fun removeLayerKeepsValidActive() {+ var doc = EditDocument(canvasWidth = 8, canvasHeight = 8)+ doc = doc.addLayer(pixelLayer("A")).addLayer(pixelLayer("B")).addLayer(pixelLayer("C"))+ doc = doc.removeLayer(1)+ assertEquals(2, doc.layers.size)+ assertEquals(listOf("A", "C"), doc.layers.map { it.name })+ // Single layer cannot be removed.+ var single = EditDocument(canvasWidth = 8, canvasHeight = 8).addLayer(pixelLayer("only"))+ single = single.removeLayer(0)+ assertEquals(1, single.layers.size)+ // Out-of-range is a no-op.+ assertEquals(doc, doc.removeLayer(99))+ }++ @Test+ fun moveLayerReorders() {+ var doc = EditDocument(canvasWidth = 8, canvasHeight = 8)+ doc = doc.addLayer(pixelLayer("A")).addLayer(pixelLayer("B")).addLayer(pixelLayer("C"))+ doc = doc.setActiveLayer(0).moveLayer(0, 2)+ assertEquals(listOf("B", "C", "A"), doc.layers.map { it.name })+ assertEquals(2, doc.activeLayerIndex)+ // Same-position move is a no-op.+ assertEquals(doc, doc.moveLayer(1, 1))+ assertEquals(doc, doc.moveLayer(99, 0))+ }++ @Test+ fun updateLayerAndCopyBase() {+ var doc = EditDocument(canvasWidth = 8, canvasHeight = 8).addLayer(pixelLayer("A"))+ doc = doc.updateLayer(0) { it.copyBase(name = "Renamed", opacity = 0.5f, visible = false) }+ val layer = doc.layers[0]+ assertEquals("Renamed", layer.name)+ assertEquals(0.5f, layer.opacity)+ assertFalse(layer.visible)+ // Out-of-range update is a no-op.+ assertEquals(doc, doc.updateLayer(9) { it })+ // Blend mode + lock + clip + group survive copyBase.+ val updated = layer.copyBase(+ blendMode = LayerBlendMode.Multiply, locked = true, clipped = true, groupId = "g1",+ )+ assertEquals(LayerBlendMode.Multiply, updated.blendMode)+ assertTrue(updated.locked)+ assertTrue(updated.clipped)+ assertEquals("g1", updated.groupId)+ }++ @Test+ fun duplicateLayerCopiesAbove() {+ var doc = EditDocument(canvasWidth = 8, canvasHeight = 8)+ doc = doc.addLayer(pixelLayer("A")).addLayer(pixelLayer("B"))+ doc = doc.duplicateLayer(0)+ assertEquals(3, doc.layers.size)+ assertEquals("A copy", doc.layers[1].name)+ assertFalse(doc.layers[1].id == doc.layers[0].id)+ assertEquals(doc.layers[1].id, doc.activeLayer?.id)+ assertEquals(doc, doc.duplicateLayer(42))+ }++ @Test+ fun groupAndUngroup() {+ var doc = EditDocument(canvasWidth = 8, canvasHeight = 8)+ doc = doc.addLayer(pixelLayer("A")).addLayer(pixelLayer("B")).setActiveLayer(1)+ doc = doc.groupActiveWithBelow()+ assertEquals(1, doc.groups.size)+ val gid = doc.groups[0].id+ assertEquals(gid, doc.layers[0].groupId)+ assertEquals(gid, doc.layers[1].groupId)+ doc = doc.ungroupActive()+ assertTrue(doc.groups.isEmpty())+ assertNull(doc.layers[0].groupId)+ // Grouping with nothing below is a no-op.+ val single = EditDocument(canvasWidth = 8, canvasHeight = 8).addLayer(pixelLayer("solo"))+ assertEquals(single, single.groupActiveWithBelow())+ }++ @Test+ fun layerStyleIdentity() {+ assertTrue(LayerStyle().isIdentity())+ assertFalse(LayerStyle(dropShadow = true).isIdentity())+ assertFalse(LayerStyle(stroke = true).isIdentity())+ assertFalse(LayerStyle(outerGlow = true).isIdentity())+ }++ @Test+ fun basicAdjustmentIdentityAndLabel() {+ val identity = BasicAdjustment()+ assertTrue(identity.isIdentity())+ assertEquals("Adjustments", identity.label)+ val tuned = BasicAdjustment(ImageAdjustments(brightness = 10f))+ assertFalse(tuned.isIdentity())+ assertEquals("Adjustments", tuned.label)+ }++ @Test+ fun adjustmentLayerDefaultsNameFromLabel() {+ val layer = AdjustmentLayer(BasicAdjustment())+ assertEquals("Adjustments", layer.name)+ assertTrue(layer.visible)+ }++ @Test+ fun textAndDrawingLayersCarryContent() {+ val el = TextElement(id = "t1", text = "hello", x = 0.5f, y = 0.5f, rotation = 0f, color = 0xFFFFFFFF.toInt(), fontSize = 24f)+ val text = TextLayer(el)+ assertEquals("hello", text.textElement.text)+ val drawing = DrawingLayer(strokes = emptyList(), sourceWidth = 8f, sourceHeight = 8f)+ assertTrue(drawing.strokes.isEmpty())+ }++ @Test+ fun maskInvertRoundTripOnLayer() {+ val doc = EditDocument(canvasWidth = 8, canvasHeight = 8).addLayer(pixelLayer("A"))+ val mask = LayerMask.full(2, 2)+ val withMask = doc.updateLayer(0) { it.copyBase(mask = mask) }+ assertNotNull(withMask.layers[0].mask)+ val cleared = withMask.updateLayer(0) { it.copyBase(mask = null) }+ assertNull(cleared.layers[0].mask)+ }++ @Test+ fun adjustmentTypeGetSetRoundTrip() {+ val base = ImageAdjustments()+ for (type in AdjustmentType.entries) {+ assertEquals(0f, type.get(base), "default ${type.name}")+ val updated = type.set(base, 25f)+ assertEquals(25f, type.get(updated), "round trip ${type.name}")+ // Bounds are sane.+ assertTrue(type.min <= 25f && 25f <= type.max, "bounds ${type.name}")+ }+ // Sharpness/vignette/grain/fade start at 0 min.+ assertEquals(0f, AdjustmentType.Sharpness.min)+ assertEquals(-100f, AdjustmentType.Brightness.min)+ }++ @Test+ fun categoryToolsCoverAllCategories() {+ // Every category except Draw/Select-details has at least one tool.+ for (cat in listOf(ToolCategory.Adjust, ToolCategory.Filters, ToolCategory.Retouch, ToolCategory.Transform, ToolCategory.Layers, ToolCategory.Paint)) {+ assertFalse(categoryTools[cat].isNullOrEmpty(), "tools for $cat")+ }+ assertEquals(ToolCategory.Adjust, ToolCategory.entries.first())+ }++ @Test+ fun selectionToolsEnumerated() {+ assertEquals(5, SelectionTool.entries.size)+ assertTrue(SelectionTool.entries.contains(SelectionTool.Wand))+ }++ @Test+ fun editorModeDescriptionsResolve() {+ val ctx = androidx.test.core.app.ApplicationProvider.getApplicationContext<android.content.Context>()+ for (mode in EditorMode.entries) {+ val res = mode.descriptionRes()+ assertTrue(res != 0, "description for $mode")+ // Resolves to a non-empty string.+ assertTrue(ctx.getString(res).isNotEmpty(), "string for $mode")+ }+ }++ @Test+ fun groupInfoDefaults() {+ val g = GroupInfo(name = "My Group")+ assertEquals("My Group", g.name)+ assertTrue(g.visible)+ assertFalse(g.collapsed)+ assertEquals(1f, g.opacity)+ }+}
photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2PanelsTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2PanelsTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2PanelsTest.ktnew file mode 100644index 0000000000..1c80c8241c--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2PanelsTest.kt@@ -0,0 +1,138 @@+package com.vayunmathur.photos.photoslice2++import android.app.Application+import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.photos.data.BitmapReference+import com.vayunmathur.photos.data.EditDocument+import com.vayunmathur.photos.data.ImageAdjustments+import com.vayunmathur.photos.data.PixelLayer+import com.vayunmathur.photos.ui.CategoryBar+import com.vayunmathur.photos.ui.LayersPanel+import com.vayunmathur.photos.ui.ToolCategory+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Slice-2 Compose tests for the editing panels ([CategoryBar] selection,+ * [LayersPanel] list + visibility toggles). Robolectric + Compose rule.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class PhotoSlice2PanelsTest {++ @get:Rule+ val composeRule = createComposeRule()++ private fun pixelLayer(name: String): PixelLayer {+ val bmp = android.graphics.Bitmap.createBitmap(4, 4, android.graphics.Bitmap.Config.ARGB_8888)+ bmp.eraseColor(0xFF112233.toInt())+ return PixelLayer(BitmapReference(bmp), name = name)+ }++ @Test+ fun categoryBarShowsAllCategoriesAndSelects() {+ var selected: ToolCategory? = null+ composeRule.setContent {+ CategoryBar(onSelect = { selected = it })+ }+ val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()+ // The bar scrolls horizontally: the first categories are displayed.+ composeRule.onNodeWithText(ctx.getString(ToolCategory.Adjust.labelRes)).assertIsDisplayed()+ composeRule.onNodeWithText(ctx.getString(ToolCategory.Filters.labelRes)).assertIsDisplayed()+ // Clicking Adjust selects it.+ composeRule.onNodeWithText(ctx.getString(ToolCategory.Adjust.labelRes)).performClick()+ assertEquals(ToolCategory.Adjust, selected)+ }++ @Test+ fun layersPanelListsLayersAndTogglesVisibility() {+ val doc = EditDocument(canvasWidth = 8, canvasHeight = 8)+ .addLayer(pixelLayer("Background"))+ .addLayer(pixelLayer("Foreground"))+ var toggled: Pair<Int, Boolean>? = null+ var selectedIndex: Int? = null+ composeRule.setContent {+ LayersPanel(+ document = doc,+ hasSelection = false,+ onSelectLayer = { selectedIndex = it },+ onToggleVisibility = { i, v -> toggled = i to v },+ onOpacityChange = { _, _ -> },+ onBlendModeChange = { _, _ -> },+ onAddAdjustment = {},+ onAddPixelLayer = {},+ onDuplicate = {},+ onMergeDown = {},+ onDelete = {},+ onFlatten = {},+ onAddMaskFromSelection = {},+ onDeleteMask = {},+ onInvertMask = {},+ onToggleClip = { _, _ -> },+ onSetStyle = { _, _ -> },+ onGroupActive = {},+ onUngroup = {},+ onUpdateGroup = {},+ onMoveLayer = { _, _ -> },+ onEditText = {},+ onResumeDrawing = {},+ )+ }+ composeRule.onNodeWithText("Background").assertIsDisplayed()+ composeRule.onNodeWithText("Foreground").assertIsDisplayed()+ // Tapping a layer row selects it.+ composeRule.onNodeWithText("Foreground").performClick()+ assertEquals(1, selectedIndex)+ assertTrue(toggled == null)+ }++ @Test+ fun layersPanelEmptyState() {+ composeRule.setContent {+ LayersPanel(+ document = EditDocument(),+ hasSelection = true,+ onSelectLayer = {},+ onToggleVisibility = { _, _ -> },+ onOpacityChange = { _, _ -> },+ onBlendModeChange = { _, _ -> },+ onAddAdjustment = {},+ onAddPixelLayer = {},+ onDuplicate = {},+ onMergeDown = {},+ onDelete = {},+ onFlatten = {},+ onAddMaskFromSelection = {},+ onDeleteMask = {},+ onInvertMask = {},+ onToggleClip = { _, _ -> },+ onSetStyle = { _, _ -> },+ onGroupActive = {},+ onUngroup = {},+ onUpdateGroup = {},+ onMoveLayer = { _, _ -> },+ onEditText = {},+ onResumeDrawing = {},+ )+ }+ // Empty document renders without crashing (no layer rows).+ composeRule.waitForIdle()+ }++ @Test+ fun imageAdjustmentsDefaultsAreNeutral() {+ val adj = ImageAdjustments()+ assertEquals(0f, adj.brightness)+ assertEquals(0f, adj.contrast)+ assertEquals(0f, adj.saturation)+ }+}
photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2SelectionTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2SelectionTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2SelectionTest.ktnew file mode 100644index 0000000000..0c82a83959--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2SelectionTest.kt@@ -0,0 +1,162 @@+package com.vayunmathur.photos.photoslice2++import com.vayunmathur.photos.data.LayerMask+import com.vayunmathur.photos.data.Selection+import com.vayunmathur.photos.data.SelectionCombine+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++/**+ * Slice-2 tests for [Selection] (mask rasterization, feather, combine) and+ * [LayerMask] (invert, full). Pure float-array math — no framework needed.+ */+class PhotoSlice2SelectionTest {++ @Test+ fun emptySelectionDetects() {+ val sel = Selection(FloatArray(16), 4, 4)+ assertTrue(sel.isEmpty())+ val full = Selection(FloatArray(16) { 1f }, 4, 4)+ assertFalse(full.isEmpty())+ }++ @Test+ fun invertFlips() {+ val sel = Selection(floatArrayOf(0f, 0.25f, 0.75f, 1f), 2, 2)+ val inv = sel.invert()+ assertEquals(listOf(1f, 0.75f, 0.25f, 0f), inv.mask.toList())+ assertEquals(2, inv.width)+ assertEquals(2, inv.height)+ }++ @Test+ fun featherZeroCopies() {+ val sel = Selection(floatArrayOf(0f, 1f, 1f, 0f), 2, 2)+ val feathered = sel.applyFeather(0f)+ assertEquals(sel.mask.toList(), feathered.mask.toList())+ assertEquals(0f, feathered.featherRadius)+ }++ @Test+ fun featherBlursHardEdge() {+ // 8x1 hard edge: left half 0, right half 1. Feather radius 1 softens.+ val mask = FloatArray(8) { if (it < 4) 0f else 1f }+ val sel = Selection(mask, 8, 1)+ val feathered = sel.applyFeather(1f)+ // Interior far from edge unchanged-ish; edge pixels become fractional.+ assertTrue(feathered.mask[0] < 0.05f)+ assertTrue(feathered.mask[7] > 0.95f)+ assertTrue(feathered.mask[3] in 0.2f..0.5f, "left edge=${feathered.mask[3]}")+ assertTrue(feathered.mask[4] in 0.5f..0.8f, "right edge=${feathered.mask[4]}")+ }++ @Test+ fun toLayerMaskCopies() {+ val sel = Selection(floatArrayOf(0.5f, 0.5f), 2, 1)+ val mask = sel.toLayerMask()+ assertEquals(2, mask.width)+ assertEquals(listOf(0.5f, 0.5f), mask.alphaData.toList())+ }++ @Test+ fun combineNewReturnsOther() {+ val a = Selection(floatArrayOf(1f, 0f), 2, 1)+ val b = Selection(floatArrayOf(0f, 1f), 2, 1)+ assertEquals(b, a.combine(b, SelectionCombine.New))+ }++ @Test+ fun combineAddTakesMax() {+ val a = Selection(floatArrayOf(0.2f, 0.8f), 2, 1)+ val b = Selection(floatArrayOf(0.6f, 0.3f), 2, 1)+ val out = a.combine(b, SelectionCombine.Add)+ assertEquals(listOf(0.6f, 0.8f), out.mask.toList())+ }++ @Test+ fun combineSubtractAndIntersect() {+ val a = Selection(floatArrayOf(1f, 1f), 2, 1)+ val b = Selection(floatArrayOf(1f, 0f), 2, 1)+ assertEquals(listOf(0f, 1f), a.combine(b, SelectionCombine.Subtract).mask.toList())+ assertEquals(listOf(1f, 0f), a.combine(b, SelectionCombine.Intersect).mask.toList())+ }++ @Test+ fun combineResamplesDifferentSizes() {+ val big = Selection(FloatArray(16) { 1f }, 4, 4)+ val small = Selection(floatArrayOf(0f), 1, 1)+ val out = big.combine(small, SelectionCombine.Intersect)+ assertEquals(16, out.mask.size)+ assertTrue(out.mask.all { it == 0f })+ }++ @Test+ fun rectangleRasterizes() {+ val sel = Selection.rectangle(4, 4, 0.25f, 0.25f, 0.75f, 0.75f)+ // Center 2x2 set.+ assertEquals(1f, sel.mask[1 * 4 + 1])+ assertEquals(1f, sel.mask[2 * 4 + 2])+ assertEquals(0f, sel.mask[0])+ assertEquals(0f, sel.mask[15])+ assertFalse(sel.isEmpty())+ }++ @Test+ fun ellipseRasterizes() {+ val sel = Selection.ellipse(8, 8, 0.5f, 0.5f, 0.5f, 0.5f)+ assertEquals(1f, sel.mask[4 * 8 + 4])+ assertEquals(0f, sel.mask[0])+ // Degenerate radii select nothing.+ assertTrue(Selection.ellipse(4, 4, 0.5f, 0.5f, 0f, 0f).isEmpty())+ }++ @Test+ fun polygonRasterizesTriangle() {+ val sel = Selection.polygon(+ 8, 8,+ listOf(0f to 0f, 1f to 0f, 0.5f to 1f),+ )+ // Top row mostly inside, bottom corners outside.+ assertEquals(1f, sel.mask[1 * 8 + 4])+ assertEquals(0f, sel.mask[7 * 8 + 0])+ assertEquals(0f, sel.mask[7 * 8 + 7])+ // Too few vertices selects nothing.+ assertTrue(Selection.polygon(4, 4, listOf(0f to 0f, 1f to 1f)).isEmpty())+ }++ @Test+ fun selectionEqualityIsContentBased() {+ val a = Selection(floatArrayOf(1f, 0f), 2, 1)+ val b = Selection(floatArrayOf(1f, 0f), 2, 1)+ val c = Selection(floatArrayOf(0f, 1f), 2, 1)+ assertEquals(a, b)+ assertEquals(a.hashCode(), b.hashCode())+ assertFalse(a == c)+ assertFalse(a.equals("not a selection"))+ }++ @Test+ fun layerMaskInvertAndFull() {+ val mask = LayerMask(floatArrayOf(0f, 0.5f, 1f), 3, 1)+ val inv = mask.invert()+ assertEquals(listOf(1f, 0.5f, 0f), inv.alphaData.toList())+ // Invert gets a fresh id (cache invalidation).+ assertFalse(inv.id == mask.id)+ val full = LayerMask.full(2, 2)+ assertTrue(full.alphaData.all { it == 1f })+ assertEquals(2, full.width)+ }++ @Test+ fun layerMaskEqualityIsIdBased() {+ val shared = FloatArray(4) { 1f }+ val a = LayerMask(shared, 2, 2, id = "same")+ val b = LayerMask(shared, 2, 2, id = "same")+ val c = LayerMask(shared, 2, 2, id = "different")+ assertEquals(a, b)+ assertEquals(a.hashCode(), b.hashCode())+ assertFalse(a == c)+ }+}
photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2VideoStateTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2VideoStateTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2VideoStateTest.ktnew file mode 100644index 0000000000..5182e1b518--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2VideoStateTest.kt@@ -0,0 +1,96 @@+package com.vayunmathur.photos.photoslice2++import com.vayunmathur.photos.data.PhotoFilter+import com.vayunmathur.photos.data.PhotoFilters+import com.vayunmathur.photos.data.VideoEditState+import com.vayunmathur.photos.data.VideoFilterPreset+import com.vayunmathur.photos.data.VideoTool+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++/**+ * Slice-2 tests for [VideoEditState] predicates, [VideoFilterPreset] /+ * [VideoTool] enums, and the [PhotoFilters] preset catalog. Pure data —+ * no framework needed.+ */+class PhotoSlice2VideoStateTest {++ @Test+ fun defaultStateHasNoEdits() {+ val state = VideoEditState(durationMs = 10_000L, trimEndMs = 10_000L)+ assertFalse(state.isTrimmed)+ assertFalse(state.isCropped)+ assertFalse(state.hasEdits)+ assertFalse(state.hasVideoEffects)+ }++ @Test+ fun trimDetection() {+ val full = VideoEditState(durationMs = 10_000L, trimStartMs = 0L, trimEndMs = 10_000L)+ assertFalse(full.isTrimmed)+ val cutHead = VideoEditState(durationMs = 10_000L, trimStartMs = 1_000L, trimEndMs = 10_000L)+ assertTrue(cutHead.isTrimmed)+ assertTrue(cutHead.hasEdits)+ val cutTail = VideoEditState(durationMs = 10_000L, trimStartMs = 0L, trimEndMs = 9_000L)+ assertTrue(cutTail.isTrimmed)+ // Zero duration never counts as trimmed.+ assertFalse(VideoEditState().isTrimmed)+ }++ @Test+ fun cropDetection() {+ assertFalse(VideoEditState().isCropped)+ // Partial bounds (nulls) are not a crop.+ assertFalse(VideoEditState(cropLeft = 0.1f).isCropped)+ val full = VideoEditState(cropLeft = 0f, cropTop = 0f, cropRight = 1f, cropBottom = 1f)+ assertFalse(full.isCropped)+ val cropped = VideoEditState(cropLeft = 0.1f, cropTop = 0f, cropRight = 1f, cropBottom = 1f)+ assertTrue(cropped.isCropped)+ assertTrue(cropped.hasEdits)+ assertTrue(cropped.hasVideoEffects)+ }++ @Test+ fun editFlags() {+ assertTrue(VideoEditState(rotationDegrees = 90).hasEdits)+ assertTrue(VideoEditState(rotationDegrees = 90).hasVideoEffects)+ assertTrue(VideoEditState(flipHorizontal = true).hasEdits)+ assertTrue(VideoEditState(muted = true).hasEdits)+ // Mute is not a video effect.+ assertFalse(VideoEditState(muted = true).hasVideoEffects)+ assertTrue(VideoEditState(brightness = 0.5f).hasEdits)+ assertTrue(VideoEditState(filterPreset = VideoFilterPreset.Mono).hasEdits)+ assertTrue(VideoEditState(filterPreset = VideoFilterPreset.Vivid).hasVideoEffects)+ }++ @Test+ fun videoToolsAndPresetsEnumerated() {+ assertEquals(4, VideoTool.entries.size)+ assertTrue(VideoTool.entries.contains(VideoTool.Trim))+ assertEquals(5, VideoFilterPreset.entries.size)+ assertEquals(VideoFilterPreset.None, VideoFilterPreset.entries.first())+ }++ @Test+ fun photoFiltersCatalog() {+ assertTrue(PhotoFilters.all.size >= 15, "catalog size=${PhotoFilters.all.size}")+ assertEquals("None", PhotoFilters.all.first().name)+ // Names are unique.+ assertEquals(PhotoFilters.all.size, PhotoFilters.all.map { it.name }.distinct().size)+ // Mono is full desaturation.+ val mono = PhotoFilters.all.first { it.name == "Mono" }+ assertEquals(-100f, mono.adjustments.saturation)+ // None is the identity.+ assertEquals(com.vayunmathur.photos.data.ImageAdjustments(), PhotoFilters.all.first().adjustments)+ }++ @Test+ fun photoFilterDataClass() {+ val f = PhotoFilter("Custom", com.vayunmathur.photos.data.ImageAdjustments(contrast = 10f))+ assertEquals("Custom", f.name)+ assertEquals(10f, f.adjustments.contrast)+ assertEquals(f, f.copy())+ }+}