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