Merge "Improve the loading time of DataSaverSummary" into udc-qpr-dev

This commit is contained in:
Chaohui Wang
2023-05-25 09:38:35 +00:00
committed by Android (Google) Code Review
3 changed files with 150 additions and 64 deletions

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datausage
import android.content.Context
import android.content.pm.ApplicationInfo
import android.net.NetworkPolicyManager
import android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.datausage.DataSaverSummary.Companion.getUnrestrictedSummary
import com.android.settingslib.spaprivileged.model.app.AppListRepository
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Spy
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.mockito.Mockito.`when` as whenever
@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class DataSaverSummaryTest {
@get:Rule
val mockito: MockitoRule = MockitoJUnit.rule()
@Spy
private val context: Context = ApplicationProvider.getApplicationContext()
@Mock
private lateinit var networkPolicyManager: NetworkPolicyManager
@Before
fun setUp() {
whenever(context.applicationContext).thenReturn(context)
whenever(NetworkPolicyManager.from(context)).thenReturn(networkPolicyManager)
}
@Test
fun getUnrestrictedSummary_whenTwoAppsAllowed() = runTest {
whenever(
networkPolicyManager.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND)
).thenReturn(intArrayOf(APP1.uid, APP2.uid))
val summary =
getUnrestrictedSummary(context = context, appListRepository = FakeAppListRepository)
assertThat(summary)
.isEqualTo("2 apps allowed to use unrestricted data when Data Saver is on")
}
@Test
fun getUnrestrictedSummary_whenNoAppsAllowed() = runTest {
whenever(
networkPolicyManager.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND)
).thenReturn(intArrayOf())
val summary =
getUnrestrictedSummary(context = context, appListRepository = FakeAppListRepository)
assertThat(summary)
.isEqualTo("0 apps allowed to use unrestricted data when Data Saver is on")
}
private companion object {
val APP1 = ApplicationInfo().apply { uid = 10001 }
val APP2 = ApplicationInfo().apply { uid = 10002 }
val APP3 = ApplicationInfo().apply { uid = 10003 }
object FakeAppListRepository : AppListRepository {
override suspend fun loadApps(
userId: Int,
loadInstantApps: Boolean,
matchAnyUserForAdmin: Boolean,
) = emptyList<ApplicationInfo>()
override fun showSystemPredicate(
userIdFlow: Flow<Int>,
showSystemFlow: Flow<Boolean>,
): Flow<(app: ApplicationInfo) -> Boolean> = flowOf { false }
override fun getSystemPackageNamesBlocking(userId: Int): Set<String> = emptySet()
override suspend fun loadAndFilterApps(userId: Int, isSystemApp: Boolean) =
listOf(APP1, APP2, APP3)
}
}
}