1 package com.android.settings.applications; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import static org.mockito.Mockito.when; 6 7 import android.app.AppOpsManager; 8 import android.content.Context; 9 import android.content.pm.ApplicationInfo; 10 11 import androidx.test.core.app.ApplicationProvider; 12 import androidx.test.ext.junit.runners.AndroidJUnit4; 13 14 import com.android.settingslib.applications.ApplicationsState.AppEntry; 15 import com.android.settingslib.fuelgauge.PowerAllowlistBackend; 16 17 import org.junit.Before; 18 import org.junit.Test; 19 import org.junit.runner.RunWith; 20 import org.mockito.Mock; 21 import org.mockito.MockitoAnnotations; 22 23 @RunWith(AndroidJUnit4.class) 24 public final class AppStateAppBatteryUsageBridgeTest { 25 private static final String TEST_PACKAGE_1 = "com.example.test.pkg1"; 26 private static final String TEST_PACKAGE_2 = "com.example.test.pkg2"; 27 private static final int UID_1 = 12345; 28 private static final int UID_2 = 7654321; 29 30 private Context mContext; 31 private ApplicationInfo mApplicationInfo; 32 33 @Mock 34 private AppOpsManager mAppOpsManager; 35 @Mock 36 private PowerAllowlistBackend mPowerAllowlistBackend; 37 38 @Before initMocks()39 public void initMocks() { 40 MockitoAnnotations.initMocks(this); 41 mContext = ApplicationProvider.getApplicationContext(); 42 mApplicationInfo = new ApplicationInfo(); 43 mApplicationInfo.sourceDir = "test_dir"; 44 } 45 46 @Test updateExtraInfo_updatesRestricted()47 public void updateExtraInfo_updatesRestricted() { 48 when(mPowerAllowlistBackend.isAllowlisted(TEST_PACKAGE_1, UID_1)).thenReturn(false); 49 when(mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, 50 UID_1, TEST_PACKAGE_1)).thenReturn(AppOpsManager.MODE_IGNORED); 51 AppStateAppBatteryUsageBridge bridge = 52 new AppStateAppBatteryUsageBridge(mContext, null, null); 53 bridge.mAppOpsManager = mAppOpsManager; 54 bridge.mPowerAllowlistBackend = mPowerAllowlistBackend; 55 AppEntry entry = new AppEntry(mContext, mApplicationInfo, 0); 56 57 bridge.updateExtraInfo(entry, TEST_PACKAGE_1, UID_1); 58 59 assertThat(entry.extraInfo.getClass()) 60 .isEqualTo(AppStateAppBatteryUsageBridge.AppBatteryUsageDetails.class); 61 assertThat(AppStateAppBatteryUsageBridge.getAppBatteryUsageDetailsMode(entry)) 62 .isEqualTo(AppStateAppBatteryUsageBridge.MODE_RESTRICTED); 63 } 64 65 @Test updateExtraInfo_updatesUnrestricted()66 public void updateExtraInfo_updatesUnrestricted() { 67 when(mPowerAllowlistBackend.isAllowlisted(TEST_PACKAGE_2, UID_2)).thenReturn(true); 68 when(mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, 69 UID_2, TEST_PACKAGE_2)).thenReturn(AppOpsManager.MODE_ALLOWED); 70 AppStateAppBatteryUsageBridge bridge = 71 new AppStateAppBatteryUsageBridge(mContext, null, null); 72 bridge.mAppOpsManager = mAppOpsManager; 73 bridge.mPowerAllowlistBackend = mPowerAllowlistBackend; 74 AppEntry entry = new AppEntry(mContext, mApplicationInfo, 0); 75 76 bridge.updateExtraInfo(entry, TEST_PACKAGE_2, UID_2); 77 78 assertThat(entry.extraInfo.getClass()) 79 .isEqualTo(AppStateAppBatteryUsageBridge.AppBatteryUsageDetails.class); 80 assertThat(AppStateAppBatteryUsageBridge.getAppBatteryUsageDetailsMode(entry)) 81 .isEqualTo(AppStateAppBatteryUsageBridge.MODE_UNRESTRICTED); 82 } 83 } 84