1 /* 2 * Copyright (C) 2023 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.applications.specialaccess 18 19 import android.content.Context 20 import android.content.pm.ApplicationInfo 21 import android.content.res.Resources 22 import android.net.NetworkPolicyManager 23 import android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND 24 import androidx.test.core.app.ApplicationProvider 25 import androidx.test.ext.junit.runners.AndroidJUnit4 26 import com.android.settings.R 27 import com.android.settings.applications.specialaccess.DataSaverController.Companion.getUnrestrictedSummary 28 import com.android.settings.core.BasePreferenceController.AVAILABLE 29 import com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE 30 import com.android.settingslib.spaprivileged.model.app.AppListRepository 31 import com.google.common.truth.Truth.assertThat 32 import kotlinx.coroutines.ExperimentalCoroutinesApi 33 import kotlinx.coroutines.flow.Flow 34 import kotlinx.coroutines.flow.flowOf 35 import kotlinx.coroutines.test.runTest 36 import org.junit.Before 37 import org.junit.Rule 38 import org.junit.Test 39 import org.junit.runner.RunWith 40 import org.mockito.Mock 41 import org.mockito.Spy 42 import org.mockito.junit.MockitoJUnit 43 import org.mockito.junit.MockitoRule 44 import org.mockito.Mockito.`when` as whenever 45 46 @OptIn(ExperimentalCoroutinesApi::class) 47 @RunWith(AndroidJUnit4::class) 48 class DataSaverControllerTest { 49 @get:Rule 50 val mockito: MockitoRule = MockitoJUnit.rule() 51 52 @Spy 53 private val context: Context = ApplicationProvider.getApplicationContext() 54 55 @Spy 56 private val resources: Resources = context.resources 57 58 @Mock 59 private lateinit var networkPolicyManager: NetworkPolicyManager 60 61 @Mock 62 private lateinit var dataSaverController: DataSaverController 63 64 @Before setUpnull65 fun setUp() { 66 whenever(context.applicationContext).thenReturn(context) 67 whenever(context.resources).thenReturn(resources) 68 whenever(NetworkPolicyManager.from(context)).thenReturn(networkPolicyManager) 69 70 dataSaverController = DataSaverController(context, "key") 71 } 72 73 @Test getAvailabilityStatus_whenConfigOn_availablenull74 fun getAvailabilityStatus_whenConfigOn_available() { 75 whenever(resources.getBoolean(R.bool.config_show_data_saver)).thenReturn(true) 76 assertThat(dataSaverController.availabilityStatus).isEqualTo(AVAILABLE) 77 } 78 79 @Test getAvailabilityStatus_whenConfigOff_unsupportedOnDevicenull80 fun getAvailabilityStatus_whenConfigOff_unsupportedOnDevice() { 81 whenever(resources.getBoolean(R.bool.config_show_data_saver)).thenReturn(false) 82 assertThat(dataSaverController.availabilityStatus).isEqualTo(UNSUPPORTED_ON_DEVICE) 83 } 84 85 @Test <lambda>null86 fun getUnrestrictedSummary_whenTwoAppsAllowed() = runTest { 87 whenever( 88 networkPolicyManager.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND) 89 ).thenReturn(intArrayOf(APP1.uid, APP2.uid)) 90 91 val summary = 92 getUnrestrictedSummary(context = context, appListRepository = FakeAppListRepository) 93 94 assertThat(summary) 95 .isEqualTo("2 apps allowed to use unrestricted data when Data Saver is on") 96 } 97 98 @Test <lambda>null99 fun getUnrestrictedSummary_whenNoAppsAllowed() = runTest { 100 whenever( 101 networkPolicyManager.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND) 102 ).thenReturn(intArrayOf()) 103 104 val summary = 105 getUnrestrictedSummary(context = context, appListRepository = FakeAppListRepository) 106 107 assertThat(summary) 108 .isEqualTo("0 apps allowed to use unrestricted data when Data Saver is on") 109 } 110 111 private companion object { <lambda>null112 val APP1 = ApplicationInfo().apply { uid = 10001 } <lambda>null113 val APP2 = ApplicationInfo().apply { uid = 10002 } <lambda>null114 val APP3 = ApplicationInfo().apply { uid = 10003 } 115 116 object FakeAppListRepository : AppListRepository { loadAppsnull117 override suspend fun loadApps( 118 userId: Int, 119 loadInstantApps: Boolean, 120 matchAnyUserForAdmin: Boolean, 121 ) = emptyList<ApplicationInfo>() 122 123 override fun showSystemPredicate( 124 userIdFlow: Flow<Int>, 125 showSystemFlow: Flow<Boolean>, 126 ): Flow<(app: ApplicationInfo) -> Boolean> = flowOf { false } 127 getSystemPackageNamesBlockingnull128 override fun getSystemPackageNamesBlocking(userId: Int): Set<String> = emptySet() 129 130 override suspend fun loadAndFilterApps(userId: Int, isSystemApp: Boolean) = 131 listOf(APP1, APP2, APP3) 132 } 133 } 134 }