Merge "Fix bugs in auto restriction." into pi-dev am: f6e2c19fcc

am: a9e4d4a42a

Change-Id: If82969696eb492ea3875a0e0fb92f038024f00f9
This commit is contained in:
Lei Yu
2018-04-23 10:13:31 -07:00
committed by android-build-merger
14 changed files with 234 additions and 51 deletions

View File

@@ -67,8 +67,6 @@ public class RestrictAppPreferenceControllerTest {
@Mock
private AppOpsManager.PackageOps mOtherUserPackageOps;
@Mock
private SettingsActivity mSettingsActivity;
@Mock
private InstrumentedPreferenceFragment mFragment;
@Mock
private UserManager mUserManager;
@@ -102,9 +100,9 @@ public class RestrictAppPreferenceControllerTest {
mContext = spy(RuntimeEnvironment.application);
doReturn(mAppOpsManager).when(mContext).getSystemService(Context.APP_OPS_SERVICE);
doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
doReturn(mContext).when(mSettingsActivity).getApplicationContext();
doReturn(mContext).when(mFragment).getContext();
mRestrictAppPreferenceController =
new RestrictAppPreferenceController(mSettingsActivity, mFragment);
new RestrictAppPreferenceController(mFragment);
mPackageOpsList = new ArrayList<>();
mPreference = new Preference(mContext);
mPreference.setKey(mRestrictAppPreferenceController.getPreferenceKey());
@@ -171,7 +169,7 @@ public class RestrictAppPreferenceControllerTest {
mRestrictAppPreferenceController.handlePreferenceTreeClick(mPreference);
verify(mSettingsActivity).startActivity(intent.capture());
verify(mContext).startActivity(intent.capture());
assertThat(intent.getValue().getStringExtra(EXTRA_SHOW_FRAGMENT))
.isEqualTo(RestrictedAppDetails.class.getName());
assertThat(intent.getValue().getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1))

View File

@@ -69,8 +69,6 @@ public class RestrictedAppDetailsTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceManager mPreferenceManager;
@Mock
private SettingsActivity mSettingsActivity;
@Mock
private InstrumentedPreferenceFragment mFragment;
private RestrictedAppDetails mRestrictedAppDetails;
private Context mContext;
@@ -90,6 +88,7 @@ public class RestrictedAppDetailsTest {
doReturn(mPreferenceManager).when(mRestrictedAppDetails).getPreferenceManager();
doReturn(mContext).when(mPreferenceManager).getContext();
doReturn(mContext).when(mFragment).getContext();
mRestrictedAppDetails.mPackageManager = mPackageManager;
mRestrictedAppDetails.mIconDrawableFactory = mIconDrawableFactory;
mRestrictedAppDetails.mAppInfos = new ArrayList<>();
@@ -124,9 +123,9 @@ public class RestrictedAppDetailsTest {
// Get the intent in which it has the app info bundle
mIntent = captor.getValue();
return true;
}).when(mSettingsActivity).startActivity(captor.capture());
}).when(mContext).startActivity(captor.capture());
RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mFragment,
RestrictedAppDetails.startRestrictedAppDetails(mFragment,
mRestrictedAppDetails.mAppInfos);
final Bundle bundle = mIntent.getBundleExtra(

View File

@@ -0,0 +1,99 @@
/*
* 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.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.core.InstrumentedPreferenceFragment;
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.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
import com.android.settings.testutils.DatabaseTestUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
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.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
public class OpenRestrictAppFragmentActionTest {
private static final String PACKAGE_NAME_1 = "com.android.app1";
private static final String PACKAGE_NAME_2 = "com.android.app2";
private static final int ANOMALY_WAKEUP = 0;
private static final int ANOMALY_BT = 1;
private static final int METRICS_KEY = 1;
@Mock
private InstrumentedPreferenceFragment mFragment;
@Mock
private BatteryDatabaseManager mBatteryDatabaseManager;
private OpenRestrictAppFragmentAction mAction;
private FakeFeatureFactory mFeatureFactory;
private Context mContext;
private List<AppInfo> mAppInfos;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mAppInfos = new ArrayList<>();
mAppInfos.add(new AppInfo.Builder()
.setPackageName(PACKAGE_NAME_1)
.addAnomalyType(ANOMALY_BT)
.build());
mAppInfos.add(new AppInfo.Builder()
.setPackageName(PACKAGE_NAME_2)
.addAnomalyType(ANOMALY_WAKEUP)
.build());
mFeatureFactory = FakeFeatureFactory.setupForTest();
when(mFragment.getContext()).thenReturn(mContext);
mAction = new OpenRestrictAppFragmentAction(mFragment,
new RestrictAppTip(BatteryTip.StateType.HANDLED, mAppInfos));
mAction.mBatteryDatabaseManager = mBatteryDatabaseManager;
}
@After
public void cleanUp() {
DatabaseTestUtils.clearDb(mContext);
}
@Test
public void testHandlePositiveAction() {
mAction.handlePositiveAction(METRICS_KEY);
verify(mFeatureFactory.metricsFeatureProvider).action(mContext,
MetricsProto.MetricsEvent.ACTION_TIP_OPEN_APP_RESTRICTION_PAGE, METRICS_KEY);
verify(mBatteryDatabaseManager).updateAnomalies(mAppInfos,
AnomalyDatabaseHelper.State.HANDLED);
}
}

View File

@@ -55,9 +55,11 @@ import java.util.List;
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;
@@ -88,9 +90,12 @@ public class RestrictAppDetectorTest {
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);
doReturn(mPackageManager).when(mContext).getPackageManager();
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(eq(PACKAGE_NAME),
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(any(),
anyInt());
doReturn(PACKAGE_NAME).when(mApplicationInfo).loadLabel(any());
doThrow(new PackageManager.NameNotFoundException()).when(
@@ -116,6 +121,10 @@ public class RestrictAppDetectorTest {
@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)
@@ -126,7 +135,7 @@ public class RestrictAppDetectorTest {
}
@Test
public void testDetect_hasUninstalledAnomaly_removeIt() {
public void testDetect_typeNewHasUninstalledAnomaly_removeIt() {
mAppInfoList.add(new AppInfo.Builder()
.setPackageName(UNINSTALLED_PACKAGE_NAME)
.build());
@@ -139,7 +148,7 @@ public class RestrictAppDetectorTest {
}
@Test
public void testDetect_hasRestrictedAnomaly_removeIt() throws
public void testDetect_typeNewHasRestrictedAnomaly_removeIt() throws
PackageManager.NameNotFoundException {
mAppInfoList.add(new AppInfo.Builder()
.setUid(RESTRICTED_UID)
@@ -155,6 +164,25 @@ public class RestrictAppDetectorTest {
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)