• 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 package com.android.settings.bluetooth;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.Mockito.doNothing;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.bluetooth.BluetoothAdapter;
28 import android.bluetooth.BluetoothDevice;
29 import android.bluetooth.BluetoothProfile;
30 import android.content.Context;
31 import android.media.AudioManager;
32 
33 import com.android.settings.connecteddevice.DevicePreferenceCallback;
34 import com.android.settings.dashboard.DashboardFragment;
35 import com.android.settings.testutils.shadow.ShadowAudioManager;
36 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
37 import com.android.settings.testutils.shadow.ShadowCachedBluetoothDeviceManager;
38 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
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 import org.robolectric.annotation.Config;
48 import org.robolectric.shadow.api.Shadow;
49 
50 import java.util.ArrayList;
51 import java.util.Collection;
52 
53 @RunWith(RobolectricTestRunner.class)
54 @Config(shadows = {ShadowAudioManager.class, ShadowBluetoothAdapter.class,
55         ShadowCachedBluetoothDeviceManager.class})
56 public class ConnectedBluetoothDeviceUpdaterTest {
57 
58     private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
59 
60     @Mock
61     private DashboardFragment mDashboardFragment;
62     @Mock
63     private DevicePreferenceCallback mDevicePreferenceCallback;
64     @Mock
65     private CachedBluetoothDevice mCachedBluetoothDevice;
66     @Mock
67     private BluetoothDevice mBluetoothDevice;
68 
69     private Context mContext;
70     private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater;
71     private Collection<CachedBluetoothDevice> mCachedDevices;
72     private AudioManager mAudioManager;
73     private ShadowBluetoothAdapter mShadowBluetoothAdapter;
74     private ShadowCachedBluetoothDeviceManager mShadowCachedBluetoothDeviceManager;
75 
76     @Before
setUp()77     public void setUp() {
78         MockitoAnnotations.initMocks(this);
79 
80         mContext = RuntimeEnvironment.application;
81         mAudioManager = mContext.getSystemService(AudioManager.class);
82         mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
83         mShadowBluetoothAdapter.setEnabled(true);
84         mShadowCachedBluetoothDeviceManager = Shadow.extract(
85                 Utils.getLocalBtManager(mContext).getCachedDeviceManager());
86         doReturn(mContext).when(mDashboardFragment).getContext();
87         mCachedDevices = new ArrayList<>();
88 
89         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
90         when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS);
91         mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices);
92         mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mContext,
93                 mDashboardFragment, mDevicePreferenceCallback));
94         mBluetoothDeviceUpdater.setPrefContext(mContext);
95         doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
96         doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
97     }
98 
99     @Test
onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference()100     public void onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference() {
101         mAudioManager.setMode(AudioManager.MODE_NORMAL);
102         when(mBluetoothDeviceUpdater.
103                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
104         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
105         mCachedDevices.add(mCachedBluetoothDevice);
106 
107         mBluetoothDeviceUpdater.onAudioModeChanged();
108 
109         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
110     }
111 
112     @Test
onAudioModeChanged_hfpDeviceConnected_inCall_removePreference()113     public void onAudioModeChanged_hfpDeviceConnected_inCall_removePreference() {
114         mAudioManager.setMode(AudioManager.MODE_IN_CALL);
115         when(mBluetoothDeviceUpdater.
116                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
117         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
118         mCachedDevices.add(mCachedBluetoothDevice);
119 
120         mBluetoothDeviceUpdater.onAudioModeChanged();
121 
122         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
123     }
124 
125     @Test
onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference()126     public void onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference() {
127         mAudioManager.setMode(AudioManager.MODE_NORMAL);
128         when(mBluetoothDeviceUpdater.
129                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
130         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
131         mCachedDevices.add(mCachedBluetoothDevice);
132 
133         mBluetoothDeviceUpdater.onAudioModeChanged();
134 
135         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
136     }
137 
138     @Test
onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference()139     public void onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference() {
140         mAudioManager.setMode(AudioManager.MODE_IN_CALL);
141         when(mBluetoothDeviceUpdater.
142                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
143         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
144         mCachedDevices.add(mCachedBluetoothDevice);
145 
146         mBluetoothDeviceUpdater.onAudioModeChanged();
147 
148         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
149     }
150 
151     @Test
onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference()152     public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference() {
153         mAudioManager.setMode(AudioManager.MODE_IN_CALL);
154         when(mBluetoothDeviceUpdater.
155                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
156         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
157 
158         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
159                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
160 
161         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
162     }
163 
164     @Test
onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference()165     public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference() {
166         mAudioManager.setMode(AudioManager.MODE_NORMAL);
167         when(mBluetoothDeviceUpdater.
168                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
169         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
170 
171         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
172                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
173 
174         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
175     }
176 
177     @Test
onProfileConnectionStateChanged_hfpDeviceConnected_inCall_removePreference()178     public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_removePreference() {
179         mAudioManager.setMode(AudioManager.MODE_IN_CALL);
180         when(mBluetoothDeviceUpdater.
181                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
182         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
183 
184         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
185                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
186 
187         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
188     }
189 
190     @Test
onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference()191     public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference() {
192         mAudioManager.setMode(AudioManager.MODE_NORMAL);
193         when(mBluetoothDeviceUpdater.
194                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
195         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
196 
197         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
198                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
199 
200         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
201     }
202 
203     @Test
onProfileConnectionStateChanged_hearingAidDeviceConnected_inCall_removePreference()204     public void onProfileConnectionStateChanged_hearingAidDeviceConnected_inCall_removePreference()
205     {
206         mAudioManager.setMode(AudioManager.MODE_IN_CALL);
207         when(mBluetoothDeviceUpdater.
208                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
209         when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);
210 
211         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
212                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.HEARING_AID);
213 
214         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
215     }
216 
217     @Test
onProfileConnectionStateChanged_hearingAidDeviceConnected_notInCall_removePreference()218     public void onProfileConnectionStateChanged_hearingAidDeviceConnected_notInCall_removePreference
219             () {
220         mAudioManager.setMode(AudioManager.MODE_NORMAL);
221         when(mBluetoothDeviceUpdater.
222                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
223         when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);
224 
225         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
226                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.HEARING_AID);
227 
228         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
229     }
230 
231     @Test
onProfileConnectionStateChanged_deviceDisconnected_removePreference()232     public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() {
233         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
234                 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP);
235 
236         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
237     }
238 
239     @Test
addPreference_addPreference_shouldHideSecondTarget()240     public void addPreference_addPreference_shouldHideSecondTarget() {
241         BluetoothDevicePreference btPreference =
242                 new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, true);
243         mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, btPreference);
244 
245         mBluetoothDeviceUpdater.addPreference(mCachedBluetoothDevice);
246 
247         assertThat(btPreference.shouldHideSecondTarget()).isTrue();
248     }
249 }
250