|
| 1 | +package me.rosuh.easywatermark.render |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.graphics.Bitmap |
| 5 | +import android.graphics.Color |
| 6 | +import android.graphics.Paint |
| 7 | +import android.net.Uri |
| 8 | +import android.text.TextPaint |
| 9 | +import kotlinx.coroutines.Dispatchers |
| 10 | +import kotlinx.coroutines.runBlocking |
| 11 | +import me.rosuh.easywatermark.data.model.ImageInfo |
| 12 | +import me.rosuh.easywatermark.data.model.WaterMark |
| 13 | +import me.rosuh.easywatermark.ui.widget.utils.WaterMarkShader |
| 14 | +import me.rosuh.easywatermark.utils.ktx.applyConfig |
| 15 | +import org.junit.Assert.assertEquals |
| 16 | +import org.junit.Assert.assertTrue |
| 17 | +import org.junit.Test |
| 18 | +import org.junit.runner.RunWith |
| 19 | +import org.robolectric.RobolectricTestRunner |
| 20 | +import org.robolectric.annotation.Config |
| 21 | +import org.robolectric.annotation.GraphicsMode |
| 22 | + |
| 23 | +/** |
| 24 | + * S3a — image-space `textSize` behavior. `textPx = textSize * imageInfo.width / REF_WIDTH` |
| 25 | + * (`WatermarkRenderer.REF_WIDTH = 1000`). These tests pin the NEW behavior and **fail on the old |
| 26 | + * view-scale-dependent behavior**: |
| 27 | + * - old text: `textSize` raw (preview) / `textSize * imageInfo.scaleX` (export) — depended on the |
| 28 | + * preview matrix and ignored image width; |
| 29 | + * - old icon: `(scale ? imageInfo.scaleX : 1) * textSize/14` — depended on view scale at export. |
| 30 | + * |
| 31 | + * The new behavior is independent of `imageInfo.scaleX` and of the `isScale`/`scale` flags, and scales |
| 32 | + * linearly with `imageInfo.width`. At the reference width (1000, used by every existing golden) the |
| 33 | + * text paint size equals the legacy unscaled size, so existing goldens are unchanged. |
| 34 | + */ |
| 35 | +@RunWith(RobolectricTestRunner::class) |
| 36 | +@Config(sdk = [34], application = Application::class) |
| 37 | +@GraphicsMode(GraphicsMode.Mode.NATIVE) |
| 38 | +class WatermarkImageSpaceSizingTest { |
| 39 | + |
| 40 | + private fun config(size: Float = 24f) = WaterMark.default.copy( |
| 41 | + text = "GOLDEN", degree = 0f, hGap = 0, vGap = 0, |
| 42 | + textSize = size, textColor = Color.WHITE, iconUri = Uri.EMPTY, |
| 43 | + ) |
| 44 | + |
| 45 | + private fun imageInfo(width: Int, scaleX: Float = 1f) = |
| 46 | + ImageInfo.empty().apply { this.width = width; this.height = width; this.scaleX = scaleX } |
| 47 | + |
| 48 | + // ---- direct paint sizing (the precise old-vs-new pin) -------------------------------------- |
| 49 | + |
| 50 | + @Test |
| 51 | + fun text_paint_size_equals_legacy_at_reference_width() { |
| 52 | + // At REF_WIDTH=1000 the image-space size reproduces the legacy unscaled paint size exactly. |
| 53 | + val p = TextPaint().applyConfig(imageInfo(width = 1000), config(size = 24f), isScale = false) |
| 54 | + assertEquals(24f, p.textSize, 0.001f) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun text_paint_size_scales_linearly_with_image_width() { |
| 59 | + // 2x image width -> 2x text px. (OLD isScale=true ignored width -> would stay 24f -> fails.) |
| 60 | + val p1000 = TextPaint().applyConfig(imageInfo(1000), config(24f), isScale = true) |
| 61 | + val p2000 = TextPaint().applyConfig(imageInfo(2000), config(24f), isScale = true) |
| 62 | + val p500 = TextPaint().applyConfig(imageInfo(500), config(24f), isScale = true) |
| 63 | + assertEquals(24f, p1000.textSize, 0.001f) |
| 64 | + assertEquals(48f, p2000.textSize, 0.001f) |
| 65 | + assertEquals(12f, p500.textSize, 0.001f) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun text_paint_size_independent_of_view_scaleX() { |
| 70 | + // OLD export path multiplied by imageInfo.scaleX; new path must ignore it. |
| 71 | + val a = TextPaint().applyConfig(imageInfo(1000, scaleX = 1f), config(24f), isScale = false) |
| 72 | + val b = TextPaint().applyConfig(imageInfo(1000, scaleX = 4f), config(24f), isScale = false) |
| 73 | + assertEquals(a.textSize, b.textSize, 0.001f) |
| 74 | + assertEquals(24f, b.textSize, 0.001f) // not 24*4 |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun text_paint_size_independent_of_isScale_flag() { |
| 79 | + // Preview (isScale=true) and export (isScale=false) now use the SAME formula. |
| 80 | + val preview = TextPaint().applyConfig(imageInfo(2000, scaleX = 3f), config(24f), isScale = true) |
| 81 | + val export = TextPaint().applyConfig(imageInfo(2000, scaleX = 3f), config(24f), isScale = false) |
| 82 | + assertEquals(preview.textSize, export.textSize, 0.001f) |
| 83 | + assertEquals(48f, export.textSize, 0.001f) // 24 * 2000/1000 |
| 84 | + } |
| 85 | + |
| 86 | + // ---- rendered text cell end-to-end -------------------------------------------------------- |
| 87 | + |
| 88 | + private fun textCell(width: Int, scaleX: Float, size: Float = 24f): WaterMarkShader { |
| 89 | + val info = imageInfo(width, scaleX) |
| 90 | + val paint = TextPaint().applyConfig(info, config(size), isScale = false) |
| 91 | + return runBlocking { |
| 92 | + WatermarkRenderer.buildTextShader(info, config(size), paint, Dispatchers.Unconfined) |
| 93 | + }!! |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun text_cell_grows_with_image_width() { |
| 98 | + val c1000 = textCell(width = 1000, scaleX = 1f) |
| 99 | + val c2000 = textCell(width = 2000, scaleX = 1f) |
| 100 | + // ~2x paint size -> ~2x cell (allow rounding slack from .toInt()/StaticLayout). |
| 101 | + assertTrue("2000px-image cell must be larger", c2000.width > c1000.width && c2000.height > c1000.height) |
| 102 | + val ratio = c2000.width.toFloat() / c1000.width |
| 103 | + assertTrue("cell width ratio ~2x (was $ratio)", ratio in 1.8f..2.2f) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun text_cell_independent_of_view_scaleX() { |
| 108 | + val a = textCell(width = 1000, scaleX = 1f) |
| 109 | + val b = textCell(width = 1000, scaleX = 5f) |
| 110 | + assertEquals("cell width must not depend on scaleX", a.width, b.width) |
| 111 | + assertEquals("cell height must not depend on scaleX", a.height, b.height) |
| 112 | + } |
| 113 | + |
| 114 | + // ---- icon cell ---------------------------------------------------------------------------- |
| 115 | + |
| 116 | + private fun iconCell(scale: Boolean, scaleX: Float, size: Float = 14f): WaterMarkShader { |
| 117 | + val info = imageInfo(1000, scaleX) |
| 118 | + val src = Bitmap.createBitmap(40, 20, Bitmap.Config.ARGB_8888).apply { eraseColor(Color.WHITE) } |
| 119 | + return runBlocking { |
| 120 | + WatermarkRenderer.buildIconShader(info, src, config(size), Paint(), scale, Dispatchers.Unconfined) |
| 121 | + }!! |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun icon_cell_independent_of_view_scale_and_scaleX() { |
| 126 | + // OLD: scale=true multiplied by imageInfo.scaleX -> bigger at export. NEW: textSize/14 always. |
| 127 | + val previewLike = iconCell(scale = false, scaleX = 1f) |
| 128 | + val exportLike = iconCell(scale = true, scaleX = 4f) |
| 129 | + assertEquals("icon cell must not depend on scale flag/scaleX", previewLike.width, exportLike.width) |
| 130 | + assertEquals("icon cell must not depend on scale flag/scaleX", previewLike.height, exportLike.height) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun icon_cell_preserves_textsize_over_14_ratio() { |
| 135 | + // textSize 28 -> 2x of textSize 14 (ratio semantics preserved). |
| 136 | + val x1 = iconCell(scale = false, scaleX = 1f, size = 14f) |
| 137 | + val x2 = iconCell(scale = false, scaleX = 1f, size = 28f) |
| 138 | + val ratio = x2.width.toFloat() / x1.width |
| 139 | + assertTrue("icon ratio ~2x (was $ratio)", ratio in 1.8f..2.2f) |
| 140 | + } |
| 141 | +} |
0 commit comments