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

Commit 120e7a71 authored by Alex Kulesza's avatar Alex Kulesza
Browse files

Make time remaining estimate debug mode more reliable.

Previously, the long click handler was unregistered on first use and
never re-registered. This meant that, e.g., on plugging and unplugging
the phone, debug mode stopped working. This change moves long click
activation into restartBatteryInfoLoader().

Bug: 63133793
Test: make RunSettingsRoboTests
Change-Id: I4c25a1f05ab3718bf06faf455cf670b5460a7306
parent 4b351b4c
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -231,13 +231,7 @@ public class PowerUsageSummary extends PowerUsageBase implements

        initFeatureProvider();
        mBatteryLayoutPref = (LayoutPreference) findPreference(KEY_BATTERY_HEADER);
        View header = mBatteryLayoutPref.findViewById(R.id.summary1);
        // Unfortunately setting a long click listener on a means it will no longer pass the regular
        // click event to the parent, so we have to register a regular click listener as well.
        if (mPowerFeatureProvider.isEstimateDebugEnabled()) {
            header.setOnLongClickListener(this);
            header.setOnClickListener(this);
        }

        mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
        mScreenUsagePref = (PowerGaugePreference) findPreference(KEY_SCREEN_USAGE);
        mLastFullChargePref = (PowerGaugePreference) findPreference(
@@ -772,6 +766,14 @@ public class PowerUsageSummary extends PowerUsageBase implements
    void restartBatteryInfoLoader() {
        getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
                mBatteryInfoLoaderCallbacks);
        if (mPowerFeatureProvider.isEstimateDebugEnabled()) {
            // Unfortunately setting a long click listener on a view means it will no
            // longer pass the regular click event to the parent, so we have to register
            // a regular click listener as well.
            View header = mBatteryLayoutPref.findViewById(R.id.summary1);
            header.setOnLongClickListener(this);
            header.setOnClickListener(this);
        }
    }

    private static List<BatterySipper> getFakeStats() {
+22 −0
Original line number Diff line number Diff line
@@ -27,8 +27,10 @@ import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -73,6 +75,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
@@ -184,6 +187,7 @@ public class PowerUsageSummaryTest {
        mBatteryMeterView = new BatteryMeterView(mRealContext);
        mBatteryMeterView.mDrawable = new BatteryMeterView.BatteryMeterDrawable(mRealContext, 0);
        doNothing().when(mFragment).restartBatteryStatsLoader();
        doReturn(mock(LoaderManager.class)).when(mFragment).getLoaderManager();

        when(mFragment.getActivity()).thenReturn(mSettingsActivity);
        when(mAdditionalBatteryInfoMenu.getItemId())
@@ -546,6 +550,24 @@ public class PowerUsageSummaryTest {
        assertThat(mFragment.shouldHideSipper(mNormalBatterySipper)).isFalse();
    }

    @Test
    public void testDebugMode() {
        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));
    }

    public static class TestFragment extends PowerUsageSummary {

        private Context mContext;