Add early warning tip and detector

When battery cannot make to usual charging time, this tip would
suggest user to turn on battery saver.

This tip has two visible state(NEW, HANDLED) and display different
information.

This cl also adds an action to turn on the battery saver.

Bug: 70570352
Test: RunSettingsRoboTests
Change-Id: I0e96554df12a0d6508c27174e16d8dca7f4e1fce
This commit is contained in:
jackqdyulei
2018-01-10 14:29:57 -08:00
parent 80fa91891e
commit 62d6a9dca7
9 changed files with 423 additions and 2 deletions

View File

@@ -0,0 +1,101 @@
/*
* 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.detectors;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.PowerManager;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
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;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class EarlyWarningDetectorTest {
private Context mContext;
private BatteryTipPolicy mPolicy;
private EarlyWarningDetector mEarlyWarningDetector;
@Mock
private Intent mIntent;
@Mock
private PowerManager mPowerManager;
private FakeFeatureFactory mFakeFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mPolicy = spy(new BatteryTipPolicy(mContext));
doReturn(mPowerManager).when(mContext).getSystemService(Context.POWER_SERVICE);
doReturn(mIntent).when(mContext).registerReceiver(any(), any());
doReturn(0).when(mIntent).getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
doReturn(true).when(mFakeFeatureFactory.powerUsageFeatureProvider).getEarlyWarningSignal(
any(), any());
mEarlyWarningDetector = new EarlyWarningDetector(mPolicy, mContext);
}
@Test
public void testDetect_policyDisabled_tipInvisible() {
ReflectionHelpers.setField(mPolicy, "batterySaverTipEnabled", false);
assertThat(mEarlyWarningDetector.detect().isVisible()).isFalse();
}
@Test
public void testDetect_batterySaverOn_tipInvisible() {
doReturn(true).when(mPowerManager).isPowerSaveMode();
assertThat(mEarlyWarningDetector.detect().isVisible()).isFalse();
}
@Test
public void testDetect_charging_tipInvisible() {
doReturn(1).when(mIntent).getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
assertThat(mEarlyWarningDetector.detect().isVisible()).isFalse();
}
@Test
public void testDetect_noEarlyWarning_tipInvisible() {
doReturn(false).when(mFakeFeatureFactory.powerUsageFeatureProvider).getEarlyWarningSignal(
any(), any());
assertThat(mEarlyWarningDetector.detect().isVisible()).isFalse();
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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 android.content.Context;
import android.os.Parcel;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class EarlyWarningTipTest {
private Context mContext;
private EarlyWarningTip mEarlyWarningTip;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mEarlyWarningTip = new EarlyWarningTip(BatteryTip.StateType.NEW,
false /* powerSaveModeOn */);
}
@Test
public void testParcelable() {
Parcel parcel = Parcel.obtain();
mEarlyWarningTip.writeToParcel(parcel, mEarlyWarningTip.describeContents());
parcel.setDataPosition(0);
final EarlyWarningTip parcelTip = new EarlyWarningTip(parcel);
assertThat(parcelTip.isPowerSaveModeOn()).isFalse();
}
@Test
public void testInfo_stateNew_displayPowerModeInfo() {
final EarlyWarningTip tip = new EarlyWarningTip(BatteryTip.StateType.NEW,
false /* powerModeOn */);
assertThat(tip.getTitle(mContext)).isEqualTo("Turn on Low Battery Mode");
assertThat(tip.getSummary(mContext)).isEqualTo("Extend your battery life");
assertThat(tip.getIconId()).isEqualTo(R.drawable.ic_battery_alert_24dp);
}
@Test
public void testInfo_stateHandled_displayPowerModeHandledInfo() {
final EarlyWarningTip tip = new EarlyWarningTip(BatteryTip.StateType.HANDLED,
false /* powerModeOn */);
assertThat(tip.getTitle(mContext)).isEqualTo("Low Battery Mode is on");
assertThat(tip.getSummary(mContext)).isEqualTo("Some features are limited");
assertThat(tip.getIconId()).isEqualTo(R.drawable.ic_perm_device_information_green_24dp);
}
@Test
public void testUpdate_powerModeTurnedOn_typeBecomeHandled() {
final EarlyWarningTip nextTip = new EarlyWarningTip(BatteryTip.StateType.INVISIBLE,
true /* powerModeOn */);
mEarlyWarningTip.updateState(nextTip);
assertThat(mEarlyWarningTip.getState()).isEqualTo(BatteryTip.StateType.HANDLED);
}
}