• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.bluetooth.BluetoothDevice.BOND_BONDED;
4 import static android.bluetooth.BluetoothDevice.BOND_NONE;
5 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
6 import static com.google.common.truth.Truth.assertThat;
7 import static org.robolectric.Shadows.shadowOf;
8 
9 import android.bluetooth.BluetoothAdapter;
10 import android.bluetooth.BluetoothDevice;
11 import android.bluetooth.BluetoothGattCallback;
12 import android.os.ParcelUuid;
13 import androidx.test.core.app.ApplicationProvider;
14 import androidx.test.ext.junit.runners.AndroidJUnit4;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.robolectric.annotation.Config;
18 
19 @RunWith(AndroidJUnit4.class)
20 public class ShadowBluetoothDeviceTest {
21 
22   private static final String MOCK_MAC_ADDRESS = "00:11:22:33:AA:BB";
23 
24   @Test
canCreateBluetoothDeviceViaNewInstance()25   public void canCreateBluetoothDeviceViaNewInstance() throws Exception {
26     // This test passes as long as no Exception is thrown. It tests if the constructor can be
27     // executed without throwing an Exception when getService() is called inside.
28     BluetoothDevice bluetoothDevice = ShadowBluetoothDevice.newInstance(MOCK_MAC_ADDRESS);
29     assertThat(bluetoothDevice).isNotNull();
30   }
31 
32   @Test
canSetAndGetUuids()33   public void canSetAndGetUuids() throws Exception {
34     BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS);
35     ParcelUuid[] uuids =
36         new ParcelUuid[] {
37           ParcelUuid.fromString("00000000-1111-2222-3333-000000000011"),
38           ParcelUuid.fromString("00000000-1111-2222-3333-0000000000aa")
39         };
40 
41     shadowOf(device).setUuids(uuids);
42     assertThat(device.getUuids()).isEqualTo(uuids);
43   }
44 
45   @Test
getUuids_setUuidsNotCalled_shouldReturnNull()46   public void getUuids_setUuidsNotCalled_shouldReturnNull() throws Exception {
47     BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS);
48     assertThat(device.getUuids()).isNull();
49   }
50 
51   @Test
canSetAndGetBondState()52   public void canSetAndGetBondState() throws Exception {
53     BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS);
54 
55     assertThat(device.getBondState()).isEqualTo(BOND_NONE);
56 
57     shadowOf(device).setBondState(BOND_BONDED);
58     assertThat(device.getBondState()).isEqualTo(BOND_BONDED);
59   }
60 
61   @Test
canSetAndGetCreatedBond()62   public void canSetAndGetCreatedBond() {
63     BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS);
64 
65     assertThat(device.createBond()).isFalse();
66 
67     shadowOf(device).setCreatedBond(true);
68     assertThat(device.createBond()).isTrue();
69   }
70 
71   @Test
canSetAndGetFetchUuidsWithSdpResult()72   public void canSetAndGetFetchUuidsWithSdpResult() throws Exception {
73     BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS);
74     assertThat(device.fetchUuidsWithSdp()).isFalse();
75 
76     shadowOf(device).setFetchUuidsWithSdpResult(true);
77     assertThat(device.fetchUuidsWithSdp()).isTrue();
78   }
79 
80   @Test
getCorrectFetchUuidsWithSdpCount()81   public void getCorrectFetchUuidsWithSdpCount() throws Exception {
82     BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS);
83     assertThat(shadowOf(device).getFetchUuidsWithSdpCount()).isEqualTo(0);
84 
85     device.fetchUuidsWithSdp();
86     assertThat(shadowOf(device).getFetchUuidsWithSdpCount()).isEqualTo(1);
87 
88     device.fetchUuidsWithSdp();
89     assertThat(shadowOf(device).getFetchUuidsWithSdpCount()).isEqualTo(2);
90   }
91 
92   @Test
93   @Config(minSdk = JELLY_BEAN_MR2)
connectGatt_doesntCrash()94   public void connectGatt_doesntCrash() throws Exception {
95     BluetoothDevice bluetoothDevice = ShadowBluetoothDevice.newInstance(MOCK_MAC_ADDRESS);
96     assertThat(
97             bluetoothDevice.connectGatt(
98                 ApplicationProvider.getApplicationContext(), false, new BluetoothGattCallback() {}))
99         .isNotNull();
100   }
101 }
102