1 /* 2 * Copyright (C) 2023 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.development; 18 19 import static com.android.settings.development.BluetoothLeAudioDeviceDetailsPreferenceController 20 .LE_AUDIO_TOGGLE_VISIBLE_PROPERTY; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.bluetooth.BluetoothAdapter; 29 import android.bluetooth.BluetoothStatusCodes; 30 import android.content.Context; 31 import android.os.SystemProperties; 32 33 import androidx.preference.PreferenceScreen; 34 import androidx.preference.SwitchPreference; 35 36 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 37 38 import org.junit.After; 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RobolectricTestRunner; 45 import org.robolectric.RuntimeEnvironment; 46 import org.robolectric.annotation.Config; 47 48 @RunWith(RobolectricTestRunner.class) 49 @Config(shadows = {ShadowDeviceConfig.class}) 50 public class BluetoothLeAudioDeviceDetailsPreferenceControllerTest { 51 52 @Mock 53 private PreferenceScreen mPreferenceScreen; 54 @Mock 55 private BluetoothAdapter mBluetoothAdapter; 56 @Mock 57 private SwitchPreference mPreference; 58 59 private Context mContext; 60 private BluetoothLeAudioDeviceDetailsPreferenceController mController; 61 62 @Before setup()63 public void setup() { 64 MockitoAnnotations.initMocks(this); 65 mContext = RuntimeEnvironment.application; 66 mController = spy(new BluetoothLeAudioDeviceDetailsPreferenceController(mContext)); 67 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 68 .thenReturn(mPreference); 69 mController.mBluetoothAdapter = mBluetoothAdapter; 70 mController.displayPreference(mPreferenceScreen); 71 } 72 73 @After tearDown()74 public void tearDown() { 75 ShadowDeviceConfig.reset(); 76 } 77 78 @Test onPreferenceChanged_settingEnabled_shouldTurnOnLeAudioDeviceDetailSetting()79 public void onPreferenceChanged_settingEnabled_shouldTurnOnLeAudioDeviceDetailSetting() { 80 mController.sLeAudioSupportedStateCache = BluetoothStatusCodes.FEATURE_SUPPORTED; 81 mController.onPreferenceChange(mPreference, true /* new value */); 82 final boolean isEnabled = SystemProperties.getBoolean( 83 LE_AUDIO_TOGGLE_VISIBLE_PROPERTY, false); 84 assertThat(isEnabled).isTrue(); 85 } 86 87 @Test onPreferenceChanged_settingDisabled_shouldTurnOffLeAudioDeviceDetailSetting()88 public void onPreferenceChanged_settingDisabled_shouldTurnOffLeAudioDeviceDetailSetting() { 89 mController.sLeAudioSupportedStateCache = BluetoothStatusCodes.FEATURE_SUPPORTED; 90 mController.onPreferenceChange(mPreference, false /* new value */); 91 final boolean isEnabled = SystemProperties.getBoolean( 92 LE_AUDIO_TOGGLE_VISIBLE_PROPERTY, true); 93 assertThat(isEnabled).isFalse(); 94 } 95 96 @Test updateState_settingEnabled_preferenceShouldBeChecked()97 public void updateState_settingEnabled_preferenceShouldBeChecked() { 98 mController.sLeAudioSupportedStateCache = BluetoothStatusCodes.FEATURE_SUPPORTED; 99 SystemProperties.set(LE_AUDIO_TOGGLE_VISIBLE_PROPERTY, "true"); 100 mController.mLeAudioEnabledByDefault = false; 101 mController.updateState(mPreference); 102 verify(mPreference).setChecked(true); 103 } 104 105 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()106 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 107 mController.sLeAudioSupportedStateCache = BluetoothStatusCodes.FEATURE_SUPPORTED; 108 SystemProperties.set(LE_AUDIO_TOGGLE_VISIBLE_PROPERTY, "false"); 109 mController.mLeAudioEnabledByDefault = false; 110 mController.updateState(mPreference); 111 verify(mPreference).setChecked(false); 112 } 113 114 @Test isAvailable_leAudioSupported()115 public void isAvailable_leAudioSupported() { 116 mController.mLeAudioEnabledByDefault = false; 117 mController.sLeAudioSupportedStateCache = BluetoothStatusCodes.ERROR_UNKNOWN; 118 when(mBluetoothAdapter.isLeAudioSupported()) 119 .thenReturn(BluetoothStatusCodes.FEATURE_SUPPORTED); 120 assertThat(mController.isAvailable()).isTrue(); 121 } 122 123 @Test isAvailable_leAudioNotSupported()124 public void isAvailable_leAudioNotSupported() { 125 mController.mLeAudioEnabledByDefault = false; 126 mController.sLeAudioSupportedStateCache = BluetoothStatusCodes.ERROR_UNKNOWN; 127 when(mBluetoothAdapter.isLeAudioSupported()) 128 .thenReturn(BluetoothStatusCodes.FEATURE_NOT_SUPPORTED); 129 assertThat(mController.isAvailable()).isFalse(); 130 } 131 132 @Test isUnAvailable_ifLeAudioConnectionByDefault()133 public void isUnAvailable_ifLeAudioConnectionByDefault() { 134 mController.mLeAudioEnabledByDefault = true; 135 assertThat(mController.isAvailable()).isFalse(); 136 } 137 } 138