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.deviceinfo.storage; 18 19 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.CACHE_APPS_SIZE_KEY; 20 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.EXTERNAL_APP_BYTES; 21 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.EXTERNAL_AUDIO_BYTES; 22 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.EXTERNAL_IMAGE_BYTES; 23 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.EXTERNAL_TOTAL_BYTES; 24 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.EXTERNAL_VIDEO_BYTES; 25 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.FREE_BYTES_KEY; 26 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.GAME_APPS_SIZE_KEY; 27 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.MUSIC_APPS_SIZE_KEY; 28 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.OTHER_APPS_SIZE_KEY; 29 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.PHOTO_APPS_SIZE_KEY; 30 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.SHARED_PREFERENCES_NAME; 31 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.TIMESTAMP_KEY; 32 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.TOTAL_BYTES_KEY; 33 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.USER_ID_KEY; 34 import static com.android.settings.deviceinfo.storage.CachedStorageValuesHelper.VIDEO_APPS_SIZE_KEY; 35 import static com.google.common.truth.Truth.assertThat; 36 import static org.mockito.Mockito.when; 37 38 import android.content.Context; 39 import android.content.SharedPreferences; 40 import android.util.SparseArray; 41 42 import com.android.settings.testutils.SettingsRobolectricTestRunner; 43 import com.android.settingslib.applications.StorageStatsSource; 44 import com.android.settingslib.deviceinfo.PrivateStorageInfo; 45 46 import org.junit.Before; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 import org.robolectric.RuntimeEnvironment; 52 53 @RunWith(SettingsRobolectricTestRunner.class) 54 public class CachedStorageValuesHelperTest { 55 56 private Context mContext; 57 58 @Mock private CachedStorageValuesHelper.Clock mMockClock; 59 private CachedStorageValuesHelper mCachedValuesHelper; 60 private SharedPreferences mSharedPreferences; 61 62 @Before 63 public void setUp() throws Exception { 64 MockitoAnnotations.initMocks(this); 65 mContext = RuntimeEnvironment.application; 66 mSharedPreferences = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, 0); 67 mCachedValuesHelper = new CachedStorageValuesHelper(mContext, 0); 68 mCachedValuesHelper.mClock = mMockClock; 69 } 70 71 @Test 72 public void getCachedPrivateStorageInfo_cachedValuesAreLoaded() throws Exception { 73 when(mMockClock.getCurrentTime()).thenReturn(10001L); 74 mSharedPreferences 75 .edit() 76 .putLong(GAME_APPS_SIZE_KEY, 0) 77 .putLong(MUSIC_APPS_SIZE_KEY, 10) 78 .putLong(VIDEO_APPS_SIZE_KEY, 100) 79 .putLong(PHOTO_APPS_SIZE_KEY, 1000) 80 .putLong(OTHER_APPS_SIZE_KEY, 10000) 81 .putLong(CACHE_APPS_SIZE_KEY, 100000) 82 .putLong(EXTERNAL_TOTAL_BYTES, 2) 83 .putLong(EXTERNAL_AUDIO_BYTES, 22) 84 .putLong(EXTERNAL_VIDEO_BYTES, 222) 85 .putLong(EXTERNAL_IMAGE_BYTES, 2222) 86 .putLong(EXTERNAL_APP_BYTES, 22222) 87 .putLong(FREE_BYTES_KEY, 1000L) 88 .putLong(TOTAL_BYTES_KEY, 6000L) 89 .putInt(USER_ID_KEY, 0) 90 .putLong(TIMESTAMP_KEY, 10000L) 91 .apply(); 92 93 final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); 94 95 assertThat(info.freeBytes).isEqualTo(1000L); 96 assertThat(info.totalBytes).isEqualTo(6000L); 97 } 98 99 @Test 100 public void getCachedAppsStorageResult_cachedValuesAreLoaded() throws Exception { 101 when(mMockClock.getCurrentTime()).thenReturn(10001L); 102 mSharedPreferences 103 .edit() 104 .putLong(GAME_APPS_SIZE_KEY, 1) 105 .putLong(MUSIC_APPS_SIZE_KEY, 10) 106 .putLong(VIDEO_APPS_SIZE_KEY, 100) 107 .putLong(PHOTO_APPS_SIZE_KEY, 1000) 108 .putLong(OTHER_APPS_SIZE_KEY, 10000) 109 .putLong(CACHE_APPS_SIZE_KEY, 100000) 110 .putLong(EXTERNAL_TOTAL_BYTES, 222222) 111 .putLong(EXTERNAL_AUDIO_BYTES, 22) 112 .putLong(EXTERNAL_VIDEO_BYTES, 222) 113 .putLong(EXTERNAL_IMAGE_BYTES, 2222) 114 .putLong(EXTERNAL_APP_BYTES, 22222) 115 .putLong(FREE_BYTES_KEY, 1000L) 116 .putLong(TOTAL_BYTES_KEY, 5000L) 117 .putInt(USER_ID_KEY, 0) 118 .putLong(TIMESTAMP_KEY, 10000L) 119 .apply(); 120 121 final SparseArray<StorageAsyncLoader.AppsStorageResult> result = 122 mCachedValuesHelper.getCachedAppsStorageResult(); 123 124 StorageAsyncLoader.AppsStorageResult primaryResult = result.get(0); 125 assertThat(primaryResult.gamesSize).isEqualTo(1L); 126 assertThat(primaryResult.musicAppsSize).isEqualTo(10L); 127 assertThat(primaryResult.videoAppsSize).isEqualTo(100L); 128 assertThat(primaryResult.photosAppsSize).isEqualTo(1000L); 129 assertThat(primaryResult.otherAppsSize).isEqualTo(10000L); 130 assertThat(primaryResult.cacheSize).isEqualTo(100000L); 131 assertThat(primaryResult.externalStats.totalBytes).isEqualTo(222222L); 132 assertThat(primaryResult.externalStats.audioBytes).isEqualTo(22L); 133 assertThat(primaryResult.externalStats.videoBytes).isEqualTo(222L); 134 assertThat(primaryResult.externalStats.imageBytes).isEqualTo(2222L); 135 assertThat(primaryResult.externalStats.appBytes).isEqualTo(22222L); 136 } 137 138 @Test 139 public void getCachedPrivateStorageInfo_nullIfDataIsStale() throws Exception { 140 when(mMockClock.getCurrentTime()).thenReturn(10000000L); 141 mSharedPreferences 142 .edit() 143 .putLong(GAME_APPS_SIZE_KEY, 0) 144 .putLong(MUSIC_APPS_SIZE_KEY, 10) 145 .putLong(VIDEO_APPS_SIZE_KEY, 100) 146 .putLong(PHOTO_APPS_SIZE_KEY, 1000) 147 .putLong(OTHER_APPS_SIZE_KEY, 10000) 148 .putLong(CACHE_APPS_SIZE_KEY, 100000) 149 .putLong(EXTERNAL_TOTAL_BYTES, 2) 150 .putLong(EXTERNAL_AUDIO_BYTES, 22) 151 .putLong(EXTERNAL_VIDEO_BYTES, 222) 152 .putLong(EXTERNAL_IMAGE_BYTES, 2222) 153 .putLong(EXTERNAL_APP_BYTES, 22222) 154 .putLong(FREE_BYTES_KEY, 1000L) 155 .putLong(TOTAL_BYTES_KEY, 5000L) 156 .putInt(USER_ID_KEY, 0) 157 .putLong(TIMESTAMP_KEY, 10000L) 158 .apply(); 159 160 final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); 161 assertThat(info).isNull(); 162 } 163 164 @Test 165 public void getCachedAppsStorageResult_nullIfDataIsStale() throws Exception { 166 when(mMockClock.getCurrentTime()).thenReturn(10000000L); 167 mSharedPreferences 168 .edit() 169 .putLong(GAME_APPS_SIZE_KEY, 0) 170 .putLong(MUSIC_APPS_SIZE_KEY, 10) 171 .putLong(VIDEO_APPS_SIZE_KEY, 100) 172 .putLong(PHOTO_APPS_SIZE_KEY, 1000) 173 .putLong(OTHER_APPS_SIZE_KEY, 10000) 174 .putLong(CACHE_APPS_SIZE_KEY, 100000) 175 .putLong(EXTERNAL_TOTAL_BYTES, 2) 176 .putLong(EXTERNAL_AUDIO_BYTES, 22) 177 .putLong(EXTERNAL_VIDEO_BYTES, 222) 178 .putLong(EXTERNAL_IMAGE_BYTES, 2222) 179 .putLong(EXTERNAL_APP_BYTES, 22222) 180 .putLong(FREE_BYTES_KEY, 1000L) 181 .putLong(TOTAL_BYTES_KEY, 5000L) 182 .putInt(USER_ID_KEY, 0) 183 .putLong(TIMESTAMP_KEY, 10000L) 184 .apply(); 185 186 final SparseArray<StorageAsyncLoader.AppsStorageResult> result = 187 mCachedValuesHelper.getCachedAppsStorageResult(); 188 assertThat(result).isNull(); 189 } 190 191 @Test 192 public void getCachedPrivateStorageInfo_nullIfWrongUser() throws Exception { 193 when(mMockClock.getCurrentTime()).thenReturn(10001L); 194 mSharedPreferences 195 .edit() 196 .putLong(GAME_APPS_SIZE_KEY, 0) 197 .putLong(MUSIC_APPS_SIZE_KEY, 10) 198 .putLong(VIDEO_APPS_SIZE_KEY, 100) 199 .putLong(PHOTO_APPS_SIZE_KEY, 1000) 200 .putLong(OTHER_APPS_SIZE_KEY, 10000) 201 .putLong(CACHE_APPS_SIZE_KEY, 100000) 202 .putLong(EXTERNAL_TOTAL_BYTES, 2) 203 .putLong(EXTERNAL_AUDIO_BYTES, 22) 204 .putLong(EXTERNAL_VIDEO_BYTES, 222) 205 .putLong(EXTERNAL_IMAGE_BYTES, 2222) 206 .putLong(EXTERNAL_APP_BYTES, 22222) 207 .putLong(FREE_BYTES_KEY, 1000L) 208 .putLong(TOTAL_BYTES_KEY, 5000L) 209 .putInt(USER_ID_KEY, 1) 210 .putLong(TIMESTAMP_KEY, 10000L) 211 .apply(); 212 213 final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); 214 assertThat(info).isNull(); 215 } 216 217 @Test 218 public void getCachedAppsStorageResult_nullIfWrongUser() throws Exception { 219 when(mMockClock.getCurrentTime()).thenReturn(10001L); 220 mSharedPreferences 221 .edit() 222 .putLong(GAME_APPS_SIZE_KEY, 0) 223 .putLong(MUSIC_APPS_SIZE_KEY, 10) 224 .putLong(VIDEO_APPS_SIZE_KEY, 100) 225 .putLong(PHOTO_APPS_SIZE_KEY, 1000) 226 .putLong(OTHER_APPS_SIZE_KEY, 10000) 227 .putLong(CACHE_APPS_SIZE_KEY, 100000) 228 .putLong(EXTERNAL_TOTAL_BYTES, 2) 229 .putLong(EXTERNAL_AUDIO_BYTES, 22) 230 .putLong(EXTERNAL_VIDEO_BYTES, 222) 231 .putLong(EXTERNAL_IMAGE_BYTES, 2222) 232 .putLong(EXTERNAL_APP_BYTES, 22222) 233 .putLong(FREE_BYTES_KEY, 1000L) 234 .putLong(TOTAL_BYTES_KEY, 5000L) 235 .putInt(USER_ID_KEY, 1) 236 .putLong(TIMESTAMP_KEY, 10000L) 237 .apply(); 238 239 final SparseArray<StorageAsyncLoader.AppsStorageResult> result = 240 mCachedValuesHelper.getCachedAppsStorageResult(); 241 assertThat(result).isNull(); 242 } 243 244 @Test 245 public void getCachedPrivateStorageInfo_nullIfEmpty() throws Exception { 246 final PrivateStorageInfo info = mCachedValuesHelper.getCachedPrivateStorageInfo(); 247 assertThat(info).isNull(); 248 } 249 250 @Test 251 public void getCachedAppsStorageResult_nullIfEmpty() throws Exception { 252 final SparseArray<StorageAsyncLoader.AppsStorageResult> result = 253 mCachedValuesHelper.getCachedAppsStorageResult(); 254 assertThat(result).isNull(); 255 } 256 257 @Test 258 public void cacheResult_succeeds() throws Exception { 259 when(mMockClock.getCurrentTime()).thenReturn(10000L); 260 final StorageStatsSource.ExternalStorageStats externalStats = 261 new StorageStatsSource.ExternalStorageStats(22222L, 2L, 20L, 200L, 2000L); 262 final StorageAsyncLoader.AppsStorageResult result = 263 new StorageAsyncLoader.AppsStorageResult(); 264 result.gamesSize = 1L; 265 result.musicAppsSize = 10L; 266 result.videoAppsSize = 100L; 267 result.photosAppsSize = 1000L; 268 result.otherAppsSize = 10000L; 269 result.cacheSize = 100000L; 270 result.externalStats = externalStats; 271 final PrivateStorageInfo info = new PrivateStorageInfo(1000L, 6000L); 272 273 mCachedValuesHelper.cacheResult(info, result); 274 275 assertThat(mSharedPreferences.getLong(GAME_APPS_SIZE_KEY, -1)).isEqualTo(1L); 276 assertThat(mSharedPreferences.getLong(MUSIC_APPS_SIZE_KEY, -1)).isEqualTo(10L); 277 assertThat(mSharedPreferences.getLong(VIDEO_APPS_SIZE_KEY, -1)).isEqualTo(100L); 278 assertThat(mSharedPreferences.getLong(PHOTO_APPS_SIZE_KEY, -1)).isEqualTo(1000L); 279 assertThat(mSharedPreferences.getLong(OTHER_APPS_SIZE_KEY, -1)).isEqualTo(10000L); 280 assertThat(mSharedPreferences.getLong(CACHE_APPS_SIZE_KEY, -1)).isEqualTo(100000L); 281 assertThat(mSharedPreferences.getLong(EXTERNAL_TOTAL_BYTES, -1)).isEqualTo(22222L); 282 assertThat(mSharedPreferences.getLong(EXTERNAL_AUDIO_BYTES, -1)).isEqualTo(2L); 283 assertThat(mSharedPreferences.getLong(EXTERNAL_VIDEO_BYTES, -1)).isEqualTo(20L); 284 assertThat(mSharedPreferences.getLong(EXTERNAL_IMAGE_BYTES, -1)).isEqualTo(200L); 285 assertThat(mSharedPreferences.getLong(EXTERNAL_APP_BYTES, -1)).isEqualTo(2000L); 286 assertThat(mSharedPreferences.getLong(FREE_BYTES_KEY, -1)).isEqualTo(1000L); 287 assertThat(mSharedPreferences.getLong(TOTAL_BYTES_KEY, -1)).isEqualTo(6000L); 288 assertThat(mSharedPreferences.getInt(USER_ID_KEY, -1)).isEqualTo(0); 289 assertThat(mSharedPreferences.getLong(TIMESTAMP_KEY, -1)).isEqualTo(10000L); 290 }; 291 } 292