1 /* 2 * Copyright (C) 2022 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 android.bluetooth.BluetoothStatusCodes.FEATURE_SUPPORTED; 20 21 import static com.android.settings.development.BluetoothA2dpHwOffloadPreferenceController 22 .A2DP_OFFLOAD_DISABLED_PROPERTY; 23 import static com.android.settings.development.BluetoothA2dpHwOffloadPreferenceController 24 .A2DP_OFFLOAD_SUPPORTED_PROPERTY; 25 import static com.android.settings.development.BluetoothLeAudioHwOffloadPreferenceController 26 .LE_AUDIO_OFFLOAD_DISABLED_PROPERTY; 27 import static com.android.settings.development.BluetoothLeAudioHwOffloadPreferenceController 28 .LE_AUDIO_OFFLOAD_SUPPORTED_PROPERTY; 29 30 import static com.google.common.truth.Truth.assertThat; 31 32 import static org.mockito.Mockito.spy; 33 import static org.mockito.Mockito.when; 34 35 import android.bluetooth.BluetoothAdapter; 36 import android.content.Context; 37 import android.os.SystemProperties; 38 39 import androidx.preference.PreferenceScreen; 40 import androidx.preference.SwitchPreference; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class BluetoothLeAudioHwOffloadPreferenceControllerTest { 52 53 @Mock 54 private PreferenceScreen mPreferenceScreen; 55 @Mock 56 private DevelopmentSettingsDashboardFragment mFragment; 57 @Mock 58 private BluetoothAdapter mBluetoothAdapter; 59 60 private Context mContext; 61 private SwitchPreference mPreference; 62 private BluetoothLeAudioHwOffloadPreferenceController mController; 63 64 @Before setup()65 public void setup() { 66 MockitoAnnotations.initMocks(this); 67 mContext = RuntimeEnvironment.application; 68 mPreference = new SwitchPreference(mContext); 69 mController = spy(new BluetoothLeAudioHwOffloadPreferenceController(mContext, mFragment)); 70 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 71 .thenReturn(mPreference); 72 mController.displayPreference(mPreferenceScreen); 73 mController.mBluetoothAdapter = mBluetoothAdapter; 74 when(mBluetoothAdapter.isLeAudioSupported()) 75 .thenReturn(FEATURE_SUPPORTED); 76 } 77 78 @Test onLeAudioHwDialogConfirmedAsLeAudioOffloadDisabled_shouldChangeProperty()79 public void onLeAudioHwDialogConfirmedAsLeAudioOffloadDisabled_shouldChangeProperty() { 80 SystemProperties.set(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(false)); 81 mController.mChanged = true; 82 83 mController.onRebootDialogConfirmed(); 84 final boolean mode = SystemProperties.getBoolean(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, false); 85 assertThat(mode).isTrue(); 86 } 87 88 @Test onLeAudioHwDialogConfirmedAsLeAudioOffloadEnabled_shouldChangeProperty()89 public void onLeAudioHwDialogConfirmedAsLeAudioOffloadEnabled_shouldChangeProperty() { 90 SystemProperties.set(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(true)); 91 mController.mChanged = true; 92 93 mController.onRebootDialogConfirmed(); 94 final boolean mode2 = SystemProperties.getBoolean( 95 LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, true); 96 assertThat(mode2).isFalse(); 97 } 98 99 @Test onLeAudioHwDialogCanceled_shouldNotChangeProperty()100 public void onLeAudioHwDialogCanceled_shouldNotChangeProperty() { 101 SystemProperties.set(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(false)); 102 mController.mChanged = true; 103 104 mController.onRebootDialogCanceled(); 105 final boolean mode = SystemProperties.getBoolean(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, false); 106 assertThat(mode).isFalse(); 107 } 108 109 @Test asA2dpOffloadDisabled_shouldNotSwitchLeAudioOffloadStatus()110 public void asA2dpOffloadDisabled_shouldNotSwitchLeAudioOffloadStatus() { 111 SystemProperties.set(LE_AUDIO_OFFLOAD_SUPPORTED_PROPERTY, Boolean.toString(true)); 112 SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(true)); 113 114 SystemProperties.set(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(false)); 115 mController.updateState(null); 116 boolean leAueioDisabled = 117 SystemProperties.getBoolean(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, false); 118 assertThat(leAueioDisabled).isFalse(); 119 120 SystemProperties.set(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(true)); 121 mController.updateState(null); 122 leAueioDisabled = SystemProperties.getBoolean(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, false); 123 assertThat(leAueioDisabled).isTrue(); 124 } 125 126 @Test asDisableDeveloperOption_ResetLEOffloadBasedOnA2dpLeAudioOffloadSupported()127 public void asDisableDeveloperOption_ResetLEOffloadBasedOnA2dpLeAudioOffloadSupported() { 128 SystemProperties.set(LE_AUDIO_OFFLOAD_SUPPORTED_PROPERTY, Boolean.toString(true)); 129 SystemProperties.set(A2DP_OFFLOAD_SUPPORTED_PROPERTY, Boolean.toString(true)); 130 131 SystemProperties.set( 132 LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(true)); 133 mController.onDeveloperOptionsSwitchDisabled(); 134 boolean leAueioDisabled = 135 SystemProperties.getBoolean(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, false); 136 assertThat(leAueioDisabled).isFalse(); 137 } 138 } 139