Add AnomalyDetectionPolicy
AnomalyDetectionPolicy contains all the knobs for anomaly detection. Since Robolectric doesn't support KeyValueListParser, this cl creates KeyValueListParserWrapper so we could mock it and test AnomalyDetectionPolicy Bug: 36924669 Test: RunSettingsRoboTests Change-Id: I71138f85d37a21b5c4c9c806a41e00a4aa924f5f
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.KeyValueListParser;
|
||||
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AnomalyDetectionPolicyTest {
|
||||
private static final String ANOMALY_DETECTION_CONSTANTS_VALUE = "anomaly_detection_enabled=true"
|
||||
+ ",wakelock_enabled=false"
|
||||
+ ",wakelock_threshold=3000";
|
||||
private Context mContext;
|
||||
private KeyValueListParserWrapper mKeyValueListParserWrapper;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mKeyValueListParserWrapper = spy(
|
||||
new KeyValueListParserWrapperImpl(new KeyValueListParser(',')));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit_containsDataFromSettings() {
|
||||
Settings.Global.putString(mContext.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS, ANOMALY_DETECTION_CONSTANTS_VALUE);
|
||||
// Mock it to avoid noSuchMethodError
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_ANOMALY_DETECTION_ENABLED, true);
|
||||
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, true);
|
||||
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext,
|
||||
mKeyValueListParserWrapper);
|
||||
|
||||
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(3000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit_containsDefaultData() {
|
||||
Settings.Global.putString(mContext.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS, "");
|
||||
// Mock it to avoid noSuchMethodError
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(true));
|
||||
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(false));
|
||||
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext,
|
||||
mKeyValueListParserWrapper);
|
||||
|
||||
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(DateUtils.HOUR_IN_MILLIS);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user