Merge "Add BatteryMeterView in Settings"

This commit is contained in:
TreeHugger Robot
2017-02-13 20:45:17 +00:00
committed by Android (Google) Code Review
15 changed files with 366 additions and 37 deletions

View File

@@ -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);
}
}

View File

@@ -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;
@@ -57,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 BATTERY_SYSTEM_USAGE = 600;
private static final double PRECISION = 0.001;
@@ -83,6 +91,18 @@ public class PowerUsageSummaryTest {
private BatterySipper mSystemBatterySipper;
@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;
@@ -113,6 +133,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;
@@ -242,6 +268,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;

View File

@@ -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 {