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 android.Manifest; 22 import android.content.Context; 23 import android.content.pm.PackageManager; 24 25 import com.android.settings.TestConfig; 26 import com.android.settings.fuelgauge.anomaly.Anomaly; 27 import com.android.settings.testutils.FakeFeatureFactory; 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 29 import com.android.settings.testutils.shadow.ShadowPermissionChecker; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Answers; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.annotation.Config; 38 39 @RunWith(SettingsRobolectricTestRunner.class) 40 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows = 41 ShadowPermissionChecker.class) 42 public class LocationCheckActionTest { 43 private static final String PACKAGE_NAME = "com.android.app"; 44 private static final int UID = 12345; 45 46 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 47 private Context mContext; 48 private Anomaly mAnomaly; 49 private LocationCheckAction mLocationCheckAction; 50 51 @Before 52 public void setUp() throws Exception { 53 MockitoAnnotations.initMocks(this); 54 FakeFeatureFactory.setupForTest(mContext); 55 mLocationCheckAction = new LocationCheckAction(mContext, null); 56 mAnomaly = new Anomaly.Builder() 57 .setType(Anomaly.AnomalyType.BLUETOOTH_SCAN) 58 .setPackageName(PACKAGE_NAME) 59 .setUid(UID) 60 .build(); 61 ShadowPermissionChecker.clear(); 62 } 63 64 @Test 65 public void testIsActionActive_coarseLocationGranted_returnTrue() { 66 ShadowPermissionChecker.addPermission(Manifest.permission.ACCESS_COARSE_LOCATION, -1, UID, 67 PACKAGE_NAME, PackageManager.PERMISSION_GRANTED); 68 69 assertThat(mLocationCheckAction.isActionActive(mAnomaly)).isTrue(); 70 } 71 72 @Test 73 public void testIsActionActive_fineLocationGranted_returnTrue() { 74 ShadowPermissionChecker.addPermission(Manifest.permission.ACCESS_FINE_LOCATION, -1, UID, 75 PACKAGE_NAME, PackageManager.PERMISSION_GRANTED); 76 77 assertThat(mLocationCheckAction.isActionActive(mAnomaly)).isTrue(); 78 } 79 80 @Test 81 public void testIsActionActive_noLocationGranted_returnFalse() { 82 assertThat(mLocationCheckAction.isActionActive(mAnomaly)).isFalse(); 83 } 84 85 } 86