1 /*
2  * Copyright (C) 2020 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.controls
18 
19 import android.content.ComponentName
20 import android.graphics.drawable.Icon
21 import android.testing.AndroidTestingRunner
22 import androidx.test.filters.SmallTest
23 import com.android.systemui.SysuiTestCase
24 import org.junit.Assert.assertNull
25 import org.junit.Assert.assertTrue
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.mockito.Mock
30 import org.mockito.MockitoAnnotations
31 
32 @SmallTest
33 @RunWith(AndroidTestingRunner::class)
34 class CustomIconCacheTest : SysuiTestCase() {
35 
36     companion object {
37         private val TEST_COMPONENT1 = ComponentName.unflattenFromString("pkg/.cls1")!!
38         private val TEST_COMPONENT2 = ComponentName.unflattenFromString("pkg/.cls2")!!
39         private const val CONTROL_ID_1 = "TEST_CONTROL_1"
40         private const val CONTROL_ID_2 = "TEST_CONTROL_2"
41     }
42 
43     @Mock(stubOnly = true)
44     private lateinit var icon1: Icon
45     @Mock(stubOnly = true)
46     private lateinit var icon2: Icon
47     private lateinit var customIconCache: CustomIconCache
48 
49     @Before
50     fun setUp() {
51         MockitoAnnotations.initMocks(this)
52 
53         customIconCache = CustomIconCache()
54     }
55 
56     @Test
57     fun testIconStoredCorrectly() {
58         customIconCache.store(TEST_COMPONENT1, CONTROL_ID_1, icon1)
59 
60         assertTrue(icon1 === customIconCache.retrieve(TEST_COMPONENT1, CONTROL_ID_1))
61     }
62 
63     @Test
64     fun testIconNotStoredReturnsNull() {
65         customIconCache.store(TEST_COMPONENT1, CONTROL_ID_1, icon1)
66 
67         assertNull(customIconCache.retrieve(TEST_COMPONENT1, CONTROL_ID_2))
68     }
69 
70     @Test
71     fun testWrongComponentReturnsNull() {
72         customIconCache.store(TEST_COMPONENT1, CONTROL_ID_1, icon1)
73 
74         assertNull(customIconCache.retrieve(TEST_COMPONENT2, CONTROL_ID_1))
75     }
76 
77     @Test
78     fun testChangeComponentOldComponentIsRemoved() {
79         customIconCache.store(TEST_COMPONENT1, CONTROL_ID_1, icon1)
80         customIconCache.store(TEST_COMPONENT2, CONTROL_ID_2, icon2)
81 
82         assertNull(customIconCache.retrieve(TEST_COMPONENT1, CONTROL_ID_1))
83         assertNull(customIconCache.retrieve(TEST_COMPONENT1, CONTROL_ID_2))
84     }
85 
86     @Test
87     fun testChangeComponentCorrectIconRetrieved() {
88         customIconCache.store(TEST_COMPONENT1, CONTROL_ID_1, icon1)
89         customIconCache.store(TEST_COMPONENT2, CONTROL_ID_1, icon2)
90 
91         assertTrue(icon2 === customIconCache.retrieve(TEST_COMPONENT2, CONTROL_ID_1))
92     }
93 
94     @Test
95     fun testStoreNull() {
96         customIconCache.store(TEST_COMPONENT1, CONTROL_ID_1, icon1)
97         customIconCache.store(TEST_COMPONENT1, CONTROL_ID_1, null)
98 
99         assertNull(customIconCache.retrieve(TEST_COMPONENT1, CONTROL_ID_1))
100     }
101 }