CacheQuotaServiceImplTest.java revision dafb17e7eb9a15862ed1314dff2b82d29ad23632
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 17package android.ext.services.storage; 18 19import static com.google.common.truth.Truth.assertThat; 20 21import static org.mockito.Mockito.when; 22 23import android.app.usage.CacheQuotaHint; 24import android.app.usage.UsageStats; 25import android.content.Context; 26import android.content.ContextWrapper; 27import android.content.Intent; 28import android.os.storage.StorageManager; 29import android.os.storage.VolumeInfo; 30import android.test.ServiceTestCase; 31 32import org.junit.Before; 33import org.junit.Test; 34import org.mockito.Answers; 35import org.mockito.Mock; 36import org.mockito.Mockito; 37import org.mockito.MockitoAnnotations; 38 39import java.io.File; 40import java.util.ArrayList; 41import java.util.List; 42 43public class CacheQuotaServiceImplTest extends ServiceTestCase<CacheQuotaServiceImpl> { 44 private static final String sTestVolUuid = "uuid"; 45 private static final String sSecondTestVolUuid = "otherUuid"; 46 47 @Mock private Context mContext; 48 @Mock private File mFile; 49 @Mock private VolumeInfo mVolume; 50 @Mock(answer = Answers.RETURNS_DEEP_STUBS) private StorageManager mStorageManager; 51 52 public CacheQuotaServiceImplTest() { 53 super(CacheQuotaServiceImpl.class); 54 } 55 56 @Before 57 public void setUp() throws Exception { 58 super.setUp(); 59 MockitoAnnotations.initMocks(this); 60 mContext = Mockito.spy(new ContextWrapper(getSystemContext())); 61 setContext(mContext); 62 when(mContext.getSystemService(Context.STORAGE_SERVICE)).thenReturn(mStorageManager); 63 64 when(mFile.getUsableSpace()).thenReturn(10000L); 65 when(mVolume.getPath()).thenReturn(mFile); 66 when(mStorageManager.findVolumeByUuid(sTestVolUuid)).thenReturn(mVolume); 67 when(mStorageManager.findVolumeByUuid(sSecondTestVolUuid)).thenReturn(mVolume); 68 69 Intent intent = new Intent(getContext(), CacheQuotaServiceImpl.class); 70 startService(intent); 71 } 72 73 @Test 74 public void testNoApps() { 75 CacheQuotaServiceImpl service = getService(); 76 assertEquals(service.onComputeCacheQuotaHints(new ArrayList()).size(), 0); 77 } 78 79 @Test 80 public void testOneApp() throws Exception { 81 ArrayList<CacheQuotaHint> requests = new ArrayList<>(); 82 CacheQuotaHint request = makeNewRequest("com.test", sTestVolUuid, 1001, 100L); 83 requests.add(request); 84 85 List<CacheQuotaHint> output = getService().onComputeCacheQuotaHints(requests); 86 87 assertThat(output).hasSize(1); 88 assertThat(output.get(0).getQuota()).isEqualTo(1500L); 89 } 90 91 @Test 92 public void testTwoAppsOneVolume() throws Exception { 93 ArrayList<CacheQuotaHint> requests = new ArrayList<>(); 94 requests.add(makeNewRequest("com.test", sTestVolUuid, 1001, 100L)); 95 requests.add(makeNewRequest("com.test2", sTestVolUuid, 1002, 99L)); 96 97 List<CacheQuotaHint> output = getService().onComputeCacheQuotaHints(requests); 98 99 // Note that the sizes are just the cache area split up. 100 assertThat(output).hasSize(2); 101 assertThat(output.get(0).getQuota()).isEqualTo(883); 102 assertThat(output.get(1).getQuota()).isEqualTo(1500 - 883); 103 } 104 105 @Test 106 public void testTwoAppsTwoVolumes() throws Exception { 107 ArrayList<CacheQuotaHint> requests = new ArrayList<>(); 108 requests.add(makeNewRequest("com.test", sTestVolUuid, 1001, 100L)); 109 requests.add(makeNewRequest("com.test2", sSecondTestVolUuid, 1002, 99L)); 110 111 List<CacheQuotaHint> output = getService().onComputeCacheQuotaHints(requests); 112 113 assertThat(output).hasSize(2); 114 assertThat(output.get(0).getQuota()).isEqualTo(1500); 115 assertThat(output.get(1).getQuota()).isEqualTo(1500); 116 } 117 118 @Test 119 public void testMultipleAppsPerUidIsCollated() throws Exception { 120 ArrayList<CacheQuotaHint> requests = new ArrayList<>(); 121 requests.add(makeNewRequest("com.test", sTestVolUuid, 1001, 100L)); 122 requests.add(makeNewRequest("com.test2", sTestVolUuid, 1001, 99L)); 123 124 List<CacheQuotaHint> output = getService().onComputeCacheQuotaHints(requests); 125 126 assertThat(output).hasSize(1); 127 assertThat(output.get(0).getQuota()).isEqualTo(1500); 128 } 129 130 @Test 131 public void testTwoAppsTwoVolumesTwoUuidsShouldBESeparate() throws Exception { 132 ArrayList<CacheQuotaHint> requests = new ArrayList<>(); 133 requests.add(makeNewRequest("com.test", sTestVolUuid, 1001, 100L)); 134 requests.add(makeNewRequest("com.test2", sSecondTestVolUuid, 1001, 99L)); 135 136 List<CacheQuotaHint> output = getService().onComputeCacheQuotaHints(requests); 137 138 assertThat(output).hasSize(2); 139 assertThat(output.get(0).getQuota()).isEqualTo(1500); 140 assertThat(output.get(1).getQuota()).isEqualTo(1500); 141 } 142 143 private CacheQuotaHint makeNewRequest(String packageName, String uuid, int uid, long foregroundTime) { 144 UsageStats stats = new UsageStats(); 145 stats.mPackageName = packageName; 146 stats.mTotalTimeInForeground = foregroundTime; 147 return new CacheQuotaHint.Builder() 148 .setVolumeUuid(uuid).setUid(uid).setUsageStats(stats).setQuota(-1).build(); 149 } 150} 151