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.notetask 18 19 import android.app.role.RoleManager 20 import android.content.pm.ApplicationInfo 21 import android.content.pm.PackageManager 22 import android.test.suitebuilder.annotation.SmallTest 23 import androidx.test.runner.AndroidJUnit4 24 import com.android.systemui.SysuiTestCase 25 import com.android.systemui.util.mockito.eq 26 import com.android.systemui.util.mockito.whenever 27 import com.google.common.truth.Truth.assertThat 28 import org.junit.Before 29 import org.junit.Test 30 import org.junit.runner.RunWith 31 import org.mockito.Mock 32 import org.mockito.Mockito.any 33 import org.mockito.MockitoAnnotations 34 35 /** 36 * Tests for [NoteTaskInfoResolver]. 37 * 38 * Build/Install/Run: 39 * - atest SystemUITests:NoteTaskInfoResolverTest 40 */ 41 @SmallTest 42 @RunWith(AndroidJUnit4::class) 43 internal class NoteTaskInfoResolverTest : SysuiTestCase() { 44 45 @Mock lateinit var packageManager: PackageManager 46 @Mock lateinit var roleManager: RoleManager 47 48 private lateinit var underTest: NoteTaskInfoResolver 49 50 @Before 51 fun setUp() { 52 MockitoAnnotations.initMocks(this) 53 underTest = NoteTaskInfoResolver(roleManager, packageManager) 54 } 55 56 @Test 57 fun resolveInfo_shouldReturnInfo() { 58 val packageName = "com.android.note.app" 59 val uid = 123456 60 whenever(roleManager.getRoleHoldersAsUser(RoleManager.ROLE_NOTES, context.user)).then { 61 listOf(packageName) 62 } 63 whenever( 64 packageManager.getApplicationInfoAsUser( 65 eq(packageName), 66 any<PackageManager.ApplicationInfoFlags>(), 67 eq(context.user) 68 ) 69 ) 70 .thenReturn(ApplicationInfo().apply { this.uid = uid }) 71 72 val actual = underTest.resolveInfo(user = context.user) 73 74 requireNotNull(actual) { "Note task info must not be null" } 75 assertThat(actual.packageName).isEqualTo(packageName) 76 assertThat(actual.uid).isEqualTo(uid) 77 assertThat(actual.user).isEqualTo(context.user) 78 } 79 80 @Test 81 fun resolveInfo_packageManagerThrowsException_shouldReturnInfoWithZeroUid() { 82 val packageName = "com.android.note.app" 83 whenever(roleManager.getRoleHoldersAsUser(RoleManager.ROLE_NOTES, context.user)).then { 84 listOf(packageName) 85 } 86 whenever( 87 packageManager.getApplicationInfoAsUser( 88 eq(packageName), 89 any<PackageManager.ApplicationInfoFlags>(), 90 eq(context.user) 91 ) 92 ) 93 .thenThrow(PackageManager.NameNotFoundException(packageName)) 94 95 val actual = underTest.resolveInfo(user = context.user) 96 97 requireNotNull(actual) { "Note task info must not be null" } 98 assertThat(actual.packageName).isEqualTo(packageName) 99 assertThat(actual.uid).isEqualTo(0) 100 assertThat(actual.user).isEqualTo(context.user) 101 } 102 103 @Test 104 fun resolveInfo_noRoleHolderIsSet_shouldReturnNull() { 105 whenever(roleManager.getRoleHoldersAsUser(eq(RoleManager.ROLE_NOTES), any())).then { 106 emptyList<String>() 107 } 108 109 val actual = underTest.resolveInfo(user = context.user) 110 111 assertThat(actual).isNull() 112 } 113 } 114