Skip to content

Commit e94fde5

Browse files
tests for peripheral
1 parent 3688ca4 commit e94fde5

File tree

3 files changed

+223
-9
lines changed

3 files changed

+223
-9
lines changed

lib/src/_managers_for_classes.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class ManagerForPeripheral {
3939
String transactionId,
4040
);
4141

42-
Future<void> requestMtu(
42+
Future<int> requestMtu(
4343
Peripheral peripheral,
4444
int mtu,
4545
String transactionId,

lib/src/internal_ble_manager.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class InternalBleManager
174174
}
175175

176176
@override
177-
Future<void> requestMtu(
177+
Future<int> requestMtu(
178178
Peripheral peripheral, int mtu, String transactionId) {
179179
return _bleLib.requestMtu(peripheral, mtu, transactionId);
180180
}

test/peripheral_test.dart

Lines changed: 221 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

2-
import 'package:flutter/services.dart';
2+
import 'dart:typed_data';
3+
34
import 'package:flutter_ble_lib/flutter_ble_lib.dart';
45
import 'package:flutter_ble_lib/src/_managers_for_classes.dart';
56
import 'package:flutter_ble_lib/src/util/_transaction_id_generator.dart';
@@ -10,6 +11,9 @@ class ManagerForPeripheralMock extends Mock implements ManagerForPeripheral {}
1011
class ServiceMock extends Mock implements Service {}
1112
class CharacteristicMock extends Mock implements Characteristic {}
1213
class TransactionIdGeneratorMock extends Mock implements TransactionIdGenerator {}
14+
class CharacteristicWithValueMock extends Mock implements CharacteristicWithValue {}
15+
class DescriptorMock extends Mock implements Descriptor {}
16+
class DescriptorWithValueMock extends Mock implements DescriptorWithValue {}
1317

1418
void main() {
1519

@@ -30,7 +34,7 @@ void main() {
3034
peripheral = Peripheral.fromJson(json, managerForPeripheral, transactionIdGenerator: transactionIdGeneratorMock);
3135
});
3236

33-
group("Connect", () {
37+
group("Connection", () {
3438
test('should pass defaut values to manager', () {
3539
//when
3640
peripheral.connect();
@@ -69,9 +73,7 @@ void main() {
6973
timeout: timeout
7074
));
7175
});
72-
});
7376

74-
group("Observe connection state", () {
7577
test("should emit on every connection state change", () {
7678
//given
7779
var states = [
@@ -89,7 +91,7 @@ void main() {
8991
expect(connectionStateStream, emitsInOrder(states));
9092
});
9193

92-
test("should pass arguments to manager", () {
94+
test("should pass arguments to manager for observing connection state", () {
9395
//given
9496
var emitCurrentValue = true;
9597
var completeOnDisconnect = true;
@@ -100,9 +102,15 @@ void main() {
100102
//then
101103
verify(managerForPeripheral.observePeripheralConnectionState(PERIPHERAL_ID, emitCurrentValue, completeOnDisconnect));
102104
});
103-
});
104105

105-
group("Connection checking", () {
106+
test('should call disconnect on manager with valid arguments', () {
107+
//when
108+
peripheral.disconnectOrCancelConnection();
109+
110+
//then
111+
verify(managerForPeripheral.disconnectOrCancelPeripheralConnection(PERIPHERAL_ID));
112+
});
113+
106114
[true, false].forEach((isConnected) {
107115
test("returns isConneted information based on $isConnected returned by manager", () async {
108116
//given
@@ -172,6 +180,102 @@ void main() {
172180
//then
173181
expect(fetchedCharacteristic, characteristics);
174182
});
183+
184+
test("schould return value of characteristic", () async {
185+
//given
186+
CharacteristicWithValue mockedCharacteristicWithValue = CharacteristicWithValueMock();
187+
var transactionId = "t1";
188+
when(managerForPeripheral.readCharacteristicForDevice(any, any, any, transactionId))
189+
.thenAnswer((_) => Future.value(mockedCharacteristicWithValue));
190+
191+
//when
192+
CharacteristicWithValue characteristicWithValue = await peripheral.readCharacteristic('s1', 'c1', transactionId: transactionId);
193+
194+
//then
195+
expect(characteristicWithValue, mockedCharacteristicWithValue);
196+
});
197+
198+
test("should use autogenerated transactionId during reading operation", () async {
199+
//given
200+
var ids = ["4", "9"];
201+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
202+
203+
//when
204+
await peripheral.readCharacteristic('s1', 'c1');
205+
await peripheral.readCharacteristic('s1', 'c1');
206+
207+
//then
208+
verify(managerForPeripheral.readCharacteristicForDevice(any, any, any, '4'));
209+
verify(managerForPeripheral.readCharacteristicForDevice(any, any, any, '9'));
210+
});
211+
212+
test("should write value to characteristic", () async {
213+
//given
214+
CharacteristicWithValue mockedCharacteristicWithValue = CharacteristicWithValueMock();
215+
const serviceUuid = 's1';
216+
const characteristicUuid = 'c1';
217+
const transactionId = 't1';
218+
Uint8List value = Uint8List.fromList([1, 4, 9]);
219+
const withResponse = false;
220+
221+
when(managerForPeripheral.writeCharacteristicForDevice(any, serviceUuid, characteristicUuid, value, withResponse, transactionId))
222+
.thenAnswer((_) => Future.value(mockedCharacteristicWithValue));
223+
224+
//when
225+
CharacteristicWithValue characteristicWithValue = await peripheral.writeCharacteristic(serviceUuid, characteristicUuid, value, withResponse, transactionId: transactionId);
226+
227+
//then
228+
expect(characteristicWithValue, mockedCharacteristicWithValue);
229+
});
230+
231+
test("should use autogenerated transactionId during writing operation", () async {
232+
//given
233+
var ids = ["4", "9"];
234+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
235+
236+
//when
237+
await peripheral.writeCharacteristic('s1', 'c1', Uint8List.fromList([1]), false);
238+
await peripheral.writeCharacteristic('s1', 'c1', Uint8List.fromList([1]), false);
239+
240+
//then
241+
verify(managerForPeripheral.writeCharacteristicForDevice(any, any, any, any, any, '4'));
242+
verify(managerForPeripheral.writeCharacteristicForDevice(any, any, any, any, any, '9'));
243+
});
244+
245+
test("should return values of monitored characteristic", () {
246+
//given
247+
var emittedValues = [
248+
CharacteristicWithValueMock(),
249+
CharacteristicWithValueMock(),
250+
CharacteristicWithValueMock()
251+
];
252+
const serviceUuid = 's1';
253+
const characteristicUuid = 'c1';
254+
const transactionId = 't1';
255+
when(managerForPeripheral.monitorCharacteristicForDevice(any, serviceUuid, characteristicUuid, transactionId))
256+
.thenAnswer((_) => Stream.fromIterable(emittedValues));
257+
258+
//when
259+
Stream<CharacteristicWithValue> characteristicsStream = peripheral.monitorCharacteristic(serviceUuid, characteristicUuid, transactionId: transactionId);
260+
261+
//then
262+
expect(characteristicsStream, emitsInOrder(emittedValues));
263+
});
264+
265+
test("should use autogenerated transactionId for monitoring operation", () {
266+
//given
267+
var ids = ["4", "9"];
268+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
269+
270+
//when
271+
peripheral.monitorCharacteristic('s1', 'c1');
272+
peripheral.monitorCharacteristic('s1', 'c1');
273+
274+
//then
275+
verify(managerForPeripheral.monitorCharacteristicForDevice(any, any, any, '4'));
276+
verify(managerForPeripheral.monitorCharacteristicForDevice(any, any, any, '9'));
277+
});
278+
175279
});
176280

177281
group("Rssi", () {
@@ -204,4 +308,114 @@ void main() {
204308
});
205309
});
206310

311+
group("MTU", () {
312+
test("returns negotiated MTU", () async {
313+
//given
314+
const requestedMtu = 23;
315+
const negotiatedMtu = 135;
316+
when(managerForPeripheral.requestMtu(any, requestedMtu, any)).thenAnswer((_) => Future.value(negotiatedMtu));
317+
318+
//when
319+
int obtainedMtu = await peripheral.requestMtu(requestedMtu);
320+
321+
//then
322+
expect(negotiatedMtu, obtainedMtu);
323+
});
324+
325+
test("use generated transactionId", () async {
326+
//given
327+
var ids = ["4", "9"];
328+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
329+
330+
//when
331+
await peripheral.requestMtu(23);
332+
await peripheral.requestMtu(45);
333+
334+
//then
335+
verify(managerForPeripheral.requestMtu(any, any, '4'));
336+
verify(managerForPeripheral.requestMtu(any, any, '9'));
337+
});
338+
});
339+
340+
group("Descriptor", () {
341+
test("should return desriptors of characteristic", () async {
342+
//given
343+
List<Descriptor> descriptors = [DescriptorMock(), DescriptorMock()];
344+
const serviceUuid = "123uuid";
345+
const characteristicUuid = "c1";
346+
when(managerForPeripheral.descriptorsForPeripheral(any, serviceUuid, characteristicUuid)).thenAnswer((_) => Future.value(descriptors));
347+
348+
//when
349+
List<Descriptor> fetchedDescriptors = await peripheral.descriptorsForCharacteristic(serviceUuid, characteristicUuid);
350+
351+
//then
352+
expect(fetchedDescriptors, descriptors);
353+
});
354+
355+
test("should return Descriptor value", () async {
356+
//given
357+
DescriptorWithValue descriptorWithValue = DescriptorWithValueMock();
358+
const serviceUuid = "123uuid";
359+
const characteristicUuid = "c1";
360+
const descriptorUuid = "c1";
361+
const transactionId = "t1";
362+
when(managerForPeripheral.readDescriptorForPeripheral(any, serviceUuid, characteristicUuid, descriptorUuid, transactionId))
363+
.thenAnswer((_) => Future.value(descriptorWithValue));
364+
365+
//when
366+
DescriptorWithValueMock obtainedDescriptorWithValue = await peripheral.readDescriptor(serviceUuid, characteristicUuid, descriptorUuid, transactionId: transactionId);
367+
368+
//then
369+
expect(obtainedDescriptorWithValue, descriptorWithValue);
370+
});
371+
372+
test("should use autogenerated transactionId during reading operation", () async {
373+
//given
374+
var ids = ["4", "9"];
375+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
376+
377+
//when
378+
await peripheral.readDescriptor('s1', 'c1', 'd1');
379+
await peripheral.readDescriptor('s1', 'c1', 'd1');
380+
381+
//then
382+
verify(managerForPeripheral.readDescriptorForPeripheral(any, any, any, any, '4'));
383+
verify(managerForPeripheral.readDescriptorForPeripheral(any, any, any, any, '9'));
384+
});
385+
386+
test("should write value to descriptor", () async {
387+
//given
388+
DescriptorWithValue descriptorWithValue = DescriptorWithValueMock();
389+
const serviceUuid = "123uuid";
390+
const characteristicUuid = "c1";
391+
const descriptorUuid = "c1";
392+
const transactionId = "t1";
393+
Uint8List value = Uint8List.fromList([1, 4, 9]);
394+
395+
when(managerForPeripheral.writeDescriptorForPeripheral(any, serviceUuid, characteristicUuid, descriptorUuid, value, transactionId))
396+
.thenAnswer((_) => Future.value(descriptorWithValue));
397+
398+
//when
399+
DescriptorWithValue obtainedDescriptorWithValue = await peripheral.writeDescriptor(serviceUuid, characteristicUuid, descriptorUuid, value, transactionId: transactionId);
400+
401+
//then
402+
expect(obtainedDescriptorWithValue, descriptorWithValue);
403+
});
404+
405+
test("should use autogenerated transactionId during writing operation", () async {
406+
//given
407+
var ids = ["4", "9"];
408+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
409+
410+
//when
411+
await peripheral.writeDescriptor('s1', 'c1', 'd1', Uint8List.fromList([1]));
412+
await peripheral.writeDescriptor('s1', 'c1', 'd1', Uint8List.fromList([1]));
413+
414+
//then
415+
verify(managerForPeripheral.writeDescriptorForPeripheral(any, any, any, any, any, '4'));
416+
verify(managerForPeripheral.writeDescriptorForPeripheral(any, any, any, any, any, '9'));
417+
});
418+
});
419+
420+
207421
}

0 commit comments

Comments
 (0)