• 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.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.bluetooth.BluetoothCodecConfig;
26 import android.content.Context;
27 
28 import androidx.lifecycle.LifecycleOwner;
29 import androidx.preference.ListPreference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settingslib.core.lifecycle.Lifecycle;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class BluetoothAudioCodecPreferenceControllerTest {
44 
45     @Mock
46     private BluetoothCodecConfig mBluetoothCodecConfig;
47     @Mock
48     private ListPreference mPreference;
49     @Mock
50     private PreferenceScreen mScreen;
51     @Mock
52     private BluetoothA2dpConfigStore mBluetoothA2dpConfigStore;
53 
54     /**
55      * 0: Use System Selection (Default)
56      * 1: SBC
57      * 2: AAC
58      * 3: Qualcomm aptX audio
59      * 4: Qualcomm aptX HD audio
60      * 5: LDAC
61      * 6: Enable Optional Codecs
62      * 7: Disable Optional Codecs
63      */
64     private String[] mListValues;
65     private Context mContext;
66     private BluetoothAudioCodecPreferenceController mController;
67     private LifecycleOwner mLifecycleOwner;
68     private Lifecycle mLifecycle;
69 
70     @Before
setup()71     public void setup() {
72         MockitoAnnotations.initMocks(this);
73         mContext = RuntimeEnvironment.application;
74         mLifecycleOwner = () -> mLifecycle;
75         mLifecycle = new Lifecycle(mLifecycleOwner);
76         mController = spy(new BluetoothAudioCodecPreferenceController(mContext, mLifecycle,
77                 mBluetoothA2dpConfigStore));
78         mListValues = mController.getListValues();
79         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
80         mController.displayPreference(mScreen);
81     }
82 
83     @Test
writeConfigurationValues_option2_shouldWriteOption2ToSharedStore()84     public void writeConfigurationValues_option2_shouldWriteOption2ToSharedStore() {
85         when(mPreference.findIndexOfValue(mListValues[2])).thenReturn(2);
86         mController.writeConfigurationValues(mListValues[2]);
87 
88         verify(mBluetoothA2dpConfigStore).setCodecType(BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC);
89         verify(mBluetoothA2dpConfigStore).setCodecPriority(
90                 BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST);
91     }
92 
93     @Test
writeConfigurationValues_default_shouldSetDefaultPriority()94     public void writeConfigurationValues_default_shouldSetDefaultPriority() {
95         when(mPreference.findIndexOfValue(mListValues[0])).thenReturn(0);
96         mController.writeConfigurationValues(mListValues[0]);
97 
98         verify(mBluetoothA2dpConfigStore).setCodecPriority(
99                 BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT);
100     }
101 
102     @Test
getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex()103     public void getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex() {
104         when(mBluetoothCodecConfig.getCodecType()).thenReturn(
105                 BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC);
106 
107         final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig);
108 
109         assertThat(index).isEqualTo(2);
110     }
111 
112     @Test
getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault()113     public void getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault() {
114         when(mBluetoothCodecConfig.getCodecType()).thenReturn(1381391835);
115 
116         final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig);
117 
118         assertThat(index).isEqualTo(0);
119     }
120 }
121