|
| 1 | +package io.sentry.android.replay.util |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.graphics.Color |
| 5 | +import android.os.Bundle |
| 6 | +import android.os.Looper |
| 7 | +import android.text.SpannableString |
| 8 | +import android.text.Spanned |
| 9 | +import android.text.style.ForegroundColorSpan |
| 10 | +import android.widget.LinearLayout |
| 11 | +import android.widget.LinearLayout.LayoutParams |
| 12 | +import android.widget.TextView |
| 13 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 14 | +import io.sentry.SentryOptions |
| 15 | +import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode |
| 16 | +import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.TextViewHierarchyNode |
| 17 | +import org.junit.runner.RunWith |
| 18 | +import org.robolectric.Robolectric.buildActivity |
| 19 | +import org.robolectric.Shadows.shadowOf |
| 20 | +import org.robolectric.annotation.Config |
| 21 | +import kotlin.test.Test |
| 22 | +import kotlin.test.assertEquals |
| 23 | +import kotlin.test.assertNull |
| 24 | +import kotlin.test.assertTrue |
| 25 | + |
| 26 | +@RunWith(AndroidJUnit4::class) |
| 27 | +@Config(sdk = [30]) |
| 28 | +class TextViewDominantColorTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `when no spans, returns currentTextColor`() { |
| 32 | + val controller = buildActivity(TextViewActivity::class.java, null).setup() |
| 33 | + controller.create().start().resume() |
| 34 | + |
| 35 | + TextViewActivity.textView?.setTextColor(Color.WHITE) |
| 36 | + |
| 37 | + val node = ViewHierarchyNode.fromView(TextViewActivity.textView!!, null, 0, SentryOptions()) |
| 38 | + assertTrue(node is TextViewHierarchyNode) |
| 39 | + assertNull(node.layout.dominantTextColor) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `when has a foreground color span, returns its color`() { |
| 44 | + val controller = buildActivity(TextViewActivity::class.java, null).setup() |
| 45 | + controller.create().start().resume() |
| 46 | + |
| 47 | + val text = "Hello, World!" |
| 48 | + TextViewActivity.textView?.text = SpannableString(text).apply { |
| 49 | + setSpan(ForegroundColorSpan(Color.RED), 0, text.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE) |
| 50 | + } |
| 51 | + TextViewActivity.textView?.setTextColor(Color.WHITE) |
| 52 | + TextViewActivity.textView?.requestLayout() |
| 53 | + |
| 54 | + shadowOf(Looper.getMainLooper()).idle() |
| 55 | + |
| 56 | + val node = ViewHierarchyNode.fromView(TextViewActivity.textView!!, null, 0, SentryOptions()) |
| 57 | + assertTrue(node is TextViewHierarchyNode) |
| 58 | + assertEquals(Color.RED, node.layout.dominantTextColor) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `when has multiple foreground color spans, returns color of the longest span`() { |
| 63 | + val controller = buildActivity(TextViewActivity::class.java, null).setup() |
| 64 | + controller.create().start().resume() |
| 65 | + |
| 66 | + val text = "Hello, World!" |
| 67 | + TextViewActivity.textView?.text = SpannableString(text).apply { |
| 68 | + setSpan(ForegroundColorSpan(Color.RED), 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE) |
| 69 | + setSpan(ForegroundColorSpan(Color.BLACK), 6, text.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE) |
| 70 | + } |
| 71 | + TextViewActivity.textView?.setTextColor(Color.WHITE) |
| 72 | + TextViewActivity.textView?.requestLayout() |
| 73 | + |
| 74 | + shadowOf(Looper.getMainLooper()).idle() |
| 75 | + |
| 76 | + val node = ViewHierarchyNode.fromView(TextViewActivity.textView!!, null, 0, SentryOptions()) |
| 77 | + assertTrue(node is TextViewHierarchyNode) |
| 78 | + assertEquals(Color.BLACK, node.layout.dominantTextColor) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +private class TextViewActivity : Activity() { |
| 83 | + |
| 84 | + companion object { |
| 85 | + var textView: TextView? = null |
| 86 | + } |
| 87 | + |
| 88 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 89 | + super.onCreate(savedInstanceState) |
| 90 | + val linearLayout = LinearLayout(this).apply { |
| 91 | + setBackgroundColor(android.R.color.white) |
| 92 | + orientation = LinearLayout.VERTICAL |
| 93 | + layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) |
| 94 | + } |
| 95 | + |
| 96 | + textView = TextView(this).apply { |
| 97 | + text = "Hello, World!" |
| 98 | + layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) |
| 99 | + } |
| 100 | + linearLayout.addView(textView) |
| 101 | + |
| 102 | + setContentView(linearLayout) |
| 103 | + } |
| 104 | +} |
0 commit comments