Update detector and action for restrict app

1. In detector, read data from database and display it.
2. Update the RestrictAppAction to mark anomaly as handled
if restriction is toggled.
3. Update the RestrictAppTip to handle state change.

Bug: 72385333
Test: RunSettingsRoboTests

Change-Id: I0bbe6f6fd049bf2e7a2bee1dee08d5199f922e31
This commit is contained in:
jackqdyulei
2018-02-08 10:07:11 -08:00
parent fe13d2813a
commit af2ece7387
7 changed files with 194 additions and 8 deletions

View File

@@ -27,8 +27,10 @@ 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.DatabaseTestUtils;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -69,6 +71,11 @@ public class RestrictAppActionTest {
mRestrictAppAction.mBatteryUtils = mBatteryUtils;
}
@After
public void cleanUp() {
DatabaseTestUtils.clearDb(mContext);
}
@Test
public void testHandlePositiveAction() {
mRestrictAppAction.handlePositiveAction();
@@ -79,5 +86,4 @@ public class RestrictAppActionTest {
eq(AppOpsManager.MODE_IGNORED));
}
}

View File

@@ -0,0 +1,113 @@
/*
* 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.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import com.android.settings.TestConfig;
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.BatteryTip;
import com.android.settings.testutils.DatabaseTestUtils;
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 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 RestrictAppDetectorTest {
private static final String PACKAGE_NAME = "com.android.app";
private Context mContext;
private BatteryTipPolicy mPolicy;
private RestrictAppDetector mRestrictAppDetector;
private List<AppInfo> mAppInfoList;
@Mock
private BatteryDatabaseManager mBatteryDatabaseManager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mAppInfoList = new ArrayList<>();
mAppInfoList.add(new AppInfo.Builder()
.setPackageName(PACKAGE_NAME)
.build());
mContext = RuntimeEnvironment.application;
mPolicy = spy(new BatteryTipPolicy(mContext));
mRestrictAppDetector = new RestrictAppDetector(mContext, mPolicy);
mRestrictAppDetector.mBatteryDatabaseManager = mBatteryDatabaseManager;
}
@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() {
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_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();
}
}

View File

@@ -49,6 +49,7 @@ public class RestrictAppTipTest {
private Context mContext;
private RestrictAppTip mNewBatteryTip;
private RestrictAppTip mHandledBatteryTip;
private RestrictAppTip mInvisibleBatteryTip;
private List<AppInfo> mUsageAppList;
@Mock
private ApplicationInfo mApplicationInfo;
@@ -71,6 +72,7 @@ public class RestrictAppTipTest {
.build());
mNewBatteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, mUsageAppList);
mHandledBatteryTip = new RestrictAppTip(BatteryTip.StateType.HANDLED, mUsageAppList);
mInvisibleBatteryTip = new RestrictAppTip(BatteryTip.StateType.INVISIBLE, mUsageAppList);
}
@Test
@@ -108,4 +110,20 @@ public class RestrictAppTipTest {
assertThat(mHandledBatteryTip.getSummary(mContext)).isEqualTo(
"App changes are in progress");
}
@Test
public void testUpdate_anomalyBecomeInvisible_stateHandled() {
mNewBatteryTip.updateState(mInvisibleBatteryTip);
assertThat(mNewBatteryTip.getState()).isEqualTo(BatteryTip.StateType.HANDLED);
}
@Test
public void testUpdate_newAnomalyComes_stateNew() {
mInvisibleBatteryTip.updateState(mNewBatteryTip);
assertThat(mInvisibleBatteryTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
mHandledBatteryTip.updateState(mNewBatteryTip);
assertThat(mHandledBatteryTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
}
}