1 /*
2  * Copyright (C) 2022 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.settingslib.spaprivileged.template.app
18 
19 import android.app.usage.StorageStats
20 import android.app.usage.StorageStatsManager
21 import android.content.Context
22 import android.content.pm.ApplicationInfo
23 import android.content.pm.PackageManager.NameNotFoundException
24 import androidx.compose.runtime.CompositionLocalProvider
25 import androidx.compose.ui.platform.LocalContext
26 import androidx.compose.ui.test.junit4.createComposeRule
27 import androidx.test.core.app.ApplicationProvider
28 import androidx.test.ext.junit.runners.AndroidJUnit4
29 import com.android.settingslib.spa.framework.compose.stateOf
30 import com.android.settingslib.spaprivileged.framework.common.storageStatsManager
31 import com.android.settingslib.spaprivileged.model.app.userHandle
32 import java.util.UUID
33 import org.junit.Before
34 import org.junit.Rule
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.mockito.Mock
38 import org.mockito.Spy
39 import org.mockito.junit.MockitoJUnit
40 import org.mockito.junit.MockitoRule
41 import org.mockito.Mockito.`when` as whenever
42 
43 @RunWith(AndroidJUnit4::class)
44 class AppStorageSizeTest {
45     @get:Rule
46     val mockito: MockitoRule = MockitoJUnit.rule()
47 
48     @get:Rule
49     val composeTestRule = createComposeRule()
50 
51     @Spy
52     private val context: Context = ApplicationProvider.getApplicationContext()
53 
54     @Mock
55     private lateinit var storageStatsManager: StorageStatsManager
56 
57     private val app = ApplicationInfo().apply {
58         storageUuid = UUID.randomUUID()
59     }
60 
61     @Before
62     fun setUp() {
63         whenever(context.storageStatsManager).thenReturn(storageStatsManager)
64         whenever(
65             storageStatsManager.queryStatsForPackage(
66                 app.storageUuid, app.packageName, app.userHandle
67             )
68         ).thenReturn(STATS)
69     }
70 
71     @Test
72     fun getStorageSize() {
73         var storageSize = stateOf("")
74 
75         composeTestRule.setContent {
76             CompositionLocalProvider(LocalContext provides context) {
77                 storageSize = app.getStorageSize()
78             }
79         }
80 
81         composeTestRule.waitUntil { storageSize.value == "120 B" }
82     }
83 
84     @Test
85     fun getStorageSize_throwException() {
86         var storageSize = stateOf("Computing")
87         whenever(
88             storageStatsManager.queryStatsForPackage(
89                 app.storageUuid, app.packageName, app.userHandle
90             )
91         ).thenThrow(NameNotFoundException())
92 
93         composeTestRule.setContent {
94             CompositionLocalProvider(LocalContext provides context) {
95                 storageSize = app.getStorageSize()
96             }
97         }
98 
99         composeTestRule.waitUntil { storageSize.value == "" }
100     }
101 
102     companion object {
103         private val STATS = StorageStats().apply {
104             codeBytes = 100
105             dataBytes = 20
106             cacheBytes = 3
107         }
108     }
109 }
110