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

Commit bdd0f0f6 authored by Jaewoong Jung's avatar Jaewoong Jung Committed by Android (Google) Code Review
Browse files

Merge "Implements additional battery info menu behavior."

parents 41fa24b9 6a43a06c
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.settings.fuelgauge;

import android.content.Intent;

/**
 * Feature Provider used in power usage
 */
@@ -25,5 +27,13 @@ public interface PowerUsageFeatureProvider {
   */
  boolean isLocationSettingEnabled(String[] packages);

  /**
   * Check whether additional battery info feature is enabled.
   */
  boolean isAdditionalBatteryInfoEnabled();

  /**
   * Gets an {@link Intent} to show additional battery info.
   */
  Intent getAdditionalBatteryInfoIntent();
}
+5 −0
Original line number Diff line number Diff line
@@ -175,6 +175,11 @@ public class PowerUsageSummary extends PowerUsageBase {
                sa.startPreferencePanel(ManageApplications.class.getName(), args,
                        R.string.high_power_apps, null, null, 0);
                return true;
            case MENU_ADDITIONAL_BATTERY_INFO:
                startActivity(FeatureFactory.getFactory(getContext())
                        .getPowerUsageFeatureProvider(getContext())
                        .getAdditionalBatteryInfoIntent());
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
+31 −5
Original line number Diff line number Diff line
package com.android.settings.fuelgauge;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@@ -9,6 +10,7 @@ import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
@@ -18,6 +20,7 @@ import org.robolectric.annotation.Config;

import static com.android.settings.fuelgauge.PowerUsageBase.MENU_STATS_REFRESH;
import static com.android.settings.fuelgauge.PowerUsageSummary.MENU_ADDITIONAL_BATTERY_INFO;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -25,16 +28,22 @@ import static org.mockito.Mockito.when;
/**
 * Unit tests for {@link PowerUsageSummary}.
 */
// TODO: Improve this test class so that it starts up the real activity and fragment.
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class PowerUsageSummaryTest {

    private static final Intent ADDITIONAL_BATTERY_INFO_INTENT =
            new Intent("com.example.app.ADDITIONAL_BATTERY_INFO");

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private Context mContext;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private Menu mMenu;
    @Mock
    private MenuItem mRefresh;
    private MenuItem mRefreshMenu;
    @Mock
    private MenuItem mAdditionalBatteryInfoMenu;
    @Mock
    private MenuInflater mMenuInflater;

@@ -49,15 +58,19 @@ public class PowerUsageSummaryTest {

        mFragment = new TestFragment(mContext);

        when(mMenu.add(Menu.NONE, PowerUsageBase.MENU_STATS_REFRESH, Menu.NONE,
        when(mMenu.add(Menu.NONE, MENU_STATS_REFRESH, Menu.NONE,
                R.string.menu_stats_refresh)
                .setIcon(com.android.internal.R.drawable.ic_menu_refresh)
                .setAlphabeticShortcut('r'))
                .thenReturn(mRefresh);
                .thenReturn(mRefreshMenu);
        when(mAdditionalBatteryInfoMenu.getItemId())
                .thenReturn(MENU_ADDITIONAL_BATTERY_INFO);
        when(mFeatureFactory.powerUsageFeatureProvider.getAdditionalBatteryInfoIntent())
                .thenReturn(ADDITIONAL_BATTERY_INFO_INTENT);
    }

    @Test
    public void testOnCreateOptionsMenu_additionalBatteryInfoEnabled() {
    public void testOptionsMenu_additionalBatteryInfoEnabled() {
        when(mFeatureFactory.powerUsageFeatureProvider.isAdditionalBatteryInfoEnabled())
                .thenReturn(true);

@@ -65,10 +78,15 @@ public class PowerUsageSummaryTest {

        verify(mMenu).add(Menu.NONE, MENU_ADDITIONAL_BATTERY_INFO,
                Menu.NONE, R.string.additional_battery_info);

        mFragment.onOptionsItemSelected(mAdditionalBatteryInfoMenu);

        assertThat(mFragment.mStartActivityCalled).isTrue();
        assertThat(mFragment.mStartActivityIntent).isEqualTo(ADDITIONAL_BATTERY_INFO_INTENT);
    }

    @Test
    public void testOnCreateOptionsMenu_additionalBatteryInfoDisabled() {
    public void testOptionsMenu_additionalBatteryInfoDisabled() {
        when(mFeatureFactory.powerUsageFeatureProvider.isAdditionalBatteryInfoEnabled())
                .thenReturn(false);

@@ -81,6 +99,8 @@ public class PowerUsageSummaryTest {
    public static class TestFragment extends PowerUsageSummary {

        private Context mContext;
        private boolean mStartActivityCalled;
        private Intent mStartActivityIntent;

        public TestFragment(Context context) {
            mContext = context;
@@ -90,5 +110,11 @@ public class PowerUsageSummaryTest {
        public Context getContext() {
            return mContext;
        }

        @Override
        public void startActivity(Intent intent) {
            mStartActivityCalled = true;
            mStartActivityIntent = intent;
        }
    }
}