Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 52aa68f7 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

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
parent 3dea2a96
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -166,7 +166,8 @@ public class PowerUsageSummary extends PowerUsageBase implements
                }
            };

    private LoaderManager.LoaderCallbacks<Cursor> mBatteryPredictionLoaderCallbacks =
    @VisibleForTesting
    LoaderManager.LoaderCallbacks<Cursor> mBatteryPredictionLoaderCallbacks =
            new LoaderManager.LoaderCallbacks<Cursor>() {

                @Override
@@ -177,8 +178,11 @@ public class PowerUsageSummary extends PowerUsageBase implements

                @Override
                public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
                    if (cursor == null) {
                        return;
                    }
                    try {
                        if (cursor != null && cursor.moveToFirst()) {
                        if (cursor.moveToFirst()) {
                            mEnhancedEstimate =
                                    mPowerFeatureProvider.getTimeRemainingEstimate(cursor);
                        }
@@ -746,7 +750,8 @@ public class PowerUsageSummary extends PowerUsageBase implements

    @VisibleForTesting
    void useEnhancedEstimateIfAvailable(Context context, BatteryInfo batteryInfo) {
        if (mEnhancedEstimate > 0) {
        if (mEnhancedEstimate > 0
                && mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
            final Resources resources = context.getResources();
            batteryInfo.remainingTimeUs = mEnhancedEstimate;
            String timeString = Formatter.formatShortElapsedTime(context, mEnhancedEstimate);
+25 −0
Original line number Diff line number Diff line
@@ -494,6 +494,7 @@ public class PowerUsageSummaryTest {
        // mock out the provider
        final long time = 60 * 1000 * 1000;
        PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
        when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(true);
        mFragment.mPowerFeatureProvider = provider;
        mFragment.mEnhancedEstimate = time;

@@ -505,6 +506,30 @@ public class PowerUsageSummaryTest {
        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
    public void testInitAnomalyDetectionIfPossible_detectionEnabled_init() {
        when(mFeatureFactory.powerUsageFeatureProvider.isAnomalyDetectionEnabled()).thenReturn(