Add BatteryMeterView in Settings
1. Show gauge icon at the top of battery main page instead of battery usage graph. 2. Move the click action from battery usage graph to gauge icon. Bug: 34387464 Test: RunSettingsRoboTest Change-Id: Ib182619d6805b401cde03a50e2ae907cf4df7b94
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import android.content.Context;
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources.SettingsShadowTheme;
|
||||
import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
// TODO: Consider making the shadow class set global using a robolectric.properties file.
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH,
|
||||
sdk = TestConfig.SDK_VERSION,
|
||||
shadows = {
|
||||
SettingsShadowResources.class,
|
||||
SettingsShadowTheme.class,
|
||||
ShadowDynamicIndexableContentMonitor.class
|
||||
})
|
||||
public class BatteryMeterViewTest {
|
||||
private static final int BATTERY_LEVEL = 100;
|
||||
@Mock
|
||||
private BatteryMeterView.BatteryMeterDrawable mDrawable;
|
||||
private Context mContext;
|
||||
private BatteryMeterView mBatteryMeterView;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mBatteryMeterView = new BatteryMeterView(mContext);
|
||||
mBatteryMeterView.setBatteryDrawable(mDrawable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBatteryInfo_SetCorrectly() {
|
||||
mBatteryMeterView.setBatteryInfo(BATTERY_LEVEL);
|
||||
|
||||
verify(mDrawable).setBatteryLevel(BATTERY_LEVEL);
|
||||
}
|
||||
}
|
@@ -23,11 +23,15 @@ import android.text.format.DateUtils;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsImpl;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.BatteryInfo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -36,6 +40,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -44,7 +49,6 @@ 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.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -58,8 +62,11 @@ import static org.mockito.Mockito.when;
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class PowerUsageSummaryTest {
|
||||
private static final String[] PACKAGE_NAMES = {"com.app1", "com.app2"};
|
||||
private static final String TIME_LEFT = "2h30min";
|
||||
private static final int UID = 123;
|
||||
private static final int POWER_MAH = 100;
|
||||
private static final int BATTERY_LEVEL_FULL = 100;
|
||||
private static final int BATTERY_LEVEL_HALF = 50;
|
||||
private static final double BATTERY_SCREEN_USAGE = 300;
|
||||
private static final double PRECISION = 0.001;
|
||||
private static final Intent ADDITIONAL_BATTERY_INFO_INTENT =
|
||||
@@ -81,6 +88,18 @@ public class PowerUsageSummaryTest {
|
||||
private BatterySipper mScreenBatterySipper;
|
||||
@Mock
|
||||
private PowerGaugePreference mPreference;
|
||||
@Mock
|
||||
private LayoutPreference mBatteryLayoutPref;
|
||||
@Mock
|
||||
private BatteryMeterView mBatteryMeterView;
|
||||
@Mock
|
||||
private TextView mTimeText;
|
||||
@Mock
|
||||
private TextView mSummary1;
|
||||
@Mock
|
||||
private TextView mSummary2;
|
||||
@Mock
|
||||
private BatteryInfo mBatteryInfo;
|
||||
|
||||
private TestFragment mFragment;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
@@ -111,6 +130,12 @@ public class PowerUsageSummaryTest {
|
||||
when(mNormalBatterySipper.getPackages()).thenReturn(PACKAGE_NAMES);
|
||||
when(mNormalBatterySipper.getUid()).thenReturn(UID);
|
||||
mNormalBatterySipper.totalPowerMah = POWER_MAH;
|
||||
when(mBatteryLayoutPref.findViewById(R.id.summary1)).thenReturn(mSummary1);
|
||||
when(mBatteryLayoutPref.findViewById(R.id.summary2)).thenReturn(mSummary2);
|
||||
when(mBatteryLayoutPref.findViewById(R.id.time)).thenReturn(mTimeText);
|
||||
when(mBatteryLayoutPref.findViewById(R.id.battery_header_icon))
|
||||
.thenReturn(mBatteryMeterView);
|
||||
mPowerUsageSummary.setBatteryLayoutPreference(mBatteryLayoutPref);
|
||||
|
||||
mScreenBatterySipper.drainType = BatterySipper.DrainType.SCREEN;
|
||||
mScreenBatterySipper.totalPowerMah = BATTERY_SCREEN_USAGE;
|
||||
@@ -230,6 +255,28 @@ public class PowerUsageSummaryTest {
|
||||
verify(mPreference).setSummary(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePreference_BatteryFull_DoNotShowSummary() {
|
||||
mBatteryInfo.mBatteryLevel = BATTERY_LEVEL_FULL;
|
||||
mBatteryInfo.remainingLabel = TIME_LEFT;
|
||||
mPowerUsageSummary.updateHeaderPreference(mBatteryInfo);
|
||||
|
||||
verify(mSummary1).setVisibility(View.INVISIBLE);
|
||||
verify(mSummary2).setVisibility(View.INVISIBLE);
|
||||
verify(mTimeText).setText(mBatteryInfo.remainingLabel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePreference_BatteryNotFull_ShowSummary() {
|
||||
mBatteryInfo.mBatteryLevel = BATTERY_LEVEL_HALF;
|
||||
mBatteryInfo.remainingLabel = TIME_LEFT;
|
||||
mPowerUsageSummary.updateHeaderPreference(mBatteryInfo);
|
||||
|
||||
verify(mSummary1).setVisibility(View.VISIBLE);
|
||||
verify(mSummary2).setVisibility(View.VISIBLE);
|
||||
verify(mTimeText).setText(mBatteryInfo.remainingLabel);
|
||||
}
|
||||
|
||||
public static class TestFragment extends PowerUsageSummary {
|
||||
|
||||
private Context mContext;
|
||||
|
@@ -7,6 +7,7 @@ import android.content.res.Resources.Theme;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.ArrayRes;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
@@ -59,6 +60,16 @@ public class SettingsShadowResources extends ShadowResources {
|
||||
return super.loadDrawable(value, id, theme);
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public int[] getIntArray(@ArrayRes int id) throws NotFoundException {
|
||||
// The Robolectric isn't aware of resources in settingslib, so we need to stub it here
|
||||
if (id == com.android.settings.R.array.batterymeter_bolt_points
|
||||
|| id == com.android.settings.R.array.batterymeter_plus_points) {
|
||||
return new int[2];
|
||||
}
|
||||
return directlyOn(realResources, Resources.class).getIntArray(id);
|
||||
}
|
||||
|
||||
@Implements(Theme.class)
|
||||
public static class SettingsShadowTheme extends ShadowTheme {
|
||||
|
||||
|
Reference in New Issue
Block a user