1 /* 2 * Copyright (C) 2019 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.car.dialer.testutils; 18 19 import static org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.when; 21 22 import android.bluetooth.BluetoothAdapter; 23 import android.bluetooth.BluetoothDevice; 24 import android.bluetooth.BluetoothHeadsetClient; 25 import android.bluetooth.BluetoothProfile; 26 import android.content.Context; 27 28 import androidx.annotation.Nullable; 29 30 import org.robolectric.annotation.Implementation; 31 import org.robolectric.annotation.Implements; 32 import org.robolectric.shadows.ShadowApplication; 33 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Map; 37 38 /** 39 * Derived from {@link org.robolectric.shadows.ShadowBluetoothAdapter} 40 * 41 * Needed for Bluetooth related tests because the default ShadowBluetooth Adapter does not include 42 * an implementation of setProfileConnectionState. 43 */ 44 @Implements(value = BluetoothAdapter.class) 45 public class ShadowBluetoothAdapterForDialer extends 46 org.robolectric.shadows.ShadowBluetoothAdapter { 47 48 private static boolean bluetoothAvailable = true; 49 private Map<Integer, Integer> profileConnectionStateData = new HashMap<>(); 50 private final BluetoothHeadsetClient mMockBluetoothHeadsetClient = mock( 51 BluetoothHeadsetClient.class); 52 53 @Nullable 54 @Implementation getDefaultAdapter()55 public static synchronized BluetoothAdapter getDefaultAdapter() { 56 if (!bluetoothAvailable) { 57 return null; 58 } 59 return (BluetoothAdapter) ShadowApplication.getInstance().getBluetoothAdapter(); 60 } 61 62 @Implementation getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)63 public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, 64 int profile) { 65 if (profile == BluetoothProfile.HEADSET_CLIENT) { 66 listener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT, 67 mMockBluetoothHeadsetClient); 68 return true; 69 } 70 return false; 71 } 72 73 /** 74 * Sets if the default Bluetooth Adapter is null 75 */ setBluetoothAvailable(boolean available)76 public static void setBluetoothAvailable(boolean available) { 77 bluetoothAvailable = available; 78 } 79 setHfpDevices(List<BluetoothDevice> bluetoothDevices)80 public void setHfpDevices(List<BluetoothDevice> bluetoothDevices) { 81 when(mMockBluetoothHeadsetClient.getConnectedDevices()).thenReturn(bluetoothDevices); 82 } 83 } 84