1 /* 2 * Copyright (C) 2021 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.settings.applications; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.eq; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.content.pm.ApplicationInfo; 29 import android.content.pm.InstallSourceInfo; 30 import android.content.pm.PackageManager; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class AppStoreUtilTest { 42 43 private static final String PACKAGE_NAME = "com.android.app"; 44 private static final String INSTALLING_PACKAGE_NAME = "com.android.installing"; 45 private static final String INITIATING_PACKAGE_NAME = "com.android.initiating"; 46 private static final String ORIGINATING_PACKAGE_NAME = "com.android.originating"; 47 48 @Mock 49 private PackageManager mPackageManager; 50 @Mock 51 private ApplicationInfo mInitiatingAppInfo; 52 @Mock 53 private InstallSourceInfo mInstallSourceInfo; 54 55 private Context mContext; 56 57 @Before setUp()58 public void setUp() throws PackageManager.NameNotFoundException { 59 MockitoAnnotations.initMocks(this); 60 mContext = spy(RuntimeEnvironment.application); 61 when(mContext.getPackageManager()).thenReturn(mPackageManager); 62 when(mPackageManager.getInstallSourceInfo(anyString())).thenReturn(mInstallSourceInfo); 63 when(mInstallSourceInfo.getInstallingPackageName()).thenReturn(INSTALLING_PACKAGE_NAME); 64 when(mInstallSourceInfo.getInitiatingPackageName()).thenReturn(INITIATING_PACKAGE_NAME); 65 when(mInstallSourceInfo.getOriginatingPackageName()).thenReturn(ORIGINATING_PACKAGE_NAME); 66 when(mPackageManager.getApplicationInfo(eq(INITIATING_PACKAGE_NAME), anyInt())) 67 .thenReturn(mInitiatingAppInfo); 68 } 69 70 @Test getInstallerPackageName_hasOriginatingByNonSystem_shouldReturnInstalling()71 public void getInstallerPackageName_hasOriginatingByNonSystem_shouldReturnInstalling() { 72 assertThat(AppStoreUtil.getInstallerPackageName(mContext, PACKAGE_NAME)) 73 .isEqualTo(INSTALLING_PACKAGE_NAME); 74 } 75 76 @Test getInstallerPackageName_hasOriginatingBySystem_shouldReturnOriginating()77 public void getInstallerPackageName_hasOriginatingBySystem_shouldReturnOriginating() { 78 mInitiatingAppInfo.flags = ApplicationInfo.FLAG_SYSTEM; 79 assertThat(AppStoreUtil.getInstallerPackageName(mContext, PACKAGE_NAME)) 80 .isEqualTo(ORIGINATING_PACKAGE_NAME); 81 } 82 83 @Test getInstallerPackageName_noInitiating_shouldReturnInstalling()84 public void getInstallerPackageName_noInitiating_shouldReturnInstalling() { 85 when(mInstallSourceInfo.getInitiatingPackageName()).thenReturn(null); 86 assertThat(AppStoreUtil.getInstallerPackageName(mContext, PACKAGE_NAME)) 87 .isEqualTo(INSTALLING_PACKAGE_NAME); 88 } 89 90 @Test getInstallerPackageName_noOriginating_shouldReturnInstalling()91 public void getInstallerPackageName_noOriginating_shouldReturnInstalling() { 92 when(mInstallSourceInfo.getOriginatingPackageName()).thenReturn(null); 93 assertThat(AppStoreUtil.getInstallerPackageName(mContext, PACKAGE_NAME)) 94 .isEqualTo(INSTALLING_PACKAGE_NAME); 95 } 96 } 97