diff --git a/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java b/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java index 437ffdad280..e622f4c1b3b 100644 --- a/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java +++ b/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java @@ -18,6 +18,7 @@ package com.android.settings.fuelgauge.batterytip; import android.content.Context; import android.os.BatteryUsageStats; +import android.os.PowerManager; import androidx.annotation.VisibleForTesting; @@ -61,11 +62,14 @@ public class BatteryTipLoader extends AsyncLoaderCompat> { final BatteryTipPolicy policy = new BatteryTipPolicy(getContext()); final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG); final Context context = getContext().getApplicationContext(); + final boolean isPowerSaveMode = + context.getSystemService(PowerManager.class).isPowerSaveMode(); - tips.add(new LowBatteryDetector(context, policy, batteryInfo).detect()); + tips.add(new LowBatteryDetector(context, policy, batteryInfo, isPowerSaveMode).detect()); tips.add(new HighUsageDetector(context, policy, mBatteryUsageStats, batteryInfo).detect()); tips.add(new SmartBatteryDetector( - context, policy, batteryInfo, context.getContentResolver()).detect()); + context, policy, batteryInfo, context.getContentResolver(), isPowerSaveMode) + .detect()); tips.add(new BatteryDefenderDetector(batteryInfo, context).detect()); tips.add(new DockDefenderDetector(batteryInfo, context).detect()); tips.add(new IncompatibleChargerDetector(context, batteryInfo).detect()); diff --git a/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java b/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java index 08df2e494f6..2dc057ea7a3 100644 --- a/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java +++ b/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java @@ -37,11 +37,13 @@ public class BatteryDefenderDetector implements BatteryTipDetector { @Override public BatteryTip detect() { - if (mBatteryInfo.isOverheated && !FeatureFactory.getFactory(mContext) - .getPowerUsageFeatureProvider(mContext) - .isExtraDefend()) { - return new BatteryDefenderTip(BatteryTip.StateType.NEW); - } - return new BatteryDefenderTip(BatteryTip.StateType.INVISIBLE); + final boolean isBasicBatteryDefend = mBatteryInfo.isOverheated + && !FeatureFactory.getFactory(mContext) + .getPowerUsageFeatureProvider(mContext) + .isExtraDefend(); + final int state = isBasicBatteryDefend + ? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE; + final boolean isPluggedIn = mBatteryInfo.pluggedStatus != 0; + return new BatteryDefenderTip(state, isPluggedIn); } } diff --git a/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java b/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java index 8d668fed45b..ed8cc62de59 100644 --- a/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java +++ b/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java @@ -17,7 +17,6 @@ package com.android.settings.fuelgauge.batterytip.detectors; import android.content.Context; -import android.os.PowerManager; import com.android.settings.fuelgauge.BatteryInfo; import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy; @@ -30,26 +29,27 @@ import java.util.concurrent.TimeUnit; * Detect whether the battery is too low */ public class LowBatteryDetector implements BatteryTipDetector { - private BatteryInfo mBatteryInfo; - private BatteryTipPolicy mPolicy; - private PowerManager mPowerManager; - private int mWarningLevel; + private final BatteryInfo mBatteryInfo; + private final BatteryTipPolicy mPolicy; + private final boolean mIsPowerSaveMode; + private final int mWarningLevel; - public LowBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo) { + + public LowBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo, + boolean isPowerSaveMode) { mPolicy = policy; mBatteryInfo = batteryInfo; - mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mWarningLevel = context.getResources().getInteger( com.android.internal.R.integer.config_lowBatteryWarningLevel); + mIsPowerSaveMode = isPowerSaveMode; } @Override public BatteryTip detect() { - final boolean powerSaveModeOn = mPowerManager.isPowerSaveMode(); final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel || (mBatteryInfo.discharging && mBatteryInfo.remainingTimeUs != 0 && mBatteryInfo.remainingTimeUs < TimeUnit.HOURS.toMicros(mPolicy.lowBatteryHour)); - final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !powerSaveModeOn; + final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !mIsPowerSaveMode; final boolean dischargingLowBatteryState = mPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery); @@ -61,6 +61,6 @@ public class LowBatteryDetector implements BatteryTipDetector { state = BatteryTip.StateType.NEW; } - return new LowBatteryTip(state, powerSaveModeOn); + return new LowBatteryTip(state, mIsPowerSaveMode); } } diff --git a/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetector.java b/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetector.java index 23409a1ee3a..0487ac3eafe 100644 --- a/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetector.java +++ b/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetector.java @@ -18,7 +18,6 @@ package com.android.settings.fuelgauge.batterytip.detectors; import android.content.ContentResolver; import android.content.Context; -import android.os.PowerManager; import android.provider.Settings; import com.android.settings.fuelgauge.BatteryInfo; @@ -32,17 +31,17 @@ import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip; public class SmartBatteryDetector implements BatteryTipDetector { private static final int EXPECTED_BATTERY_LEVEL = 30; - private BatteryInfo mBatteryInfo; - private BatteryTipPolicy mPolicy; - private ContentResolver mContentResolver; - private PowerManager mPowerManager; + private final BatteryInfo mBatteryInfo; + private final BatteryTipPolicy mPolicy; + private final ContentResolver mContentResolver; + private final boolean mIsPowerSaveMode; public SmartBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo, - ContentResolver contentResolver) { + ContentResolver contentResolver, boolean isPowerSaveMode) { mPolicy = policy; mBatteryInfo = batteryInfo; mContentResolver = contentResolver; - mPowerManager = context.getSystemService(PowerManager.class); + mIsPowerSaveMode = isPowerSaveMode; } @Override @@ -53,7 +52,7 @@ public class SmartBatteryDetector implements BatteryTipDetector { mBatteryInfo.batteryLevel <= EXPECTED_BATTERY_LEVEL; // Show it if in test or smart battery is off. final boolean enableSmartBatteryTip = - smartBatteryOff && !mPowerManager.isPowerSaveMode() && isUnderExpectedBatteryLevel + smartBatteryOff && !mIsPowerSaveMode && isUnderExpectedBatteryLevel || mPolicy.testSmartBatteryTip; final int state = enableSmartBatteryTip ? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE; diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java index 2035f230bf2..df0c192f36e 100644 --- a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java +++ b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java @@ -19,9 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips; import android.app.settings.SettingsEnums; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; -import android.os.BatteryManager; -import android.os.Parcel; import android.util.Log; import androidx.preference.Preference; @@ -39,12 +36,11 @@ public class BatteryDefenderTip extends BatteryTip { private static final String TAG = "BatteryDefenderTip"; - public BatteryDefenderTip(@StateType int state) { - super(TipType.BATTERY_DEFENDER, state, false /* showDialog */); - } + private boolean mIsPluggedIn; - private BatteryDefenderTip(Parcel in) { - super(in); + public BatteryDefenderTip(@StateType int state, boolean isPluggedIn) { + super(TipType.BATTERY_DEFENDER, state, false /* showDialog */); + mIsPluggedIn = isPluggedIn; } @Override @@ -92,7 +88,7 @@ public class BatteryDefenderTip extends BatteryTip { resumeCharging(context); preference.setVisible(false); }); - cardPreference.setPrimaryButtonVisible(isPluggedIn(context)); + cardPreference.setPrimaryButtonVisible(mIsPluggedIn); cardPreference.setSecondaryButtonText(context.getString(R.string.learn_more)); cardPreference.setSecondaryButtonClickListener( @@ -117,23 +113,4 @@ public class BatteryDefenderTip extends BatteryTip { Log.i(TAG, "send resume charging broadcast intent=" + intent); } - - private boolean isPluggedIn(Context context) { - final Intent batteryIntent = - context.registerReceiver( - /* receiver= */ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - return batteryIntent != null - && batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0; - } - - public static final Creator CREATOR = new Creator() { - public BatteryTip createFromParcel(Parcel in) { - return new BatteryDefenderTip(in); - } - - public BatteryTip[] newArray(int size) { - return new BatteryDefenderTip[size]; - } - }; - } diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java index 327f974da28..134cfa0ed39 100644 --- a/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java +++ b/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java @@ -19,7 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips; import android.app.settings.SettingsEnums; import android.content.Context; import android.content.Intent; -import android.os.Parcel; import android.util.Log; import androidx.preference.Preference; @@ -44,10 +43,6 @@ public class DockDefenderTip extends BatteryTip { mMode = mode; } - private DockDefenderTip(Parcel in) { - super(in); - } - public int getMode() { return mMode; } @@ -162,14 +157,4 @@ public class DockDefenderTip extends BatteryTip { Log.i(TAG, "send resume charging broadcast intent=" + intent); } - - public static final Creator CREATOR = new Creator() { - public BatteryTip createFromParcel(Parcel in) { - return new DockDefenderTip(in); - } - - public BatteryTip[] newArray(int size) { - return new DockDefenderTip[size]; - } - }; } diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java index 7ad3d136db5..7ce6b144620 100644 --- a/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java +++ b/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java @@ -19,7 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips; import android.app.settings.SettingsEnums; import android.content.Context; import android.content.Intent; -import android.os.Parcel; import android.util.Log; import androidx.preference.Preference; @@ -37,10 +36,6 @@ public final class IncompatibleChargerTip extends BatteryTip { super(TipType.INCOMPATIBLE_CHARGER, state, /* showDialog */ false); } - private IncompatibleChargerTip(Parcel in) { - super(in); - } - @Override public CharSequence getTitle(Context context) { return context.getString(R.string.battery_tip_incompatible_charging_title); @@ -89,14 +84,4 @@ public final class IncompatibleChargerTip extends BatteryTip { cardPreference.setSecondaryButtonContentDescription(context.getString( R.string.battery_tip_incompatible_charging_content_description)); } - - public static final Creator CREATOR = new Creator() { - public BatteryTip createFromParcel(Parcel in) { - return new IncompatibleChargerTip(in); - } - - public BatteryTip[] newArray(int size) { - return new IncompatibleChargerTip[size]; - } - }; } diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/LowBatteryTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/LowBatteryTip.java index 79d78e27a1f..7ec881106bf 100644 --- a/src/com/android/settings/fuelgauge/batterytip/tips/LowBatteryTip.java +++ b/src/com/android/settings/fuelgauge/batterytip/tips/LowBatteryTip.java @@ -91,5 +91,4 @@ public class LowBatteryTip extends BatteryTip { return new LowBatteryTip[size]; } }; - } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java index 1aefd14a8bd..58bc06118f8 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java @@ -114,7 +114,7 @@ public class BatteryTipDialogFragmentTest { new ArrayList<>(restrictApps)); mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo); - mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW); + mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW, false /* isPluggedIn */); } @After diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipUtilsTest.java index 3312e16277c..e4f286d491b 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipUtilsTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipUtilsTest.java @@ -63,7 +63,8 @@ public class BatteryTipUtilsTest { mRestrictAppTip = spy(new RestrictAppTip(BatteryTip.StateType.NEW, new ArrayList<>())); mLowBatteryTip = spy( new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */)); - mBatteryDefenderTip = spy(new BatteryDefenderTip(BatteryTip.StateType.NEW)); + mBatteryDefenderTip = spy(new BatteryDefenderTip(BatteryTip.StateType.NEW, + false /* isPluggedIn */)); } @Test diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetectorTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetectorTest.java index 245bacc2c17..71042068913 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetectorTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetectorTest.java @@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.spy; import android.content.Context; -import android.os.PowerManager; import com.android.settings.fuelgauge.BatteryInfo; import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy; @@ -34,8 +33,6 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; -import org.robolectric.Shadows; -import org.robolectric.shadows.ShadowPowerManager; import org.robolectric.util.ReflectionHelpers; import java.util.concurrent.TimeUnit; @@ -47,7 +44,6 @@ public class LowBatteryDetectorTest { private BatteryInfo mBatteryInfo; private BatteryTipPolicy mPolicy; private LowBatteryDetector mLowBatteryDetector; - private ShadowPowerManager mShadowPowerManager; private Context mContext; @Before @@ -56,18 +52,19 @@ public class LowBatteryDetectorTest { 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(mContext, mPolicy, mBatteryInfo); + mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo, + false /* isPowerSaveMode */); } @Test public void testDetect_disabledByPolicy_tipInvisible() { ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", false); - mShadowPowerManager.setIsPowerSaveMode(true); + mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo, + true /* isPowerSaveMode */); assertThat(mLowBatteryDetector.detect().isVisible()).isFalse(); } @@ -92,7 +89,8 @@ public class LowBatteryDetectorTest { @Test public void testDetect_batterySaverOn_tipInvisible() { - mShadowPowerManager.setIsPowerSaveMode(true); + mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo, + true /* isPowerSaveMode */); assertThat(mLowBatteryDetector.detect().getState()) .isEqualTo(BatteryTip.StateType.INVISIBLE); diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetectorTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetectorTest.java index 00dcbd46806..d40f5b860f8 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetectorTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/SmartBatteryDetectorTest.java @@ -36,8 +36,6 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; -import org.robolectric.Shadows; -import org.robolectric.shadows.ShadowPowerManager; import org.robolectric.util.ReflectionHelpers; @RunWith(RobolectricTestRunner.class) @@ -50,7 +48,7 @@ public class SmartBatteryDetectorTest { private ContentResolver mContentResolver; private SmartBatteryDetector mSmartBatteryDetector; private BatteryTipPolicy mPolicy; - private ShadowPowerManager mShadowPowerManager; + @Mock private BatteryInfo mBatteryInfo; @@ -61,9 +59,8 @@ public class SmartBatteryDetectorTest { mContext = RuntimeEnvironment.application; mContentResolver = mContext.getContentResolver(); mPolicy = spy(new BatteryTipPolicy(mContext)); - mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class)); - mSmartBatteryDetector = - new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo, mContentResolver); + mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo, + mContentResolver, false /* isPowerSaveMode */); } @Test @@ -77,7 +74,6 @@ public class SmartBatteryDetectorTest { public void testDetect_smartBatteryOff_tipVisible() { Settings.Global.putInt(mContentResolver, Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0); - mShadowPowerManager.setIsPowerSaveMode(false); mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL; assertThat(mSmartBatteryDetector.detect().isVisible()).isTrue(); @@ -87,8 +83,9 @@ public class SmartBatteryDetectorTest { public void testDetect_batterySaverOn_tipInvisible() { Settings.Global.putInt(mContentResolver, Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0); - mShadowPowerManager.setIsPowerSaveMode(true); mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL; + mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo, + mContentResolver, true /* isPowerSaveMode */); assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse(); } @@ -97,8 +94,9 @@ public class SmartBatteryDetectorTest { public void testDetect_unexpectedBatteryLevel_tipInvisible() { Settings.Global.putInt(mContentResolver, Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0); - mShadowPowerManager.setIsPowerSaveMode(true); mBatteryInfo.batteryLevel = UNEXPECTED_BATTERY_LEVEL; + mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo, + mContentResolver, true /* isPowerSaveMode */); assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse(); } @@ -107,7 +105,6 @@ public class SmartBatteryDetectorTest { public void testDetect_smartBatteryOn_tipInvisible() { Settings.Global.putInt(mContentResolver, Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 1); - mShadowPowerManager.setIsPowerSaveMode(false); mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL; assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse(); diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java index 8b6033a8811..244c3946dcd 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTipTest.java @@ -26,9 +26,6 @@ import static org.mockito.Mockito.when; import android.app.settings.SettingsEnums; import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.os.BatteryManager; import android.util.Log; import androidx.preference.Preference; @@ -66,7 +63,8 @@ public class BatteryDefenderTipTest { mFeatureFactory = FakeFeatureFactory.setupForTest(); mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider; mContext = RuntimeEnvironment.application; - mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW); + mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW, + false /* isPluggedIn */); when(mPreference.getContext()).thenReturn(mContext); when(mCardPreference.getContext()).thenReturn(mContext); @@ -133,7 +131,8 @@ public class BatteryDefenderTipTest { @Test public void updatePreference_whenCharging_setPrimaryButtonVisibleToBeTrue() { - fakeDeviceIsCharging(true); + mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW, + true /* isPluggedIn */); mBatteryDefenderTip.updatePreference(mCardPreference); @@ -142,8 +141,6 @@ public class BatteryDefenderTipTest { @Test public void updatePreference_whenNotCharging_setPrimaryButtonVisibleToBeFalse() { - fakeDeviceIsCharging(false); - mBatteryDefenderTip.updatePreference(mCardPreference); verify(mCardPreference).setPrimaryButtonVisible(false); @@ -158,23 +155,10 @@ public class BatteryDefenderTipTest { verify(mCardPreference).setPrimaryButtonVisible(false); } - private void fakeDeviceIsCharging(boolean charging) { - int charged = charging ? 1 : 0; // 1 means charging, 0:not charging - Intent batteryChangedIntent = new Intent(Intent.ACTION_BATTERY_CHANGED); - batteryChangedIntent.putExtra(BatteryManager.EXTRA_PLUGGED, charged); - - Context mockContext = mock(Context.class); - when(mockContext.getString(anyInt())).thenReturn("fake_string"); - when(mCardPreference.getContext()).thenReturn(mockContext); - when(mockContext.registerReceiver(eq(null), any(IntentFilter.class))) - .thenReturn(batteryChangedIntent); - } - private void fakeGetChargingStatusFailed() { Context mockContext = mock(Context.class); when(mockContext.getString(anyInt())).thenReturn("fake_string"); when(mCardPreference.getContext()).thenReturn(mockContext); - when(mockContext.registerReceiver(eq(null), any(IntentFilter.class))).thenReturn(null); } private String getLastErrorLog() {