Clean up useless battery tips and detectors
Bug: 263193978 Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.fuelgauge.batterytip" Change-Id: Idfe03eee5acee33126f1805ef45ccc4a9fc7e7a8
This commit is contained in:
@@ -30,7 +30,6 @@ import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
|
||||
import com.android.settings.fuelgauge.batterytip.detectors.SmartBatteryDetector;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
||||
import com.android.settingslib.fuelgauge.EstimateKt;
|
||||
import com.android.settingslib.utils.AsyncLoaderCompat;
|
||||
|
||||
@@ -45,8 +44,6 @@ import java.util.List;
|
||||
public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
|
||||
private static final String TAG = "BatteryTipLoader";
|
||||
|
||||
private static final boolean USE_FAKE_DATA = false;
|
||||
|
||||
private BatteryUsageStats mBatteryUsageStats;
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
@@ -59,9 +56,6 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
|
||||
|
||||
@Override
|
||||
public List<BatteryTip> loadInBackground() {
|
||||
if (USE_FAKE_DATA) {
|
||||
return getFakeData();
|
||||
}
|
||||
final List<BatteryTip> tips = new ArrayList<>();
|
||||
final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
|
||||
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
|
||||
@@ -81,14 +75,4 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
|
||||
@Override
|
||||
protected void onDiscardResult(List<BatteryTip> result) {
|
||||
}
|
||||
|
||||
private List<BatteryTip> getFakeData() {
|
||||
final List<BatteryTip> tips = new ArrayList<>();
|
||||
tips.add(new SummaryTip(BatteryTip.StateType.NEW,
|
||||
EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
|
||||
tips.add(new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */));
|
||||
|
||||
return tips;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* 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.detectors;
|
||||
|
||||
import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper;
|
||||
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipUtils;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.AppLabelPredicate;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.AppRestrictionPredicate;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Detector whether to show summary tip. This detector should be executed as the last
|
||||
* {@link BatteryTipDetector} since it need the most up-to-date {@code visibleTips}
|
||||
*/
|
||||
public class RestrictAppDetector implements BatteryTipDetector {
|
||||
@VisibleForTesting
|
||||
static final boolean USE_FAKE_DATA = false;
|
||||
private BatteryTipPolicy mPolicy;
|
||||
@VisibleForTesting
|
||||
BatteryDatabaseManager mBatteryDatabaseManager;
|
||||
private Context mContext;
|
||||
|
||||
private AppRestrictionPredicate mAppRestrictionPredicate;
|
||||
private AppLabelPredicate mAppLabelPredicate;
|
||||
|
||||
public RestrictAppDetector(Context context, BatteryTipPolicy policy) {
|
||||
mContext = context;
|
||||
mPolicy = policy;
|
||||
mBatteryDatabaseManager = BatteryDatabaseManager.getInstance(context);
|
||||
mAppRestrictionPredicate = AppRestrictionPredicate.getInstance(context);
|
||||
mAppLabelPredicate = AppLabelPredicate.getInstance(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BatteryTip detect() {
|
||||
if (USE_FAKE_DATA) {
|
||||
return getFakeData();
|
||||
}
|
||||
if (mPolicy.appRestrictionEnabled) {
|
||||
final long oneDayBeforeMs = System.currentTimeMillis()
|
||||
- TimeUnit.HOURS.toMillis(mPolicy.appRestrictionActiveHour);
|
||||
final List<AppInfo> highUsageApps = BatteryTipUtils.detectAnomalies(mContext,
|
||||
oneDayBeforeMs);
|
||||
if (!highUsageApps.isEmpty()) {
|
||||
// If there are new anomalies, show them
|
||||
return new RestrictAppTip(BatteryTip.StateType.NEW, highUsageApps);
|
||||
} else {
|
||||
// Otherwise, show auto-handled one if it exists
|
||||
final List<AppInfo> autoHandledApps = mBatteryDatabaseManager.queryAllAnomalies(
|
||||
oneDayBeforeMs, AnomalyDatabaseHelper.State.AUTO_HANDLED);
|
||||
// Remove it if it doesn't have label or unrestricted
|
||||
autoHandledApps.removeIf(mAppLabelPredicate.or(mAppRestrictionPredicate.negate()));
|
||||
return new RestrictAppTip(autoHandledApps.isEmpty() ? BatteryTip.StateType.INVISIBLE
|
||||
: BatteryTip.StateType.HANDLED, autoHandledApps);
|
||||
}
|
||||
} else {
|
||||
return new RestrictAppTip(BatteryTip.StateType.INVISIBLE, new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
private BatteryTip getFakeData() {
|
||||
final List<AppInfo> highUsageApps = new ArrayList<>();
|
||||
highUsageApps.add(new AppInfo.Builder()
|
||||
.setPackageName(SETTINGS_PACKAGE_NAME)
|
||||
.build());
|
||||
return new RestrictAppTip(BatteryTip.StateType.NEW, highUsageApps);
|
||||
}
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* 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 com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
||||
|
||||
/**
|
||||
* Detector whether to show summary tip. This detector should be executed as the last
|
||||
* {@link BatteryTipDetector} since it need the most up-to-date {@code visibleTips}
|
||||
*/
|
||||
public class SummaryDetector implements BatteryTipDetector {
|
||||
private BatteryTipPolicy mPolicy;
|
||||
private long mAverageTimeMs;
|
||||
|
||||
public SummaryDetector(BatteryTipPolicy policy, long averageTimeMs) {
|
||||
mPolicy = policy;
|
||||
mAverageTimeMs = averageTimeMs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BatteryTip detect() {
|
||||
// Show it if there is no other tips shown
|
||||
final int state = mPolicy.summaryEnabled
|
||||
? BatteryTip.StateType.NEW
|
||||
: BatteryTip.StateType.INVISIBLE;
|
||||
return new SummaryTip(state, mAverageTimeMs);
|
||||
}
|
||||
}
|
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* 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.tips;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
/**
|
||||
* Tip to show general summary about battery life
|
||||
*/
|
||||
public class SummaryTip extends BatteryTip {
|
||||
private long mAverageTimeMs;
|
||||
|
||||
public SummaryTip(@StateType int state, long averageTimeMs) {
|
||||
super(TipType.SUMMARY, state, true /* showDialog */);
|
||||
mAverageTimeMs = averageTimeMs;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
SummaryTip(Parcel in) {
|
||||
super(in);
|
||||
mAverageTimeMs = in.readLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getTitle(Context context) {
|
||||
return context.getString(R.string.battery_tip_summary_title);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary(Context context) {
|
||||
return context.getString(R.string.battery_tip_summary_summary);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconId() {
|
||||
return R.drawable.ic_battery_status_good_24dp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconTintColorId() {
|
||||
return R.color.battery_good_color_light;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(BatteryTip tip) {
|
||||
mState = tip.mState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeLong(mAverageTimeMs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Context context, MetricsFeatureProvider metricsFeatureProvider) {
|
||||
metricsFeatureProvider.action(context, SettingsEnums.ACTION_SUMMARY_TIP,
|
||||
mState);
|
||||
}
|
||||
|
||||
public long getAverageTimeMs() {
|
||||
return mAverageTimeMs;
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
|
||||
public BatteryTip createFromParcel(Parcel in) {
|
||||
return new SummaryTip(in);
|
||||
}
|
||||
|
||||
public BatteryTip[] newArray(int size) {
|
||||
return new SummaryTip[size];
|
||||
}
|
||||
};
|
||||
}
|
@@ -35,7 +35,6 @@ import com.android.settings.fuelgauge.batterytip.tips.BatteryDefenderTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||
@@ -74,7 +73,6 @@ public class BatteryTipDialogFragmentTest {
|
||||
private RestrictAppTip mRestrictedOneAppTip;
|
||||
private RestrictAppTip mRestrictTwoAppsTip;
|
||||
private UnrestrictAppTip mUnrestrictAppTip;
|
||||
private SummaryTip mSummaryTip;
|
||||
private BatteryDefenderTip mDefenderTip;
|
||||
private AppInfo mAppInfo;
|
||||
private ShadowPackageManager mPackageManager;
|
||||
@@ -116,8 +114,6 @@ public class BatteryTipDialogFragmentTest {
|
||||
new ArrayList<>(restrictApps));
|
||||
|
||||
mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
|
||||
mSummaryTip = spy(new SummaryTip(BatteryTip.StateType.NEW,
|
||||
EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
|
||||
mDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW);
|
||||
}
|
||||
|
||||
@@ -229,19 +225,4 @@ public class BatteryTipDialogFragmentTest {
|
||||
assertThat(shadowDialog.getMessage())
|
||||
.isEqualTo(mContext.getString(R.string.battery_tip_unrestrict_app_dialog_message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnCreateDialog_summaryTip_fireDialog() {
|
||||
doReturn(AVERAGE_TIME_MS).when(mSummaryTip).getAverageTimeMs();
|
||||
mDialogFragment = BatteryTipDialogFragment.newInstance(mSummaryTip, METRICS_KEY);
|
||||
|
||||
FragmentController.setupFragment(mDialogFragment, FragmentActivity.class,
|
||||
0 /* containerViewId */, null /* bundle */);
|
||||
|
||||
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
|
||||
|
||||
assertThat(shadowDialog.getMessage()).isEqualTo(
|
||||
mContext.getText(R.string.battery_tip_dialog_summary_message));
|
||||
}
|
||||
}
|
||||
|
@@ -37,7 +37,6 @@ import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.widget.CardPreference;
|
||||
|
||||
@@ -88,9 +87,7 @@ public class BatteryTipPreferenceControllerTest {
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
|
||||
mOldBatteryTips = new ArrayList<>();
|
||||
mOldBatteryTips.add(new SummaryTip(BatteryTip.StateType.NEW, AVERAGE_TIME_MS));
|
||||
mNewBatteryTips = new ArrayList<>();
|
||||
mNewBatteryTips.add(new SummaryTip(BatteryTip.StateType.INVISIBLE, AVERAGE_TIME_MS));
|
||||
|
||||
mBatteryTipPreferenceController = buildBatteryTipPreferenceController();
|
||||
mBatteryTipPreferenceController.mCardPreference = mCardPreference;
|
||||
@@ -104,13 +101,6 @@ public class BatteryTipPreferenceControllerTest {
|
||||
assertThat(mCardPreference.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBatteryTips_tipsStateNew_isVisible() {
|
||||
mBatteryTipPreferenceController.updateBatteryTips(mOldBatteryTips);
|
||||
|
||||
assertThat(mCardPreference.isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBatteryTips_tipsStateInvisible_isInvisible() {
|
||||
mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips);
|
||||
@@ -118,15 +108,6 @@ public class BatteryTipPreferenceControllerTest {
|
||||
assertThat(mCardPreference.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBatteryTips_logBatteryTip() {
|
||||
mBatteryTipPreferenceController.updateBatteryTips(mOldBatteryTips);
|
||||
|
||||
verify(mFeatureFactory.metricsFeatureProvider).action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_SUMMARY_TIP,
|
||||
BatteryTip.StateType.NEW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCurrentBatteryTip_noTips_isNull() {
|
||||
assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull();
|
||||
@@ -138,28 +119,6 @@ public class BatteryTipPreferenceControllerTest {
|
||||
assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCurrentBatteryTip_tipsVisible_returnTips() {
|
||||
mBatteryTipPreferenceController.updateBatteryTips(mOldBatteryTips);
|
||||
|
||||
assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip().getType()).isEqualTo(
|
||||
BatteryTip.TipType.SUMMARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveAndRestore() {
|
||||
mBatteryTipPreferenceController.updateBatteryTips(mOldBatteryTips);
|
||||
final Bundle bundle = new Bundle();
|
||||
mBatteryTipPreferenceController.saveInstanceState(bundle);
|
||||
|
||||
final BatteryTipPreferenceController controller = buildBatteryTipPreferenceController();
|
||||
controller.mCardPreference = mCardPreference;
|
||||
controller.mPrefContext = mContext;
|
||||
controller.restoreInstanceState(bundle);
|
||||
|
||||
assertOnlyContainsSummaryTip(mCardPreference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestoreFromNull_shouldNotCrash() {
|
||||
final Bundle bundle = new Bundle();
|
||||
@@ -192,13 +151,6 @@ public class BatteryTipPreferenceControllerTest {
|
||||
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
private void assertOnlyContainsSummaryTip(CardPreference preference) {
|
||||
assertThat(preference.getTitle()).isEqualTo(
|
||||
mContext.getString(R.string.battery_tip_summary_title));
|
||||
assertThat(preference.getSummary()).isEqualTo(
|
||||
mContext.getString(R.string.battery_tip_summary_summary));
|
||||
}
|
||||
|
||||
private BatteryTipPreferenceController buildBatteryTipPreferenceController() {
|
||||
final BatteryTipPreferenceController controller = new BatteryTipPreferenceController(
|
||||
mContext, KEY_PREF);
|
||||
|
@@ -1,208 +0,0 @@
|
||||
/*
|
||||
* 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.detectors;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper;
|
||||
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.AppLabelPredicate;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.AppRestrictionPredicate;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||
import com.android.settings.testutils.BatteryTestUtils;
|
||||
import com.android.settings.testutils.DatabaseTestUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class RestrictAppDetectorTest {
|
||||
|
||||
private static final int RESTRICTED_UID = 222;
|
||||
private static final int UNRESTRICTED_UID = 333;
|
||||
private static final String PACKAGE_NAME = "com.android.app";
|
||||
private static final String UNINSTALLED_PACKAGE_NAME = "com.android.uninstalled";
|
||||
private static final String RESTRICTED_PACKAGE_NAME = "com.android.restricted";
|
||||
private static final String UNRESTRICTED_PACKAGE_NAME = "com.android.unrestricted";
|
||||
private Context mContext;
|
||||
private BatteryTipPolicy mPolicy;
|
||||
private RestrictAppDetector mRestrictAppDetector;
|
||||
private List<AppInfo> mAppInfoList;
|
||||
private AppInfo mAppInfo;
|
||||
@Mock
|
||||
private BatteryDatabaseManager mBatteryDatabaseManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private ApplicationInfo mApplicationInfo;
|
||||
@Mock
|
||||
private AppOpsManager mAppOpsManager;
|
||||
|
||||
@Before
|
||||
public void setUp() throws PackageManager.NameNotFoundException {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mAppInfoList = new ArrayList<>();
|
||||
mAppInfo = new AppInfo.Builder().setPackageName(PACKAGE_NAME).build();
|
||||
mAppInfoList.add(mAppInfo);
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mPolicy = spy(new BatteryTipPolicy(mContext));
|
||||
|
||||
doReturn(mContext).when(mContext).getApplicationContext();
|
||||
doReturn(mAppOpsManager).when(mContext).getSystemService(AppOpsManager.class);
|
||||
doReturn(AppOpsManager.MODE_IGNORED).when(mAppOpsManager).checkOpNoThrow(
|
||||
AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, RESTRICTED_UID, RESTRICTED_PACKAGE_NAME);
|
||||
doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager).checkOpNoThrow(
|
||||
AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UNRESTRICTED_UID,
|
||||
UNRESTRICTED_PACKAGE_NAME);
|
||||
|
||||
BatteryDatabaseManager.setUpForTest(mBatteryDatabaseManager);
|
||||
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(any(),
|
||||
anyInt());
|
||||
doReturn(PACKAGE_NAME).when(mApplicationInfo).loadLabel(any());
|
||||
doThrow(new PackageManager.NameNotFoundException()).when(
|
||||
mPackageManager).getApplicationInfo(eq(UNINSTALLED_PACKAGE_NAME), anyInt());
|
||||
|
||||
mRestrictAppDetector = new RestrictAppDetector(mContext, mPolicy);
|
||||
mRestrictAppDetector.mBatteryDatabaseManager = mBatteryDatabaseManager;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
ReflectionHelpers.setStaticField(AppLabelPredicate.class, "sInstance", null);
|
||||
ReflectionHelpers.setStaticField(AppRestrictionPredicate.class, "sInstance", null);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
DatabaseTestUtils.clearDb(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_hasAnomaly_tipNew() {
|
||||
doReturn(mAppInfoList).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), eq(AnomalyDatabaseHelper.State.NEW));
|
||||
|
||||
assertThat(mRestrictAppDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_hasAutoHandledAnomaly_tipHandled() {
|
||||
mAppInfoList.add(new AppInfo.Builder()
|
||||
.setUid(RESTRICTED_UID)
|
||||
.setPackageName(RESTRICTED_PACKAGE_NAME)
|
||||
.build());
|
||||
doReturn(new ArrayList<AppInfo>()).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), eq(AnomalyDatabaseHelper.State.NEW));
|
||||
doReturn(mAppInfoList).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), eq(AnomalyDatabaseHelper.State.AUTO_HANDLED));
|
||||
|
||||
assertThat(mRestrictAppDetector.detect().getState())
|
||||
.isEqualTo(BatteryTip.StateType.HANDLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_typeNewHasUninstalledAnomaly_removeIt() {
|
||||
mAppInfoList.add(new AppInfo.Builder()
|
||||
.setPackageName(UNINSTALLED_PACKAGE_NAME)
|
||||
.build());
|
||||
doReturn(mAppInfoList).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), eq(AnomalyDatabaseHelper.State.NEW));
|
||||
|
||||
final RestrictAppTip restrictAppTip = (RestrictAppTip) mRestrictAppDetector.detect();
|
||||
assertThat(restrictAppTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
assertThat(restrictAppTip.getRestrictAppList()).containsExactly(mAppInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_typeNewHasRestrictedAnomaly_removeIt() throws
|
||||
PackageManager.NameNotFoundException {
|
||||
mAppInfoList.add(new AppInfo.Builder()
|
||||
.setUid(RESTRICTED_UID)
|
||||
.setPackageName(RESTRICTED_PACKAGE_NAME)
|
||||
.build());
|
||||
doReturn(mAppInfoList).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), eq(AnomalyDatabaseHelper.State.NEW));
|
||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(
|
||||
eq(RESTRICTED_PACKAGE_NAME), anyInt());
|
||||
|
||||
final RestrictAppTip restrictAppTip = (RestrictAppTip) mRestrictAppDetector.detect();
|
||||
assertThat(restrictAppTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
assertThat(restrictAppTip.getRestrictAppList()).containsExactly(mAppInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_typeHandledHasUnRestrictedAnomaly_removeIt() throws
|
||||
PackageManager.NameNotFoundException {
|
||||
mAppInfoList.clear();
|
||||
mAppInfoList.add(new AppInfo.Builder()
|
||||
.setUid(UNRESTRICTED_UID)
|
||||
.setPackageName(UNRESTRICTED_PACKAGE_NAME)
|
||||
.build());
|
||||
doReturn(new ArrayList<>()).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), eq(AnomalyDatabaseHelper.State.NEW));
|
||||
doReturn(mAppInfoList).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), eq(AnomalyDatabaseHelper.State.AUTO_HANDLED));
|
||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(
|
||||
eq(UNRESTRICTED_PACKAGE_NAME), anyInt());
|
||||
|
||||
final RestrictAppTip restrictAppTip = (RestrictAppTip) mRestrictAppDetector.detect();
|
||||
assertThat(restrictAppTip.getState()).isEqualTo(BatteryTip.StateType.INVISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_noAnomaly_tipInvisible() {
|
||||
doReturn(new ArrayList<AppInfo>()).when(mBatteryDatabaseManager)
|
||||
.queryAllAnomalies(anyLong(), anyInt());
|
||||
|
||||
assertThat(mRestrictAppDetector.detect().getState())
|
||||
.isEqualTo(BatteryTip.StateType.INVISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseFakeData_alwaysFalse() {
|
||||
assertThat(RestrictAppDetector.USE_FAKE_DATA).isFalse();
|
||||
}
|
||||
}
|
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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.spy;
|
||||
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class SummaryDetectorTest {
|
||||
|
||||
private BatteryTipPolicy mPolicy;
|
||||
private static final long AVERAGE_TIME_MS = DateUtils.HOUR_IN_MILLIS;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mPolicy = spy(new BatteryTipPolicy(RuntimeEnvironment.application));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_disabledByPolicy_tipInvisible() {
|
||||
ReflectionHelpers.setField(mPolicy, "summaryEnabled", false);
|
||||
SummaryDetector detector = new SummaryDetector(mPolicy, AVERAGE_TIME_MS);
|
||||
|
||||
assertThat(detector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_notDisabled_tipInvisible() {
|
||||
SummaryDetector detector = new SummaryDetector(mPolicy, AVERAGE_TIME_MS);
|
||||
|
||||
assertThat(detector.detect().isVisible()).isFalse();
|
||||
}
|
||||
}
|
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* 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 static org.mockito.Mockito.verify;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class SummaryTipTest {
|
||||
|
||||
private static final long AVERAGE_TIME_MS = DateUtils.HOUR_IN_MILLIS;
|
||||
|
||||
@Mock
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
private Context mContext;
|
||||
private SummaryTip mSummaryTip;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mSummaryTip = new SummaryTip(BatteryTip.StateType.NEW, AVERAGE_TIME_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
Parcel parcel = Parcel.obtain();
|
||||
mSummaryTip.writeToParcel(parcel, mSummaryTip.describeContents());
|
||||
parcel.setDataPosition(0);
|
||||
|
||||
final SummaryTip parcelTip = new SummaryTip(parcel);
|
||||
|
||||
assertThat(parcelTip.getAverageTimeMs()).isEqualTo(AVERAGE_TIME_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLog() {
|
||||
mSummaryTip.log(mContext, mMetricsFeatureProvider);
|
||||
|
||||
verify(mMetricsFeatureProvider).action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_SUMMARY_TIP, BatteryTip.StateType.NEW);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user