1 /*
2  * Copyright (C) 2018 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.BluetoothA2dpHwOffloadPreferenceController
20         .A2DP_OFFLOAD_DISABLED_PROPERTY;
21 import static com.android.settings.development.BluetoothLeAudioHwOffloadPreferenceController
22         .LE_AUDIO_OFFLOAD_DISABLED_PROPERTY;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28 
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 BluetoothA2dpHwOffloadPreferenceControllerTest {
45 
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48     @Mock
49     private DevelopmentSettingsDashboardFragment mFragment;
50 
51     private Context mContext;
52     private SwitchPreference mPreference;
53     private BluetoothA2dpHwOffloadPreferenceController mController;
54 
55     @Before
setup()56     public void setup() {
57         MockitoAnnotations.initMocks(this);
58         mContext = RuntimeEnvironment.application;
59         mPreference = new SwitchPreference(mContext);
60         mController = spy(new BluetoothA2dpHwOffloadPreferenceController(mContext, mFragment));
61         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
62             .thenReturn(mPreference);
63         mController.displayPreference(mPreferenceScreen);
64     }
65 
66     @Test
onA2dpHwDialogConfirmedAsA2dpOffloadDisabled_shouldChangeProperty()67     public void onA2dpHwDialogConfirmedAsA2dpOffloadDisabled_shouldChangeProperty() {
68         SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(true));
69         mController.mChanged = true;
70 
71         mController.onRebootDialogConfirmed();
72         final boolean mode = SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
73         assertThat(mode).isFalse();
74     }
75 
76     @Test
onA2dpHwDialogConfirmedAsA2dpOffloadEnabled_shouldChangeProperty()77     public void onA2dpHwDialogConfirmedAsA2dpOffloadEnabled_shouldChangeProperty() {
78         SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(false));
79         SystemProperties.set(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(false));
80 
81         mController.mChanged = true;
82 
83         mController.onRebootDialogConfirmed();
84         final boolean a2dpMode = SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, true);
85         final boolean leAudioMode = SystemProperties
86                 .getBoolean(LE_AUDIO_OFFLOAD_DISABLED_PROPERTY, true);
87         assertThat(a2dpMode).isTrue();
88         assertThat(leAudioMode).isTrue();
89     }
90 
91     @Test
onA2dpHwDialogCanceled_shouldNotChangeProperty()92     public void onA2dpHwDialogCanceled_shouldNotChangeProperty() {
93         SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(false));
94         mController.mChanged = true;
95 
96         mController.onRebootDialogCanceled();
97         final boolean mode = SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
98         assertThat(mode).isFalse();
99     }
100 }
101