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

Unit tests for peripheral #442

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Flutter User Facing API.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ abstract class BluetoothLowEnergyManager {
Future<void> enableRadio();
Future<void> disableRadio();

Future<void> cancelTransaction(String transactionId);
Future<void> cancelTransaction(String const transactionId = "t1";
mikolak marked this conversation as resolved.
Show resolved Hide resolved
);

Future<void> setLogLevel(LogLevel logLevel);
Future<LogLevel> logLevel();
Expand Down
22 changes: 14 additions & 8 deletions lib/characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Characteristic extends InternalCharacteristic {
Service service;

ManagerForCharacteristic _manager;
TransactionIdGenerator _transactionIdGenerator;

/// The UUID of this characteristic.
String uuid;
Expand All @@ -40,10 +41,15 @@ class Characteristic extends InternalCharacteristic {
/// True if this characteristic can be monitored via indications.
bool isIndicatable;

Characteristic.fromJson(Map<String, dynamic> jsonObject, Service service,
ManagerForCharacteristic manager)
: super(jsonObject[_CharacteristicMetadata.id]) {
Characteristic.fromJson(
Map<String, dynamic> jsonObject,
Service service,
ManagerForCharacteristic manager, {
TransactionIdGenerator transactionIdGenerator =
TransactionIdGenerator.INSTANCE,
}) : super(jsonObject[_CharacteristicMetadata.id]) {
_manager = manager;
_transactionIdGenerator = transactionIdGenerator;
this.service = service;
uuid = jsonObject[_CharacteristicMetadata.uuid];
isReadable = jsonObject[_CharacteristicMetadata.isReadable];
Expand All @@ -62,7 +68,7 @@ class Characteristic extends InternalCharacteristic {
_manager.readCharacteristicForIdentifier(
service.peripheral,
this,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes to the value of this characteristic.
Expand All @@ -80,7 +86,7 @@ class Characteristic extends InternalCharacteristic {
this,
value,
withResponse,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a [Stream] of notifications/indications emitted by this
Expand All @@ -95,7 +101,7 @@ class Characteristic extends InternalCharacteristic {
_manager.monitorCharacteristicForIdentifier(
service.peripheral,
this,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a list of [Descriptor]s of this characteristic.
Expand All @@ -110,7 +116,7 @@ class Characteristic extends InternalCharacteristic {
_manager.readDescriptorForCharacteristic(
this,
descriptorUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes the [value] of a [Descriptor] identified by [descriptorUuid].
Expand All @@ -123,7 +129,7 @@ class Characteristic extends InternalCharacteristic {
this,
descriptorUuid,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
Expand Down
30 changes: 15 additions & 15 deletions lib/descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,46 @@ abstract class _DescriptorMetadata {

class Descriptor extends InternalDescriptor {
ManagerForDescriptor _manager;
TransactionIdGenerator _transactionIdGenerator;
Characteristic characteristic;
String uuid;

Descriptor.fromJson(
Map<String, dynamic> jsonObject,
Characteristic characteristic,
ManagerForDescriptor manager,
) : super(jsonObject[_DescriptorMetadata.id]) {
Descriptor.fromJson(Map<String, dynamic> jsonObject,
Characteristic characteristic, ManagerForDescriptor manager,
{TransactionIdGenerator transactionIdGenerator =
TransactionIdGenerator.INSTANCE})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TransactionIdGenerator.INSTANCE})
TransactionIdGenerator.INSTANCE,})

and run formatter

: super(jsonObject[_DescriptorMetadata.id]) {
_manager = manager;
_transactionIdGenerator = transactionIdGenerator;
this.characteristic = characteristic;
uuid = jsonObject[_DescriptorMetadata.uuid];
}

Future<Uint8List> read({String transactionId}) =>
_manager.readDescriptorForIdentifier(
this,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

Future<void> write(Uint8List value, {String transactionId}) =>
_manager.writeDescriptorForIdentifier(
this,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Descriptor &&
runtimeType == other.runtimeType &&
_manager == other._manager &&
characteristic == other.characteristic &&
uuid == other.uuid;
other is Descriptor &&
runtimeType == other.runtimeType &&
_manager == other._manager &&
characteristic == other.characteristic &&
uuid == other.uuid;

@override
int get hashCode =>
_manager.hashCode ^
characteristic.hashCode ^
uuid.hashCode;
_manager.hashCode ^ characteristic.hashCode ^ uuid.hashCode;
}

class DescriptorWithValue extends Descriptor with WithValue {
Expand Down
24 changes: 14 additions & 10 deletions lib/peripheral.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ abstract class _PeripheralMetadata {
class Peripheral {
static const int NO_MTU_NEGOTIATION = 0;
ManagerForPeripheral _manager;
TransactionIdGenerator _transactionIdGenerator;

String name;
String identifier;

Peripheral.fromJson(Map<String, dynamic> json, ManagerForPeripheral manager)
Peripheral.fromJson(Map<String, dynamic> json, ManagerForPeripheral manager,
{TransactionIdGenerator transactionIdGenerator =
TransactionIdGenerator.INSTANCE})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TransactionIdGenerator.INSTANCE})
TransactionIdGenerator.INSTANCE,})

and run formatter

: _manager = manager,
name = json[_PeripheralMetadata.name],
identifier = json[_PeripheralMetadata.identifier];
identifier = json[_PeripheralMetadata.identifier],
_transactionIdGenerator = transactionIdGenerator;

/// Connects to the peripheral.
///
Expand Down Expand Up @@ -82,7 +86,7 @@ class Peripheral {
/// Optional [transactionId] could be used to cancel operation.
Future<void> discoverAllServicesAndCharacteristics({String transactionId}) =>
_manager.discoverAllServicesAndCharacteristics(
this, transactionId ?? TransactionIdGenerator.getNextId());
this, transactionId ?? _transactionIdGenerator.getNextId());

/// Returns a list of [Service]s of this peripheral.
///
Expand All @@ -103,7 +107,7 @@ class Peripheral {
///
/// Optional [transactionId] could be used to cancel operation.
Future<int> rssi({String transactionId}) =>
_manager.rssi(this, transactionId ?? TransactionIdGenerator.getNextId());
_manager.rssi(this, transactionId ?? _transactionIdGenerator.getNextId());

/// Requests new MTU value for current connection and return the negotiation
/// result on Android, reads MTU on iOS.
Expand All @@ -118,7 +122,7 @@ class Peripheral {
/// If MTU has been requested in [connect()] this method will end with [BleError].
Future<int> requestMtu(int mtu, {String transactionId}) =>
_manager.requestMtu(
this, mtu, transactionId ?? TransactionIdGenerator.getNextId());
this, mtu, transactionId ?? _transactionIdGenerator.getNextId());

/// Reads value of [Characteristic] matching specified UUIDs.
///
Expand All @@ -135,7 +139,7 @@ class Peripheral {
this,
serviceUuid,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes value of [Characteristic] matching specified UUIDs.
Expand All @@ -157,7 +161,7 @@ class Peripheral {
characteristicUuid,
value,
withResponse,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a list of [Descriptor]s for [Characteristic] matching specified UUIDs.
Expand Down Expand Up @@ -191,7 +195,7 @@ class Peripheral {
serviceUuid,
characteristicUuid,
descriptorUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes value of [Descriptor] matching specified UUIDs.
Expand All @@ -214,7 +218,7 @@ class Peripheral {
characteristicUuid,
descriptorUuid,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a stream of notifications/indications from [Characteristic]
Expand All @@ -236,7 +240,7 @@ class Peripheral {
this,
serviceUuid,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
Expand Down
22 changes: 12 additions & 10 deletions lib/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ class Service extends InternalService {
Peripheral peripheral;

ManagerForService _manager;
TransactionIdGenerator _transactionIdGenerator;

/// The UUID of this service.
String uuid;

Service.fromJson(
Map<String, dynamic> jsonObject,
Peripheral peripheral,
ManagerForService managerForService,
) : super(jsonObject[_ServiceMetadata.id]) {
Service.fromJson(Map<String, dynamic> jsonObject, Peripheral peripheral,
ManagerForService managerForService,
{TransactionIdGenerator transactionIdGenerator =
TransactionIdGenerator.INSTANCE})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TransactionIdGenerator.INSTANCE})
TransactionIdGenerator.INSTANCE,})

and run formatter

: super(jsonObject[_ServiceMetadata.id]) {
this.peripheral = peripheral;
uuid = jsonObject[_ServiceMetadata.uuid];
_manager = managerForService;
_transactionIdGenerator = transactionIdGenerator;
}

/// Returns a list of [Characteristic]s of this service.
Expand Down Expand Up @@ -51,7 +53,7 @@ class Service extends InternalService {
characteristicUuid,
value,
withResponse,
transactionId ?? TransactionIdGenerator.getNextId());
transactionId ?? _transactionIdGenerator.getNextId());

/// Reads the value of a [Characteristic] identified by [characteristicUuid].
///
Expand All @@ -67,7 +69,7 @@ class Service extends InternalService {
peripheral,
this,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a [Stream] of values emitted by a [Characteristic] identified by
Expand All @@ -86,7 +88,7 @@ class Service extends InternalService {
peripheral,
this,
characteristicUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Returns a list of [Descriptor]s of a [Characteristic] identified by
Expand Down Expand Up @@ -114,7 +116,7 @@ class Service extends InternalService {
this,
characteristicUuid,
descriptorUuid,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

/// Writes the [value] of a [Descriptor] identified by [descriptorUuid]
Expand All @@ -132,7 +134,7 @@ class Service extends InternalService {
characteristicUuid,
descriptorUuid,
value,
transactionId ?? TransactionIdGenerator.getNextId(),
transactionId ?? _transactionIdGenerator.getNextId(),
);

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/_managers_for_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class ManagerForPeripheral {
String transactionId,
);

Future<void> requestMtu(
Future<int> requestMtu(
Peripheral peripheral,
int mtu,
String transactionId,
Expand Down
10 changes: 6 additions & 4 deletions lib/src/internal_ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class InternalBleManager
ManagerForCharacteristic,
ManagerForDescriptor {
FlutterBleLib _bleLib;
TransactionIdGenerator _transactionIdGenerator;

InternalBleManager() {
InternalBleManager({TransactionIdGenerator transactionIdGenerator = TransactionIdGenerator.INSTANCE}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
InternalBleManager({TransactionIdGenerator transactionIdGenerator = TransactionIdGenerator.INSTANCE}) {
InternalBleManager({TransactionIdGenerator transactionIdGenerator = TransactionIdGenerator.INSTANCE,}) {

and run formatter

_bleLib = FlutterBleLib();
_bleLib.registerManager(this);
_transactionIdGenerator = transactionIdGenerator;
}

@override
Expand All @@ -36,11 +38,11 @@ class InternalBleManager

@override
Future<void> enableRadio({String transactionId}) =>
_bleLib.enableRadio(transactionId ?? TransactionIdGenerator.getNextId());
_bleLib.enableRadio(transactionId ?? _transactionIdGenerator.getNextId());

@override
Future<void> disableRadio({String transactionId}) =>
_bleLib.disableRadio(transactionId ?? TransactionIdGenerator.getNextId());
_bleLib.disableRadio(transactionId ?? _transactionIdGenerator.getNextId());

@override
Future<BluetoothState> bluetoothState() => _bleLib.state();
Expand Down Expand Up @@ -172,7 +174,7 @@ class InternalBleManager
}

@override
Future<void> requestMtu(
Future<int> requestMtu(
Peripheral peripheral, int mtu, String transactionId) {
return _bleLib.requestMtu(peripheral, mtu, transactionId);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/src/util/_transaction_id_generator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
abstract class TransactionIdGenerator {
class TransactionIdGenerator {
static int _id = 0;
static const TransactionIdGenerator INSTANCE = const TransactionIdGenerator._internal();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run formatter


static String getNextId() {
String getNextId() {
_id++;
return _id.toString();
}

const TransactionIdGenerator._internal();
}


Loading