Merge "Revamp battery Ui in low battery mode" into oc-dev

This commit is contained in:
Lei Yu
2017-04-13 21:43:32 +00:00
committed by Android (Google) Code Review
7 changed files with 138 additions and 27 deletions

View File

@@ -15,12 +15,17 @@
*/
package com.android.settings.fuelgauge;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.graphics.ColorFilter;
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;
@@ -29,6 +34,8 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@RunWith(SettingsRobolectricTestRunner.class)
@@ -42,10 +49,15 @@ import static org.mockito.Mockito.verify;
})
public class BatteryMeterViewTest {
private static final int BATTERY_LEVEL = 100;
private static final int BATTERY_CRITICAL_LEVEL = 15;
private static final int BATTERY_LOW_LEVEL = 3;
@Mock
private BatteryMeterView.BatteryMeterDrawable mDrawable;
private ColorFilter mErrorColorFilter;
@Mock
private ColorFilter mAccentColorFilter;
private Context mContext;
private BatteryMeterView mBatteryMeterView;
private BatteryMeterView.BatteryMeterDrawable mDrawable;
@Before
public void setUp() {
@@ -53,13 +65,33 @@ public class BatteryMeterViewTest {
mContext = RuntimeEnvironment.application;
mBatteryMeterView = new BatteryMeterView(mContext);
mBatteryMeterView.setBatteryDrawable(mDrawable);
mDrawable = spy(new BatteryMeterView.BatteryMeterDrawable(mContext, 0));
mBatteryMeterView.mDrawable = mDrawable;
mBatteryMeterView.mAccentColorFilter = mAccentColorFilter;
mBatteryMeterView.mErrorColorFilter = mErrorColorFilter;
doReturn(BATTERY_CRITICAL_LEVEL).when(mDrawable).getCriticalLevel();
}
@Test
public void testSetBatteryInfo_SetCorrectly() {
mBatteryMeterView.setBatteryInfo(BATTERY_LEVEL);
public void testSetBatteryInfo_setCorrectly() {
mBatteryMeterView.setBatteryLevel(BATTERY_LEVEL);
verify(mDrawable).setBatteryLevel(BATTERY_LEVEL);
assertThat(mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
}
@Test
public void testSetBatteryInfo_levelLow_setErrorColor() {
mBatteryMeterView.setBatteryLevel(BATTERY_LOW_LEVEL);
verify(mDrawable).setBatteryColorFilter(mErrorColorFilter);
}
@Test
public void testSetBatteryInfo_levelNormal_setNormalColor() {
mBatteryMeterView.setBatteryLevel(BATTERY_LEVEL);
verify(mDrawable).setBatteryColorFilter(mAccentColorFilter);
}
}

View File

@@ -37,6 +37,8 @@ import com.android.settings.TestConfig;
import com.android.settings.Utils;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
import com.android.settingslib.BatteryInfo;
import org.junit.Before;
@@ -74,7 +76,13 @@ import static org.mockito.Mockito.when;
*/
// TODO: Improve this test class so that it starts up the real activity and fragment.
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@Config(manifest = TestConfig.MANIFEST_PATH,
sdk = TestConfig.SDK_VERSION,
shadows = {
SettingsShadowResources.class,
SettingsShadowResources.SettingsShadowTheme.class,
ShadowDynamicIndexableContentMonitor.class
})
public class PowerUsageSummaryTest {
private static final String[] PACKAGE_NAMES = {"com.app1", "com.app2"};
private static final String TIME_LEFT = "2h30min";
@@ -119,8 +127,6 @@ public class PowerUsageSummaryTest {
@Mock
private LayoutPreference mBatteryLayoutPref;
@Mock
private BatteryMeterView mBatteryMeterView;
@Mock
private TextView mBatteryPercentText;
@Mock
private TextView mSummary1;
@@ -141,6 +147,7 @@ public class PowerUsageSummaryTest {
private Context mRealContext;
private TestFragment mFragment;
private FakeFeatureFactory mFeatureFactory;
private BatteryMeterView mBatteryMeterView;
@Before
public void setUp() {
@@ -153,6 +160,8 @@ public class PowerUsageSummaryTest {
mFragment = spy(new TestFragment(mContext));
mFragment.initFeatureProvider();
mBatteryMeterView = new BatteryMeterView(mRealContext);
mBatteryMeterView.mDrawable = new BatteryMeterView.BatteryMeterDrawable(mRealContext, 0);
when(mFragment.getActivity()).thenReturn(mSettingsActivity);
when(mAdditionalBatteryInfoMenu.getItemId())
@@ -192,7 +201,7 @@ public class PowerUsageSummaryTest {
mFragment.mScreenUsagePref = mScreenUsagePref;
mFragment.mLastFullChargePref = mLastFullChargePref;
mBatteryInfo.mBatteryLevel = BATTERY_LEVEL;
mBatteryInfo.batteryLevel = BATTERY_LEVEL;
}
@Test
@@ -316,6 +325,18 @@ public class PowerUsageSummaryTest {
verify(mSummary1).setText(mBatteryInfo.remainingLabel);
}
@Test
public void testUpdatePreference_updateBatteryInfo() {
mBatteryInfo.remainingLabel = TIME_LEFT;
mBatteryInfo.batteryLevel = BATTERY_LEVEL;
mBatteryInfo.discharging = true;
mFragment.updateHeaderPreference(mBatteryInfo);
assertThat(mBatteryMeterView.mDrawable.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
assertThat(mBatteryMeterView.mDrawable.getCharging()).isEqualTo(false);
}
@Test
public void testUpdatePreference_noRemainingTime_showStatusLabel() {
mBatteryInfo.remainingLabel = null;

View File

@@ -5,9 +5,12 @@ import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.ArrayRes;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
@@ -52,6 +55,14 @@ public class SettingsShadowResources extends ShadowResources {
return directlyOn(realResources, Resources.class).getDimensionPixelSize(id);
}
@Implementation
public int getColor(@ColorRes int id, @Nullable Theme theme) throws NotFoundException {
if (id == R.color.battery_icon_color_error) {
return Color.WHITE;
}
return directlyOn(realResources, Resources.class).getColor(id, theme);
}
@Implementation
public Drawable loadDrawable(TypedValue value, int id, Theme theme)
throws NotFoundException {