1 /*
2  * Copyright (C) 2017 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.AbstractBluetoothA2dpPreferenceController
20         .STREAMING_LABEL_ID;
21 
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doNothing;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.bluetooth.BluetoothA2dp;
31 import android.bluetooth.BluetoothCodecConfig;
32 import android.content.Context;
33 
34 import androidx.lifecycle.LifecycleOwner;
35 import androidx.preference.ListPreference;
36 import androidx.preference.PreferenceScreen;
37 
38 import com.android.settingslib.core.lifecycle.Lifecycle;
39 
40 import org.junit.Before;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class AbstractBluetoothA2dpPreferenceControllerTest {
51 
52     @Mock
53     private BluetoothA2dp mBluetoothA2dp;
54     @Mock
55     private BluetoothCodecConfig mBluetoothCodecConfig;
56     @Mock
57     private ListPreference mPreference;
58     @Mock
59     private PreferenceScreen mScreen;
60     @Mock
61     private BluetoothA2dpConfigStore mBluetoothA2dpConfigStore;
62 
63     private LifecycleOwner mLifecycleOwner;
64     private Lifecycle mLifecycle;
65     private Context mContext;
66     private AbstractBluetoothA2dpPreferenceController mController;
67 
68     @Before
setup()69     public void setup() {
70         MockitoAnnotations.initMocks(this);
71         mContext = RuntimeEnvironment.application;
72         mLifecycleOwner = () -> mLifecycle;
73         mLifecycle = new Lifecycle(mLifecycleOwner);
74         mController = spy(new AbstractBluetoothA2dpPreferenceControllerImpl(mContext, mLifecycle,
75                 mBluetoothA2dpConfigStore));
76         mController.mBluetoothAdapter = null;
77         doReturn(mBluetoothCodecConfig).when(mController).getCodecConfig(null);
78         doNothing().when(mController).setCodecConfigPreference(any(), any());
79         when(mBluetoothA2dpConfigStore.createCodecConfig()).thenReturn(mBluetoothCodecConfig);
80         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
81         mController.displayPreference(mScreen);
82     }
83 
84     @Test
85     @Ignore
onPreferenceChange_bluetoothConnected_shouldUpdateCodec()86     public void onPreferenceChange_bluetoothConnected_shouldUpdateCodec() {
87         mController.onBluetoothServiceConnected(mBluetoothA2dp);
88 
89         mController.onPreferenceChange(mPreference, "" /* new value */);
90 
91         verify(mController).setCodecConfigPreference(any(), any());
92     }
93 
94     @Test
onPreferenceChange_bluetoothNotConnected_shouldNotUpdateCodec()95     public void onPreferenceChange_bluetoothNotConnected_shouldNotUpdateCodec() {
96         mController.onBluetoothServiceDisconnected();
97 
98         mController.onPreferenceChange(mPreference, "" /* new value */);
99 
100         verify(mController, never()).setCodecConfigPreference(any(), any());
101     }
102 
103     @Test
104     @Ignore
updateState_option2Set_shouldUpdateToOption2()105     public void updateState_option2Set_shouldUpdateToOption2() {
106         when(mBluetoothCodecConfig.getSampleRate()).thenReturn(
107                 BluetoothCodecConfig.SAMPLE_RATE_48000);
108 
109         doReturn(2).when(mController).getCurrentA2dpSettingIndex(any());
110         mController.updateState(mPreference);
111 
112         verify(mPreference).setValue(mController.getListValues()[2]);
113         verify(mPreference).setSummary(mContext.getString(STREAMING_LABEL_ID,
114             mController.getListSummaries()[2]));
115     }
116 
117     @Test
onBluetoothServiceConnected_shouldUpdateState()118     public void onBluetoothServiceConnected_shouldUpdateState() {
119         mController.onBluetoothServiceConnected(mBluetoothA2dp);
120 
121         verify(mController).updateState(mPreference);
122     }
123 
124     private static class AbstractBluetoothA2dpPreferenceControllerImpl
125         extends AbstractBluetoothA2dpPreferenceController {
126 
AbstractBluetoothA2dpPreferenceControllerImpl(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)127         private AbstractBluetoothA2dpPreferenceControllerImpl(Context context,
128                 Lifecycle lifecycle, BluetoothA2dpConfigStore store) {
129             super(context, lifecycle, store);
130         }
131 
132         @Override
getPreferenceKey()133         public String getPreferenceKey() {
134             return null;
135         }
136 
137         @Override
getListValues()138         protected String[] getListValues() {
139             return new String[]{"1", "2", "3"};
140         }
141 
142         @Override
getListSummaries()143         protected String[] getListSummaries() {
144             return new String[]{"foo", "bar", "foobar"};
145         }
146 
147         @Override
writeConfigurationValues(Object newValue)148         protected void writeConfigurationValues(Object newValue) {
149         }
150 
151         @Override
getCurrentA2dpSettingIndex(BluetoothCodecConfig config)152         protected int getCurrentA2dpSettingIndex(BluetoothCodecConfig config) {
153             return 0;
154         }
155 
156         @Override
getDefaultIndex()157         protected int getDefaultIndex() {
158             return 0;
159         }
160     }
161 }
162