Merge changes Ib0328f03,Ic10efc63

* changes:
  Add restrict and unrestrict dialog
  Add app restrict tip and detector
This commit is contained in:
TreeHugger Robot
2018-02-06 05:20:26 +00:00
committed by Android (Google) Code Review
21 changed files with 822 additions and 90 deletions

View File

@@ -142,18 +142,17 @@ public class BackgroundActivityPreferenceControllerTest {
mController.handlePreferenceTreeClick(mPreference);
verify(mController).showDialog();
verify(mController).showDialog(false /* restrict */);
}
@Test
public void testHandlePreferenceTreeClick_unRestrictApp_setModeAllowed() {
public void testHandlePreferenceTreeClick_unRestrictApp_showDialog() {
doReturn(AppOpsManager.MODE_IGNORED).when(mAppOpsManager).checkOpNoThrow(anyInt(),
anyInt(), anyString());
mController.handlePreferenceTreeClick(mPreference);
verify(mBatteryUtils).setForceAppStandby(UID_LOW_SDK, LOW_SDK_PACKAGE,
AppOpsManager.MODE_ALLOWED);
verify(mController).showDialog(true /* restrict */);
}
@Test
@@ -211,17 +210,4 @@ public class BackgroundActivityPreferenceControllerTest {
public void testIsAvailable_ReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testWarningDialog() {
BackgroundActivityPreferenceController.WarningDialogFragment dialogFragment =
new BackgroundActivityPreferenceController.WarningDialogFragment();
dialogFragment.setTargetFragment(mFragment, 0);
FragmentTestUtil.startFragment(dialogFragment);
final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
ShadowAlertDialog shadowDialog = shadowOf(dialog);
final Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
shadowDialog.clickOn(okButton.getId());
verify(mFragment).onLimitBackgroundActivity();
}
}

View File

@@ -32,6 +32,7 @@ import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -523,4 +524,27 @@ public class BatteryUtilsTest {
public void testIsLegacyApp_SdkLargerOrEqualThanO_ReturnFalse() {
assertThat(mBatteryUtils.isLegacyApp(HIGH_SDK_PACKAGE)).isFalse();
}
@Test
public void testSetForceAppStandby_forcePreOApp_forceTwoRestrictions() {
mBatteryUtils.setForceAppStandby(UID, LOW_SDK_PACKAGE, AppOpsManager.MODE_IGNORED);
// Restrict both OP_RUN_IN_BACKGROUND and OP_RUN_ANY_IN_BACKGROUND
verify(mAppOpsManager).setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, UID, LOW_SDK_PACKAGE,
AppOpsManager.MODE_IGNORED);
verify(mAppOpsManager).setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UID, LOW_SDK_PACKAGE,
AppOpsManager.MODE_IGNORED);
}
@Test
public void testSetForceAppStandby_forceOApp_forceOneRestriction() {
mBatteryUtils.setForceAppStandby(UID, HIGH_SDK_PACKAGE, AppOpsManager.MODE_IGNORED);
// Don't restrict OP_RUN_IN_BACKGROUND because it is already been restricted for O app
verify(mAppOpsManager, never()).setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, UID,
HIGH_SDK_PACKAGE, AppOpsManager.MODE_IGNORED);
// Restrict OP_RUN_ANY_IN_BACKGROUND
verify(mAppOpsManager).setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, UID,
HIGH_SDK_PACKAGE, AppOpsManager.MODE_IGNORED);
}
}

View File

@@ -27,10 +27,13 @@ import android.text.format.DateUtils;
import com.android.settings.R;
import com.android.settings.TestConfig;
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.UnrestrictAppTip;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowRuntimePermissionPresenter;
import com.android.settings.testutils.shadow.ShadowUtils;
import org.junit.Before;
import org.junit.Test;
@@ -47,14 +50,18 @@ import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = ShadowRuntimePermissionPresenter.class)
shadows = ShadowUtils.class)
public class BatteryTipDialogFragmentTest {
private static final String PACKAGE_NAME = "com.android.app";
private static final String DISPLAY_NAME = "app";
private static final long SCREEN_TIME_MS = DateUtils.HOUR_IN_MILLIS;
private BatteryTipDialogFragment mDialogFragment;
private Context mContext;
private HighUsageTip mHighUsageTip;
private RestrictAppTip mRestrictedOneAppTip;
private RestrictAppTip mRestrictAppsTip;
private UnrestrictAppTip mUnrestrictAppTip;
@Before
public void setUp() {
@@ -64,9 +71,22 @@ public class BatteryTipDialogFragmentTest {
FakeFeatureFactory.setupForTest();
List<AppInfo> highUsageTips = new ArrayList<>();
highUsageTips.add(new AppInfo.Builder().setScreenOnTimeMs(SCREEN_TIME_MS).setPackageName(
PACKAGE_NAME).build());
final AppInfo appInfo = new AppInfo.Builder()
.setScreenOnTimeMs(SCREEN_TIME_MS)
.setPackageName(PACKAGE_NAME)
.build();
highUsageTips.add(appInfo);
mHighUsageTip = new HighUsageTip(SCREEN_TIME_MS, highUsageTips);
final List<AppInfo> restrictApps = new ArrayList<>();
restrictApps.add(appInfo);
mRestrictedOneAppTip = new RestrictAppTip(BatteryTip.StateType.NEW,
new ArrayList<>(restrictApps));
restrictApps.add(appInfo);
mRestrictAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
new ArrayList<>(restrictApps));
mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, appInfo);
}
@Test
@@ -82,5 +102,49 @@ public class BatteryTipDialogFragmentTest {
mContext.getString(R.string.battery_tip_dialog_message, "1h"));
}
@Test
public void testOnCreateDialog_restrictOneAppTip_fireRestrictOneAppDialog() {
mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictedOneAppTip);
FragmentTestUtil.startFragment(mDialogFragment);
final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
ShadowAlertDialog shadowDialog = shadowOf(dialog);
assertThat(shadowDialog.getTitle()).isEqualTo("Restrict app?");
assertThat(shadowDialog.getMessage()).isEqualTo(
mContext.getString(R.string.battery_tip_restrict_app_dialog_message));
}
@Test
public void testOnCreateDialog_restrictAppsTip_fireRestrictAppsDialog() {
mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictAppsTip);
FragmentTestUtil.startFragment(mDialogFragment);
final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
ShadowAlertDialog shadowDialog = shadowOf(dialog);
assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 2 apps?");
assertThat(shadowDialog.getMessage()).isEqualTo(
mContext.getString(R.string.battery_tip_restrict_app_dialog_message));
assertThat(shadowDialog.getView()).isNotNull();
}
@Test
public void testOnCreateDialog_unRestrictAppTip_fireUnRestrictDialog() {
mDialogFragment = BatteryTipDialogFragment.newInstance(mUnrestrictAppTip);
ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
FragmentTestUtil.startFragment(mDialogFragment);
final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
ShadowAlertDialog shadowDialog = shadowOf(dialog);
assertThat(shadowDialog.getTitle()).isEqualTo("Remove restriction for app?");
assertThat(shadowDialog.getMessage()).isEqualTo(
mContext.getString(R.string.battery_tip_unrestrict_app_dialog_message));
}
}

View File

@@ -51,6 +51,7 @@ import java.util.List;
public class BatteryTipLoaderTest {
private static final int[] TIP_ORDER = {
BatteryTip.TipType.SMART_BATTERY_MANAGER,
BatteryTip.TipType.APP_RESTRICTION,
BatteryTip.TipType.HIGH_DEVICE_USAGE,
BatteryTip.TipType.BATTERY_SAVER,
BatteryTip.TipType.LOW_BATTERY,

View File

@@ -0,0 +1,83 @@
/*
* 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.actions;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import android.app.AppOpsManager;
import android.content.Context;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.AppInfo;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
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 java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class RestrictAppActionTest {
private static final String PACKAGE_NAME_1 = "com.android.app1";
private static final String PACKAGE_NAME_2 = "com.android.app2";
@Mock
private BatteryUtils mBatteryUtils;
private Context mContext;
private RestrictAppAction mRestrictAppAction;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
final List<AppInfo> mAppInfos = new ArrayList<>();
mAppInfos.add(new AppInfo.Builder()
.setPackageName(PACKAGE_NAME_1)
.build());
mAppInfos.add(new AppInfo.Builder()
.setPackageName(PACKAGE_NAME_2)
.build());
mRestrictAppAction = new RestrictAppAction(mContext, new RestrictAppTip(
BatteryTip.StateType.NEW, mAppInfos));
mRestrictAppAction.mBatteryUtils = mBatteryUtils;
}
@Test
public void testHandlePositiveAction() {
mRestrictAppAction.handlePositiveAction();
verify(mBatteryUtils).setForceAppStandby(anyInt(), eq(PACKAGE_NAME_1),
eq(AppOpsManager.MODE_IGNORED));
verify(mBatteryUtils).setForceAppStandby(anyInt(), eq(PACKAGE_NAME_2),
eq(AppOpsManager.MODE_IGNORED));
}
}

View File

@@ -0,0 +1,111 @@
/*
* 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.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Parcel;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.batterytip.AppInfo;
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 java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class RestrictAppTipTest {
private static final String PACKAGE_NAME = "com.android.app";
private static final String DISPLAY_NAME = "app";
private Context mContext;
private RestrictAppTip mNewBatteryTip;
private RestrictAppTip mHandledBatteryTip;
private List<AppInfo> mUsageAppList;
@Mock
private ApplicationInfo mApplicationInfo;
@Mock
private PackageManager mPackageManager;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mPackageManager).when(mContext).getPackageManager();
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME,
PackageManager.MATCH_DISABLED_COMPONENTS | PackageManager.MATCH_ANY_USER);
doReturn(DISPLAY_NAME).when(mApplicationInfo).loadLabel(mPackageManager);
mUsageAppList = new ArrayList<>();
mUsageAppList.add(new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.build());
mNewBatteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, mUsageAppList);
mHandledBatteryTip = new RestrictAppTip(BatteryTip.StateType.HANDLED, mUsageAppList);
}
@Test
public void testParcelable() {
Parcel parcel = Parcel.obtain();
mNewBatteryTip.writeToParcel(parcel, mNewBatteryTip.describeContents());
parcel.setDataPosition(0);
final RestrictAppTip parcelTip = new RestrictAppTip(parcel);
assertThat(parcelTip.getType()).isEqualTo(BatteryTip.TipType.APP_RESTRICTION);
assertThat(parcelTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
final AppInfo app = parcelTip.getRestrictAppList().get(0);
assertThat(app.packageName).isEqualTo(PACKAGE_NAME);
}
@Test
public void testGetTitle_stateNew_showRestrictTitle() {
assertThat(mNewBatteryTip.getTitle(mContext)).isEqualTo("Restrict 1 app");
}
@Test
public void testGetTitle_stateHandled_showHandledTitle() {
assertThat(mHandledBatteryTip.getTitle(mContext)).isEqualTo("1 recently restricted");
}
@Test
public void testGetSummary_stateNew_showRestrictSummary() {
assertThat(mNewBatteryTip.getSummary(mContext)).isEqualTo(
"app has high battery usage");
}
@Test
public void testGetSummary_stateHandled_showHandledSummary() {
assertThat(mHandledBatteryTip.getSummary(mContext)).isEqualTo(
"App changes are in progress");
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.os.Parcel;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.batterytip.AppInfo;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class UnrestrictAppTipTest {
private static final String PACKAGE_NAME = "com.android.app";
private UnrestrictAppTip mBatteryTip;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
AppInfo appInfo = new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.build();
mBatteryTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, appInfo);
}
@Test
public void testParcelable() {
Parcel parcel = Parcel.obtain();
mBatteryTip.writeToParcel(parcel, mBatteryTip.describeContents());
parcel.setDataPosition(0);
final UnrestrictAppTip parcelTip = new UnrestrictAppTip(parcel);
assertThat(parcelTip.getType()).isEqualTo(BatteryTip.TipType.REMOVE_APP_RESTRICTION);
assertThat(parcelTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
assertThat(parcelTip.getPackageName()).isEqualTo(PACKAGE_NAME);
}
}

View File

@@ -27,6 +27,9 @@ import com.android.settings.wrapper.FingerprintManagerWrapper;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.HashMap;
import java.util.Map;
@Implements(Utils.class)
public class ShadowUtils {
@@ -34,6 +37,7 @@ public class ShadowUtils {
private static boolean sIsUserAMonkey;
private static boolean sIsDemoUser;
private static ComponentName sDeviceOwnerComponentName;
private static Map<String, String> sAppNameMap;
@Implementation
public static int enforceSameOwner(Context context, int userId) {
@@ -89,4 +93,19 @@ public class ShadowUtils {
public static int getManagedProfileId(UserManager um, int parentUserId) {
return UserHandle.USER_NULL;
}
@Implementation
public static CharSequence getApplicationLabel(Context context, String packageName) {
if (sAppNameMap != null) {
return sAppNameMap.get(packageName);
}
return null;
}
public static void setApplicationLabel(String packageName, String appLabel) {
if (sAppNameMap == null) {
sAppNameMap = new HashMap<>();
}
sAppNameMap.put(packageName, appLabel);
}
}