Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock callbacks #707

Merged
merged 36 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e2634f2
Added support for custom handling of characteristic+descriptor read/w…
nrbrook Jul 23, 2020
05db374
Moved DeviceBuilder to RxBleDeviceMock.Builder
nrbrook Jul 24, 2020
f019a1e
Structural changes to creating devices
nrbrook Jul 24, 2020
b062052
Merge branch 'mock-scan-record' into mock-structure-changes
nrbrook Jul 24, 2020
0568cc6
Readme updates
nrbrook Jul 24, 2020
d0dbb51
Merge branch 'mock-structure-changes' into mock-callbacks
nrbrook Jul 24, 2020
b7b9d59
Updated tests using scanBleDevices to use new method
nrbrook Jul 24, 2020
f346268
Merge branch 'mock-structure-changes' into mock-callbacks
nrbrook Jul 24, 2020
603336b
Merge fixes
nrbrook Jul 24, 2020
a1fd4fc
Added tests
nrbrook Jul 24, 2020
a603aac
Merge branch 'mock-scan-record' into mock-structure-changes
nrbrook Jul 24, 2020
9643fc3
Merge branch 'mock-structure-changes' into mock-callbacks
nrbrook Jul 24, 2020
e9f92e8
Merge branch 'mock-scan-record' into mock-structure-changes
nrbrook Jul 24, 2020
5b18628
Merge branch 'mock-structure-changes' into mock-callbacks
nrbrook Jul 24, 2020
5910089
Removed unused imports
nrbrook Jul 24, 2020
aaf5bd7
Merge branch '2-mock-structure-changes' into 3-mock-callbacks
nrbrook Jul 24, 2020
ebdc6bb
Added missing imports
nrbrook Jul 24, 2020
16aa351
Added callbacks to mock readme
nrbrook Jul 24, 2020
bb9f134
Fixed test
nrbrook Jul 24, 2020
d0de819
Merge branch '1-mock-scan-record' into 2-mock-structure-changes
nrbrook Jul 27, 2020
0e0fbf1
Merge branch '2-mock-structure-changes' into 3-mock-callbacks
nrbrook Jul 27, 2020
35b2181
Merge commit 'da9692e1fdc3589197c09d0ceaac24aeea32ffe7' into mock-str…
dariuszseweryn Jul 30, 2020
42df28c
Switch to lambdas in mock docs
nrbrook Jul 30, 2020
749d563
Merge branch '2-mock-structure-changes' into 3-mock-callbacks
nrbrook Jul 30, 2020
eaca7ac
Documentation improvements
nrbrook Jul 30, 2020
d9ac41a
Merge branch '2-mock-structure-changes' into 3-mock-callbacks
nrbrook Jul 30, 2020
aa20611
Merge branch 'master' into mock-callbacks
dariuszseweryn Jul 30, 2020
4ce5dd2
Removed duplicate implementation
nrbrook Jul 31, 2020
010a09f
Modified mock read+write callbacks to accept device and result parame…
nrbrook Aug 11, 2020
9352122
Fixed checkstyle violations
nrbrook Aug 11, 2020
5ac59a0
Fixed callback interfaces
nrbrook Aug 11, 2020
931a2f7
Merge branch 'master' into 3-mock-callbacks
nrbrook Aug 13, 2020
602e356
Make new namespaces lower case
nrbrook Aug 13, 2020
c4901e7
Remove unused imports from unit test
nrbrook Aug 13, 2020
096d93d
Added convenience functions for read result closures to tests
nrbrook Aug 13, 2020
858838f
Move connection mock tests to seperate test suite
nrbrook Aug 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion mockrxandroidble/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ RxBleClient rxBleClientMock = new RxBleClientMock.Builder()
.addDescriptor(descriptorUUID, descriptorData) // <-- adding descriptor mocks
.build() // to the characteristic, there can be multiple of them
).build()
).build()
).characteristicReadCallback(characteristicUUID, (device, characteristic, result) -> {
result.success(characteristicValue);
})
.characteristicWriteCallback(characteristicUUID, (device, characteristic, bytes, result) -> {
if(writeData(characteristic, bytes)) {
result.success();
} else {
result.failure(0x80);
// can also use result.disconnect(0x80);
}
}).build()
).build()
).build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.polidea.rxandroidble2.mockrxandroidble.Callbacks.Results;

/**
* An interface for the user to respond to a read request
*/
public interface RxBleGattReadResultMock {
/**
* Respond with success
* @param data The data to be returned in response to the read request
*/
void success(byte[] data);

/**
* Respond with failure
* @param status The ATT status (error code)
*/
void failure(int status);

/**
* Trigger a disconnection
* @param status The disconnection status (error code)
*/
void disconnect(int status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.polidea.rxandroidble2.mockrxandroidble.Callbacks.Results;
nrbrook marked this conversation as resolved.
Show resolved Hide resolved

/**
* An interface for the user to respond to a write request
*/
public interface RxBleGattWriteResultMock {
/**
* Respond with success
*/
void success();

/**
* Respond with failure
* @param status The ATT status (error code)
*/
void failure(int status);

/**
* Trigger a disconnection
* @param status The disconnection status (error code)
*/
void disconnect(int status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.polidea.rxandroidble2.mockrxandroidble.Callbacks;

import android.bluetooth.BluetoothGattCharacteristic;

import com.polidea.rxandroidble2.mockrxandroidble.Callbacks.Results.RxBleGattReadResultMock;
import com.polidea.rxandroidble2.mockrxandroidble.RxBleDeviceMock;

/**
* An interface for a user callback for handling characteristic read requests
*/
public interface RxBleCharacteristicReadCallback {

/**
* Handles a read on a GATT characteristic
* @param device the device being read from
* @param characteristic the characteristic being read from
* @param result the result handler
* @throws Exception on error
*/
void handle(RxBleDeviceMock device, BluetoothGattCharacteristic characteristic, RxBleGattReadResultMock result) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.polidea.rxandroidble2.mockrxandroidble.Callbacks;

import android.bluetooth.BluetoothGattCharacteristic;

import com.polidea.rxandroidble2.mockrxandroidble.Callbacks.Results.RxBleGattWriteResultMock;
import com.polidea.rxandroidble2.mockrxandroidble.RxBleDeviceMock;

/**
* An interface for a user callback for handling characteristic write requests
*/
public interface RxBleCharacteristicWriteCallback {

/**
* Handles a write on a GATT characteristic
* @param device the device being written to
* @param characteristic the characteristic being written to
* @param data the data being written
* @param result the result handler
* @throws Exception on error
*/
void handle(RxBleDeviceMock device,
BluetoothGattCharacteristic characteristic,
byte[] data,
RxBleGattWriteResultMock result) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.polidea.rxandroidble2.mockrxandroidble.Callbacks;

import android.bluetooth.BluetoothGattDescriptor;

import com.polidea.rxandroidble2.mockrxandroidble.Callbacks.Results.RxBleGattReadResultMock;
import com.polidea.rxandroidble2.mockrxandroidble.RxBleDeviceMock;

/**
* An interface for a user callback for handling descriptor read requests
*/
public interface RxBleDescriptorReadCallback {

/**
* Handles a read on a GATT descriptor
* @param device the device being read from
* @param descriptor the descriptor being read from
* @param result the result handler
* @throws Exception on error
*/
void handle(RxBleDeviceMock device, BluetoothGattDescriptor descriptor, RxBleGattReadResultMock result) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.polidea.rxandroidble2.mockrxandroidble.Callbacks;

import android.bluetooth.BluetoothGattDescriptor;

import com.polidea.rxandroidble2.mockrxandroidble.Callbacks.Results.RxBleGattWriteResultMock;
import com.polidea.rxandroidble2.mockrxandroidble.RxBleDeviceMock;

/**
* An interface for a user callback for handling descriptor write requests
*/
public interface RxBleDescriptorWriteCallback {

/**
* Handles a write on a GATT descriptor
* @param device the device being written to
* @param descriptor the descriptor being written to
* @param data the data being written
* @param result the result handler
* @throws Exception on error
*/
void handle(RxBleDeviceMock device, BluetoothGattDescriptor descriptor, byte[] data, RxBleGattWriteResultMock result) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,49 @@ public CharacteristicsBuilder() {
this.bluetoothGattCharacteristics = new ArrayList<>();
}

/**
* Adds a {@link BluetoothGattCharacteristic}
*
* @param characteristic The characteristic to add
*/
public CharacteristicsBuilder addCharacteristic(BluetoothGattCharacteristic characteristic) {
this.bluetoothGattCharacteristics.add(characteristic);
return this;
}

/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
* @param properties OR-ed {@link BluetoothGattCharacteristic} property constants
* @param descriptors list of characteristic descriptors. Use {@link DescriptorsBuilder} to create them.
*/
public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
@NonNull byte[] data,
int properties,
List<BluetoothGattDescriptor> descriptors) {
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(uuid, properties, 0);
for (BluetoothGattDescriptor descriptor : descriptors) {
characteristic.addDescriptor(descriptor);
}
characteristic.setValue(data);
return addCharacteristic(characteristic);
}

/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
* @param properties OR-ed {@link BluetoothGattCharacteristic} property constants
*/
public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
@NonNull byte[] data,
int properties) {
return addCharacteristic(uuid, data, properties, new ArrayList<BluetoothGattDescriptor>());
}

/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
Expand All @@ -115,6 +158,19 @@ public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
return addCharacteristic(uuid, data, 0, descriptors);
}

/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
* @param descriptors list of characteristic descriptors. Use {@link DescriptorsBuilder} to create them.
*/
public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
@NonNull byte[] data,
BluetoothGattDescriptor... descriptors) {
return addCharacteristic(uuid, data, 0, descriptors);
}

/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
Expand All @@ -126,14 +182,19 @@ public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
@NonNull byte[] data,
int properties,
List<BluetoothGattDescriptor> descriptors) {
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(uuid, properties, 0);
for (BluetoothGattDescriptor descriptor : descriptors) {
characteristic.addDescriptor(descriptor);
}
characteristic.setValue(data);
this.bluetoothGattCharacteristics.add(characteristic);
return this;
BluetoothGattDescriptor... descriptors) {
return addCharacteristic(uuid, data, properties, Arrays.asList(descriptors));
}

/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
*/
public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
@NonNull byte[] data) {
return addCharacteristic(uuid, data, 0);
}

/**
Expand All @@ -156,6 +217,16 @@ public DescriptorsBuilder() {
this.bluetoothGattDescriptors = new ArrayList<>();
}

/**
* Adds a {@link BluetoothGattDescriptor}.
*
* @param descriptor the descriptor
*/
public DescriptorsBuilder addDescriptor(@NonNull BluetoothGattDescriptor descriptor) {
bluetoothGattDescriptors.add(descriptor);
return this;
}

/**
* Adds a {@link BluetoothGattDescriptor} with specified parameters.
*
Expand All @@ -165,8 +236,7 @@ public DescriptorsBuilder() {
public DescriptorsBuilder addDescriptor(@NonNull UUID uuid, @NonNull byte[] data) {
BluetoothGattDescriptor bluetoothGattDescriptor = new BluetoothGattDescriptor(uuid, 0);
bluetoothGattDescriptor.setValue(data);
bluetoothGattDescriptors.add(bluetoothGattDescriptor);
return this;
return addDescriptor(bluetoothGattDescriptor);
}

/**
Expand Down