Fix bugs in auto restriction.
1. Dismiss this tip once user clicks it and goes to detail page. 2. In auto restriction, since apps are automatically restricted, we need to remove apps in list that unrestricted by user. 3. Refactor the code to remove the unnecessary parameter(i.e. SettingsActivity) Bug: 78187414 Test: RunSettingsRoboTests Change-Id: I1c950f7c55df35795641c2ea8579ce9e011dba28
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -67,8 +67,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;
|
||||
@@ -88,6 +86,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<>();
|
||||
@@ -119,9 +118,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(
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user