Optimize the tip detector loading performance
Optimize the tip detector loading performance by removing 3 times redundant IPC with PowerManager and BatteryManager Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.fuelgauge.batterytip" Change-Id: I0df8b0c600b743c93dfcdd66aeae07e2aba6b797
This commit is contained in:
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge.batterytip;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.BatteryUsageStats;
|
import android.os.BatteryUsageStats;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
@@ -61,11 +62,14 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
|
|||||||
final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
|
final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
|
||||||
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
|
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
|
||||||
final Context context = getContext().getApplicationContext();
|
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 HighUsageDetector(context, policy, mBatteryUsageStats, batteryInfo).detect());
|
||||||
tips.add(new SmartBatteryDetector(
|
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 BatteryDefenderDetector(batteryInfo, context).detect());
|
||||||
tips.add(new DockDefenderDetector(batteryInfo, context).detect());
|
tips.add(new DockDefenderDetector(batteryInfo, context).detect());
|
||||||
tips.add(new IncompatibleChargerDetector(context, batteryInfo).detect());
|
tips.add(new IncompatibleChargerDetector(context, batteryInfo).detect());
|
||||||
|
@@ -37,11 +37,13 @@ public class BatteryDefenderDetector implements BatteryTipDetector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BatteryTip detect() {
|
public BatteryTip detect() {
|
||||||
if (mBatteryInfo.isOverheated && !FeatureFactory.getFactory(mContext)
|
final boolean isBasicBatteryDefend = mBatteryInfo.isOverheated
|
||||||
|
&& !FeatureFactory.getFactory(mContext)
|
||||||
.getPowerUsageFeatureProvider(mContext)
|
.getPowerUsageFeatureProvider(mContext)
|
||||||
.isExtraDefend()) {
|
.isExtraDefend();
|
||||||
return new BatteryDefenderTip(BatteryTip.StateType.NEW);
|
final int state = isBasicBatteryDefend
|
||||||
}
|
? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE;
|
||||||
return new BatteryDefenderTip(BatteryTip.StateType.INVISIBLE);
|
final boolean isPluggedIn = mBatteryInfo.pluggedStatus != 0;
|
||||||
|
return new BatteryDefenderTip(state, isPluggedIn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,7 +17,6 @@
|
|||||||
package com.android.settings.fuelgauge.batterytip.detectors;
|
package com.android.settings.fuelgauge.batterytip.detectors;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.PowerManager;
|
|
||||||
|
|
||||||
import com.android.settings.fuelgauge.BatteryInfo;
|
import com.android.settings.fuelgauge.BatteryInfo;
|
||||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||||
@@ -30,26 +29,27 @@ import java.util.concurrent.TimeUnit;
|
|||||||
* Detect whether the battery is too low
|
* Detect whether the battery is too low
|
||||||
*/
|
*/
|
||||||
public class LowBatteryDetector implements BatteryTipDetector {
|
public class LowBatteryDetector implements BatteryTipDetector {
|
||||||
private BatteryInfo mBatteryInfo;
|
private final BatteryInfo mBatteryInfo;
|
||||||
private BatteryTipPolicy mPolicy;
|
private final BatteryTipPolicy mPolicy;
|
||||||
private PowerManager mPowerManager;
|
private final boolean mIsPowerSaveMode;
|
||||||
private int mWarningLevel;
|
private final int mWarningLevel;
|
||||||
|
|
||||||
public LowBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo) {
|
|
||||||
|
public LowBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo,
|
||||||
|
boolean isPowerSaveMode) {
|
||||||
mPolicy = policy;
|
mPolicy = policy;
|
||||||
mBatteryInfo = batteryInfo;
|
mBatteryInfo = batteryInfo;
|
||||||
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
||||||
mWarningLevel = context.getResources().getInteger(
|
mWarningLevel = context.getResources().getInteger(
|
||||||
com.android.internal.R.integer.config_lowBatteryWarningLevel);
|
com.android.internal.R.integer.config_lowBatteryWarningLevel);
|
||||||
|
mIsPowerSaveMode = isPowerSaveMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BatteryTip detect() {
|
public BatteryTip detect() {
|
||||||
final boolean powerSaveModeOn = mPowerManager.isPowerSaveMode();
|
|
||||||
final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel
|
final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel
|
||||||
|| (mBatteryInfo.discharging && mBatteryInfo.remainingTimeUs != 0
|
|| (mBatteryInfo.discharging && mBatteryInfo.remainingTimeUs != 0
|
||||||
&& mBatteryInfo.remainingTimeUs < TimeUnit.HOURS.toMicros(mPolicy.lowBatteryHour));
|
&& mBatteryInfo.remainingTimeUs < TimeUnit.HOURS.toMicros(mPolicy.lowBatteryHour));
|
||||||
final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !powerSaveModeOn;
|
final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !mIsPowerSaveMode;
|
||||||
final boolean dischargingLowBatteryState =
|
final boolean dischargingLowBatteryState =
|
||||||
mPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery);
|
mPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery);
|
||||||
|
|
||||||
@@ -61,6 +61,6 @@ public class LowBatteryDetector implements BatteryTipDetector {
|
|||||||
state = BatteryTip.StateType.NEW;
|
state = BatteryTip.StateType.NEW;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new LowBatteryTip(state, powerSaveModeOn);
|
return new LowBatteryTip(state, mIsPowerSaveMode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,7 +18,6 @@ package com.android.settings.fuelgauge.batterytip.detectors;
|
|||||||
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.PowerManager;
|
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import com.android.settings.fuelgauge.BatteryInfo;
|
import com.android.settings.fuelgauge.BatteryInfo;
|
||||||
@@ -32,17 +31,17 @@ import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip;
|
|||||||
public class SmartBatteryDetector implements BatteryTipDetector {
|
public class SmartBatteryDetector implements BatteryTipDetector {
|
||||||
private static final int EXPECTED_BATTERY_LEVEL = 30;
|
private static final int EXPECTED_BATTERY_LEVEL = 30;
|
||||||
|
|
||||||
private BatteryInfo mBatteryInfo;
|
private final BatteryInfo mBatteryInfo;
|
||||||
private BatteryTipPolicy mPolicy;
|
private final BatteryTipPolicy mPolicy;
|
||||||
private ContentResolver mContentResolver;
|
private final ContentResolver mContentResolver;
|
||||||
private PowerManager mPowerManager;
|
private final boolean mIsPowerSaveMode;
|
||||||
|
|
||||||
public SmartBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo,
|
public SmartBatteryDetector(Context context, BatteryTipPolicy policy, BatteryInfo batteryInfo,
|
||||||
ContentResolver contentResolver) {
|
ContentResolver contentResolver, boolean isPowerSaveMode) {
|
||||||
mPolicy = policy;
|
mPolicy = policy;
|
||||||
mBatteryInfo = batteryInfo;
|
mBatteryInfo = batteryInfo;
|
||||||
mContentResolver = contentResolver;
|
mContentResolver = contentResolver;
|
||||||
mPowerManager = context.getSystemService(PowerManager.class);
|
mIsPowerSaveMode = isPowerSaveMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,7 +52,7 @@ public class SmartBatteryDetector implements BatteryTipDetector {
|
|||||||
mBatteryInfo.batteryLevel <= EXPECTED_BATTERY_LEVEL;
|
mBatteryInfo.batteryLevel <= EXPECTED_BATTERY_LEVEL;
|
||||||
// Show it if in test or smart battery is off.
|
// Show it if in test or smart battery is off.
|
||||||
final boolean enableSmartBatteryTip =
|
final boolean enableSmartBatteryTip =
|
||||||
smartBatteryOff && !mPowerManager.isPowerSaveMode() && isUnderExpectedBatteryLevel
|
smartBatteryOff && !mIsPowerSaveMode && isUnderExpectedBatteryLevel
|
||||||
|| mPolicy.testSmartBatteryTip;
|
|| mPolicy.testSmartBatteryTip;
|
||||||
final int state =
|
final int state =
|
||||||
enableSmartBatteryTip ? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE;
|
enableSmartBatteryTip ? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE;
|
||||||
|
@@ -19,9 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips;
|
|||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.os.BatteryManager;
|
|
||||||
import android.os.Parcel;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
@@ -39,12 +36,11 @@ public class BatteryDefenderTip extends BatteryTip {
|
|||||||
|
|
||||||
private static final String TAG = "BatteryDefenderTip";
|
private static final String TAG = "BatteryDefenderTip";
|
||||||
|
|
||||||
public BatteryDefenderTip(@StateType int state) {
|
private boolean mIsPluggedIn;
|
||||||
super(TipType.BATTERY_DEFENDER, state, false /* showDialog */);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BatteryDefenderTip(Parcel in) {
|
public BatteryDefenderTip(@StateType int state, boolean isPluggedIn) {
|
||||||
super(in);
|
super(TipType.BATTERY_DEFENDER, state, false /* showDialog */);
|
||||||
|
mIsPluggedIn = isPluggedIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -92,7 +88,7 @@ public class BatteryDefenderTip extends BatteryTip {
|
|||||||
resumeCharging(context);
|
resumeCharging(context);
|
||||||
preference.setVisible(false);
|
preference.setVisible(false);
|
||||||
});
|
});
|
||||||
cardPreference.setPrimaryButtonVisible(isPluggedIn(context));
|
cardPreference.setPrimaryButtonVisible(mIsPluggedIn);
|
||||||
|
|
||||||
cardPreference.setSecondaryButtonText(context.getString(R.string.learn_more));
|
cardPreference.setSecondaryButtonText(context.getString(R.string.learn_more));
|
||||||
cardPreference.setSecondaryButtonClickListener(
|
cardPreference.setSecondaryButtonClickListener(
|
||||||
@@ -117,23 +113,4 @@ public class BatteryDefenderTip extends BatteryTip {
|
|||||||
|
|
||||||
Log.i(TAG, "send resume charging broadcast intent=" + intent);
|
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];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -19,7 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips;
|
|||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Parcel;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
@@ -44,10 +43,6 @@ public class DockDefenderTip extends BatteryTip {
|
|||||||
mMode = mode;
|
mMode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DockDefenderTip(Parcel in) {
|
|
||||||
super(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getMode() {
|
public int getMode() {
|
||||||
return mMode;
|
return mMode;
|
||||||
}
|
}
|
||||||
@@ -162,14 +157,4 @@ public class DockDefenderTip extends BatteryTip {
|
|||||||
|
|
||||||
Log.i(TAG, "send resume charging broadcast intent=" + intent);
|
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];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
@@ -19,7 +19,6 @@ package com.android.settings.fuelgauge.batterytip.tips;
|
|||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Parcel;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
@@ -37,10 +36,6 @@ public final class IncompatibleChargerTip extends BatteryTip {
|
|||||||
super(TipType.INCOMPATIBLE_CHARGER, state, /* showDialog */ false);
|
super(TipType.INCOMPATIBLE_CHARGER, state, /* showDialog */ false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IncompatibleChargerTip(Parcel in) {
|
|
||||||
super(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CharSequence getTitle(Context context) {
|
public CharSequence getTitle(Context context) {
|
||||||
return context.getString(R.string.battery_tip_incompatible_charging_title);
|
return context.getString(R.string.battery_tip_incompatible_charging_title);
|
||||||
@@ -89,14 +84,4 @@ public final class IncompatibleChargerTip extends BatteryTip {
|
|||||||
cardPreference.setSecondaryButtonContentDescription(context.getString(
|
cardPreference.setSecondaryButtonContentDescription(context.getString(
|
||||||
R.string.battery_tip_incompatible_charging_content_description));
|
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];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
@@ -91,5 +91,4 @@ public class LowBatteryTip extends BatteryTip {
|
|||||||
return new LowBatteryTip[size];
|
return new LowBatteryTip[size];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -114,7 +114,7 @@ public class BatteryTipDialogFragmentTest {
|
|||||||
new ArrayList<>(restrictApps));
|
new ArrayList<>(restrictApps));
|
||||||
|
|
||||||
mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
|
mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
|
||||||
mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW);
|
mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW, false /* isPluggedIn */);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
@@ -63,7 +63,8 @@ public class BatteryTipUtilsTest {
|
|||||||
mRestrictAppTip = spy(new RestrictAppTip(BatteryTip.StateType.NEW, new ArrayList<>()));
|
mRestrictAppTip = spy(new RestrictAppTip(BatteryTip.StateType.NEW, new ArrayList<>()));
|
||||||
mLowBatteryTip = spy(
|
mLowBatteryTip = spy(
|
||||||
new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */));
|
new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */));
|
||||||
mBatteryDefenderTip = spy(new BatteryDefenderTip(BatteryTip.StateType.NEW));
|
mBatteryDefenderTip = spy(new BatteryDefenderTip(BatteryTip.StateType.NEW,
|
||||||
|
false /* isPluggedIn */));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.PowerManager;
|
|
||||||
|
|
||||||
import com.android.settings.fuelgauge.BatteryInfo;
|
import com.android.settings.fuelgauge.BatteryInfo;
|
||||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||||
@@ -34,8 +33,6 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.Shadows;
|
|
||||||
import org.robolectric.shadows.ShadowPowerManager;
|
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -47,7 +44,6 @@ public class LowBatteryDetectorTest {
|
|||||||
private BatteryInfo mBatteryInfo;
|
private BatteryInfo mBatteryInfo;
|
||||||
private BatteryTipPolicy mPolicy;
|
private BatteryTipPolicy mPolicy;
|
||||||
private LowBatteryDetector mLowBatteryDetector;
|
private LowBatteryDetector mLowBatteryDetector;
|
||||||
private ShadowPowerManager mShadowPowerManager;
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -56,18 +52,19 @@ public class LowBatteryDetectorTest {
|
|||||||
|
|
||||||
mPolicy = spy(new BatteryTipPolicy(RuntimeEnvironment.application));
|
mPolicy = spy(new BatteryTipPolicy(RuntimeEnvironment.application));
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
|
|
||||||
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true);
|
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true);
|
||||||
ReflectionHelpers.setField(mPolicy, "lowBatteryHour", 3);
|
ReflectionHelpers.setField(mPolicy, "lowBatteryHour", 3);
|
||||||
mBatteryInfo.discharging = true;
|
mBatteryInfo.discharging = true;
|
||||||
|
|
||||||
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo);
|
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo,
|
||||||
|
false /* isPowerSaveMode */);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDetect_disabledByPolicy_tipInvisible() {
|
public void testDetect_disabledByPolicy_tipInvisible() {
|
||||||
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", false);
|
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", false);
|
||||||
mShadowPowerManager.setIsPowerSaveMode(true);
|
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo,
|
||||||
|
true /* isPowerSaveMode */);
|
||||||
|
|
||||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||||
}
|
}
|
||||||
@@ -92,7 +89,8 @@ public class LowBatteryDetectorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDetect_batterySaverOn_tipInvisible() {
|
public void testDetect_batterySaverOn_tipInvisible() {
|
||||||
mShadowPowerManager.setIsPowerSaveMode(true);
|
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo,
|
||||||
|
true /* isPowerSaveMode */);
|
||||||
|
|
||||||
assertThat(mLowBatteryDetector.detect().getState())
|
assertThat(mLowBatteryDetector.detect().getState())
|
||||||
.isEqualTo(BatteryTip.StateType.INVISIBLE);
|
.isEqualTo(BatteryTip.StateType.INVISIBLE);
|
||||||
|
@@ -36,8 +36,6 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.Shadows;
|
|
||||||
import org.robolectric.shadows.ShadowPowerManager;
|
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@@ -50,7 +48,7 @@ public class SmartBatteryDetectorTest {
|
|||||||
private ContentResolver mContentResolver;
|
private ContentResolver mContentResolver;
|
||||||
private SmartBatteryDetector mSmartBatteryDetector;
|
private SmartBatteryDetector mSmartBatteryDetector;
|
||||||
private BatteryTipPolicy mPolicy;
|
private BatteryTipPolicy mPolicy;
|
||||||
private ShadowPowerManager mShadowPowerManager;
|
|
||||||
@Mock
|
@Mock
|
||||||
private BatteryInfo mBatteryInfo;
|
private BatteryInfo mBatteryInfo;
|
||||||
|
|
||||||
@@ -61,9 +59,8 @@ public class SmartBatteryDetectorTest {
|
|||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
mContentResolver = mContext.getContentResolver();
|
mContentResolver = mContext.getContentResolver();
|
||||||
mPolicy = spy(new BatteryTipPolicy(mContext));
|
mPolicy = spy(new BatteryTipPolicy(mContext));
|
||||||
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
|
mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo,
|
||||||
mSmartBatteryDetector =
|
mContentResolver, false /* isPowerSaveMode */);
|
||||||
new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo, mContentResolver);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -77,7 +74,6 @@ public class SmartBatteryDetectorTest {
|
|||||||
public void testDetect_smartBatteryOff_tipVisible() {
|
public void testDetect_smartBatteryOff_tipVisible() {
|
||||||
Settings.Global.putInt(mContentResolver,
|
Settings.Global.putInt(mContentResolver,
|
||||||
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
|
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
|
||||||
mShadowPowerManager.setIsPowerSaveMode(false);
|
|
||||||
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
|
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
|
||||||
|
|
||||||
assertThat(mSmartBatteryDetector.detect().isVisible()).isTrue();
|
assertThat(mSmartBatteryDetector.detect().isVisible()).isTrue();
|
||||||
@@ -87,8 +83,9 @@ public class SmartBatteryDetectorTest {
|
|||||||
public void testDetect_batterySaverOn_tipInvisible() {
|
public void testDetect_batterySaverOn_tipInvisible() {
|
||||||
Settings.Global.putInt(mContentResolver,
|
Settings.Global.putInt(mContentResolver,
|
||||||
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
|
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
|
||||||
mShadowPowerManager.setIsPowerSaveMode(true);
|
|
||||||
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
|
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
|
||||||
|
mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo,
|
||||||
|
mContentResolver, true /* isPowerSaveMode */);
|
||||||
|
|
||||||
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
|
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
|
||||||
}
|
}
|
||||||
@@ -97,8 +94,9 @@ public class SmartBatteryDetectorTest {
|
|||||||
public void testDetect_unexpectedBatteryLevel_tipInvisible() {
|
public void testDetect_unexpectedBatteryLevel_tipInvisible() {
|
||||||
Settings.Global.putInt(mContentResolver,
|
Settings.Global.putInt(mContentResolver,
|
||||||
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
|
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 0);
|
||||||
mShadowPowerManager.setIsPowerSaveMode(true);
|
|
||||||
mBatteryInfo.batteryLevel = UNEXPECTED_BATTERY_LEVEL;
|
mBatteryInfo.batteryLevel = UNEXPECTED_BATTERY_LEVEL;
|
||||||
|
mSmartBatteryDetector = new SmartBatteryDetector(mContext, mPolicy, mBatteryInfo,
|
||||||
|
mContentResolver, true /* isPowerSaveMode */);
|
||||||
|
|
||||||
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
|
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
|
||||||
}
|
}
|
||||||
@@ -107,7 +105,6 @@ public class SmartBatteryDetectorTest {
|
|||||||
public void testDetect_smartBatteryOn_tipInvisible() {
|
public void testDetect_smartBatteryOn_tipInvisible() {
|
||||||
Settings.Global.putInt(mContentResolver,
|
Settings.Global.putInt(mContentResolver,
|
||||||
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 1);
|
Settings.Global.ADAPTIVE_BATTERY_MANAGEMENT_ENABLED, 1);
|
||||||
mShadowPowerManager.setIsPowerSaveMode(false);
|
|
||||||
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
|
mBatteryInfo.batteryLevel = EXPECTED_BATTERY_LEVEL;
|
||||||
|
|
||||||
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
|
assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
|
||||||
|
@@ -26,9 +26,6 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.os.BatteryManager;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
@@ -66,7 +63,8 @@ public class BatteryDefenderTipTest {
|
|||||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
|
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW);
|
mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW,
|
||||||
|
false /* isPluggedIn */);
|
||||||
|
|
||||||
when(mPreference.getContext()).thenReturn(mContext);
|
when(mPreference.getContext()).thenReturn(mContext);
|
||||||
when(mCardPreference.getContext()).thenReturn(mContext);
|
when(mCardPreference.getContext()).thenReturn(mContext);
|
||||||
@@ -133,7 +131,8 @@ public class BatteryDefenderTipTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updatePreference_whenCharging_setPrimaryButtonVisibleToBeTrue() {
|
public void updatePreference_whenCharging_setPrimaryButtonVisibleToBeTrue() {
|
||||||
fakeDeviceIsCharging(true);
|
mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW,
|
||||||
|
true /* isPluggedIn */);
|
||||||
|
|
||||||
mBatteryDefenderTip.updatePreference(mCardPreference);
|
mBatteryDefenderTip.updatePreference(mCardPreference);
|
||||||
|
|
||||||
@@ -142,8 +141,6 @@ public class BatteryDefenderTipTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updatePreference_whenNotCharging_setPrimaryButtonVisibleToBeFalse() {
|
public void updatePreference_whenNotCharging_setPrimaryButtonVisibleToBeFalse() {
|
||||||
fakeDeviceIsCharging(false);
|
|
||||||
|
|
||||||
mBatteryDefenderTip.updatePreference(mCardPreference);
|
mBatteryDefenderTip.updatePreference(mCardPreference);
|
||||||
|
|
||||||
verify(mCardPreference).setPrimaryButtonVisible(false);
|
verify(mCardPreference).setPrimaryButtonVisible(false);
|
||||||
@@ -158,23 +155,10 @@ public class BatteryDefenderTipTest {
|
|||||||
verify(mCardPreference).setPrimaryButtonVisible(false);
|
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() {
|
private void fakeGetChargingStatusFailed() {
|
||||||
Context mockContext = mock(Context.class);
|
Context mockContext = mock(Context.class);
|
||||||
when(mockContext.getString(anyInt())).thenReturn("fake_string");
|
when(mockContext.getString(anyInt())).thenReturn("fake_string");
|
||||||
when(mCardPreference.getContext()).thenReturn(mockContext);
|
when(mCardPreference.getContext()).thenReturn(mockContext);
|
||||||
when(mockContext.registerReceiver(eq(null), any(IntentFilter.class))).thenReturn(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getLastErrorLog() {
|
private String getLastErrorLog() {
|
||||||
|
Reference in New Issue
Block a user