|
1 | 1 | package io.sentry.android.replay.util
|
2 | 2 |
|
| 3 | +import android.app.Activity |
3 | 4 | import android.graphics.Color
|
| 5 | +import android.os.Bundle |
| 6 | +import android.os.Looper |
4 | 7 | import android.text.SpannableString
|
5 | 8 | import android.text.Spanned
|
6 | 9 | import android.text.style.ForegroundColorSpan
|
| 10 | +import android.widget.LinearLayout |
| 11 | +import android.widget.LinearLayout.LayoutParams |
7 | 12 | import android.widget.TextView
|
8 |
| -import androidx.test.core.app.ApplicationProvider |
9 | 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 |
10 | 17 | import org.junit.runner.RunWith
|
| 18 | +import org.robolectric.Robolectric.buildActivity |
| 19 | +import org.robolectric.Shadows.shadowOf |
11 | 20 | import org.robolectric.annotation.Config
|
12 | 21 | import kotlin.test.Test
|
13 | 22 | import kotlin.test.assertEquals
|
| 23 | +import kotlin.test.assertNull |
| 24 | +import kotlin.test.assertTrue |
14 | 25 |
|
15 | 26 | @RunWith(AndroidJUnit4::class)
|
16 | 27 | @Config(sdk = [30])
|
17 | 28 | class TextViewDominantColorTest {
|
18 | 29 |
|
19 | 30 | @Test
|
20 | 31 | fun `when no spans, returns currentTextColor`() {
|
21 |
| - val textView = TextView(ApplicationProvider.getApplicationContext()) |
22 |
| - textView.text = "Hello, World!" |
23 |
| - textView.setTextColor(Color.WHITE) |
| 32 | + val controller = buildActivity(TextViewActivity::class.java, null).setup() |
| 33 | + controller.create().start().resume() |
24 | 34 |
|
25 |
| - assertEquals(Color.WHITE, textView.dominantTextColor) |
| 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) |
26 | 40 | }
|
27 | 41 |
|
28 | 42 | @Test
|
29 | 43 | fun `when has a foreground color span, returns its color`() {
|
30 |
| - val textView = TextView(ApplicationProvider.getApplicationContext()) |
| 44 | + val controller = buildActivity(TextViewActivity::class.java, null).setup() |
| 45 | + controller.create().start().resume() |
| 46 | + |
31 | 47 | val text = "Hello, World!"
|
32 |
| - textView.text = SpannableString(text).apply { |
| 48 | + TextViewActivity.textView?.text = SpannableString(text).apply { |
33 | 49 | setSpan(ForegroundColorSpan(Color.RED), 0, text.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
|
34 | 50 | }
|
35 |
| - textView.setTextColor(Color.WHITE) |
| 51 | + TextViewActivity.textView?.setTextColor(Color.WHITE) |
| 52 | + TextViewActivity.textView?.requestLayout() |
36 | 53 |
|
37 |
| - assertEquals(Color.RED, textView.dominantTextColor) |
| 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) |
38 | 59 | }
|
39 | 60 |
|
40 | 61 | @Test
|
41 | 62 | fun `when has multiple foreground color spans, returns color of the longest span`() {
|
42 |
| - val textView = TextView(ApplicationProvider.getApplicationContext()) |
| 63 | + val controller = buildActivity(TextViewActivity::class.java, null).setup() |
| 64 | + controller.create().start().resume() |
| 65 | + |
43 | 66 | val text = "Hello, World!"
|
44 |
| - textView.text = SpannableString(text).apply { |
| 67 | + TextViewActivity.textView?.text = SpannableString(text).apply { |
45 | 68 | setSpan(ForegroundColorSpan(Color.RED), 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
|
46 | 69 | setSpan(ForegroundColorSpan(Color.BLACK), 6, text.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
|
47 | 70 | }
|
48 |
| - textView.setTextColor(Color.WHITE) |
| 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) |
49 | 101 |
|
50 |
| - assertEquals(Color.BLACK, textView.dominantTextColor) |
| 102 | + setContentView(linearLayout) |
51 | 103 | }
|
52 | 104 | }
|
0 commit comments