• 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.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         doReturn(mBluetoothCodecConfig).when(mController).getCodecConfig(null);
77         doNothing().when(mController).setCodecConfigPreference(any(), any());
78         when(mBluetoothA2dpConfigStore.createCodecConfig()).thenReturn(mBluetoothCodecConfig);
79         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
80         mController.displayPreference(mScreen);
81     }
82 
83     @Test
84     @Ignore
onPreferenceChange_bluetoothConnected_shouldUpdateCodec()85     public void onPreferenceChange_bluetoothConnected_shouldUpdateCodec() {
86         mController.onBluetoothServiceConnected(mBluetoothA2dp);
87 
88         mController.onPreferenceChange(mPreference, "" /* new value */);
89 
90         verify(mController).setCodecConfigPreference(any(), any());
91     }
92 
93     @Test
onPreferenceChange_bluetoothNotConnected_shouldNotUpdateCodec()94     public void onPreferenceChange_bluetoothNotConnected_shouldNotUpdateCodec() {
95         mController.onBluetoothServiceDisconnected();
96 
97         mController.onPreferenceChange(mPreference, "" /* new value */);
98 
99         verify(mController, never()).setCodecConfigPreference(any(), any());
100     }
101 
102     @Test
103     @Ignore
updateState_option2Set_shouldUpdateToOption2()104     public void updateState_option2Set_shouldUpdateToOption2() {
105         when(mBluetoothCodecConfig.getSampleRate()).thenReturn(
106                 BluetoothCodecConfig.SAMPLE_RATE_48000);
107 
108         doReturn(2).when(mController).getCurrentA2dpSettingIndex(any());
109         mController.updateState(mPreference);
110 
111         verify(mPreference).setValue(mController.getListValues()[2]);
112         verify(mPreference).setSummary(mContext.getString(STREAMING_LABEL_ID,
113             mController.getListSummaries()[2]));
114     }
115 
116     @Test
onBluetoothServiceConnected_shouldUpdateState()117     public void onBluetoothServiceConnected_shouldUpdateState() {
118         mController.onBluetoothServiceConnected(mBluetoothA2dp);
119 
120         verify(mController).updateState(mPreference);
121     }
122 
123     private static class AbstractBluetoothA2dpPreferenceControllerImpl
124         extends AbstractBluetoothA2dpPreferenceController {
125 
AbstractBluetoothA2dpPreferenceControllerImpl(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)126         private AbstractBluetoothA2dpPreferenceControllerImpl(Context context,
127                 Lifecycle lifecycle, BluetoothA2dpConfigStore store) {
128             super(context, lifecycle, store);
129         }
130 
131         @Override
getPreferenceKey()132         public String getPreferenceKey() {
133             return null;
134         }
135 
136         @Override
getListValues()137         protected String[] getListValues() {
138             return new String[]{"1", "2", "3"};
139         }
140 
141         @Override
getListSummaries()142         protected String[] getListSummaries() {
143             return new String[]{"foo", "bar", "foobar"};
144         }
145 
146         @Override
writeConfigurationValues(Object newValue)147         protected void writeConfigurationValues(Object newValue) {
148         }
149 
150         @Override
getCurrentA2dpSettingIndex(BluetoothCodecConfig config)151         protected int getCurrentA2dpSettingIndex(BluetoothCodecConfig config) {
152             return 0;
153         }
154 
155         @Override
getDefaultIndex()156         protected int getDefaultIndex() {
157             return 0;
158         }
159     }
160 }
161