|
| 1 | +package com.sysadmindoc.callshield.ui |
| 2 | + |
| 3 | +import android.graphics.Bitmap |
| 4 | +import android.graphics.Canvas |
| 5 | +import android.view.View |
| 6 | +import androidx.compose.foundation.layout.Box |
| 7 | +import androidx.compose.foundation.layout.Column |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.runtime.Composable |
| 10 | +import androidx.compose.runtime.CompositionLocalProvider |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.graphics.PixelMap |
| 13 | +import androidx.compose.ui.graphics.asImageBitmap |
| 14 | +import androidx.compose.ui.graphics.toPixelMap |
| 15 | +import androidx.compose.ui.platform.LocalLayoutDirection |
| 16 | +import androidx.compose.ui.platform.LocalView |
| 17 | +import androidx.compose.ui.test.junit4.v2.createComposeRule |
| 18 | +import androidx.compose.ui.test.onNodeWithText |
| 19 | +import androidx.compose.ui.test.onRoot |
| 20 | +import androidx.compose.ui.unit.LayoutDirection |
| 21 | +import com.sysadmindoc.callshield.data.model.BlockedCall |
| 22 | +import com.sysadmindoc.callshield.ui.screens.main.GroupedCallItem |
| 23 | +import com.sysadmindoc.callshield.ui.screens.stats.RegionTileMap |
| 24 | +import com.sysadmindoc.callshield.ui.screens.stats.WeeklyBarChart |
| 25 | +import org.junit.Assert.assertEquals |
| 26 | +import org.junit.Assert.assertTrue |
| 27 | +import org.junit.Rule |
| 28 | +import org.junit.Test |
| 29 | +import org.junit.runner.RunWith |
| 30 | +import org.robolectric.RobolectricTestRunner |
| 31 | +import org.robolectric.annotation.Config |
| 32 | +import org.robolectric.annotation.GraphicsMode |
| 33 | + |
| 34 | +/** Canvas drawing doesn't mirror with the layout, so these read the drawn pixels on both sides. */ |
| 35 | +@RunWith(RobolectricTestRunner::class) |
| 36 | +@Config(sdk = [35], qualifiers = "w420dp-h900dp-xhdpi") |
| 37 | +@GraphicsMode(GraphicsMode.Mode.NATIVE) |
| 38 | +class RightToLeftDrawingTest { |
| 39 | + @get:Rule |
| 40 | + val composeRule = createComposeRule() |
| 41 | + |
| 42 | + /** Draws straight to a bitmap, since captureToImage waits for a frame Robolectric never delivers. */ |
| 43 | + private fun render( |
| 44 | + direction: LayoutDirection, |
| 45 | + content: @Composable () -> Unit, |
| 46 | + ): PixelMap { |
| 47 | + lateinit var root: View |
| 48 | + composeRule.setContent { |
| 49 | + root = LocalView.current |
| 50 | + CompositionLocalProvider(LocalLayoutDirection provides direction, content = content) |
| 51 | + } |
| 52 | + return composeRule.runOnIdle { |
| 53 | + val bitmap = Bitmap.createBitmap(root.width, root.height, Bitmap.Config.ARGB_8888) |
| 54 | + root.draw(Canvas(bitmap)) |
| 55 | + bitmap.asImageBitmap().toPixelMap() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** The mean column of the matching pixels, as a share of the width. */ |
| 60 | + private fun PixelMap.centerOf(matches: (red: Float, green: Float, blue: Float) -> Boolean): Float { |
| 61 | + var columns = 0L |
| 62 | + var count = 0L |
| 63 | + for (y in 0 until height) { |
| 64 | + for (x in 0 until width) { |
| 65 | + val pixel = this[x, y] |
| 66 | + if (matches(pixel.red, pixel.green, pixel.blue)) { |
| 67 | + columns += x |
| 68 | + count++ |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + assertTrue("nothing was drawn in the color this test looks for", count > 0) |
| 73 | + return columns.toFloat() / count / width |
| 74 | + } |
| 75 | + |
| 76 | + /** The column with the most warm pixels, as a share of the width. A log row's strip runs its full height. */ |
| 77 | + private fun PixelMap.tallestWarmColumn(): Float { |
| 78 | + val warmPixels = IntArray(width) |
| 79 | + for (y in 0 until height) { |
| 80 | + for (x in 0 until width) { |
| 81 | + val pixel = this[x, y] |
| 82 | + if (pixel.red - pixel.blue > 0.06f) warmPixels[x]++ |
| 83 | + } |
| 84 | + } |
| 85 | + val tallest = warmPixels.indices.maxBy { warmPixels[it] } |
| 86 | + assertTrue("nothing warm was drawn", warmPixels[tallest] > 0) |
| 87 | + return tallest.toFloat() / width |
| 88 | + } |
| 89 | + |
| 90 | + private fun renderChart(direction: LayoutDirection): Float = |
| 91 | + render(direction) { |
| 92 | + Column(Modifier.fillMaxWidth()) { |
| 93 | + WeeklyBarChart(listOf("Sun" to 10, "Mon" to 0, "Tue" to 0, "Wed" to 0)) |
| 94 | + } |
| 95 | + }.centerOf { red, green, blue -> green - red > 0.2f && green - blue > 0.1f } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `the first day's bar sits on the left in English`() { |
| 99 | + val center = renderChart(LayoutDirection.Ltr) |
| 100 | + |
| 101 | + assertTrue("center at $center", center < 0.25f) |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `the first day's bar sits on the right in Arabic, over its label`() { |
| 106 | + val center = renderChart(LayoutDirection.Rtl) |
| 107 | + |
| 108 | + assertTrue("center at $center", center > 0.75f) |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `a day's label sits centered under its bar`() { |
| 113 | + val bar = renderChart(LayoutDirection.Ltr) |
| 114 | + |
| 115 | + val label = composeRule.onNodeWithText("Sun").fetchSemanticsNode().boundsInRoot.center.x |
| 116 | + val width = composeRule.onRoot().fetchSemanticsNode().size.width |
| 117 | + |
| 118 | + assertEquals(bar, label / width, 0.01f) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun `California stays on the west side of the map in Arabic`() { |
| 123 | + val center = |
| 124 | + render(LayoutDirection.Rtl) { |
| 125 | + Box(Modifier.fillMaxWidth()) { RegionTileMap(mapOf("CA" to 10)) } |
| 126 | + }.centerOf { red, _, blue -> red - blue > 0.2f } |
| 127 | + |
| 128 | + assertTrue("center at $center", center < 0.25f) |
| 129 | + } |
| 130 | + |
| 131 | + private fun renderRow(direction: LayoutDirection): Float = |
| 132 | + render(direction) { |
| 133 | + Box(Modifier.fillMaxWidth()) { |
| 134 | + GroupedCallItem( |
| 135 | + call = BlockedCall(number = "+14155550123", type = "robocall", matchReason = "database"), |
| 136 | + count = 1, |
| 137 | + onTap = {}, |
| 138 | + onBlock = {}, |
| 139 | + ) |
| 140 | + } |
| 141 | + }.tallestWarmColumn() |
| 142 | + |
| 143 | + @Test |
| 144 | + fun `a log row's accent strip is on the left in English`() { |
| 145 | + val column = renderRow(LayoutDirection.Ltr) |
| 146 | + |
| 147 | + assertTrue("strip at $column", column < 0.05f) |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + fun `a log row's accent strip moves to the right in Arabic, where its padding went`() { |
| 152 | + val column = renderRow(LayoutDirection.Rtl) |
| 153 | + |
| 154 | + assertTrue("strip at $column", column > 0.95f) |
| 155 | + } |
| 156 | +} |
0 commit comments