Merge "Implement OP_SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS exemption"

This commit is contained in:
Alex Johnston
2023-02-15 09:37:12 +00:00
committed by Android (Google) Code Review
13 changed files with 57 additions and 53 deletions

View File

@@ -81,6 +81,7 @@ public final class BatteryBackupHelperTest {
private static final String PACKAGE_NAME1 = "com.android.testing.1";
private static final String PACKAGE_NAME2 = "com.android.testing.2";
private static final String PACKAGE_NAME3 = "com.android.testing.3";
private static final int UID1 = 1;
private Context mContext;
private BatteryBackupHelper mBatteryBackupHelper;
@@ -208,7 +209,7 @@ public final class BatteryBackupHelperTest {
@Test
public void backupOptimizationMode_backupOptimizationMode() throws Exception {
final List<String> allowlistedApps = Arrays.asList(PACKAGE_NAME1);
createTestingData(PACKAGE_NAME1, PACKAGE_NAME2, PACKAGE_NAME3);
createTestingData(PACKAGE_NAME1, UID1, PACKAGE_NAME2, PACKAGE_NAME3);
mBatteryBackupHelper.backupOptimizationMode(mBackupDataOutput, allowlistedApps);
@@ -221,10 +222,10 @@ public final class BatteryBackupHelperTest {
public void backupOptimizationMode_backupOptimizationModeAndIgnoreSystemApp()
throws Exception {
final List<String> allowlistedApps = Arrays.asList(PACKAGE_NAME1);
createTestingData(PACKAGE_NAME1, PACKAGE_NAME2, PACKAGE_NAME3);
createTestingData(PACKAGE_NAME1, UID1, PACKAGE_NAME2, PACKAGE_NAME3);
// Sets "com.android.testing.1" as system app.
doReturn(true).when(mPowerAllowlistBackend).isSysAllowlisted(PACKAGE_NAME1);
doReturn(false).when(mPowerAllowlistBackend).isDefaultActiveApp(anyString());
doReturn(false).when(mPowerAllowlistBackend).isDefaultActiveApp(anyString(), anyInt());
mBatteryBackupHelper.backupOptimizationMode(mBackupDataOutput, allowlistedApps);
@@ -237,9 +238,9 @@ public final class BatteryBackupHelperTest {
public void backupOptimizationMode_backupOptimizationModeAndIgnoreDefaultApp()
throws Exception {
final List<String> allowlistedApps = Arrays.asList(PACKAGE_NAME1);
createTestingData(PACKAGE_NAME1, PACKAGE_NAME2, PACKAGE_NAME3);
createTestingData(PACKAGE_NAME1, UID1, PACKAGE_NAME2, PACKAGE_NAME3);
// Sets "com.android.testing.1" as device default app.
doReturn(true).when(mPowerAllowlistBackend).isDefaultActiveApp(PACKAGE_NAME1);
doReturn(true).when(mPowerAllowlistBackend).isDefaultActiveApp(PACKAGE_NAME1, UID1);
doReturn(false).when(mPowerAllowlistBackend).isSysAllowlisted(anyString());
mBatteryBackupHelper.backupOptimizationMode(mBackupDataOutput, allowlistedApps);
@@ -371,15 +372,15 @@ public final class BatteryBackupHelperTest {
assertThat(actualResultSet).isEqualTo(expectedResultSet);
}
private void createTestingData(
String packageName1, String packageName2, String packageName3) throws Exception {
private void createTestingData(String packageName1, int uid1, String packageName2,
String packageName3) throws Exception {
// Sets the getInstalledApplications() method for testing.
final UserInfo userInfo =
new UserInfo(/*userId=*/ 0, /*userName=*/ "google", /*flag=*/ 0);
doReturn(Arrays.asList(userInfo)).when(mUserManager).getProfiles(anyInt());
final ApplicationInfo applicationInfo1 = new ApplicationInfo();
applicationInfo1.enabled = true;
applicationInfo1.uid = 1;
applicationInfo1.uid = uid1;
applicationInfo1.packageName = packageName1;
final ApplicationInfo applicationInfo2 = new ApplicationInfo();
applicationInfo2.enabled = false;

View File

@@ -94,7 +94,7 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testGetAppOptimizationMode_returnRestricted() {
when(mMockBackend.isAllowlisted(anyString())).thenReturn(false);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(false);
when(mMockAppOpsManager.checkOpNoThrow(anyInt(), anyInt(), anyString()))
.thenReturn(AppOpsManager.MODE_IGNORED);
@@ -104,7 +104,7 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testGetAppOptimizationMode_returnUnrestricted() {
when(mMockBackend.isAllowlisted(anyString())).thenReturn(true);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(true);
when(mMockAppOpsManager.checkOpNoThrow(anyInt(), anyInt(), anyString()))
.thenReturn(AppOpsManager.MODE_ALLOWED);
@@ -114,7 +114,7 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testGetAppOptimizationMode_returnOptimized() {
when(mMockBackend.isAllowlisted(anyString())).thenReturn(false);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(false);
when(mMockAppOpsManager.checkOpNoThrow(anyInt(), anyInt(), anyString()))
.thenReturn(AppOpsManager.MODE_ALLOWED);
@@ -124,8 +124,8 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testIsSystemOrDefaultApp_isSystemOrDefaultApp_returnTrue() {
when(mMockBackend.isAllowlisted(anyString())).thenReturn(true);
when(mMockBackend.isDefaultActiveApp(anyString())).thenReturn(true);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(true);
when(mMockBackend.isDefaultActiveApp(anyString(), anyInt())).thenReturn(true);
assertThat(mBatteryOptimizeUtils.isSystemOrDefaultApp()).isTrue();
}
@@ -151,7 +151,7 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testSetAppUsageState_Restricted_verifyAction() throws Exception {
// Sets the current mode as MODE_UNRESTRICTED.
when(mMockBackend.isAllowlisted(anyString())).thenReturn(true);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(true);
when(mMockAppOpsManager.checkOpNoThrow(anyInt(), anyInt(), anyString()))
.thenReturn(AppOpsManager.MODE_ALLOWED);
@@ -164,7 +164,7 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testSetAppUsageState_Unrestricted_verifyAction() throws Exception {
// Sets the current mode as MODE_RESTRICTED.
when(mMockBackend.isAllowlisted(anyString())).thenReturn(false);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(false);
when(mMockAppOpsManager.checkOpNoThrow(anyInt(), anyInt(), anyString()))
.thenReturn(AppOpsManager.MODE_IGNORED);
@@ -178,7 +178,7 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testSetAppUsageState_Optimized_verifyAction() throws Exception {
// Sets the current mode as MODE_UNRESTRICTED.
when(mMockBackend.isAllowlisted(anyString())).thenReturn(true);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(true);
when(mMockAppOpsManager.checkOpNoThrow(anyInt(), anyInt(), anyString()))
.thenReturn(AppOpsManager.MODE_ALLOWED);
@@ -191,7 +191,7 @@ public class BatteryOptimizeUtilsTest {
@Test
public void testSetAppUsageState_sameUnrestrictedMode_verifyNoAction() throws Exception {
// Sets the current mode as MODE_UNRESTRICTED.
when(mMockBackend.isAllowlisted(anyString())).thenReturn(true);
when(mMockBackend.isAllowlisted(anyString(), anyInt())).thenReturn(true);
when(mMockAppOpsManager.checkOpNoThrow(anyInt(), anyInt(), anyString()))
.thenReturn(AppOpsManager.MODE_ALLOWED);
@@ -267,7 +267,7 @@ public class BatteryOptimizeUtilsTest {
final InOrder inOrder = inOrder(mMockBackend);
inOrder.verify(mMockBackend).refreshList();
inOrder.verify(mMockBackend).isAllowlisted(PACKAGE_NAME);
inOrder.verify(mMockBackend).isAllowlisted(PACKAGE_NAME, UID);
verifyNoMoreInteractions(mMockBackend);
}
@@ -281,7 +281,7 @@ public class BatteryOptimizeUtilsTest {
final InOrder inOrder = inOrder(mMockBackend);
inOrder.verify(mMockBackend).refreshList();
inOrder.verify(mMockBackend).isAllowlisted(PACKAGE_NAME);
inOrder.verify(mMockBackend).isAllowlisted(PACKAGE_NAME, UID);
inOrder.verify(mMockBackend).isSysAllowlisted(PACKAGE_NAME);
verifyNoMoreInteractions(mMockBackend);
}
@@ -324,13 +324,13 @@ public class BatteryOptimizeUtilsTest {
.checkOpNoThrow(anyInt(), anyInt(), anyString());
doReturn(allowListed)
.when(mMockBackend)
.isAllowlisted(anyString());
.isAllowlisted(anyString(), anyInt());
doReturn(isSystemOrDefaultApp)
.when(mMockBackend)
.isSysAllowlisted(anyString());
doReturn(isSystemOrDefaultApp)
.when(mMockBackend)
.isDefaultActiveApp(anyString());
.isDefaultActiveApp(anyString(), anyInt());
BatteryOptimizeUtils.resetAppOptimizationMode(
mContext, mMockIPackageManager, mMockAppOpsManager, mMockBackend,

View File

@@ -398,7 +398,8 @@ public class BatteryUtilsTest {
@Test
public void testShouldHideAnomaly_AppInDozeList_returnTrue() {
doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID);
doReturn(true).when(mPowerAllowlistBackend).isAllowlisted(new String[]{HIGH_SDK_PACKAGE});
doReturn(true).when(mPowerAllowlistBackend)
.isAllowlisted(new String[]{HIGH_SDK_PACKAGE}, UID);
assertThat(mBatteryUtils.shouldHideAnomaly(mPowerAllowlistBackend, UID,
mAnomalyInfo)).isTrue();

View File

@@ -85,7 +85,7 @@ public class HighPowerDetailTest {
@Test
public void onClick_appAddedToDozeAllowlist_getsUnrestricted() {
mFragment.mIsEnabled = true;
when(mPowerAllowlistBackend.isAllowlisted(TEST_PACKAGE)).thenReturn(false);
when(mPowerAllowlistBackend.isAllowlisted(TEST_PACKAGE, TEST_UID)).thenReturn(false);
mFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
verify(mBatteryUtils).setForceAppStandby(TEST_UID, TEST_PACKAGE,
AppOpsManager.MODE_ALLOWED);
@@ -93,9 +93,9 @@ public class HighPowerDetailTest {
@Test
public void getSummary_defaultActivePackage_returnUnavailable() {
doReturn(true).when(mPowerAllowlistBackend).isDefaultActiveApp(TEST_PACKAGE);
doReturn(true).when(mPowerAllowlistBackend).isDefaultActiveApp(TEST_PACKAGE, TEST_UID);
assertThat(HighPowerDetail.getSummary(mContext, mPowerAllowlistBackend, TEST_PACKAGE))
.isEqualTo(mContext.getString(R.string.high_power_system));
assertThat(HighPowerDetail.getSummary(mContext, mPowerAllowlistBackend, TEST_PACKAGE,
TEST_UID)).isEqualTo(mContext.getString(R.string.high_power_system));
}
}

View File

@@ -146,7 +146,8 @@ public class AnomalyDetectionJobServiceTest {
@Test
public void saveAnomalyToDatabase_systemAllowlisted_doNotSave() {
doReturn(UID).when(mAnomalyDetectionJobService).extractUidFromStatsDimensionsValue(any());
doReturn(true).when(mPowerAllowlistBackend).isAllowlisted(any(String[].class));
doReturn(true).when(mPowerAllowlistBackend)
.isAllowlisted(any(String[].class), any(Integer.class));
mAnomalyDetectionJobService.saveAnomalyToDatabase(mContext,
mUserManager, mBatteryDatabaseManager, mBatteryUtils, mPolicy,