Merge "Add framework for anomaly detection flags"

This commit is contained in:
Lei Yu
2017-05-10 17:39:55 +00:00
committed by Android (Google) Code Review
7 changed files with 143 additions and 5 deletions

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2017 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.anomaly;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
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 AnomalyLoaderTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private BatteryStatsHelper mBatteryStatsHelper;
private WakeLockAnomalyDetector mWakeLockAnomalyDetector;
private Anomaly mWakeLockAnomaly;
private List<Anomaly> mWakeLockAnomalies;
private AnomalyLoader mAnomalyLoader;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mWakeLockAnomalyDetector = spy(new WakeLockAnomalyDetector(mContext));
mWakeLockAnomalies = new ArrayList<>();
mWakeLockAnomaly = new Anomaly.Builder()
.setType(Anomaly.AnomalyType.WAKE_LOCK)
.build();
mWakeLockAnomalies.add(mWakeLockAnomaly);
mAnomalyLoader = new AnomalyLoader(mContext, mBatteryStatsHelper);
mAnomalyLoader.mAnomalyUtils = spy(new AnomalyUtils(mContext));
}
@Test
public void testLoadInBackground_containsValidAnomalies() {
doReturn(mWakeLockAnomalyDetector).when(mAnomalyLoader.mAnomalyUtils).getAnomalyDetector(
Anomaly.AnomalyType.WAKE_LOCK);
doReturn(mWakeLockAnomalies).when(mWakeLockAnomalyDetector).detectAnomalies(any());
when(mFeatureFactory.powerUsageFeatureProvider.isAnomalyDetectorEnabled(
Anomaly.AnomalyType.WAKE_LOCK)).thenReturn(true);
List<Anomaly> anomalies = mAnomalyLoader.loadInBackground();
assertThat(anomalies).containsExactly(mWakeLockAnomaly);
}
}

View File

@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
import org.junit.Before;
import org.junit.Test;
@@ -43,4 +44,10 @@ public class AnomalyUtilsTest {
assertThat(mAnomalyUtils.getAnomalyAction(Anomaly.AnomalyType.WAKE_LOCK)).isInstanceOf(
ForceStopAction.class);
}
@Test
public void testGetAnomalyDetector_typeWakeLock_returnWakeLockDetector() {
assertThat(mAnomalyUtils.getAnomalyDetector(Anomaly.AnomalyType.WAKE_LOCK)).isInstanceOf(
WakeLockAnomalyDetector.class);
}
}