Fix null pointer error && add settings side guard

The null check for the cursor happens in the try block
which causes a null pointer error in the finally block
even though we avoid executing the other code because
we still try to close the cursor. This change moves
that outside of the try block to avoid that. Also added
a test to verify that a no-op occurs in the method that
uses the enhanced prediction that would have caught this.

Additionally, the method for checking if the enhanced
prediction was available was not being called in Settings.
This CL adds that check and a relevant tests to ensure it
is respected.

Test: Robotests
Bug: 38031439
Change-Id: I6924acb5552baf09a9ff0cdef8e30881115aa1ca
This commit is contained in:
Salvador Martinez
2017-05-05 11:04:49 -07:00
parent 3dea2a9654
commit 52aa68f704
2 changed files with 33 additions and 3 deletions

View File

@@ -166,7 +166,8 @@ public class PowerUsageSummary extends PowerUsageBase implements
} }
}; };
private LoaderManager.LoaderCallbacks<Cursor> mBatteryPredictionLoaderCallbacks = @VisibleForTesting
LoaderManager.LoaderCallbacks<Cursor> mBatteryPredictionLoaderCallbacks =
new LoaderManager.LoaderCallbacks<Cursor>() { new LoaderManager.LoaderCallbacks<Cursor>() {
@Override @Override
@@ -177,8 +178,11 @@ public class PowerUsageSummary extends PowerUsageBase implements
@Override @Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (cursor == null) {
return;
}
try { try {
if (cursor != null && cursor.moveToFirst()) { if (cursor.moveToFirst()) {
mEnhancedEstimate = mEnhancedEstimate =
mPowerFeatureProvider.getTimeRemainingEstimate(cursor); mPowerFeatureProvider.getTimeRemainingEstimate(cursor);
} }
@@ -746,7 +750,8 @@ public class PowerUsageSummary extends PowerUsageBase implements
@VisibleForTesting @VisibleForTesting
void useEnhancedEstimateIfAvailable(Context context, BatteryInfo batteryInfo) { void useEnhancedEstimateIfAvailable(Context context, BatteryInfo batteryInfo) {
if (mEnhancedEstimate > 0) { if (mEnhancedEstimate > 0
&& mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
final Resources resources = context.getResources(); final Resources resources = context.getResources();
batteryInfo.remainingTimeUs = mEnhancedEstimate; batteryInfo.remainingTimeUs = mEnhancedEstimate;
String timeString = Formatter.formatShortElapsedTime(context, mEnhancedEstimate); String timeString = Formatter.formatShortElapsedTime(context, mEnhancedEstimate);

View File

@@ -494,6 +494,7 @@ public class PowerUsageSummaryTest {
// mock out the provider // mock out the provider
final long time = 60 * 1000 * 1000; final long time = 60 * 1000 * 1000;
PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext); PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(true);
mFragment.mPowerFeatureProvider = provider; mFragment.mPowerFeatureProvider = provider;
mFragment.mEnhancedEstimate = time; mFragment.mEnhancedEstimate = time;
@@ -505,6 +506,30 @@ public class PowerUsageSummaryTest {
assertThat(mBatteryInfo.remainingLabel).contains("About 17 hrs"); assertThat(mBatteryInfo.remainingLabel).contains("About 17 hrs");
} }
@Test
public void testUseEnhancedEstimateIfAvailable_noOpsOnDisabled() {
// mock out the provider
final long time = 60 * 1000 * 1000;
PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(false);
mFragment.mPowerFeatureProvider = provider;
mFragment.mEnhancedEstimate = time;
mBatteryInfo.remainingTimeUs = TIME_SINCE_LAST_FULL_CHARGE_US;
mBatteryInfo.remainingLabel = TIME_LEFT;
mFragment.useEnhancedEstimateIfAvailable(mRealContext, mBatteryInfo);
// check to make sure the values did not change
assertThat(mBatteryInfo.remainingTimeUs).isEqualTo(TIME_SINCE_LAST_FULL_CHARGE_US);
assertThat(mBatteryInfo.remainingLabel).contains(TIME_LEFT);
}
@Test
public void testBatteryPredictionLoaderCallbacks_DoesNotCrashOnNull() {
// Sanity test to check for crash
mFragment.mBatteryPredictionLoaderCallbacks.onLoadFinished(null, null);
}
@Test @Test
public void testInitAnomalyDetectionIfPossible_detectionEnabled_init() { public void testInitAnomalyDetectionIfPossible_detectionEnabled_init() {
when(mFeatureFactory.powerUsageFeatureProvider.isAnomalyDetectionEnabled()).thenReturn( when(mFeatureFactory.powerUsageFeatureProvider.isAnomalyDetectionEnabled()).thenReturn(