Merge changes Ie4c98617,I6a7248d9
* changes: Add BatteryTipDialogFragment. Add high usage battery tip
This commit is contained in:
committed by
Android (Google) Code Review
commit
92aa8583c6
@@ -4,13 +4,16 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.LinkAddress;
|
||||
@@ -25,6 +28,7 @@ import android.os.storage.VolumeInfo;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.format.DateUtils;
|
||||
import android.text.style.TtsSpan;
|
||||
import android.util.IconDrawableFactory;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -46,8 +50,8 @@ import java.util.List;
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class UtilsTest {
|
||||
|
||||
private static final String PACKAGE_NAME = "com.android.app";
|
||||
private static final int USER_ID = 1;
|
||||
|
||||
@Mock
|
||||
private WifiManager wifiManager;
|
||||
@@ -59,6 +63,12 @@ public class UtilsTest {
|
||||
private DevicePolicyManagerWrapper mDevicePolicyManager;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private IconDrawableFactory mIconDrawableFactory;
|
||||
@Mock
|
||||
private ApplicationInfo mApplicationInfo;
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
@@ -332,4 +342,17 @@ public class UtilsTest {
|
||||
|
||||
assertThat(editText.getSelectionEnd()).isEqualTo(length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBadgedIcon_usePackageNameAndUserId() throws
|
||||
PackageManager.NameNotFoundException {
|
||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME,
|
||||
PackageManager.GET_META_DATA);
|
||||
|
||||
Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, PACKAGE_NAME, USER_ID);
|
||||
|
||||
// Verify that it uses the correct user id
|
||||
verify(mIconDrawableFactory).getBadgedIcon(mApplicationInfo, USER_ID);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,7 +20,9 @@ import static android.os.BatteryStats.Uid.PROCESS_STATE_FOREGROUND;
|
||||
import static android.os.BatteryStats.Uid.PROCESS_STATE_FOREGROUND_SERVICE;
|
||||
import static android.os.BatteryStats.Uid.PROCESS_STATE_TOP;
|
||||
import static android.os.BatteryStats.Uid.PROCESS_STATE_TOP_SLEEPING;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
@@ -141,6 +143,7 @@ public class BatteryUtilsTest {
|
||||
private BatteryUtils mBatteryUtils;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private PowerUsageFeatureProvider mProvider;
|
||||
private List<BatterySipper> mUsageList;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -194,6 +197,12 @@ public class BatteryUtilsTest {
|
||||
mBatteryUtils.mPowerUsageFeatureProvider = mProvider;
|
||||
doReturn(0L).when(mBatteryUtils).getForegroundServiceTotalTimeUs(
|
||||
any(BatteryStats.Uid.class), anyLong());
|
||||
|
||||
mUsageList = new ArrayList<>();
|
||||
mUsageList.add(mNormalBatterySipper);
|
||||
mUsageList.add(mScreenBatterySipper);
|
||||
mUsageList.add(mCellBatterySipper);
|
||||
doReturn(mUsageList).when(mBatteryStatsHelper).getUsageList();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -468,4 +477,28 @@ public class BatteryUtilsTest {
|
||||
verify(mBatteryStatsHelper).refreshStats(BatteryStats.STATS_SINCE_CHARGED,
|
||||
mUserManager.getUserProfiles());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindBatterySipperByType_findTypeScreen() {
|
||||
BatterySipper sipper = mBatteryUtils.findBatterySipperByType(mUsageList,
|
||||
BatterySipper.DrainType.SCREEN);
|
||||
|
||||
assertThat(sipper).isSameAs(mScreenBatterySipper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindBatterySipperByType_findTypeApp() {
|
||||
BatterySipper sipper = mBatteryUtils.findBatterySipperByType(mUsageList,
|
||||
BatterySipper.DrainType.APP);
|
||||
|
||||
assertThat(sipper).isSameAs(mNormalBatterySipper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculateScreenUsageTime_returnCorrectTime() {
|
||||
mScreenBatterySipper.usageTimeMs = TIME_EXPECTED_FOREGROUND;
|
||||
|
||||
assertThat(mBatteryUtils.calculateScreenUsageTime(mBatteryStatsHelper)).isEqualTo(
|
||||
TIME_EXPECTED_FOREGROUND);
|
||||
}
|
||||
}
|
||||
|
@@ -67,7 +67,6 @@ public class PowerUsageAnomalyDetailsTest {
|
||||
private static final String PACKAGE_NAME_1 = "com.android.app1";
|
||||
private static final String PACKAGE_NAME_2 = "com.android.app2";
|
||||
private static final String PACKAGE_NAME_3 = "com.android.app3";
|
||||
private static final int USER_ID = 1;
|
||||
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
@@ -198,16 +197,4 @@ public class PowerUsageAnomalyDetailsTest {
|
||||
assertThat(mBundle.getParcelableArrayList(
|
||||
PowerUsageAnomalyDetails.EXTRA_ANOMALY_LIST)).isEqualTo(mAnomalyList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBadgedIcon_usePackageNameAndUserId() throws
|
||||
PackageManager.NameNotFoundException {
|
||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME_1,
|
||||
PackageManager.GET_META_DATA);
|
||||
|
||||
mFragment.getBadgedIcon(PACKAGE_NAME_1, USER_ID);
|
||||
|
||||
// Verify that it uses the correct user id
|
||||
verify(mIconDrawableFactory).getBadgedIcon(mApplicationInfo, USER_ID);
|
||||
}
|
||||
}
|
||||
|
@@ -247,34 +247,6 @@ public class PowerUsageSummaryTest {
|
||||
assertThat(mFragment.mShowAllApps).isEqualTo(!isShowApps);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindBatterySipperByType_findTypeScreen() {
|
||||
BatterySipper sipper = mFragment.findBatterySipperByType(mUsageList,
|
||||
BatterySipper.DrainType.SCREEN);
|
||||
|
||||
assertThat(sipper).isSameAs(mScreenBatterySipper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindBatterySipperByType_findTypeApp() {
|
||||
BatterySipper sipper = mFragment.findBatterySipperByType(mUsageList,
|
||||
BatterySipper.DrainType.APP);
|
||||
|
||||
assertThat(sipper).isSameAs(mNormalBatterySipper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateScreenPreference_showCorrectSummary() {
|
||||
doReturn(mScreenBatterySipper).when(mFragment).findBatterySipperByType(any(), any());
|
||||
doReturn(mRealContext).when(mFragment).getContext();
|
||||
final CharSequence expectedSummary = Utils.formatElapsedTime(mRealContext, USAGE_TIME_MS,
|
||||
false);
|
||||
|
||||
mFragment.updateScreenPreference();
|
||||
|
||||
assertThat(mScreenUsagePref.getSubtitle()).isEqualTo(expectedSummary);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateLastFullChargePreference_showCorrectSummary() {
|
||||
doReturn(mRealContext).when(mFragment).getContext();
|
||||
@@ -284,16 +256,6 @@ public class PowerUsageSummaryTest {
|
||||
assertThat(mLastFullChargePref.getSubtitle()).isEqualTo("2 hr. ago");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePreference_usageListEmpty_shouldNotCrash() {
|
||||
when(mBatteryHelper.getUsageList()).thenReturn(new ArrayList<BatterySipper>());
|
||||
doReturn(STUB_STRING).when(mFragment).getString(anyInt(), any());
|
||||
doReturn(mRealContext).when(mFragment).getContext();
|
||||
|
||||
// Should not crash when update
|
||||
mFragment.updateScreenPreference();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonIndexableKeys_MatchPreferenceKeys() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
|
@@ -85,7 +85,7 @@ public class BatteryTipPreferenceControllerTest {
|
||||
mNewBatteryTips.add(new SummaryTip(BatteryTip.StateType.INVISIBLE));
|
||||
|
||||
mBatteryTipPreferenceController = new BatteryTipPreferenceController(mContext, KEY_PREF,
|
||||
mBatteryTipListener);
|
||||
null, mBatteryTipListener);
|
||||
mBatteryTipPreferenceController.mPreferenceGroup = mPreferenceGroup;
|
||||
mBatteryTipPreferenceController.mPrefContext = mContext;
|
||||
}
|
||||
|
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.batterytip.detectors;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.BatteryStats;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.android.internal.os.BatterySipper;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.fuelgauge.BatteryInfo;
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
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 org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class HighUsageDetectorTest {
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
@Mock
|
||||
private BatteryUtils mBatteryUtils;
|
||||
@Mock
|
||||
private BatterySipper mBatterySipper;
|
||||
|
||||
private BatteryTipPolicy mPolicy;
|
||||
private HighUsageDetector mHighUsageDetector;
|
||||
private List<BatterySipper> mUsageList;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mPolicy = spy(new BatteryTipPolicy(mContext));
|
||||
mHighUsageDetector = new HighUsageDetector(mContext, mPolicy, mBatteryStatsHelper);
|
||||
mHighUsageDetector.mBatteryUtils = mBatteryUtils;
|
||||
|
||||
mUsageList = new ArrayList<>();
|
||||
mUsageList.add(mBatterySipper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_disabledByPolicy_tipInvisible() {
|
||||
ReflectionHelpers.setField(mPolicy, "highUsageEnabled", false);
|
||||
|
||||
assertThat(mHighUsageDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_containsHighUsageApp_tipVisible() {
|
||||
doReturn(2 * DateUtils.HOUR_IN_MILLIS).when(mBatteryUtils).calculateScreenUsageTime(
|
||||
mBatteryStatsHelper);
|
||||
doReturn(mUsageList).when(mBatteryStatsHelper).getUsageList();
|
||||
doReturn(DateUtils.HOUR_IN_MILLIS).when(mBatteryUtils).getProcessTimeMs(
|
||||
BatteryUtils.StatusType.FOREGROUND, mBatterySipper.uidObj,
|
||||
BatteryStats.STATS_SINCE_CHARGED);
|
||||
|
||||
assertThat(mHighUsageDetector.detect().isVisible()).isTrue();
|
||||
}
|
||||
}
|
@@ -19,6 +19,8 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.IdRes;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
@@ -58,10 +60,32 @@ public class BatteryTipTest {
|
||||
assertThat(preference.getIcon()).isEqualTo(mContext.getDrawable(ICON_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
final BatteryTip batteryTip = new TestBatteryTip();
|
||||
|
||||
Parcel parcel = Parcel.obtain();
|
||||
batteryTip.writeToParcel(parcel, batteryTip.describeContents());
|
||||
parcel.setDataPosition(0);
|
||||
|
||||
final BatteryTip parcelTip = new TestBatteryTip(parcel);
|
||||
|
||||
assertThat(parcelTip.getTitle(mContext)).isEqualTo(TITLE);
|
||||
assertThat(parcelTip.getSummary(mContext)).isEqualTo(SUMMARY);
|
||||
assertThat(parcelTip.getIconId()).isEqualTo(ICON_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to test the non abstract methods in {@link TestBatteryTip}
|
||||
*/
|
||||
public class TestBatteryTip extends BatteryTip {
|
||||
public static class TestBatteryTip extends BatteryTip {
|
||||
TestBatteryTip() {
|
||||
super(TipType.SUMMARY, StateType.NEW, true);
|
||||
}
|
||||
|
||||
TestBatteryTip(Parcel in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle(Context context) {
|
||||
@@ -88,10 +112,15 @@ public class BatteryTipTest {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog buildDialog() {
|
||||
return null;
|
||||
}
|
||||
public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
|
||||
public BatteryTip createFromParcel(Parcel in) {
|
||||
return new TestBatteryTip(in);
|
||||
}
|
||||
|
||||
public BatteryTip[] newArray(int size) {
|
||||
return new TestBatteryTip[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.batterytip.tips;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.fuelgauge.batterytip.HighUsageApp;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class HighUsageTipTest {
|
||||
private static final String PACKAGE_NAME = "com.android.app";
|
||||
private static final long SCREEN_TIME = 30 * DateUtils.MINUTE_IN_MILLIS;
|
||||
|
||||
private Context mContext;
|
||||
private HighUsageTip mBatteryTip;
|
||||
private List<HighUsageApp> mUsageAppList;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
|
||||
mUsageAppList = new ArrayList<>();
|
||||
mUsageAppList.add(new HighUsageApp(PACKAGE_NAME, SCREEN_TIME));
|
||||
mBatteryTip = new HighUsageTip(SCREEN_TIME, mUsageAppList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
|
||||
Parcel parcel = Parcel.obtain();
|
||||
mBatteryTip.writeToParcel(parcel, mBatteryTip.describeContents());
|
||||
parcel.setDataPosition(0);
|
||||
|
||||
final HighUsageTip parcelTip = new HighUsageTip(parcel);
|
||||
|
||||
assertThat(parcelTip.getTitle(mContext)).isEqualTo("Phone used heavily");
|
||||
assertThat(parcelTip.getType()).isEqualTo(BatteryTip.TipType.HIGH_DEVICE_USAGE);
|
||||
assertThat(parcelTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
assertThat(parcelTip.getScreenTimeMs()).isEqualTo(SCREEN_TIME);
|
||||
assertThat(parcelTip.mHighUsageAppList.size()).isEqualTo(1);
|
||||
final HighUsageApp app = parcelTip.mHighUsageAppList.get(0);
|
||||
assertThat(app.packageName).isEqualTo(PACKAGE_NAME);
|
||||
assertThat(app.screenOnTimeMs).isEqualTo(SCREEN_TIME);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user