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.never; 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 import android.text.TextUtils; 28 29 import com.android.settings.R; 30 import com.android.settings.testutils.FakeFeatureFactory; 31 import com.android.settings.utils.AnnotationSpan; 32 import com.android.settings.widget.SwitchWidgetController; 33 import com.android.settingslib.widget.FooterPreference; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 43 @RunWith(RobolectricTestRunner.class) 44 public class BluetoothSwitchPreferenceControllerTest { 45 46 private static final String BLUETOOTH_INFO_STRING = "When Bluetooth is turned on, your device" 47 + " can communicate with other nearby Bluetooth devices"; 48 @Mock 49 private RestrictionUtils mRestrictionUtils; 50 @Mock 51 private SwitchWidgetController mSwitchController; 52 @Mock 53 private AlwaysDiscoverable mAlwaysDiscoverable; 54 55 private FooterPreference mFooterPreference; 56 private Context mContext; 57 private BluetoothSwitchPreferenceController mController; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 63 mFooterPreference = new FooterPreference(mContext); 64 FakeFeatureFactory.setupForTest(); 65 66 mController = 67 new BluetoothSwitchPreferenceController(mContext, mRestrictionUtils, 68 mSwitchController, mFooterPreference); 69 mController.mAlwaysDiscoverable = mAlwaysDiscoverable; 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(TextUtils.equals(mFooterPreference.getTitle(), text)).isTrue(); 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 @Test updateText_bluetoothOnScanningOff()95 public void updateText_bluetoothOnScanningOff() { 96 Settings.Global.putInt(mContext.getContentResolver(), 97 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 0); 98 mController.updateText(true); 99 100 assertThat(mFooterPreference.getTitle()).isEqualTo(BLUETOOTH_INFO_STRING); 101 } 102 103 @Test updateText_bluetoothOnScanningOn()104 public void updateText_bluetoothOnScanningOn() { 105 Settings.Global.putInt(mContext.getContentResolver(), 106 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE, 1); 107 mController.updateText(true); 108 109 assertThat(mFooterPreference.getTitle()).isEqualTo(BLUETOOTH_INFO_STRING); 110 } 111 112 @Test onStart_setAlwaysDiscoverableAsTrue_shouldStartAlwaysDiscoverable()113 public void onStart_setAlwaysDiscoverableAsTrue_shouldStartAlwaysDiscoverable() { 114 mController.setAlwaysDiscoverable(true); 115 mController.onStart(); 116 117 verify(mAlwaysDiscoverable).start(); 118 } 119 120 @Test onStart_setAlwaysDiscoverableAsFalse_shouldStartAlwaysDiscoverable()121 public void onStart_setAlwaysDiscoverableAsFalse_shouldStartAlwaysDiscoverable() { 122 mController.setAlwaysDiscoverable(false); 123 mController.onStart(); 124 125 verify(mAlwaysDiscoverable, never()).start(); 126 } 127 128 @Test onStop_setAlwaysDiscoverableAsTrue_shouldStopAlwaysDiscoverable()129 public void onStop_setAlwaysDiscoverableAsTrue_shouldStopAlwaysDiscoverable() { 130 mController.setAlwaysDiscoverable(true); 131 mController.onStop(); 132 133 verify(mAlwaysDiscoverable).stop(); 134 } 135 136 @Test onStop__setAlwaysDiscoverableAsFalse_shouldStopAlwaysDiscoverable()137 public void onStop__setAlwaysDiscoverableAsFalse_shouldStopAlwaysDiscoverable() { 138 mController.setAlwaysDiscoverable(false); 139 mController.onStop(); 140 141 verify(mAlwaysDiscoverable, never()).stop(); 142 } 143 } 144