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 
17 package com.android.systemui.decor
18 
19 import android.content.res.TypedArray
20 import android.graphics.drawable.Drawable
21 import android.testing.AndroidTestingRunner
22 import android.util.Size
23 import androidx.annotation.DrawableRes
24 import androidx.test.filters.SmallTest
25 import com.android.internal.R as InternalR
26 import com.android.systemui.R as SystemUIR
27 import com.android.systemui.SysuiTestCase
28 import com.android.systemui.tests.R
29 import org.junit.Assert.assertEquals
30 import org.junit.Before
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.Mock
34 import org.mockito.MockitoAnnotations
35 
36 @RunWith(AndroidTestingRunner::class)
37 @SmallTest
38 class RoundedCornerResDelegateTest : SysuiTestCase() {
39 
40     private lateinit var roundedCornerResDelegate: RoundedCornerResDelegate
41     @Mock private lateinit var mockTypedArray: TypedArray
42 
43     @Before
44     fun setUp() {
45         MockitoAnnotations.initMocks(this)
46     }
47 
48     @Test
49     fun testTopAndBottomRoundedCornerExist() {
50         setupResources(radius = 5)
51         roundedCornerResDelegate = RoundedCornerResDelegateImpl(mContext.resources, null)
52         assertEquals(true, roundedCornerResDelegate.hasTop)
53         assertEquals(true, roundedCornerResDelegate.hasBottom)
54     }
55 
56     @Test
57     fun testTopRoundedCornerExist() {
58         setupResources(radiusTop = 10)
59         roundedCornerResDelegate = RoundedCornerResDelegateImpl(mContext.resources, null)
60         assertEquals(true, roundedCornerResDelegate.hasTop)
61         assertEquals(false, roundedCornerResDelegate.hasBottom)
62     }
63 
64     @Test
65     fun testBottomRoundedCornerExist() {
66         setupResources(radiusBottom = 15)
67         roundedCornerResDelegate = RoundedCornerResDelegateImpl(mContext.resources, null)
68         assertEquals(false, roundedCornerResDelegate.hasTop)
69         assertEquals(true, roundedCornerResDelegate.hasBottom)
70     }
71 
72     @Test
73     fun testUpdateDisplayUniqueId() {
74         setupResources(radius = 100,
75                 roundedTopDrawable = getTestsDrawable(R.drawable.rounded3px),
76                 roundedBottomDrawable = getTestsDrawable(R.drawable.rounded4px))
77 
78         roundedCornerResDelegate = RoundedCornerResDelegateImpl(mContext.resources, null)
79 
80         assertEquals(Size(3, 3), roundedCornerResDelegate.topRoundedSize)
81         assertEquals(Size(4, 4), roundedCornerResDelegate.bottomRoundedSize)
82 
83         setupResources(radius = 100,
84                 roundedTopDrawable = getTestsDrawable(R.drawable.rounded4px),
85                 roundedBottomDrawable = getTestsDrawable(R.drawable.rounded5px))
86 
87         roundedCornerResDelegate.updateDisplayUniqueId("test", null)
88 
89         assertEquals(Size(4, 4), roundedCornerResDelegate.topRoundedSize)
90         assertEquals(Size(5, 5), roundedCornerResDelegate.bottomRoundedSize)
91     }
92 
93     @Test
94     fun testNotUpdateDisplayUniqueIdButChangeRefreshToken() {
95         setupResources(radius = 100,
96                 roundedTopDrawable = getTestsDrawable(R.drawable.rounded3px),
97                 roundedBottomDrawable = getTestsDrawable(R.drawable.rounded4px))
98 
99         roundedCornerResDelegate = RoundedCornerResDelegateImpl(mContext.resources, null)
100 
101         assertEquals(Size(3, 3), roundedCornerResDelegate.topRoundedSize)
102         assertEquals(Size(4, 4), roundedCornerResDelegate.bottomRoundedSize)
103 
104         roundedCornerResDelegate.physicalPixelDisplaySizeRatio = 2f
105         roundedCornerResDelegate.updateDisplayUniqueId(null, 1)
106 
107         assertEquals(Size(6, 6), roundedCornerResDelegate.topRoundedSize)
108         assertEquals(Size(8, 8), roundedCornerResDelegate.bottomRoundedSize)
109     }
110 
111     @Test
112     fun testPhysicalPixelDisplaySizeChanged() {
113         setupResources(
114                 roundedTopDrawable = getTestsDrawable(R.drawable.rounded4px),
115                 roundedBottomDrawable = getTestsDrawable(R.drawable.rounded4px))
116 
117         roundedCornerResDelegate = RoundedCornerResDelegateImpl(mContext.resources, null)
118         assertEquals(Size(4, 4), roundedCornerResDelegate.topRoundedSize)
119         assertEquals(Size(4, 4), roundedCornerResDelegate.bottomRoundedSize)
120 
121         roundedCornerResDelegate.physicalPixelDisplaySizeRatio = 0.5f
122 
123         assertEquals(Size(2, 2), roundedCornerResDelegate.topRoundedSize)
124         assertEquals(Size(2, 2), roundedCornerResDelegate.bottomRoundedSize)
125     }
126 
127     private fun getTestsDrawable(@DrawableRes drawableId: Int): Drawable? {
128         return mContext.createPackageContext("com.android.systemui.tests", 0)
129                 .getDrawable(drawableId)
130     }
131 
132     private fun setupResources(
133         radius: Int? = null,
134         radiusTop: Int? = null,
135         radiusBottom: Int? = null,
136         roundedTopDrawable: Drawable? = null,
137         roundedBottomDrawable: Drawable? = null
138     ) {
139         mContext.orCreateTestableResources.let { res ->
140             res.addOverride(InternalR.array.config_displayUniqueIdArray, arrayOf<String>())
141             res.addOverride(InternalR.array.config_roundedCornerRadiusArray, mockTypedArray)
142             res.addOverride(InternalR.array.config_roundedCornerTopRadiusArray, mockTypedArray)
143             res.addOverride(InternalR.array.config_roundedCornerBottomRadiusArray, mockTypedArray)
144             res.addOverride(SystemUIR.array.config_roundedCornerDrawableArray, mockTypedArray)
145             res.addOverride(SystemUIR.array.config_roundedCornerTopDrawableArray, mockTypedArray)
146             res.addOverride(SystemUIR.array.config_roundedCornerBottomDrawableArray, mockTypedArray)
147             res.addOverride(com.android.internal.R.dimen.rounded_corner_radius, radius ?: 0)
148             res.addOverride(com.android.internal.R.dimen.rounded_corner_radius_top, radiusTop ?: 0)
149             res.addOverride(com.android.internal.R.dimen.rounded_corner_radius_bottom,
150                     radiusBottom ?: 0)
151             roundedTopDrawable?.let { drawable ->
152                 res.addOverride(SystemUIR.drawable.rounded_corner_top, drawable)
153             }
154             roundedBottomDrawable?.let { drawable ->
155                 res.addOverride(SystemUIR.drawable.rounded_corner_bottom, drawable)
156             }
157         }
158     }
159 }
160