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.BluetoothLeAudioPreferenceController
20         .LE_AUDIO_SWITCHER_DISABLED_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.bluetooth.BluetoothStatusCodes;
29 import android.content.Context;
30 import android.os.SystemProperties;
31 
32 import androidx.preference.PreferenceScreen;
33 import androidx.preference.SwitchPreference;
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 BluetoothLeAudioPreferenceControllerTest {
45 
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48     @Mock
49     private DevelopmentSettingsDashboardFragment mFragment;
50 
51     @Mock
52     private BluetoothAdapter mBluetoothAdapter;
53 
54     private Context mContext;
55     private SwitchPreference mPreference;
56     private BluetoothLeAudioPreferenceController mController;
57 
58     @Before
setup()59     public void setup() {
60         MockitoAnnotations.initMocks(this);
61         mContext = RuntimeEnvironment.application;
62         mPreference = new SwitchPreference(mContext);
63         mController = spy(new BluetoothLeAudioPreferenceController(mContext, mFragment));
64         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
65             .thenReturn(mPreference);
66         mController.mBluetoothAdapter = mBluetoothAdapter;
67         mController.displayPreference(mPreferenceScreen);
68     }
69 
70     @Test
onRebootDialogConfirmedAsLeAudioDisabled_shouldSwitchStatus()71     public void onRebootDialogConfirmedAsLeAudioDisabled_shouldSwitchStatus() {
72         when(mBluetoothAdapter.isLeAudioSupported())
73                 .thenReturn(BluetoothStatusCodes.FEATURE_NOT_SUPPORTED);
74         mController.mChanged = true;
75 
76         mController.onRebootDialogConfirmed();
77         final boolean status = SystemProperties
78                 .getBoolean(LE_AUDIO_SWITCHER_DISABLED_PROPERTY, true);
79         assertThat(status).isFalse();
80     }
81 
82     @Test
onRebootDialogConfirmedAsLeAudioEnabled_shouldSwitchStatus()83     public void onRebootDialogConfirmedAsLeAudioEnabled_shouldSwitchStatus() {
84         when(mBluetoothAdapter.isLeAudioSupported())
85                 .thenReturn(BluetoothStatusCodes.FEATURE_SUPPORTED);
86         mController.mChanged = true;
87 
88         mController.onRebootDialogConfirmed();
89         final boolean status = SystemProperties
90                 .getBoolean(LE_AUDIO_SWITCHER_DISABLED_PROPERTY, true);
91         assertThat(status).isTrue();
92     }
93 
94     @Test
onRebootDialogCanceled_shouldNotSwitchStatus()95     public void onRebootDialogCanceled_shouldNotSwitchStatus() {
96         when(mBluetoothAdapter.isLeAudioSupported())
97                 .thenReturn(BluetoothStatusCodes.FEATURE_NOT_SUPPORTED);
98         mController.mChanged = true;
99 
100         mController.onRebootDialogCanceled();
101         final boolean status = SystemProperties
102                 .getBoolean(LE_AUDIO_SWITCHER_DISABLED_PROPERTY, true);
103         assertThat(status).isTrue();
104     }
105 }
106