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