Merge "Add early warning tip and detector"

This commit is contained in:
Lei Yu
2018-01-17 01:28:08 +00:00
committed by Android (Google) Code Review
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);
}
}