Merge changes Ifa91ad93,I14f9696a into pi-dev
* changes: Update the BatteryTipPolicy Update and expose the low battery tip.
This commit is contained in:
@@ -48,9 +48,9 @@ public class BatteryTipLoaderTest {
|
||||
BatteryTip.TipType.APP_RESTRICTION,
|
||||
BatteryTip.TipType.BATTERY_SAVER,
|
||||
BatteryTip.TipType.HIGH_DEVICE_USAGE,
|
||||
BatteryTip.TipType.LOW_BATTERY,
|
||||
BatteryTip.TipType.SUMMARY,
|
||||
BatteryTip.TipType.SMART_BATTERY_MANAGER,
|
||||
BatteryTip.TipType.LOW_BATTERY};
|
||||
BatteryTip.TipType.SMART_BATTERY_MANAGER};
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
@Mock
|
||||
|
@@ -48,7 +48,8 @@ public class BatteryTipPolicyTest {
|
||||
+ ",excessive_bg_drain_percentage=25"
|
||||
+ ",test_battery_saver_tip=true"
|
||||
+ ",test_high_usage_tip=false"
|
||||
+ ",test_smart_battery_tip=true";
|
||||
+ ",test_smart_battery_tip=true"
|
||||
+ ",test_low_battery_tip=true";
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
@@ -80,6 +81,7 @@ public class BatteryTipPolicyTest {
|
||||
assertThat(batteryTipPolicy.testBatterySaverTip).isTrue();
|
||||
assertThat(batteryTipPolicy.testHighUsageTip).isFalse();
|
||||
assertThat(batteryTipPolicy.testSmartBatteryTip).isTrue();
|
||||
assertThat(batteryTipPolicy.testLowBatteryTip).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -100,11 +102,12 @@ public class BatteryTipPolicyTest {
|
||||
assertThat(batteryTipPolicy.reducedBatteryEnabled).isFalse();
|
||||
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50);
|
||||
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
|
||||
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(16);
|
||||
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(3);
|
||||
assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(30);
|
||||
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10);
|
||||
assertThat(batteryTipPolicy.testBatterySaverTip).isFalse();
|
||||
assertThat(batteryTipPolicy.testHighUsageTip).isFalse();
|
||||
assertThat(batteryTipPolicy.testSmartBatteryTip).isFalse();
|
||||
assertThat(batteryTipPolicy.testLowBatteryTip).isFalse();
|
||||
}
|
||||
}
|
||||
|
@@ -17,12 +17,15 @@
|
||||
package com.android.settings.fuelgauge.batterytip.detectors;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.text.format.DateUtils;
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import com.android.settings.fuelgauge.BatteryInfo;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -31,8 +34,12 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.shadows.ShadowPowerManager;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class LowBatteryDetectorTest {
|
||||
|
||||
@@ -40,36 +47,68 @@ public class LowBatteryDetectorTest {
|
||||
private BatteryInfo mBatteryInfo;
|
||||
private BatteryTipPolicy mPolicy;
|
||||
private LowBatteryDetector mLowBatteryDetector;
|
||||
private ShadowPowerManager mShadowPowerManager;
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mPolicy = spy(new BatteryTipPolicy(RuntimeEnvironment.application));
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
|
||||
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true);
|
||||
ReflectionHelpers.setField(mPolicy, "lowBatteryHour", 3);
|
||||
mBatteryInfo.discharging = true;
|
||||
|
||||
mLowBatteryDetector = new LowBatteryDetector(mPolicy, mBatteryInfo);
|
||||
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_disabledByPolicy_tipInvisible() {
|
||||
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", false);
|
||||
mShadowPowerManager.setIsPowerSaveMode(true);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_shortBatteryLife_tipVisible() {
|
||||
mBatteryInfo.discharging = true;
|
||||
mBatteryInfo.remainingTimeUs = DateUtils.MINUTE_IN_MILLIS;
|
||||
public void testDetect_enabledByTest_tipNew() {
|
||||
ReflectionHelpers.setField(mPolicy, "testLowBatteryTip", true);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isTrue();
|
||||
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_longBatteryLife_tipInvisible() {
|
||||
mBatteryInfo.discharging = true;
|
||||
mBatteryInfo.remainingTimeUs = DateUtils.DAY_IN_MILLIS;
|
||||
public void testDetect_lowBattery_tipNew() {
|
||||
mBatteryInfo.batteryLevel = 3;
|
||||
mBatteryInfo.remainingTimeUs = TimeUnit.DAYS.toMillis(1);
|
||||
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
|
||||
mBatteryInfo.batteryLevel = 50;
|
||||
mBatteryInfo.remainingTimeUs = TimeUnit.MINUTES.toMillis(1);
|
||||
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_batterySaverOn_tipHandled() {
|
||||
mShadowPowerManager.setIsPowerSaveMode(true);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().getState())
|
||||
.isEqualTo(BatteryTip.StateType.HANDLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_charging_tipInvisible() {
|
||||
mBatteryInfo.discharging = false;
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_noEarlyWarning_tipInvisible() {
|
||||
mBatteryInfo.remainingTimeUs = TimeUnit.DAYS.toMicros(1);
|
||||
mBatteryInfo.batteryLevel = 100;
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.tips;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class LowBatteryTipTest {
|
||||
|
||||
private static final CharSequence SUMMARY = "Only 15 minutes left";
|
||||
|
||||
@Mock
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
private Context mContext;
|
||||
private LowBatteryTip mLowBatteryTip;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mLowBatteryTip = new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */,
|
||||
SUMMARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
Parcel parcel = Parcel.obtain();
|
||||
mLowBatteryTip.writeToParcel(parcel, mLowBatteryTip.describeContents());
|
||||
parcel.setDataPosition(0);
|
||||
|
||||
final LowBatteryTip parcelTip = new LowBatteryTip(parcel);
|
||||
|
||||
assertThat(parcelTip.isPowerSaveModeOn()).isFalse();
|
||||
assertThat(parcelTip.getSummary(mContext)).isEqualTo(SUMMARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSummary_tipHandled_showSummary() {
|
||||
mLowBatteryTip.mState = BatteryTip.StateType.HANDLED;
|
||||
|
||||
assertThat(mLowBatteryTip.getSummary(mContext)).isEqualTo("Some features may be limited");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSummary_tipNew_showSummary() {
|
||||
mLowBatteryTip.mState = BatteryTip.StateType.NEW;
|
||||
|
||||
assertThat(mLowBatteryTip.getSummary(mContext)).isEqualTo(SUMMARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void log_lowBatteryActionWithCorrectState() {
|
||||
mLowBatteryTip.log(mContext, mMetricsFeatureProvider);
|
||||
|
||||
verify(mMetricsFeatureProvider).action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_LOW_BATTERY_TIP, BatteryTip.StateType.NEW);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user