1 /*
2  * Copyright (C) 2022 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 package com.android.systemui.statusbar.notification.row
17 
18 import android.annotation.ColorInt
19 import android.graphics.Color
20 import android.testing.AndroidTestingRunner
21 import android.testing.TestableLooper.RunWithLooper
22 import android.view.View
23 import androidx.test.filters.SmallTest
24 import com.android.settingslib.Utils
25 import com.android.systemui.R
26 import com.android.systemui.SysuiTestCase
27 import com.android.systemui.statusbar.notification.FakeShadowView
28 import com.android.systemui.statusbar.notification.NotificationUtils
29 import com.android.systemui.statusbar.notification.SourceType
30 import com.android.systemui.util.mockito.mock
31 import com.google.common.truth.Truth.assertThat
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 
36 @SmallTest
37 @RunWith(AndroidTestingRunner::class)
38 @RunWithLooper
39 class ActivatableNotificationViewTest : SysuiTestCase() {
40     private val mContentView: View = mock()
41     private lateinit var mView: ActivatableNotificationView
42 
43     @ColorInt
44     private var mNormalColor = 0
45 
46     @Before
47     fun setUp() {
48         mView = object : ActivatableNotificationView(mContext, null) {
49 
50             init {
51                 onFinishInflate()
52             }
53 
54             override fun getContentView(): View {
55                 return mContentView
56             }
57 
58             override fun <T : View> findViewTraversal(id: Int): T? = when (id) {
59                 R.id.backgroundNormal -> mock<NotificationBackgroundView>()
60                 R.id.fake_shadow -> mock<FakeShadowView>()
61                 else -> null
62             } as T?
63         }
64         mNormalColor =
65             Utils.getColorAttrDefaultColor(mContext, com.android.internal.R.attr.colorSurface)
66     }
67 
68     @Test
69     fun testBackgroundBehaviors() {
70         // Color starts with the normal color
71         mView.updateBackgroundColors()
72         assertThat(mView.currentBackgroundTint).isEqualTo(mNormalColor)
73 
74         // Setting a tint changes the background to that color specifically
75         mView.setTintColor(Color.BLUE)
76         assertThat(mView.currentBackgroundTint).isEqualTo(Color.BLUE)
77 
78         // Setting an override tint blends with the previous tint
79         mView.setOverrideTintColor(Color.RED, 0.5f)
80         assertThat(mView.currentBackgroundTint)
81             .isEqualTo(NotificationUtils.interpolateColors(Color.BLUE, Color.RED, 0.5f))
82 
83         // Updating the background colors resets tints, as those won't match the latest theme
84         mView.updateBackgroundColors()
85         assertThat(mView.currentBackgroundTint).isEqualTo(mNormalColor)
86     }
87 
88     @Test
89     fun roundnessShouldBeTheSame_after_onDensityOrFontScaleChanged() {
90         val roundableState = mView.roundableState
91         assertThat(mView.topRoundness).isEqualTo(0f)
92         mView.requestTopRoundness(1f, SourceType.from(""))
93         assertThat(mView.topRoundness).isEqualTo(1f)
94 
95         mView.onDensityOrFontScaleChanged()
96 
97         assertThat(mView.topRoundness).isEqualTo(1f)
98         assertThat(mView.roundableState.hashCode()).isEqualTo(roundableState.hashCode())
99     }
100 }