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