• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.ext.services.storage;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.app.usage.CacheQuotaHint;
24 import android.app.usage.UsageStats;
25 import android.content.Context;
26 import android.content.ContextWrapper;
27 import android.content.Intent;
28 import android.os.storage.StorageManager;
29 import android.os.storage.VolumeInfo;
30 import android.test.ServiceTestCase;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Answers;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.List;
42 
43 public 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 
CacheQuotaServiceImplTest()52     public CacheQuotaServiceImplTest() {
53         super(CacheQuotaServiceImpl.class);
54     }
55 
56     @Before
setUp()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
testNoApps()74     public void testNoApps() {
75         CacheQuotaServiceImpl service = getService();
76         assertEquals(service.onComputeCacheQuotaHints(new ArrayList()).size(), 0);
77     }
78 
79     @Test
testOneApp()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
testTwoAppsOneVolume()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
testTwoAppsTwoVolumes()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
testMultipleAppsPerUidIsCollated()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
testTwoAppsTwoVolumesTwoUuidsShouldBESeparate()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 
makeNewRequest(String packageName, String uuid, int uid, long foregroundTime)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