1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.statusbar.notification.row
18 
19 import android.testing.TestableLooper.RunWithLooper
20 import android.text.PrecomputedText
21 import android.text.TextPaint
22 import android.widget.TextView
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.SysuiTestCase
26 import com.google.common.truth.Truth.assertThat
27 import org.junit.Before
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @SmallTest
32 @RunWith(AndroidJUnit4::class)
33 @RunWithLooper
34 class TextPrecomputerTest : SysuiTestCase() {
35 
36     private lateinit var textPrecomputer: TextPrecomputer
37 
38     private lateinit var textView: TextView
39 
40     @Before
beforenull41     fun before() {
42         textPrecomputer = object : TextPrecomputer {}
43         textView = TextView(mContext)
44     }
45 
46     @Test
precompute_returnRunnablenull47     fun precompute_returnRunnable() {
48         // WHEN
49         val precomputeResult = textPrecomputer.precompute(textView, TEXT)
50 
51         // THEN
52         assertThat(precomputeResult).isInstanceOf(Runnable::class.java)
53     }
54 
55     @Test
precomputeRunnable_anyText_setPrecomputedTextnull56     fun precomputeRunnable_anyText_setPrecomputedText() {
57         // WHEN
58         textPrecomputer.precompute(textView, TEXT).run()
59 
60         // THEN
61         assertThat(textView.text).isInstanceOf(PrecomputedText::class.java)
62     }
63 
64     @Test
precomputeRunnable_differentPrecomputedTextConfig_notSetPrecomputedTextnull65     fun precomputeRunnable_differentPrecomputedTextConfig_notSetPrecomputedText() {
66         // GIVEN
67         val precomputedTextRunnable =
68             textPrecomputer.precompute(textView, TEXT, logException = false)
69 
70         // WHEN
71         textView.textMetricsParams = PrecomputedText.Params.Builder(PAINT).build()
72         precomputedTextRunnable.run()
73 
74         // THEN
75         assertThat(textView.text).isInstanceOf(String::class.java)
76     }
77 
78     @Test
precomputeRunnable_nullText_setNullnull79     fun precomputeRunnable_nullText_setNull() {
80         // GIVEN
81         textView.text = TEXT
82         val precomputedTextRunnable = textPrecomputer.precompute(textView, null)
83 
84         // WHEN
85         precomputedTextRunnable.run()
86 
87         // THEN
88         assertThat(textView.text).isEqualTo("")
89     }
90 
91     private companion object {
92         private val PAINT = TextPaint()
93         private const val TEXT = "Example Notification Test"
94     }
95 }
96