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 package com.android.settings.location; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.content.ContentResolver; 24 import android.provider.Settings; 25 import android.provider.Settings.Global; 26 27 import androidx.preference.SwitchPreference; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RobolectricTestRunner; 35 import org.robolectric.RuntimeEnvironment; 36 37 @RunWith(RobolectricTestRunner.class) 38 public class BluetoothScanningPreferenceControllerTest { 39 40 @Mock 41 private SwitchPreference mPreference; 42 43 private ContentResolver mContentResolver; 44 private BluetoothScanningPreferenceController mController; 45 46 @Before setUp()47 public void setUp() { 48 MockitoAnnotations.initMocks(this); 49 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 50 mController = new BluetoothScanningPreferenceController(RuntimeEnvironment.application); 51 when(mPreference.getKey()).thenReturn(mController.getPreferenceKey()); 52 } 53 54 @Test updateState_bluetoothScanningEnabled_shouldCheckedPreference()55 public void updateState_bluetoothScanningEnabled_shouldCheckedPreference() { 56 Settings.Global.putInt(mContentResolver, Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 1); 57 58 mController.updateState(mPreference); 59 60 verify(mPreference).setChecked(true); 61 } 62 63 @Test updateState_bluetoothScanningDisabled_shouldUncheckedPreference()64 public void updateState_bluetoothScanningDisabled_shouldUncheckedPreference() { 65 Settings.Global.putInt(mContentResolver, Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0); 66 67 mController.updateState(mPreference); 68 69 verify(mPreference).setChecked(false); 70 } 71 72 @Test handlePreferenceTreeClick_checked_shouldEnableBluetoothScanning()73 public void handlePreferenceTreeClick_checked_shouldEnableBluetoothScanning() { 74 when(mPreference.isChecked()).thenReturn(true); 75 76 mController.handlePreferenceTreeClick(mPreference); 77 78 final int btScanning = Global.getInt(mContentResolver, Global.BLE_SCAN_ALWAYS_AVAILABLE, 0); 79 assertThat(btScanning).isEqualTo(1); 80 } 81 82 @Test handlePreferenceTreeClick_unchecked_shouldDisableBluetoothScanning()83 public void handlePreferenceTreeClick_unchecked_shouldDisableBluetoothScanning() { 84 when(mPreference.isChecked()).thenReturn(false); 85 86 mController.handlePreferenceTreeClick(mPreference); 87 88 final int btScanning = Global.getInt(mContentResolver, Global.BLE_SCAN_ALWAYS_AVAILABLE, 1); 89 assertThat(btScanning).isEqualTo(0); 90 } 91 } 92