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 
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.content.Context;
28 import android.os.SystemProperties;
29 
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class BluetoothA2dpHwOffloadPreferenceControllerTest {
43 
44     @Mock
45     private PreferenceScreen mPreferenceScreen;
46     @Mock
47     private DevelopmentSettingsDashboardFragment mFragment;
48 
49     private Context mContext;
50     private SwitchPreference mPreference;
51     private BluetoothA2dpHwOffloadPreferenceController mController;
52 
53     @Before
setup()54     public void setup() {
55         MockitoAnnotations.initMocks(this);
56         mContext = RuntimeEnvironment.application;
57         mPreference = new SwitchPreference(mContext);
58         mController = spy(new BluetoothA2dpHwOffloadPreferenceController(mContext, mFragment));
59         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
60             .thenReturn(mPreference);
61         mController.displayPreference(mPreferenceScreen);
62     }
63 
64     @Test
onA2dpHwDialogConfirmed_shouldChangeProperty()65     public void onA2dpHwDialogConfirmed_shouldChangeProperty() {
66         SystemProperties.set(A2DP_OFFLOAD_DISABLED_PROPERTY, Boolean.toString(false));
67 
68         mController.onA2dpHwDialogConfirmed();
69         final boolean mode = SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
70         assertThat(mode).isTrue();
71 
72         mController.onA2dpHwDialogConfirmed();
73         final boolean mode2 = SystemProperties.getBoolean(A2DP_OFFLOAD_DISABLED_PROPERTY, false);
74         assertThat(mode2).isFalse();
75     }
76 }
77