Add smart battery tip.

Also move the action method from BatteryTip to TipAction. Since it
need extra data that we don't want to store it in parcel.

Bug: 71502850
Test: RunSettingsRoboTests
Change-Id: Ib658426725158d26fcdd437fa8bf6bf24e9a8c14
This commit is contained in:
jackqdyulei
2018-01-10 09:51:52 -08:00
parent 92aa8583c6
commit e682126a11
17 changed files with 352 additions and 33 deletions

View File

@@ -15,6 +15,9 @@
*/
package com.android.settings.fuelgauge.batterytip;
import static com.android.settings.fuelgauge.batterytip.tips.BatteryTip.TipType
.SMART_BATTERY_MANAGER;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
@@ -29,6 +32,7 @@ import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
@@ -57,6 +61,8 @@ public class BatteryTipPreferenceControllerTest {
private PreferenceScreen mPreferenceScreen;
@Mock
private BatteryTip mBatteryTip;
@Mock
private SettingsActivity mSettingsActivity;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceManager mPreferenceManager;
@@ -85,7 +91,7 @@ public class BatteryTipPreferenceControllerTest {
mNewBatteryTips.add(new SummaryTip(BatteryTip.StateType.INVISIBLE));
mBatteryTipPreferenceController = new BatteryTipPreferenceController(mContext, KEY_PREF,
null, mBatteryTipListener);
mSettingsActivity, null, mBatteryTipListener);
mBatteryTipPreferenceController.mPreferenceGroup = mPreferenceGroup;
mBatteryTipPreferenceController.mPrefContext = mContext;
}
@@ -109,7 +115,8 @@ public class BatteryTipPreferenceControllerTest {
}
@Test
public void testHandlePreferenceTreeClick_noDialog_invokeAction() {
public void testHandlePreferenceTreeClick_noDialog_invokeCallback() {
doReturn(SMART_BATTERY_MANAGER).when(mBatteryTip).getType();
List<BatteryTip> batteryTips = new ArrayList<>();
batteryTips.add(mBatteryTip);
doReturn(mPreference).when(mBatteryTip).buildPreference(any());
@@ -119,7 +126,7 @@ public class BatteryTipPreferenceControllerTest {
mBatteryTipPreferenceController.handlePreferenceTreeClick(mPreference);
verify(mBatteryTip).action();
verify(mBatteryTipListener).onBatteryTipHandled(mBatteryTip);
}
private void assertOnlyContainsSummaryTip(final PreferenceGroup preferenceGroup) {

View File

@@ -0,0 +1,68 @@
/*
* 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.Mockito.spy;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SmartBatteryDetectorTest {
private Context mContext;
private BatteryTipPolicy mPolicy;
private SmartBatteryDetector mSmartBatteryDetector;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPolicy = spy(new BatteryTipPolicy(mContext));
mSmartBatteryDetector = new SmartBatteryDetector(mPolicy, mContext.getContentResolver());
}
@Test
public void testDetect_smartBatteryOff_tipVisible() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.APP_STANDBY_ENABLED, 0);
assertThat(mSmartBatteryDetector.detect().isVisible()).isTrue();
}
@Test
public void testDetect_smartBatteryOn_tipInvisible() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.APP_STANDBY_ENABLED, 1);
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
}
}

View File

@@ -107,11 +107,6 @@ public class BatteryTipTest {
// do nothing
}
@Override
public void action() {
// do nothing
}
public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new TestBatteryTip(in);