Merge changes from topic "ww_5_auto_restrict"

* changes:
  Add special check for excessive bg anomaly
  Add auto restriction for excessive background
This commit is contained in:
TreeHugger Robot
2018-02-23 22:56:48 +00:00
committed by Android (Google) Code Review
13 changed files with 380 additions and 51 deletions

View File

@@ -51,6 +51,7 @@ public class BatteryDatabaseManagerTest {
private static long NOW = System.currentTimeMillis();
private static long ONE_DAY_BEFORE = NOW - DateUtils.DAY_IN_MILLIS;
private static long TWO_DAYS_BEFORE = NOW - 2 * DateUtils.DAY_IN_MILLIS;
private Context mContext;
private BatteryDatabaseManager mBatteryDatabaseManager;
@@ -69,8 +70,10 @@ public class BatteryDatabaseManagerTest {
@Test
public void testAllFunctions() {
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_NEW, TYPE_NEW, NOW);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD, TWO_DAYS_BEFORE);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_NEW, TYPE_NEW,
AnomalyDatabaseHelper.State.NEW, NOW);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD,
AnomalyDatabaseHelper.State.NEW, TWO_DAYS_BEFORE);
// In database, it contains two record
List<AppInfo> totalAppInfos = mBatteryDatabaseManager.queryAllAnomalies(0 /* timeMsAfter */,
@@ -96,8 +99,10 @@ public class BatteryDatabaseManagerTest {
@Test
public void testUpdateAnomalies_updateSuccessfully() {
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_NEW, TYPE_NEW, NOW);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD, NOW);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_NEW, TYPE_NEW,
AnomalyDatabaseHelper.State.NEW, NOW);
mBatteryDatabaseManager.insertAnomaly(PACKAGE_NAME_OLD, TYPE_OLD,
AnomalyDatabaseHelper.State.NEW, NOW);
final AppInfo appInfo = new AppInfo.Builder().setPackageName(PACKAGE_NAME_OLD).build();
final List<AppInfo> updateAppInfos = new ArrayList<>();
updateAppInfos.add(appInfo);

View File

@@ -177,9 +177,9 @@ public class BatteryUtilsTest {
mHighApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O;
mLowApplicationInfo.targetSdkVersion = Build.VERSION_CODES.L;
mNormalBatterySipper.drainType = BatterySipper.DrainType.APP;
mNormalBatterySipper.totalPowerMah = TOTAL_BATTERY_USAGE;
doReturn(UID).when(mNormalBatterySipper).getUid();
mWifiBatterySipper.drainType = BatterySipper.DrainType.WIFI;
mWifiBatterySipper.totalPowerMah = BATTERY_WIFI_USAGE;
@@ -216,6 +216,10 @@ public class BatteryUtilsTest {
mUsageList.add(mScreenBatterySipper);
mUsageList.add(mCellBatterySipper);
doReturn(mUsageList).when(mBatteryStatsHelper).getUsageList();
doReturn(TOTAL_BATTERY_USAGE + BATTERY_SCREEN_USAGE).when(
mBatteryStatsHelper).getTotalPower();
when(mBatteryStatsHelper.getStats().getDischargeAmount(anyInt())).thenReturn(
DISCHARGE_AMOUNT);
}
@Test
@@ -547,4 +551,16 @@ public class BatteryUtilsTest {
verify(mAppOpsManager).setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UID,
HIGH_SDK_PACKAGE, AppOpsManager.MODE_IGNORED);
}
@Test
public void testIsAppHeavilyUsed_usageMoreThanThreshold_returnTrue() {
assertThat(mBatteryUtils.isAppHeavilyUsed(mBatteryStatsHelper, mUserManager, UID,
10 /* threshold */ )).isTrue();
}
@Test
public void testIsAppHeavilyUsed_usageLessThanThreshold_returnFalse() {
assertThat(mBatteryUtils.isAppHeavilyUsed(mBatteryStatsHelper, mUserManager, UID,
DISCHARGE_AMOUNT /* threshold */ )).isFalse();
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2018 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.fuelgauge.batterytip;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.robolectric.RuntimeEnvironment.application;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.Intent;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowJobScheduler;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AnomalyDetectionJobServiceTest {
@Test
public void testScheduleCleanUp() {
AnomalyDetectionJobService.scheduleAnomalyDetection(application,
new Intent());
ShadowJobScheduler shadowJobScheduler = Shadows.shadowOf(
application.getSystemService(JobScheduler.class));
List<JobInfo> pendingJobs = shadowJobScheduler.getAllPendingJobs();
assertThat(pendingJobs).hasSize(1);
JobInfo pendingJob = pendingJobs.get(0);
assertThat(pendingJob.getId()).isEqualTo(R.id.job_anomaly_detection);
assertThat(pendingJob.getMaxExecutionDelayMillis()).isEqualTo(
TimeUnit.MINUTES.toMillis(30));
}
}

View File

@@ -50,7 +50,8 @@ public class BatteryTipPolicyTest {
+ ",reduced_battery_percent=30"
+ ",low_battery_enabled=false"
+ ",low_battery_hour=10"
+ ",data_history_retain_hour=24";
+ ",data_history_retain_hour=24"
+ ",excessive_bg_drain_percentage=25";
private Context mContext;
@Before
@@ -78,6 +79,7 @@ public class BatteryTipPolicyTest {
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(10);
assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(24);
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(25);
}
@Test
@@ -100,6 +102,6 @@ public class BatteryTipPolicyTest {
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(16);
assertThat(batteryTipPolicy.dataHistoryRetainHour).isEqualTo(72);
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10);
}
}