1 /* 2 * Copyright (C) 2017 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 import static org.mockito.ArgumentMatchers.nullable; 21 import static org.mockito.Matchers.any; 22 import static org.mockito.Matchers.anyString; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.pm.ApplicationInfo; 28 import android.os.UserHandle; 29 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 import com.android.settingslib.applications.StorageStatsSource; 32 import com.android.settingslib.applications.StorageStatsSource.AppStorageStats; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 import java.io.IOException; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 public class FetchPackageStorageAsyncLoaderTest { 45 46 private static final String PACKAGE_NAME = "com.test.package"; 47 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private Context mContext; 50 @Mock 51 private StorageStatsSource mSource; 52 53 @Before 54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 } 57 58 @Test 59 public void worksForValidPackageNameAndUid() throws Exception { 60 AppStorageStats stats = mock(AppStorageStats.class); 61 when(stats.getCodeBytes()).thenReturn(1L); 62 when(stats.getDataBytes()).thenReturn(2L); 63 when(stats.getCacheBytes()).thenReturn(3L); 64 when(mSource.getStatsForPackage(nullable(String.class), nullable(String.class), 65 any(UserHandle.class))) 66 .thenReturn(stats); 67 ApplicationInfo info = new ApplicationInfo(); 68 info.packageName = PACKAGE_NAME; 69 70 FetchPackageStorageAsyncLoader task = new FetchPackageStorageAsyncLoader( 71 mContext, mSource, info, new UserHandle(0)); 72 assertThat(task.loadInBackground()).isEqualTo(stats); 73 } 74 75 @Test 76 public void installerExceptionHandledCleanly() throws Exception { 77 when(mSource.getStatsForPackage(anyString(), anyString(), any(UserHandle.class))). 78 thenThrow(new IOException("intentional failure")); 79 ApplicationInfo info = new ApplicationInfo(); 80 info.packageName = PACKAGE_NAME; 81 FetchPackageStorageAsyncLoader task = new FetchPackageStorageAsyncLoader( 82 mContext, mSource, info, new UserHandle(0)); 83 84 assertThat(task.loadInBackground()).isNull(); 85 } 86 } 87