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.privacy
18 
19 import androidx.test.filters.SmallTest
20 import androidx.test.runner.AndroidJUnit4
21 import com.android.systemui.SysuiTestCase
22 import org.junit.Assert.assertEquals
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 
26 @RunWith(AndroidJUnit4::class)
27 @SmallTest
28 class PrivacyChipBuilderTest : SysuiTestCase() {
29 
30     companion object {
31         val TEST_UID = 1
32     }
33 
34     @Test
35     fun testGenerateAppsList() {
36         val bar2 = PrivacyItem(Privacy.TYPE_CAMERA, PrivacyApplication(
37                 "Bar", TEST_UID))
38         val bar3 = PrivacyItem(Privacy.TYPE_LOCATION, PrivacyApplication(
39                 "Bar", TEST_UID))
40         val foo0 = PrivacyItem(Privacy.TYPE_MICROPHONE, PrivacyApplication(
41                 "Foo", TEST_UID))
42         val baz1 = PrivacyItem(Privacy.TYPE_CAMERA, PrivacyApplication(
43                 "Baz", TEST_UID))
44 
45         val items = listOf(bar2, foo0, baz1, bar3)
46 
47         val textBuilder = PrivacyChipBuilder(context, items)
48 
49         val list = textBuilder.appsAndTypes
50         assertEquals(3, list.size)
51         val appsList = list.map { it.first }
52         val typesList = list.map { it.second }
53         // List is sorted by number of types and then by types
54         assertEquals(listOf("Bar", "Baz", "Foo"), appsList.map { it.packageName })
55         assertEquals(listOf(Privacy.TYPE_CAMERA, Privacy.TYPE_LOCATION), typesList[0])
56         assertEquals(listOf(Privacy.TYPE_CAMERA), typesList[1])
57         assertEquals(listOf(Privacy.TYPE_MICROPHONE), typesList[2])
58     }
59 
60     @Test
61     fun testOrder() {
62         // We want location to always go last, so it will go in the "+ other apps"
63         val appCamera = PrivacyItem(PrivacyType.TYPE_CAMERA,
64                 PrivacyApplication("Camera", TEST_UID))
65         val appMicrophone =
66                 PrivacyItem(PrivacyType.TYPE_MICROPHONE,
67                         PrivacyApplication("Microphone", TEST_UID))
68         val appLocation =
69                 PrivacyItem(PrivacyType.TYPE_LOCATION,
70                         PrivacyApplication("Location", TEST_UID))
71 
72         val items = listOf(appLocation, appMicrophone, appCamera)
73         val textBuilder = PrivacyChipBuilder(context, items)
74         val appList = textBuilder.appsAndTypes.map { it.first }.map { it.packageName }
75         assertEquals(listOf("Camera", "Microphone", "Location"), appList)
76     }
77 }