Add restrict and unrestrict dialog

Add a fake unrestrict tip so we could reuse the BatteryTipDialogFragment
to build the unrestrict dialog.

After this cl, restrict dialog has two types:
1. dialog to only restrict one app
2. dialog to restrict more than one app

Will add dialog to restrict more than 5 apps when strings are finalized.

Bug: 72385333
Bug: 72227981
Test: RunSettingsRoboTests
Change-Id: Ib0328f0386efad525b331fd713dd15d060a1a649
This commit is contained in:
jackqdyulei
2018-01-25 18:00:01 -08:00
parent ab0cde6bad
commit 99a2de41ef
16 changed files with 517 additions and 106 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

@@ -30,9 +30,10 @@ 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;
@@ -49,15 +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 mRestrictedAppTip;
private RestrictAppTip mRestrictedOneAppTip;
private RestrictAppTip mRestrictAppsTip;
private UnrestrictAppTip mUnrestrictAppTip;
@Before
public void setUp() {
@@ -67,10 +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);
mRestrictedAppTip = new RestrictAppTip(BatteryTip.StateType.NEW, 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
@@ -87,18 +103,48 @@ public class BatteryTipDialogFragmentTest {
}
@Test
public void testOnCreateDialog_restrictAppTip_fireRestrictAppDialog() {
mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictedAppTip);
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 1 app");
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

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