1 /* 2 * Copyright (C) 2024 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.server.deviceconfig; 18 19 import static com.android.server.deviceconfig.ServiceResourcesHelper.RESOURCES_PACKAGE_SUFFIX; 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.times; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.content.pm.PackageInfo; 29 import android.content.pm.PackageManager; 30 31 import androidx.test.filters.SmallTest; 32 33 import org.junit.Before; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.mockito.Mock; 37 import org.mockito.junit.MockitoJUnit; 38 import org.mockito.junit.MockitoRule; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 import java.util.Optional; 43 44 @SmallTest 45 public class ServiceResourcesHelperTest { 46 @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); 47 48 @Mock private Context mContext; 49 @Mock private PackageManager mPackageManager; 50 51 @Before setUp()52 public void setUp() throws Exception { 53 ServiceResourcesHelper.setInstanceForTest(null); 54 when(mContext.getPackageManager()).thenReturn(mPackageManager); 55 } 56 57 @Test getResourcesPackageName_singleValidPackage_returnsThePackage()58 public void getResourcesPackageName_singleValidPackage_returnsThePackage() { 59 mockInstalledPackages( 60 PackageManager.MATCH_SYSTEM_ONLY, 61 "random.package.name", "abc" + RESOURCES_PACKAGE_SUFFIX, "a.b.c"); 62 63 assertThat(ServiceResourcesHelper.get(mContext).getResourcesPackageName()) 64 .isEqualTo(Optional.of("abc" + RESOURCES_PACKAGE_SUFFIX)); 65 } 66 67 @Test getResourcesPackageName_multipleValidPackages_returnsFirstOne()68 public void getResourcesPackageName_multipleValidPackages_returnsFirstOne() { 69 mockInstalledPackages( 70 PackageManager.MATCH_SYSTEM_ONLY, 71 "abc" + RESOURCES_PACKAGE_SUFFIX, "a" + RESOURCES_PACKAGE_SUFFIX); 72 73 assertThat(ServiceResourcesHelper.get(mContext).getResourcesPackageName()) 74 .isEqualTo(Optional.of("abc" + RESOURCES_PACKAGE_SUFFIX)); 75 } 76 77 @Test getResourcesPackageName_usesOnlySystemPackages()78 public void getResourcesPackageName_usesOnlySystemPackages() { 79 mockInstalledPackages( 80 PackageManager.MATCH_SYSTEM_ONLY, 81 "random.package.name", "a.b.c"); 82 // Although one of these packages has a valid resource package name format, it's not a 83 // system package, so it should not be returned from `getResourcesPackageName`. 84 mockInstalledPackages( 85 PackageManager.MATCH_ALL, 86 "random.package.name", "a.b.c", "abc" + RESOURCES_PACKAGE_SUFFIX); 87 88 assertThat(ServiceResourcesHelper.get(mContext).getResourcesPackageName()) 89 .isEqualTo(Optional.empty()); 90 } 91 92 @Test resourcePackageFetchedOnlyOnce()93 public void resourcePackageFetchedOnlyOnce() { 94 ServiceResourcesHelper.get(mContext).getResourcesPackageName(); 95 ServiceResourcesHelper.get(mContext).getResourcesPackageName(); 96 97 verify(mPackageManager, times(1)).getInstalledPackages(anyInt()); 98 } 99 createPackageInfo(String packageName)100 private PackageInfo createPackageInfo(String packageName) { 101 PackageInfo info = new PackageInfo(); 102 info.packageName = packageName; 103 return info; 104 } 105 mockInstalledPackages(int packageManagerFlag, String... packageNames)106 private void mockInstalledPackages(int packageManagerFlag, String... packageNames) { 107 List<PackageInfo> info = new ArrayList<>(); 108 for (String packageName : packageNames) { 109 info.add(createPackageInfo(packageName)); 110 } 111 when(mPackageManager.getInstalledPackages(packageManagerFlag)).thenReturn(info); 112 } 113 } 114