aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothDevice.java
diff options
context:
space:
mode:
Diffstat (limited to 'shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothDevice.java')
-rw-r--r--shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothDevice.java30
1 files changed, 27 insertions, 3 deletions
diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothDevice.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothDevice.java
index d193b79b4..56b3be662 100644
--- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothDevice.java
+++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothDevice.java
@@ -2,7 +2,6 @@ package org.robolectric.shadows;
import static android.bluetooth.BluetoothDevice.BOND_NONE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
-import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
import static android.os.Build.VERSION_CODES.KITKAT_WATCH;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.O;
@@ -30,6 +29,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
+import javax.annotation.Nullable;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@@ -44,6 +44,15 @@ import org.robolectric.util.reflector.Static;
/** Shadow for {@link BluetoothDevice}. */
@Implements(value = BluetoothDevice.class, looseSignatures = true)
public class ShadowBluetoothDevice {
+ /**
+ * Interceptor interface for {@link BluetoothGatt} objects. Tests that require configuration of
+ * their ShadowBluetoothGatt's may inject an interceptor, which will be called with the newly
+ * constructed BluetoothGatt before {@link ShadowBluetoothGatt#connectGatt} returns.
+ */
+ public static interface BluetoothGattConnectionInterceptor {
+ public void onNewGattConnection(BluetoothGatt gatt);
+ }
+
@Deprecated // Prefer {@link android.bluetooth.BluetoothAdapter#getRemoteDevice}
public static BluetoothDevice newInstance(String address) {
return ReflectionHelpers.callConstructor(
@@ -76,6 +85,7 @@ public class ShadowBluetoothDevice {
private int batteryLevel = BluetoothDevice.BATTERY_LEVEL_BLUETOOTH_OFF;
private boolean isInSilenceMode = false;
private boolean isConnected = false;
+ @Nullable private BluetoothGattConnectionInterceptor bluetoothGattConnectionInterceptor = null;
/**
* Implements getService() in the same way the original method does, but ignores any Exceptions
@@ -180,7 +190,7 @@ public class ShadowBluetoothDevice {
* @return Value set by calling {@link ShadowBluetoothDevice#setType}. If setType has not
* previously been called, will return BluetoothDevice.DEVICE_TYPE_UNKNOWN.
*/
- @Implementation(minSdk = JELLY_BEAN_MR2)
+ @Implementation
protected int getType() {
checkForBluetoothConnectPermission();
return type;
@@ -319,7 +329,7 @@ public class ShadowBluetoothDevice {
return fetchUuidsWithSdpCount;
}
- @Implementation(minSdk = JELLY_BEAN_MR2)
+ @Implementation
protected BluetoothGatt connectGatt(
Context context, boolean autoConnect, BluetoothGattCallback callback) {
checkForBluetoothConnectPermission();
@@ -350,6 +360,11 @@ public class ShadowBluetoothDevice {
bluetoothGatts.add(bluetoothGatt);
ShadowBluetoothGatt shadowBluetoothGatt = Shadow.extract(bluetoothGatt);
shadowBluetoothGatt.setGattCallback(callback);
+
+ if (bluetoothGattConnectionInterceptor != null) {
+ bluetoothGattConnectionInterceptor.onNewGattConnection(bluetoothGatt);
+ }
+
return bluetoothGatt;
}
@@ -436,6 +451,15 @@ public class ShadowBluetoothDevice {
return isInSilenceMode;
}
+ /**
+ * Allows tests to intercept the {@link BluetoothDevice.connectGatt} method and set state on both
+ * BluetoothDevice and BluetoothGatt objects. This is useful for e2e testing situations where the
+ * fine-grained execution of Bluetooth connection logic is onerous.
+ */
+ public void setGattConnectionInterceptor(BluetoothGattConnectionInterceptor interceptor) {
+ bluetoothGattConnectionInterceptor = interceptor;
+ }
+
@ForType(BluetoothDevice.class)
interface BluetoothDeviceReflector {