• 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.Matchers.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.BluetoothProfile;
28 import android.bluetooth.BluetoothDevice;
29 import android.content.Context;
30 import android.media.AudioManager;
31 
32 import com.android.settings.connecteddevice.DevicePreferenceCallback;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.testutils.SettingsRobolectricTestRunner;
35 import com.android.settings.testutils.shadow.ShadowAudioManager;
36 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
37 import com.android.settingslib.bluetooth.HeadsetProfile;
38 import com.android.settingslib.bluetooth.LocalBluetoothManager;
39 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
40 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 
50 import java.util.ArrayList;
51 import java.util.Collection;
52 
53 @RunWith(SettingsRobolectricTestRunner.class)
54 @Config(shadows = {ShadowAudioManager.class})
55 public class ConnectedBluetoothDeviceUpdaterTest {
56     @Mock
57     private DashboardFragment mDashboardFragment;
58     @Mock
59     private DevicePreferenceCallback mDevicePreferenceCallback;
60     @Mock
61     private CachedBluetoothDevice mCachedBluetoothDevice;
62     @Mock
63     private BluetoothDevice mBluetoothDevice;
64     @Mock
65     private LocalBluetoothManager mLocalManager;
66     @Mock
67     private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
68     @Mock
69     private HeadsetProfile mHeadsetProfile;
70 
71     private Context mContext;
72     private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater;
73 
74     @Mock
75     private CachedBluetoothDeviceManager mCachedDeviceManager;
76 
77     private Collection<CachedBluetoothDevice> cachedDevices;
78     private ShadowAudioManager mShadowAudioManager;
79 
80     @Before
setUp()81     public void setUp() {
82         MockitoAnnotations.initMocks(this);
83 
84         mShadowAudioManager = ShadowAudioManager.getShadow();
85         mContext = RuntimeEnvironment.application;
86         doReturn(mContext).when(mDashboardFragment).getContext();
87         cachedDevices =
88                 new ArrayList<CachedBluetoothDevice>(new ArrayList<CachedBluetoothDevice>());
89 
90         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
91         when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
92         when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
93         when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
94         when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
95 
96         mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mDashboardFragment,
97                 mDevicePreferenceCallback, mLocalManager));
98         mBluetoothDeviceUpdater.setPrefContext(mContext);
99         doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
100         doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
101     }
102 
103     @Test
onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference()104     public void onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference() {
105         mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
106         when(mBluetoothDeviceUpdater.
107                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
108         when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
109         cachedDevices.add(mCachedBluetoothDevice);
110 
111         mBluetoothDeviceUpdater.onAudioModeChanged();
112 
113         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
114     }
115 
116     @Test
onAudioModeChanged_hfpDeviceConnected_inCall_removePreference()117     public void onAudioModeChanged_hfpDeviceConnected_inCall_removePreference() {
118         mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
119         when(mBluetoothDeviceUpdater.
120                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
121         when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
122         cachedDevices.add(mCachedBluetoothDevice);
123 
124         mBluetoothDeviceUpdater.onAudioModeChanged();
125 
126         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
127     }
128 
129     @Test
onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference()130     public void onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference() {
131         mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
132         when(mBluetoothDeviceUpdater.
133                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
134         when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
135         cachedDevices.add(mCachedBluetoothDevice);
136 
137         mBluetoothDeviceUpdater.onAudioModeChanged();
138 
139         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
140     }
141 
142     @Test
onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference()143     public void onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference() {
144         mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
145         when(mBluetoothDeviceUpdater.
146                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
147         when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
148         cachedDevices.add(mCachedBluetoothDevice);
149 
150         mBluetoothDeviceUpdater.onAudioModeChanged();
151 
152         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
153     }
154 
155     @Test
onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference()156     public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference() {
157         mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
158         when(mBluetoothDeviceUpdater.
159                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
160         when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
161 
162         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
163                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
164 
165         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
166     }
167 
168     @Test
onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference()169     public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference() {
170         mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
171         when(mBluetoothDeviceUpdater.
172                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
173         when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
174 
175         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
176                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
177 
178         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
179     }
180 
181     @Test
onProfileConnectionStateChanged_hfpDeviceConnected_inCall_removePreference()182     public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_removePreference() {
183         mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
184         when(mBluetoothDeviceUpdater.
185                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
186         when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
187 
188         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
189                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
190 
191         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
192     }
193 
194     @Test
onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference()195     public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference() {
196         mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
197         when(mBluetoothDeviceUpdater.
198                 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
199         when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
200 
201         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
202                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
203 
204         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
205     }
206 
207     @Test
onProfileConnectionStateChanged_deviceDisconnected_removePreference()208     public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() {
209         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
210                 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP);
211 
212         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
213     }
214 
215     @Test
addPreference_addPreference_shouldHideSecondTarget()216     public void addPreference_addPreference_shouldHideSecondTarget() {
217         BluetoothDevicePreference btPreference =
218                 new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, true);
219         mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, btPreference);
220 
221         mBluetoothDeviceUpdater.addPreference(mCachedBluetoothDevice);
222 
223         assertThat(btPreference.shouldHideSecondTarget()).isTrue();
224     }
225 }
226