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.controls.ui
18 
19 import android.content.ContentProvider
20 import android.graphics.Bitmap
21 import android.graphics.drawable.Icon
22 import android.net.Uri
23 import android.os.UserHandle
24 import android.testing.AndroidTestingRunner
25 import androidx.test.filters.SmallTest
26 import com.android.systemui.SysuiTestCase
27 import com.google.common.truth.Truth.assertThat
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @SmallTest
32 @RunWith(AndroidTestingRunner::class)
33 class CanUseIconPredicateTest : SysuiTestCase() {
34 
35     private companion object {
36         const val USER_ID_1 = 1
37         const val USER_ID_2 = 2
38     }
39 
40     val underTest: CanUseIconPredicate = CanUseIconPredicate(USER_ID_1)
41 
42     @Test
43     fun testReturnsFalseForDifferentUser() {
44         val user2Icon =
45             Icon.createWithContentUri(
46                 ContentProvider.createContentUriForUser(
47                     Uri.parse("content://test"),
48                     UserHandle.of(USER_ID_2)
49                 )
50             )
51 
52         assertThat(underTest.invoke(user2Icon)).isFalse()
53     }
54 
55     @Test
56     fun testReturnsTrueForCorrectUser() {
57         val user1Icon =
58             Icon.createWithContentUri(
59                 ContentProvider.createContentUriForUser(
60                     Uri.parse("content://test"),
61                     UserHandle.of(USER_ID_1)
62                 )
63             )
64 
65         assertThat(underTest.invoke(user1Icon)).isTrue()
66     }
67 
68     @Test
69     fun testReturnsTrueForUriWithoutUser() {
70         val uriIcon = Icon.createWithContentUri(Uri.parse("content://test"))
71 
72         assertThat(underTest.invoke(uriIcon)).isTrue()
73     }
74 
75     @Test
76     fun testReturnsTrueForNonUriIcon() {
77         val bitmapIcon = Icon.createWithBitmap(Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888))
78 
79         assertThat(underTest.invoke(bitmapIcon)).isTrue()
80     }
81 }
82