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.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.eq; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 25 import android.content.Context; 26 import android.provider.Settings; 27 28 import com.android.settings.R; 29 import com.android.settings.testutils.FakeFeatureFactory; 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 import com.android.settings.utils.AnnotationSpan; 32 import com.android.settings.widget.SwitchWidgetController; 33 import com.android.settingslib.bluetooth.LocalBluetoothManager; 34 import com.android.settingslib.widget.FooterPreference; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Answers; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RuntimeEnvironment; 43 44 @RunWith(SettingsRobolectricTestRunner.class) 45 public class BluetoothSwitchPreferenceControllerTest { 46 47 public static final String BLUETOOTH_INFO_STRING = "When Bluetooth is turned on, your device" 48 + " can communicate with other nearby Bluetooth devices."; 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private LocalBluetoothManager mBluetoothManager; 51 @Mock 52 private RestrictionUtils mRestrictionUtils; 53 @Mock 54 private SwitchWidgetController mSwitchController; 55 56 private FooterPreference mFooterPreference; 57 private Context mContext; 58 private BluetoothSwitchPreferenceController mController; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 64 mFooterPreference = new FooterPreference(mContext); 65 FakeFeatureFactory.setupForTest(); 66 67 mController = 68 new BluetoothSwitchPreferenceController(mContext, mBluetoothManager, mRestrictionUtils, 69 mSwitchController, mFooterPreference); 70 } 71 72 @Test updateText_bluetoothOffScanningOn()73 public void updateText_bluetoothOffScanningOn() { 74 Settings.Global.putInt(mContext.getContentResolver(), 75 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 1); 76 mController.updateText(false); 77 AnnotationSpan.LinkInfo info = new AnnotationSpan.LinkInfo( 78 AnnotationSpan.LinkInfo.DEFAULT_ANNOTATION, mController); 79 CharSequence text = AnnotationSpan.linkify( 80 mContext.getText(R.string.bluetooth_scanning_on_info_message), info); 81 82 assertThat(mFooterPreference.getTitle()).isEqualTo(text); 83 } 84 85 @Test updateText_bluetoothOffScanningOff()86 public void updateText_bluetoothOffScanningOff() { 87 Settings.Global.putInt(mContext.getContentResolver(), 88 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0); 89 mController.updateText(false); 90 91 assertThat(mFooterPreference.getTitle()).isEqualTo(BLUETOOTH_INFO_STRING); 92 93 } 94 95 @Test updateText_bluetoothOnScanningOff()96 public void updateText_bluetoothOnScanningOff() { 97 Settings.Global.putInt(mContext.getContentResolver(), 98 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0); 99 mController.updateText(true); 100 101 assertThat(mFooterPreference.getTitle()).isEqualTo(BLUETOOTH_INFO_STRING); 102 } 103 104 @Test updateText_bluetoothOnScanningOn()105 public void updateText_bluetoothOnScanningOn() { 106 Settings.Global.putInt(mContext.getContentResolver(), 107 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 1); 108 mController.updateText(true); 109 110 assertThat(mFooterPreference.getTitle()).isEqualTo(BLUETOOTH_INFO_STRING); 111 } 112 } 113