Merge "Add debug data in AnomalyLoader"
This commit is contained in:
committed by
Android (Google) Code Review
commit
fbd376b1ba
@@ -17,12 +17,14 @@
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.utils.AsyncLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -33,6 +35,7 @@ import java.util.List;
|
||||
* an empty list if there is no anomaly.
|
||||
*/
|
||||
public class AnomalyLoader extends AsyncLoader<List<Anomaly>> {
|
||||
private static final boolean USE_FAKE_DATA = false;
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
private String mPackageName;
|
||||
private UserManager mUserManager;
|
||||
@@ -56,7 +59,6 @@ public class AnomalyLoader extends AsyncLoader<List<Anomaly>> {
|
||||
*
|
||||
* This constructor will create {@link BatteryStatsHelper} in background thread.
|
||||
*
|
||||
* @param context
|
||||
* @param packageName if set, only finds anomalies for this package. If {@code null},
|
||||
* detects all anomalies of this type.
|
||||
*/
|
||||
@@ -81,6 +83,9 @@ public class AnomalyLoader extends AsyncLoader<List<Anomaly>> {
|
||||
|
||||
@Override
|
||||
public List<Anomaly> loadInBackground() {
|
||||
if (USE_FAKE_DATA) {
|
||||
return generateFakeData();
|
||||
}
|
||||
if (mBatteryStatsHelper == null) {
|
||||
mBatteryStatsHelper = new BatteryStatsHelper(getContext());
|
||||
mBatteryStatsHelper.create((Bundle) null);
|
||||
@@ -99,4 +104,31 @@ public class AnomalyLoader extends AsyncLoader<List<Anomaly>> {
|
||||
return anomalies;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
List<Anomaly> generateFakeData() {
|
||||
final List<Anomaly> anomalies = new ArrayList<>();
|
||||
final Context context = getContext();
|
||||
try {
|
||||
final String packageName = "com.android.settings";
|
||||
final CharSequence displayName = "Settings";
|
||||
final int uid = context.getPackageManager().getPackageUid(packageName, 0);
|
||||
|
||||
anomalies.add(new Anomaly.Builder()
|
||||
.setUid(uid)
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setPackageName(packageName)
|
||||
.setDisplayName(displayName)
|
||||
.build());
|
||||
anomalies.add(new Anomaly.Builder()
|
||||
.setUid(uid)
|
||||
.setType(Anomaly.AnomalyType.WAKEUP_ALARM)
|
||||
.setPackageName(packageName)
|
||||
.setDisplayName(displayName)
|
||||
.build());
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return anomalies;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,11 +20,13 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
@@ -48,6 +50,9 @@ import java.util.List;
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AnomalyLoaderTest {
|
||||
private static final String PACKAGE_NAME = "com.android.settings";
|
||||
private static final CharSequence DISPLAY_NAME = "Settings";
|
||||
private static final int UID = 0;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
@Mock
|
||||
@@ -67,12 +72,13 @@ public class AnomalyLoaderTest {
|
||||
private AnomalyLoader mAnomalyLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws PackageManager.NameNotFoundException {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
FakeFeatureFactory.setupForTest(mContext);
|
||||
doReturn(true).when(mAnomalyDetectionPolicy).isAnomalyDetectorEnabled(anyInt());
|
||||
doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
|
||||
when(mContext.getPackageManager().getPackageUid(anyString(), anyInt())).thenReturn(UID);
|
||||
|
||||
mWakeLockAnomalies = new ArrayList<>();
|
||||
mWakeLockAnomaly = createAnomaly(Anomaly.AnomalyType.WAKE_LOCK);
|
||||
@@ -105,6 +111,16 @@ public class AnomalyLoaderTest {
|
||||
private Anomaly createAnomaly(@Anomaly.AnomalyType int type) {
|
||||
return new Anomaly.Builder()
|
||||
.setType(type)
|
||||
.setUid(UID)
|
||||
.setPackageName(PACKAGE_NAME)
|
||||
.setDisplayName(DISPLAY_NAME)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateFakeData() {
|
||||
List<Anomaly> anomalies = mAnomalyLoader.generateFakeData();
|
||||
|
||||
assertThat(anomalies).containsExactly(mWakeLockAnomaly, mWakeupAlarmAnomaly);
|
||||
}
|
||||
}
|
||||
|
@@ -171,7 +171,7 @@ public class WakeLockAnomalyDetectorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetectAnomalies_containsTargetpackage_detectIt() {
|
||||
public void testDetectAnomalies_containsTargetPackage_detectIt() {
|
||||
doReturn(TARGET_UID).when(mBatteryUtils).getPackageUid(TARGET_PACKAGE_NAME);
|
||||
final Anomaly targetAnomaly = new Anomaly.Builder()
|
||||
.setUid(TARGET_UID)
|
||||
|
Reference in New Issue
Block a user