1package org.robolectric.shadows; 2 3import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2; 4import static org.assertj.core.api.Assertions.assertThat; 5import static org.robolectric.Shadows.shadowOf; 6 7import android.bluetooth.BluetoothAdapter; 8import android.bluetooth.BluetoothDevice; 9import org.junit.Before; 10import org.junit.Rule; 11import org.junit.Test; 12import org.junit.rules.ExpectedException; 13import org.junit.runner.RunWith; 14import org.robolectric.RobolectricTestRunner; 15import org.robolectric.annotation.Config; 16import org.robolectric.shadow.api.Shadow; 17 18@RunWith(RobolectricTestRunner.class) 19public class ShadowBluetoothAdapterTest { 20 private BluetoothAdapter bluetoothAdapter; 21 private ShadowBluetoothAdapter shadowBluetoothAdapter; 22 23 @Rule 24 public ExpectedException thrown = ExpectedException.none(); 25 26 @Before 27 public void setUp() throws Exception { 28 bluetoothAdapter = Shadow.newInstanceOf(BluetoothAdapter.class); 29 shadowBluetoothAdapter = shadowOf(bluetoothAdapter); 30 } 31 32 @Test 33 public void testAdapterDefaultsDisabled() { 34 assertThat(bluetoothAdapter.isEnabled()).isFalse(); 35 } 36 37 @Test 38 public void testAdapterCanBeEnabled_forTesting() { 39 shadowBluetoothAdapter.setEnabled(true); 40 assertThat(bluetoothAdapter.isEnabled()).isTrue(); 41 } 42 43 @Test 44 public void canGetAndSetAddress() throws Exception { 45 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 46 shadowOf(adapter).setAddress("expected"); 47 assertThat(adapter.getAddress()).isEqualTo("expected"); 48 } 49 50 @Test 51 public void canEnable_withAndroidApi() throws Exception { 52 bluetoothAdapter.enable(); 53 assertThat(bluetoothAdapter.isEnabled()).isTrue(); 54 } 55 56 @Test 57 public void canDisable_withAndroidApi() throws Exception { 58 shadowBluetoothAdapter.setEnabled(true); 59 bluetoothAdapter.disable(); 60 assertThat(bluetoothAdapter.isEnabled()).isFalse(); 61 } 62 63 @Test 64 @Config(minSdk = JELLY_BEAN_MR2) 65 public void testLeScan() { 66 BluetoothAdapter.LeScanCallback callback1 = newLeScanCallback(); 67 BluetoothAdapter.LeScanCallback callback2 = newLeScanCallback(); 68 69 bluetoothAdapter.startLeScan(callback1); 70 assertThat(shadowBluetoothAdapter.getLeScanCallbacks()).containsOnly(callback1); 71 bluetoothAdapter.startLeScan(callback2); 72 assertThat(shadowBluetoothAdapter.getLeScanCallbacks()).containsOnly(callback1, callback2); 73 74 bluetoothAdapter.stopLeScan(callback1); 75 assertThat(shadowBluetoothAdapter.getLeScanCallbacks()).containsOnly(callback2); 76 bluetoothAdapter.stopLeScan(callback2); 77 assertThat(shadowBluetoothAdapter.getLeScanCallbacks()).isEmpty(); 78 } 79 80 @Test 81 @Config(minSdk = JELLY_BEAN_MR2) 82 public void testGetSingleLeScanCallback() { 83 BluetoothAdapter.LeScanCallback callback1 = newLeScanCallback(); 84 BluetoothAdapter.LeScanCallback callback2 = newLeScanCallback(); 85 86 bluetoothAdapter.startLeScan(callback1); 87 assertThat(shadowBluetoothAdapter.getSingleLeScanCallback()).isEqualTo(callback1); 88 89 bluetoothAdapter.startLeScan(callback2); 90 thrown.expect(IllegalStateException.class); 91 thrown.expectMessage("There are 2 callbacks"); 92 shadowBluetoothAdapter.getSingleLeScanCallback(); 93 } 94 95 private BluetoothAdapter.LeScanCallback newLeScanCallback() { 96 return new BluetoothAdapter.LeScanCallback() { 97 @Override 98 public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {} 99 }; 100 } 101} 102