Update battery percentage column to UsageProgressBarPref (1/2)

- Change battery percentage column to new design
 - Remove debug info case
 Screenshots:
   https://screenshot.googleplex.com/9rvRfK3wBtpnarZ.png
   https://screenshot.googleplex.com/5iAjNXTptDechAm.png

Bug: 177407113
Test: make RunSettingsRoboTests -j40
Change-Id: I5d046be29a036910036e72edb677b69bc2c0a03f
This commit is contained in:
Wesley.CW Wang
2021-02-04 16:44:55 +08:00
committed by Wesley Wang
parent 2e47875c06
commit f4bc35333b
5 changed files with 39 additions and 285 deletions

View File

@@ -21,11 +21,10 @@
android:title="@string/power_usage_summary_title" android:title="@string/power_usage_summary_title"
settings:keywords="@string/keywords_battery"> settings:keywords="@string/keywords_battery">
<com.android.settingslib.widget.LayoutPreference <com.android.settingslib.widget.UsageProgressBarPreference
android:key="battery_header" android:key="battery_header"
android:title="@string/summary_placeholder" android:title="@string/summary_placeholder"
android:selectable="false" android:selectable="false"
android:layout="@layout/battery_header"
settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" /> settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" />
<Preference <Preference

View File

@@ -25,11 +25,6 @@ import android.icu.text.NumberFormat;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.PowerManager; import android.os.PowerManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
@@ -39,14 +34,12 @@ import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settings.utils.AnnotationSpan;
import com.android.settings.widget.EntityHeaderController; import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.HelpUtils;
import com.android.settingslib.Utils; import com.android.settingslib.Utils;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.UsageProgressBarPreference;
/** /**
* Controller that update the battery header view * Controller that update the battery header view
@@ -57,23 +50,18 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
@VisibleForTesting @VisibleForTesting
static final String KEY_BATTERY_HEADER = "battery_header"; static final String KEY_BATTERY_HEADER = "battery_header";
private static final String ANNOTATION_URL = "url"; private static final String ANNOTATION_URL = "url";
private static final int BATTERY_MAX_LEVEL = 100;
@VisibleForTesting @VisibleForTesting
BatteryStatusFeatureProvider mBatteryStatusFeatureProvider; BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
@VisibleForTesting @VisibleForTesting
BatteryMeterView mBatteryMeterView; UsageProgressBarPreference mBatteryUsageProgressBarPref;
@VisibleForTesting
TextView mBatteryPercentText;
@VisibleForTesting
TextView mSummary1;
private Activity mActivity; private Activity mActivity;
private PreferenceFragmentCompat mHost; private PreferenceFragmentCompat mHost;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private final PowerManager mPowerManager; private final PowerManager mPowerManager;
private LayoutPreference mBatteryLayoutPref;
public BatteryHeaderPreferenceController(Context context, String key) { public BatteryHeaderPreferenceController(Context context, String key) {
super(context, key); super(context, key);
mPowerManager = context.getSystemService(PowerManager.class); mPowerManager = context.getSystemService(PowerManager.class);
@@ -96,16 +84,12 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mBatteryLayoutPref = screen.findPreference(getPreferenceKey()); mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
mBatteryMeterView = mBatteryLayoutPref
.findViewById(R.id.battery_header_icon);
mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
if (com.android.settings.Utils.isBatteryPresent(mContext)) { if (com.android.settings.Utils.isBatteryPresent(mContext)) {
quickUpdateHeaderPreference(); quickUpdateHeaderPreference();
} else { } else {
showHelpMessage(); //TODO(b/179237551): Make new progress bar widget support help message
} }
} }
@@ -116,8 +100,7 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
@Override @Override
public void onStart() { public void onStart() {
EntityHeaderController.newInstance(mActivity, mHost, EntityHeaderController.newInstance(mActivity, mHost, null /* header view */)
mBatteryLayoutPref.findViewById(R.id.battery_entity_header))
.setRecyclerView(mHost.getListView(), mLifecycle) .setRecyclerView(mHost.getListView(), mLifecycle)
.styleActionBar(mActivity); .styleActionBar(mActivity);
} }
@@ -133,21 +116,20 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
} }
public void updateHeaderPreference(BatteryInfo info) { public void updateHeaderPreference(BatteryInfo info) {
mBatteryPercentText.setText(formatBatteryPercentageText(info.batteryLevel));
if (!mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
mSummary1.setText(generateLabel(info));
}
mBatteryMeterView.setBatteryLevel(info.batteryLevel); //TODO(b/179237746): Make progress bar widget support battery state icon
mBatteryMeterView.setCharging(!info.discharging);
mBatteryMeterView.setPowerSave(mPowerManager.isPowerSaveMode()); mBatteryUsageProgressBarPref.setUsageSummary(
formatBatteryPercentageText(info.batteryLevel));
mBatteryUsageProgressBarPref.setTotalSummary(generateLabel(info));
mBatteryUsageProgressBarPref.setPercent(info.batteryLevel, BATTERY_MAX_LEVEL);
} }
/** /**
* Callback which receives text for the summary line. * Callback which receives text for the summary line.
*/ */
public void updateBatteryStatus(String label, BatteryInfo info) { public void updateBatteryStatus(String label, BatteryInfo info) {
mSummary1.setText(label != null ? label : generateLabel(info)); mBatteryUsageProgressBarPref.setTotalSummary(label != null ? label : generateLabel(info));
} }
public void quickUpdateHeaderPreference() { public void quickUpdateHeaderPreference() {
@@ -157,37 +139,10 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
final boolean discharging = final boolean discharging =
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
// Set battery level and charging status //TODO(b/179237746): Make progress bar widget support battery state icon
mBatteryMeterView.setBatteryLevel(batteryLevel);
mBatteryMeterView.setCharging(!discharging);
mBatteryMeterView.setPowerSave(mPowerManager.isPowerSaveMode());
mBatteryPercentText.setText(formatBatteryPercentageText(batteryLevel));
}
@VisibleForTesting mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
void showHelpMessage() { mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
final LinearLayout batteryInfoLayout =
mBatteryLayoutPref.findViewById(R.id.battery_info_layout);
// Remove battery meter icon
mBatteryMeterView.setVisibility(View.GONE);
// Update the width of battery info layout
final ViewGroup.LayoutParams params = batteryInfoLayout.getLayoutParams();
params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
batteryInfoLayout.setLayoutParams(params);
mBatteryPercentText.setText(mContext.getText(R.string.unknown));
// Add linkable text for learn more
final Intent helpIntent = HelpUtils.getHelpIntent(mContext,
mContext.getString(R.string.help_url_battery_missing),
mContext.getClass().getName());
final AnnotationSpan.LinkInfo linkInfo = new AnnotationSpan
.LinkInfo(mContext, ANNOTATION_URL, helpIntent);
if (linkInfo.isActionable()) {
mSummary1.setMovementMethod(LinkMovementMethod.getInstance());
mSummary1.setText(AnnotationSpan
.linkify(mContext.getText(R.string.battery_missing_help_message), linkInfo));
} else {
mSummary1.setText(mContext.getText(R.string.battery_missing_message));
}
} }
private CharSequence formatBatteryPercentageText(int batteryLevel) { private CharSequence formatBatteryPercentageText(int batteryLevel) {

View File

@@ -25,14 +25,9 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.Settings.Global; import android.provider.Settings.Global;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager; import androidx.loader.app.LoaderManager;
import androidx.loader.app.LoaderManager.LoaderCallbacks;
import androidx.loader.content.Loader; import androidx.loader.content.Loader;
import com.android.settings.R; import com.android.settings.R;
@@ -44,7 +39,6 @@ import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.utils.PowerUtil;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.LayoutPreference;
import java.util.List; import java.util.List;
@@ -54,18 +48,15 @@ import java.util.List;
* since the last time it was unplugged. * since the last time it was unplugged.
*/ */
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class PowerUsageSummary extends PowerUsageBase implements OnLongClickListener, public class PowerUsageSummary extends PowerUsageBase implements
BatteryTipPreferenceController.BatteryTipListener { BatteryTipPreferenceController.BatteryTipListener {
static final String TAG = "PowerUsageSummary"; static final String TAG = "PowerUsageSummary";
private static final String KEY_BATTERY_HEADER = "battery_header";
@VisibleForTesting @VisibleForTesting
static final int BATTERY_INFO_LOADER = 1; static final int BATTERY_INFO_LOADER = 1;
@VisibleForTesting @VisibleForTesting
static final int BATTERY_TIP_LOADER = 2; static final int BATTERY_TIP_LOADER = 2;
public static final int DEBUG_INFO_LOADER = 3;
@VisibleForTesting @VisibleForTesting
PowerUsageFeatureProvider mPowerFeatureProvider; PowerUsageFeatureProvider mPowerFeatureProvider;
@@ -112,49 +103,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
} }
}; };
LoaderManager.LoaderCallbacks<List<BatteryInfo>> mBatteryInfoDebugLoaderCallbacks =
new LoaderCallbacks<List<BatteryInfo>>() {
@Override
public Loader<List<BatteryInfo>> onCreateLoader(int i, Bundle bundle) {
return new DebugEstimatesLoader(getContext(), mStatsHelper);
}
@Override
public void onLoadFinished(Loader<List<BatteryInfo>> loader,
List<BatteryInfo> batteryInfos) {
updateViews(batteryInfos);
}
@Override
public void onLoaderReset(Loader<List<BatteryInfo>> loader) {
}
};
protected void updateViews(List<BatteryInfo> batteryInfos) {
final BatteryMeterView batteryView = mBatteryLayoutPref
.findViewById(R.id.battery_header_icon);
final TextView percentRemaining =
mBatteryLayoutPref.findViewById(R.id.battery_percent);
final TextView summary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
BatteryInfo oldInfo = batteryInfos.get(0);
BatteryInfo newInfo = batteryInfos.get(1);
percentRemaining.setText(Utils.formatPercentage(oldInfo.batteryLevel));
// set the text to the old estimate (copied from battery info). Note that this
// can sometimes say 0 time remaining because battery stats requires the phone
// be unplugged for a period of time before being willing ot make an estimate.
final String OldEstimateString = mPowerFeatureProvider.getOldEstimateDebugString(
Formatter.formatShortElapsedTime(getContext(),
PowerUtil.convertUsToMs(oldInfo.remainingTimeUs)));
final String NewEstimateString = mPowerFeatureProvider.getEnhancedEstimateDebugString(
Formatter.formatShortElapsedTime(getContext(),
PowerUtil.convertUsToMs(newInfo.remainingTimeUs)));
summary1.setText(OldEstimateString + "\n" + NewEstimateString);
batteryView.setBatteryLevel(oldInfo.batteryLevel);
batteryView.setCharging(!oldInfo.discharging);
}
private LoaderManager.LoaderCallbacks<List<BatteryTip>> mBatteryTipsCallbacks = private LoaderManager.LoaderCallbacks<List<BatteryTip>> mBatteryTipsCallbacks =
new LoaderManager.LoaderCallbacks<List<BatteryTip>>() { new LoaderManager.LoaderCallbacks<List<BatteryTip>>() {
@@ -197,7 +145,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
setAnimationAllowed(true); setAnimationAllowed(true);
initFeatureProvider(); initFeatureProvider();
mBatteryLayoutPref = (LayoutPreference) findPreference(KEY_BATTERY_HEADER);
mBatteryUtils = BatteryUtils.getInstance(getContext()); mBatteryUtils = BatteryUtils.getInstance(getContext());
@@ -274,17 +221,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
mBatteryLayoutPref = layoutPreference; mBatteryLayoutPref = layoutPreference;
} }
@VisibleForTesting
void showBothEstimates() {
final Context context = getContext();
if (context == null
|| !mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
return;
}
getLoaderManager().restartLoader(DEBUG_INFO_LOADER, Bundle.EMPTY,
mBatteryInfoDebugLoaderCallbacks);
}
@VisibleForTesting @VisibleForTesting
void initFeatureProvider() { void initFeatureProvider() {
final Context context = getContext(); final Context context = getContext();
@@ -303,11 +239,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
} }
getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY, getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
mBatteryInfoLoaderCallbacks); mBatteryInfoLoaderCallbacks);
if (mPowerFeatureProvider.isEstimateDebugEnabled()) {
// Set long click action for summary to show debug info
View header = mBatteryLayoutPref.findViewById(R.id.summary1);
header.setOnLongClickListener(this);
}
} }
@VisibleForTesting @VisibleForTesting
@@ -315,13 +246,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
mNeedUpdateBatteryTip = icicle == null || mBatteryTipPreferenceController.needUpdate(); mNeedUpdateBatteryTip = icicle == null || mBatteryTipPreferenceController.needUpdate();
} }
@Override
public boolean onLongClick(View view) {
showBothEstimates();
view.setOnLongClickListener(null);
return true;
}
@Override @Override
protected void restartBatteryStatsLoader(@BatteryUpdateType int refreshType) { protected void restartBatteryStatsLoader(@BatteryUpdateType int refreshType) {
super.restartBatteryStatsLoader(refreshType); super.restartBatteryStatsLoader(refreshType);

View File

@@ -32,9 +32,10 @@ import static org.mockito.Mockito.when;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.icu.text.NumberFormat;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.PowerManager; import android.os.PowerManager;
import android.widget.TextView; import android.text.TextUtils;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
@@ -47,7 +48,7 @@ import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.testutils.shadow.ShadowUtils; import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.widget.EntityHeaderController; import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.UsageProgressBarPreference;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@@ -57,9 +58,7 @@ 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.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPowerManager;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class}) @Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
@@ -67,6 +66,7 @@ public class BatteryHeaderPreferenceControllerTest {
private static final String PREF_KEY = "battery_header"; private static final String PREF_KEY = "battery_header";
private static final int BATTERY_LEVEL = 60; private static final int BATTERY_LEVEL = 60;
private static final int BATTERY_MAX_LEVEL = 100;
private static final String TIME_LEFT = "2h30min"; private static final String TIME_LEFT = "2h30min";
private static final String BATTERY_STATUS = "Charging"; private static final String BATTERY_STATUS = "Charging";
@@ -80,13 +80,11 @@ public class BatteryHeaderPreferenceControllerTest {
private BatteryInfo mBatteryInfo; private BatteryInfo mBatteryInfo;
@Mock @Mock
private EntityHeaderController mEntityHeaderController; private EntityHeaderController mEntityHeaderController;
@Mock
private UsageProgressBarPreference mBatteryUsageProgressBarPref;
private BatteryHeaderPreferenceController mController; private BatteryHeaderPreferenceController mController;
private Context mContext; private Context mContext;
private PowerManager mPowerManager; private PowerManager mPowerManager;
private BatteryMeterView mBatteryMeterView;
private TextView mBatteryPercentText;
private TextView mSummary;
private LayoutPreference mBatteryLayoutPref;
private Intent mBatteryIntent; private Intent mBatteryIntent;
private LifecycleOwner mLifecycleOwner; private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
@@ -98,9 +96,6 @@ public class BatteryHeaderPreferenceControllerTest {
mLifecycleOwner = () -> mLifecycle; mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner); mLifecycle = new Lifecycle(mLifecycleOwner);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
mBatteryMeterView = new BatteryMeterView(mContext);
mBatteryPercentText = new TextView(mContext);
mSummary = new TextView(mContext);
ShadowEntityHeaderController.setUseMock(mEntityHeaderController); ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
mBatteryIntent = new Intent(); mBatteryIntent = new Intent();
@@ -109,8 +104,7 @@ public class BatteryHeaderPreferenceControllerTest {
mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1); mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any()); doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any());
mBatteryLayoutPref = new LayoutPreference(mContext, R.layout.battery_header); doReturn(mBatteryUsageProgressBarPref).when(mPreferenceScreen)
doReturn(mBatteryLayoutPref).when(mPreferenceScreen)
.findPreference(BatteryHeaderPreferenceController.KEY_BATTERY_HEADER); .findPreference(BatteryHeaderPreferenceController.KEY_BATTERY_HEADER);
mBatteryInfo.batteryLevel = BATTERY_LEVEL; mBatteryInfo.batteryLevel = BATTERY_LEVEL;
@@ -122,9 +116,7 @@ public class BatteryHeaderPreferenceControllerTest {
mController.setActivity(mActivity); mController.setActivity(mActivity);
mController.setFragment(mPreferenceFragment); mController.setFragment(mPreferenceFragment);
mController.setLifecycle(mLifecycle); mController.setLifecycle(mLifecycle);
mController.mBatteryMeterView = mBatteryMeterView; mController.mBatteryUsageProgressBarPref = mBatteryUsageProgressBarPref;
mController.mBatteryPercentText = mBatteryPercentText;
mController.mSummary1 = mSummary;
} }
@After @After
@@ -137,11 +129,8 @@ public class BatteryHeaderPreferenceControllerTest {
public void displayPreference_displayBatteryLevel() { public void displayPreference_displayBatteryLevel() {
mController.displayPreference(mPreferenceScreen); mController.displayPreference(mPreferenceScreen);
assertThat(((BatteryMeterView) mBatteryLayoutPref.findViewById( verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
R.id.battery_header_icon)).getBatteryLevel()).isEqualTo(BATTERY_LEVEL); verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
assertThat(((TextView) mBatteryLayoutPref.findViewById(R.id.battery_percent))
.getText().toString())
.isEqualTo("60 %");
} }
@Test @Test
@@ -150,7 +139,7 @@ public class BatteryHeaderPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo); mController.updateHeaderPreference(mBatteryInfo);
assertThat(mSummary.getText()).isEqualTo(mBatteryInfo.remainingLabel); verify(mBatteryUsageProgressBarPref).setTotalSummary(mBatteryInfo.remainingLabel);
} }
@Test @Test
@@ -161,8 +150,9 @@ public class BatteryHeaderPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo); mController.updateHeaderPreference(mBatteryInfo);
assertThat(mBatteryMeterView.mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
assertThat(mBatteryMeterView.mDrawable.getCharging()).isEqualTo(false); verify(mBatteryUsageProgressBarPref).setTotalSummary(mBatteryInfo.remainingLabel);
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
} }
@Test @Test
@@ -172,7 +162,7 @@ public class BatteryHeaderPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo); mController.updateHeaderPreference(mBatteryInfo);
assertThat(mSummary.getText()).isEqualTo(BATTERY_STATUS); verify(mBatteryUsageProgressBarPref).setTotalSummary(BATTERY_STATUS);
} }
@Test @Test
@@ -181,7 +171,7 @@ public class BatteryHeaderPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo); mController.updateHeaderPreference(mBatteryInfo);
assertThat(mSummary.getText().toString().isEmpty()).isTrue(); verify(mBatteryUsageProgressBarPref).setTotalSummary(null);
} }
@Test @Test
@@ -197,27 +187,10 @@ public class BatteryHeaderPreferenceControllerTest {
@Test @Test
public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() { public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() {
mSummary.setText(BATTERY_STATUS);
mController.quickUpdateHeaderPreference(); mController.quickUpdateHeaderPreference();
assertThat(mBatteryMeterView.getBatteryLevel()).isEqualTo(BATTERY_LEVEL); verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
assertThat(mBatteryMeterView.getCharging()).isTrue(); verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
assertThat(mBatteryPercentText.getText().toString()).isEqualTo("60 %");
assertThat(mSummary.getText()).isEqualTo(BATTERY_STATUS);
}
@Test
public void quickUpdateHeaderPreference_showPowerSave() {
boolean testValues[] = {false, true};
ShadowPowerManager shadowPowerManager = Shadows.shadowOf(mPowerManager);
for (boolean value : testValues) {
shadowPowerManager.setIsPowerSaveMode(value);
mController.quickUpdateHeaderPreference();
assertThat(mBatteryMeterView.getPowerSave()).isEqualTo(value);
}
} }
@Test @Test
@@ -226,12 +199,8 @@ public class BatteryHeaderPreferenceControllerTest {
BasePreferenceController.AVAILABLE_UNSEARCHABLE); BasePreferenceController.AVAILABLE_UNSEARCHABLE);
} }
@Test private CharSequence formatBatteryPercentageText() {
public void displayPreference_batteryNotPresent_shouldShowHelpMessage() { return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
ShadowUtils.setIsBatteryPresent(false); NumberFormat.getIntegerInstance().format(BATTERY_LEVEL));
mController.displayPreference(mPreferenceScreen);
verify(mController).showHelpMessage();
} }
} }

View File

@@ -23,13 +23,11 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -38,8 +36,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
import android.view.View;
import android.widget.TextView;
import androidx.loader.app.LoaderManager; import androidx.loader.app.LoaderManager;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -53,19 +49,14 @@ import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.XmlTestUtils; import com.android.settings.testutils.XmlTestUtils;
import com.android.settings.testutils.shadow.ShadowUtils; import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin; import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin;
import com.android.settingslib.widget.LayoutPreference;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Answers; import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@@ -79,15 +70,12 @@ import java.util.List;
public class PowerUsageSummaryTest { public class PowerUsageSummaryTest {
private static final int UID = 123; private static final int UID = 123;
private static final int UID_2 = 234;
private static final int POWER_MAH = 100; private static final int POWER_MAH = 100;
private static final long TIME_SINCE_LAST_FULL_CHARGE_MS = 120 * 60 * 1000; private static final long TIME_SINCE_LAST_FULL_CHARGE_MS = 120 * 60 * 1000;
private static final long TIME_SINCE_LAST_FULL_CHARGE_US = private static final long TIME_SINCE_LAST_FULL_CHARGE_US =
TIME_SINCE_LAST_FULL_CHARGE_MS * 1000; TIME_SINCE_LAST_FULL_CHARGE_MS * 1000;
private static final long USAGE_TIME_MS = 65 * 60 * 1000; private static final long USAGE_TIME_MS = 65 * 60 * 1000;
private static final double TOTAL_POWER = 200; private static final double TOTAL_POWER = 200;
private static final String NEW_ML_EST_SUFFIX = "(New ML est)";
private static final String OLD_EST_SUFFIX = "(Old est)";
private static Intent sAdditionalBatteryInfoIntent; private static Intent sAdditionalBatteryInfoIntent;
@BeforeClass @BeforeClass
@@ -101,12 +89,6 @@ public class PowerUsageSummaryTest {
private BatterySipper mScreenBatterySipper; private BatterySipper mScreenBatterySipper;
@Mock @Mock
private BatterySipper mCellBatterySipper; private BatterySipper mCellBatterySipper;
@Mock
private LayoutPreference mBatteryLayoutPref;
@Mock
private TextView mBatteryPercentText;
@Mock
private TextView mSummary1;
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private BatteryStatsHelper mBatteryHelper; private BatteryStatsHelper mBatteryHelper;
@Mock @Mock
@@ -114,8 +96,6 @@ public class PowerUsageSummaryTest {
@Mock @Mock
private LoaderManager mLoaderManager; private LoaderManager mLoaderManager;
@Mock @Mock
private BatteryInfo mBatteryInfo;
@Mock
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
@Mock @Mock
private BatteryBroadcastReceiver mBatteryBroadcastReceiver; private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
@@ -128,8 +108,6 @@ public class PowerUsageSummaryTest {
private Context mRealContext; private Context mRealContext;
private TestFragment mFragment; private TestFragment mFragment;
private FakeFeatureFactory mFeatureFactory; private FakeFeatureFactory mFeatureFactory;
private BatteryMeterView mBatteryMeterView;
private Intent mIntent;
@Before @Before
public void setUp() { public void setUp() {
@@ -139,8 +117,6 @@ public class PowerUsageSummaryTest {
mFeatureFactory = FakeFeatureFactory.setupForTest(); mFeatureFactory = FakeFeatureFactory.setupForTest();
mFragment = spy(new TestFragment(mRealContext)); mFragment = spy(new TestFragment(mRealContext));
mFragment.initFeatureProvider(); mFragment.initFeatureProvider();
mBatteryMeterView = new BatteryMeterView(mRealContext);
mBatteryMeterView.mDrawable = new BatteryMeterView.BatteryMeterDrawable(mRealContext, 0);
doNothing().when(mFragment).restartBatteryStatsLoader(anyInt()); doNothing().when(mFragment).restartBatteryStatsLoader(anyInt());
doReturn(mock(LoaderManager.class)).when(mFragment).getLoaderManager(); doReturn(mock(LoaderManager.class)).when(mFragment).getLoaderManager();
@@ -158,12 +134,6 @@ public class PowerUsageSummaryTest {
mCellBatterySipper.drainType = BatterySipper.DrainType.CELL; mCellBatterySipper.drainType = BatterySipper.DrainType.CELL;
mCellBatterySipper.totalPowerMah = POWER_MAH; mCellBatterySipper.totalPowerMah = POWER_MAH;
when(mBatteryLayoutPref.findViewById(R.id.summary1)).thenReturn(mSummary1);
when(mBatteryLayoutPref.findViewById(R.id.battery_percent)).thenReturn(mBatteryPercentText);
when(mBatteryLayoutPref.findViewById(R.id.battery_header_icon))
.thenReturn(mBatteryMeterView);
mFragment.setBatteryLayoutPreference(mBatteryLayoutPref);
mScreenBatterySipper.drainType = BatterySipper.DrainType.SCREEN; mScreenBatterySipper.drainType = BatterySipper.DrainType.SCREEN;
mScreenBatterySipper.usageTimeMs = USAGE_TIME_MS; mScreenBatterySipper.usageTimeMs = USAGE_TIME_MS;
@@ -206,51 +176,6 @@ public class PowerUsageSummaryTest {
.restartLoader(eq(PowerUsageSummary.BATTERY_TIP_LOADER), eq(Bundle.EMPTY), any()); .restartLoader(eq(PowerUsageSummary.BATTERY_TIP_LOADER), eq(Bundle.EMPTY), any());
} }
@Test
public void showBothEstimates_summariesAreBothModified() {
when(mFeatureFactory.powerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(any()))
.thenReturn(true);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
return mRealContext.getString(
R.string.power_usage_old_debug, invocation.getArguments()[0]);
}
}).when(mFeatureFactory.powerUsageFeatureProvider).getOldEstimateDebugString(any());
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
return mRealContext.getString(
R.string.power_usage_enhanced_debug, invocation.getArguments()[0]);
}
}).when(mFeatureFactory.powerUsageFeatureProvider).getEnhancedEstimateDebugString(any());
doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary1);
mFragment.onLongClick(new View(mRealContext));
TextView summary1 = mFragment.mBatteryLayoutPref.findViewById(R.id.summary1);
Robolectric.flushBackgroundThreadScheduler();
assertThat(summary1.getText().toString()).contains(NEW_ML_EST_SUFFIX);
assertThat(summary1.getText().toString()).contains(OLD_EST_SUFFIX);
}
@Test
public void debugMode() {
doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isEstimateDebugEnabled();
mFragment.restartBatteryInfoLoader();
ArgumentCaptor<View.OnLongClickListener> listener = ArgumentCaptor.forClass(
View.OnLongClickListener.class);
verify(mSummary1).setOnLongClickListener(listener.capture());
// Calling the listener should disable it.
listener.getValue().onLongClick(mSummary1);
verify(mSummary1).setOnLongClickListener(null);
// Restarting the loader should reset the listener.
mFragment.restartBatteryInfoLoader();
verify(mSummary1, times(2)).setOnLongClickListener(any(View.OnLongClickListener.class));
}
@Test @Test
public void refreshUi_deviceRotate_doNotUpdateBatteryTip() { public void refreshUi_deviceRotate_doNotUpdateBatteryTip() {
mFragment.mBatteryTipPreferenceController = mock(BatteryTipPreferenceController.class); mFragment.mBatteryTipPreferenceController = mock(BatteryTipPreferenceController.class);
@@ -330,23 +255,5 @@ public class PowerUsageSummaryTest {
// Override it so we can access this method in test // Override it so we can access this method in test
return super.getContentResolver(); return super.getContentResolver();
} }
@Override
void showBothEstimates() {
List<BatteryInfo> fakeBatteryInfo = new ArrayList<>(2);
BatteryInfo info1 = new BatteryInfo();
info1.batteryLevel = 10;
info1.remainingTimeUs = 10000;
info1.discharging = true;
BatteryInfo info2 = new BatteryInfo();
info2.batteryLevel = 10;
info2.remainingTimeUs = 10000;
info2.discharging = true;
fakeBatteryInfo.add(info1);
fakeBatteryInfo.add(info2);
updateViews(fakeBatteryInfo);
}
} }
} }