• 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 BluetoothAudioChannelModePreferenceControllerTest {
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: Mono
57      * 2: Stereo
58      */
59     private String[] mListValues;
60     private Context mContext;
61     private BluetoothAudioChannelModePreferenceController mController;
62     private LifecycleOwner mLifecycleOwner;
63     private Lifecycle mLifecycle;
64 
65     @Before
setup()66     public void setup() {
67         MockitoAnnotations.initMocks(this);
68         mContext = RuntimeEnvironment.application;
69         mLifecycleOwner = () -> mLifecycle;
70         mLifecycle = new Lifecycle(mLifecycleOwner);
71         mController = spy(new BluetoothAudioChannelModePreferenceController(mContext,
72                 mLifecycle, mBluetoothA2dpConfigStore));
73         mListValues = mController.getListValues();
74         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
75         mController.displayPreference(mScreen);
76     }
77 
78     @Test
writeConfigurationValues_option2_shouldWriteOption2ToSharedStore()79     public void writeConfigurationValues_option2_shouldWriteOption2ToSharedStore() {
80         when(mPreference.findIndexOfValue(mListValues[2])).thenReturn(2);
81         mController.writeConfigurationValues(mListValues[2]);
82 
83         verify(mBluetoothA2dpConfigStore).setChannelMode(BluetoothCodecConfig.CHANNEL_MODE_STEREO);
84     }
85 
86     @Test
writeConfigurationValues_default_shouldSetDefault()87     public void writeConfigurationValues_default_shouldSetDefault() {
88         when(mPreference.findIndexOfValue(mListValues[0])).thenReturn(0);
89         mController.writeConfigurationValues(mListValues[0]);
90 
91         verify(mBluetoothA2dpConfigStore).setChannelMode(BluetoothCodecConfig.CHANNEL_MODE_NONE);
92     }
93 
94     @Test
getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex()95     public void getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex() {
96         when(mBluetoothCodecConfig.getChannelMode())
97             .thenReturn(BluetoothCodecConfig.CHANNEL_MODE_STEREO);
98 
99         final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig);
100 
101         assertThat(index).isEqualTo(2);
102     }
103 
104     @Test
getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault()105     public void getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault() {
106         when(mBluetoothCodecConfig.getCodecType()).thenReturn(1381391835);
107 
108         final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig);
109 
110         assertThat(index).isEqualTo(0);
111     }
112 }
113