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 com.android.settings.development.BluetoothLeAudioModePreferenceController
20         .LE_AUDIO_DYNAMIC_SWITCHER_MODE_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.when;
26 
27 import android.bluetooth.BluetoothAdapter;
28 import android.content.Context;
29 import android.os.SystemProperties;
30 
31 import androidx.preference.ListPreference;
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.settings.R;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class BluetoothLeAudioModePreferenceControllerTest {
46 
47     @Mock
48     private PreferenceScreen mPreferenceScreen;
49     @Mock
50     private DevelopmentSettingsDashboardFragment mFragment;
51     @Mock
52     private BluetoothAdapter mBluetoothAdapter;
53     @Mock
54     private ListPreference mPreference;
55 
56     private Context mContext;
57     private BluetoothLeAudioModePreferenceController mController;
58     private String[] mListValues;
59     private String[] mListSummaries;
60 
61     @Before
setup()62     public void setup() {
63         MockitoAnnotations.initMocks(this);
64         mContext = RuntimeEnvironment.application;
65         mListValues = mContext.getResources().getStringArray(
66                 R.array.bluetooth_leaudio_mode_values);
67         mListSummaries = mContext.getResources().getStringArray(
68                 R.array.bluetooth_leaudio_mode);
69         mController = spy(new BluetoothLeAudioModePreferenceController(mContext, mFragment));
70         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
71                 .thenReturn(mPreference);
72         mController.mBluetoothAdapter = mBluetoothAdapter;
73         mController.displayPreference(mPreferenceScreen);
74     }
75 
76     @Test
onRebootDialogConfirmed_changeLeAudioMode_shouldSetLeAudioMode()77     public void onRebootDialogConfirmed_changeLeAudioMode_shouldSetLeAudioMode() {
78         mController.mChanged = true;
79         SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0]);
80         mController.mNewMode = mListValues[1];
81 
82         mController.onRebootDialogConfirmed();
83         assertThat(SystemProperties.get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0])
84                         .equals(mController.mNewMode)).isTrue();
85     }
86 
87     @Test
onRebootDialogConfirmed_notChangeLeAudioMode_shouldNotSetLeAudioMode()88     public void onRebootDialogConfirmed_notChangeLeAudioMode_shouldNotSetLeAudioMode() {
89         mController.mChanged = false;
90         SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0]);
91         mController.mNewMode = mListValues[1];
92 
93         mController.onRebootDialogConfirmed();
94         assertThat(SystemProperties.get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0])
95                         .equals(mController.mNewMode)).isFalse();
96     }
97 
98     @Test
onRebootDialogCanceled_shouldNotSetLeAudioMode()99     public void onRebootDialogCanceled_shouldNotSetLeAudioMode() {
100         mController.mChanged = true;
101         SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0]);
102         mController.mNewMode = mListValues[1];
103 
104         mController.onRebootDialogCanceled();
105         assertThat(SystemProperties.get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0])
106                         .equals(mController.mNewMode)).isFalse();
107     }
108 
109     @Test
onBluetoothTurnOff_shouldNotChangeLeAudioMode()110     public void onBluetoothTurnOff_shouldNotChangeLeAudioMode() {
111         SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[1]);
112         when(mBluetoothAdapter.isEnabled())
113                 .thenReturn(false);
114 
115         mController.updateState(mPreference);
116         final String mode = SystemProperties
117                 .get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0]);
118         assertThat(mode.equals(mListValues[1])).isTrue();
119     }
120 }
121