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.action; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.verify; 23 24 import android.app.AppOpsManager; 25 import android.content.Context; 26 import android.os.Build; 27 28 import com.android.settings.fuelgauge.BatteryUtils; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 import com.android.settings.TestConfig; 31 import com.android.settings.fuelgauge.anomaly.Anomaly; 32 import com.android.settings.testutils.FakeFeatureFactory; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.annotation.Config; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 44 public class BackgroundCheckActionTest { 45 private static final String PACKAGE_NAME = "com.android.app"; 46 private static final int UID = 111; 47 private static final int SDK_VERSION = Build.VERSION_CODES.L; 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private Context mContext; 51 @Mock 52 private AppOpsManager mAppOpsManager; 53 @Mock 54 private BatteryUtils mBatteryUtils; 55 private Anomaly mAnomaly; 56 private BackgroundCheckAction mBackgroundCheckAction; 57 58 @Before 59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 62 FakeFeatureFactory.setupForTest(mContext); 63 doReturn(mAppOpsManager).when(mContext).getSystemService(Context.APP_OPS_SERVICE); 64 65 mAnomaly = new Anomaly.Builder() 66 .setUid(UID) 67 .setPackageName(PACKAGE_NAME) 68 .setTargetSdkVersion(SDK_VERSION) 69 .build(); 70 mBackgroundCheckAction = new BackgroundCheckAction(mContext); 71 mBackgroundCheckAction.mBatteryUtils = mBatteryUtils; 72 } 73 74 @Test 75 public void testHandlePositiveAction_forceStopPackage() { 76 mBackgroundCheckAction.handlePositiveAction(mAnomaly, 0 /* metricskey */); 77 78 verify(mAppOpsManager).setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, UID, PACKAGE_NAME, 79 AppOpsManager.MODE_IGNORED); 80 } 81 82 @Test 83 public void testIsActionActive_modeAllowed_returnTrue() { 84 doReturn(false).when(mBatteryUtils).isBackgroundRestrictionEnabled(SDK_VERSION, UID, 85 PACKAGE_NAME); 86 87 assertThat(mBackgroundCheckAction.isActionActive(mAnomaly)).isTrue(); 88 } 89 90 @Test 91 public void testIsActionActive_modeIgnored_returnFalse() { 92 doReturn(true).when(mBatteryUtils).isBackgroundRestrictionEnabled(SDK_VERSION, UID, 93 PACKAGE_NAME); 94 95 assertThat(mBackgroundCheckAction.isActionActive(mAnomaly)).isFalse(); 96 } 97 98 @Test 99 public void testConstructor_batteryUtilsNotNull() { 100 mBackgroundCheckAction = new BackgroundCheckAction(mContext); 101 102 assertThat(mBackgroundCheckAction.mBatteryUtils).isNotNull(); 103 } 104 } 105