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