1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fuelgauge.anomaly; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.os.Build; 22 23 import com.android.settings.fuelgauge.anomaly.action.StopAndBackgroundCheckAction; 24 import com.android.settings.testutils.SettingsRobolectricTestRunner; 25 import com.android.settings.TestConfig; 26 import com.android.settings.fuelgauge.anomaly.action.ForceStopAction; 27 import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector; 28 import com.android.settings.testutils.shadow.ShadowKeyValueListParserWrapperImpl; 29 import com.android.settings.fuelgauge.anomaly.checker.WakeupAlarmAnomalyDetector; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 37 @RunWith(SettingsRobolectricTestRunner.class) 38 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows = { 39 ShadowKeyValueListParserWrapperImpl.class}) 40 public class AnomalyUtilsTest { 41 private AnomalyUtils mAnomalyUtils; 42 43 @Before 44 public void setUp() { 45 mAnomalyUtils = new AnomalyUtils(RuntimeEnvironment.application); 46 } 47 48 @Test 49 public void testGetAnomalyAction_typeWakeLock_returnForceStop() { 50 Anomaly anomaly = new Anomaly.Builder() 51 .setType(Anomaly.AnomalyType.WAKE_LOCK) 52 .build(); 53 assertThat(mAnomalyUtils.getAnomalyAction(anomaly)).isInstanceOf( 54 ForceStopAction.class); 55 } 56 57 @Test 58 public void testGetAnomalyDetector_typeWakeLock_returnWakeLockDetector() { 59 assertThat(mAnomalyUtils.getAnomalyDetector(Anomaly.AnomalyType.WAKE_LOCK)).isInstanceOf( 60 WakeLockAnomalyDetector.class); 61 } 62 63 @Test 64 public void testGetAnomalyAction_typeWakeUpAlarmTargetO_returnForceStop() { 65 Anomaly anomaly = new Anomaly.Builder() 66 .setType(Anomaly.AnomalyType.WAKEUP_ALARM) 67 .setTargetSdkVersion(Build.VERSION_CODES.O) 68 .build(); 69 assertThat(mAnomalyUtils.getAnomalyAction(anomaly)).isInstanceOf( 70 ForceStopAction.class); 71 } 72 73 @Test 74 public void testGetAnomalyAction_typeWakeUpAlarmTargetPriorOAndBgOff_returnStopAndBackground() { 75 Anomaly anomaly = new Anomaly.Builder() 76 .setType(Anomaly.AnomalyType.WAKEUP_ALARM) 77 .setTargetSdkVersion(Build.VERSION_CODES.L) 78 .setBackgroundRestrictionEnabled(false) 79 .build(); 80 assertThat(mAnomalyUtils.getAnomalyAction(anomaly)).isInstanceOf( 81 StopAndBackgroundCheckAction.class); 82 } 83 84 @Test 85 public void testGetAnomalyAction_typeWakeUpAlarmTargetPriorOAndBgOn_returnForceStop() { 86 Anomaly anomaly = new Anomaly.Builder() 87 .setType(Anomaly.AnomalyType.WAKEUP_ALARM) 88 .setTargetSdkVersion(Build.VERSION_CODES.L) 89 .setBackgroundRestrictionEnabled(true) 90 .build(); 91 assertThat(mAnomalyUtils.getAnomalyAction(anomaly)).isInstanceOf( 92 ForceStopAction.class); 93 } 94 95 @Test 96 public void testGetAnomalyDetector_typeWakeUpAlarm_returnWakeUpAlarmDetector() { 97 assertThat(mAnomalyUtils.getAnomalyDetector(Anomaly.AnomalyType.WAKEUP_ALARM)).isInstanceOf( 98 WakeupAlarmAnomalyDetector.class); 99 } 100 } 101