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