Skip to content

Commit 3688ca4

Browse files
refactor usage of TransactionIdGenerator - now it could be easily mocked for unit test
1 parent b972d81 commit 3688ca4

File tree

8 files changed

+66
-35
lines changed

8 files changed

+66
-35
lines changed

lib/characteristic.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Characteristic extends InternalCharacteristic {
2121
Service service;
2222

2323
ManagerForCharacteristic _manager;
24+
TransactionIdGenerator _transactionIdGenerator;
2425

2526
/// The UUID of this characteristic.
2627
String uuid;
@@ -41,9 +42,10 @@ class Characteristic extends InternalCharacteristic {
4142
bool isIndicatable;
4243

4344
Characteristic.fromJson(Map<String, dynamic> jsonObject, Service service,
44-
ManagerForCharacteristic manager)
45+
ManagerForCharacteristic manager, {TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()})
4546
: super(jsonObject[_CharacteristicMetadata.id]) {
4647
_manager = manager;
48+
_transactionIdGenerator = transactionIdGenerator;
4749
this.service = service;
4850
uuid = jsonObject[_CharacteristicMetadata.uuid];
4951
isReadable = jsonObject[_CharacteristicMetadata.isReadable];
@@ -62,7 +64,7 @@ class Characteristic extends InternalCharacteristic {
6264
_manager.readCharacteristicForIdentifier(
6365
service.peripheral,
6466
this,
65-
transactionId ?? TransactionIdGenerator.getNextId(),
67+
transactionId ?? _transactionIdGenerator.getNextId(),
6668
);
6769

6870
/// Writes to the value of this characteristic.
@@ -80,7 +82,7 @@ class Characteristic extends InternalCharacteristic {
8082
this,
8183
value,
8284
withResponse,
83-
transactionId ?? TransactionIdGenerator.getNextId(),
85+
transactionId ?? _transactionIdGenerator.getNextId(),
8486
);
8587

8688
/// Returns a [Stream] of notifications/indications emitted by this
@@ -95,7 +97,7 @@ class Characteristic extends InternalCharacteristic {
9597
_manager.monitorCharacteristicForIdentifier(
9698
service.peripheral,
9799
this,
98-
transactionId ?? TransactionIdGenerator.getNextId(),
100+
transactionId ?? _transactionIdGenerator.getNextId(),
99101
);
100102

101103
/// Returns a list of [Descriptor]s of this characteristic.
@@ -110,7 +112,7 @@ class Characteristic extends InternalCharacteristic {
110112
_manager.readDescriptorForCharacteristic(
111113
this,
112114
descriptorUuid,
113-
transactionId ?? TransactionIdGenerator.getNextId(),
115+
transactionId ?? _transactionIdGenerator.getNextId(),
114116
);
115117

116118
/// Writes the [value] of a [Descriptor] identified by [descriptorUuid].
@@ -123,7 +125,7 @@ class Characteristic extends InternalCharacteristic {
123125
this,
124126
descriptorUuid,
125127
value,
126-
transactionId ?? TransactionIdGenerator.getNextId(),
128+
transactionId ?? _transactionIdGenerator.getNextId(),
127129
);
128130

129131
@override

lib/descriptor.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@ abstract class _DescriptorMetadata {
88

99
class Descriptor extends InternalDescriptor {
1010
ManagerForDescriptor _manager;
11+
TransactionIdGenerator _transactionIdGenerator;
1112
Characteristic characteristic;
1213
String uuid;
1314

1415
Descriptor.fromJson(
1516
Map<String, dynamic> jsonObject,
1617
Characteristic characteristic,
1718
ManagerForDescriptor manager,
19+
{TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()}
1820
) : super(jsonObject[_DescriptorMetadata.id]) {
1921
_manager = manager;
22+
_transactionIdGenerator = transactionIdGenerator;
2023
this.characteristic = characteristic;
2124
uuid = jsonObject[_DescriptorMetadata.uuid];
2225
}
2326

2427
Future<Uint8List> read({String transactionId}) =>
2528
_manager.readDescriptorForIdentifier(
2629
this,
27-
transactionId ?? TransactionIdGenerator.getNextId(),
30+
transactionId ?? _transactionIdGenerator.getNextId(),
2831
);
2932

3033
Future<void> write(Uint8List value, {String transactionId}) =>
3134
_manager.writeDescriptorForIdentifier(
3235
this,
3336
value,
34-
transactionId ?? TransactionIdGenerator.getNextId(),
37+
transactionId ?? _transactionIdGenerator.getNextId(),
3538
);
3639

3740
@override

lib/peripheral.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ abstract class _PeripheralMetadata {
1616
class Peripheral {
1717
static const int NO_MTU_NEGOTIATION = 0;
1818
ManagerForPeripheral _manager;
19+
TransactionIdGenerator _transactionIdGenerator;
1920

2021
String name;
2122
String identifier;
2223

23-
Peripheral.fromJson(Map<String, dynamic> json, ManagerForPeripheral manager)
24+
Peripheral.fromJson(Map<String, dynamic> json, ManagerForPeripheral manager, {TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()})
2425
: _manager = manager,
2526
name = json[_PeripheralMetadata.name],
26-
identifier = json[_PeripheralMetadata.identifier];
27+
identifier = json[_PeripheralMetadata.identifier],
28+
_transactionIdGenerator = transactionIdGenerator;
2729

2830
/// Connects to the peripheral.
2931
///
@@ -82,7 +84,7 @@ class Peripheral {
8284
/// Optional [transactionId] could be used to cancel operation.
8385
Future<void> discoverAllServicesAndCharacteristics({String transactionId}) =>
8486
_manager.discoverAllServicesAndCharacteristics(
85-
this, transactionId ?? TransactionIdGenerator.getNextId());
87+
this, transactionId ?? _transactionIdGenerator.getNextId());
8688

8789
/// Returns a list of [Service]s of this peripheral.
8890
///
@@ -103,7 +105,7 @@ class Peripheral {
103105
///
104106
/// Optional [transactionId] could be used to cancel operation.
105107
Future<int> rssi({String transactionId}) =>
106-
_manager.rssi(this, transactionId ?? TransactionIdGenerator.getNextId());
108+
_manager.rssi(this, transactionId ?? _transactionIdGenerator.getNextId());
107109

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

123125
/// Reads value of [Characteristic] matching specified UUIDs.
124126
///
@@ -135,7 +137,7 @@ class Peripheral {
135137
this,
136138
serviceUuid,
137139
characteristicUuid,
138-
transactionId ?? TransactionIdGenerator.getNextId(),
140+
transactionId ?? _transactionIdGenerator.getNextId(),
139141
);
140142

141143
/// Writes value of [Characteristic] matching specified UUIDs.
@@ -157,7 +159,7 @@ class Peripheral {
157159
characteristicUuid,
158160
value,
159161
withResponse,
160-
transactionId ?? TransactionIdGenerator.getNextId(),
162+
transactionId ?? _transactionIdGenerator.getNextId(),
161163
);
162164

163165
/// Returns a list of [Descriptor]s for [Characteristic] matching specified UUIDs.
@@ -191,7 +193,7 @@ class Peripheral {
191193
serviceUuid,
192194
characteristicUuid,
193195
descriptorUuid,
194-
transactionId ?? TransactionIdGenerator.getNextId(),
196+
transactionId ?? _transactionIdGenerator.getNextId(),
195197
);
196198

197199
/// Writes value of [Descriptor] matching specified UUIDs.
@@ -214,7 +216,7 @@ class Peripheral {
214216
characteristicUuid,
215217
descriptorUuid,
216218
value,
217-
transactionId ?? TransactionIdGenerator.getNextId(),
219+
transactionId ?? _transactionIdGenerator.getNextId(),
218220
);
219221

220222
/// Returns a stream of notifications/indications from [Characteristic]
@@ -236,7 +238,7 @@ class Peripheral {
236238
this,
237239
serviceUuid,
238240
characteristicUuid,
239-
transactionId ?? TransactionIdGenerator.getNextId(),
241+
transactionId ?? _transactionIdGenerator.getNextId(),
240242
);
241243

242244
@override

lib/service.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Service extends InternalService {
1111
Peripheral peripheral;
1212

1313
ManagerForService _manager;
14+
TransactionIdGenerator _transactionIdGenerator;
1415

1516
/// The UUID of this service.
1617
String uuid;
@@ -19,10 +20,12 @@ class Service extends InternalService {
1920
Map<String, dynamic> jsonObject,
2021
Peripheral peripheral,
2122
ManagerForService managerForService,
23+
{TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()}
2224
) : super(jsonObject[_ServiceMetadata.id]) {
2325
this.peripheral = peripheral;
2426
uuid = jsonObject[_ServiceMetadata.uuid];
2527
_manager = managerForService;
28+
_transactionIdGenerator = transactionIdGenerator;
2629
}
2730

2831
/// Returns a list of [Characteristic]s of this service.
@@ -51,7 +54,7 @@ class Service extends InternalService {
5154
characteristicUuid,
5255
value,
5356
withResponse,
54-
transactionId ?? TransactionIdGenerator.getNextId());
57+
transactionId ?? _transactionIdGenerator.getNextId());
5558

5659
/// Reads the value of a [Characteristic] identified by [characteristicUuid].
5760
///
@@ -67,7 +70,7 @@ class Service extends InternalService {
6770
peripheral,
6871
this,
6972
characteristicUuid,
70-
transactionId ?? TransactionIdGenerator.getNextId(),
73+
transactionId ?? _transactionIdGenerator.getNextId(),
7174
);
7275

7376
/// Returns a [Stream] of values emitted by a [Characteristic] identified by
@@ -86,7 +89,7 @@ class Service extends InternalService {
8689
peripheral,
8790
this,
8891
characteristicUuid,
89-
transactionId ?? TransactionIdGenerator.getNextId(),
92+
transactionId ?? _transactionIdGenerator.getNextId(),
9093
);
9194

9295
/// Returns a list of [Descriptor]s of a [Characteristic] identified by
@@ -114,7 +117,7 @@ class Service extends InternalService {
114117
this,
115118
characteristicUuid,
116119
descriptorUuid,
117-
transactionId ?? TransactionIdGenerator.getNextId(),
120+
transactionId ?? _transactionIdGenerator.getNextId(),
118121
);
119122

120123
/// Writes the [value] of a [Descriptor] identified by [descriptorUuid]
@@ -132,7 +135,7 @@ class Service extends InternalService {
132135
characteristicUuid,
133136
descriptorUuid,
134137
value,
135-
transactionId ?? TransactionIdGenerator.getNextId(),
138+
transactionId ?? _transactionIdGenerator.getNextId(),
136139
);
137140

138141
@override

lib/src/internal_ble_manager.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ class InternalBleManager
88
ManagerForCharacteristic,
99
ManagerForDescriptor {
1010
FlutterBleLib _bleLib;
11+
TransactionIdGenerator _transactionIdGenerator;
1112

12-
InternalBleManager() {
13+
InternalBleManager({TransactionIdGenerator transactionIdGenerator = const TransactionIdGenerator()}) {
1314
_bleLib = FlutterBleLib();
1415
_bleLib.registerManager(this);
16+
_transactionIdGenerator = transactionIdGenerator;
1517
}
1618

1719
@override
@@ -36,11 +38,11 @@ class InternalBleManager
3638

3739
@override
3840
Future<void> enableRadio({String transactionId}) =>
39-
_bleLib.enableRadio(transactionId ?? TransactionIdGenerator.getNextId());
41+
_bleLib.enableRadio(transactionId ?? _transactionIdGenerator.getNextId());
4042

4143
@override
4244
Future<void> disableRadio({String transactionId}) =>
43-
_bleLib.disableRadio(transactionId ?? TransactionIdGenerator.getNextId());
45+
_bleLib.disableRadio(transactionId ?? _transactionIdGenerator.getNextId());
4446

4547
@override
4648
Future<BluetoothState> bluetoothState() => _bleLib.state();

lib/src/util/_transaction_id_generator.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
abstract class TransactionIdGenerator {
1+
class TransactionIdGenerator {
22
static int _id = 0;
33

4-
static String getNextId() {
4+
5+
const TransactionIdGenerator();
6+
7+
String getNextId() {
58
_id++;
69
return _id.toString();
710
}

test/peripheral_test.dart

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
import 'package:flutter/services.dart';
33
import 'package:flutter_ble_lib/flutter_ble_lib.dart';
44
import 'package:flutter_ble_lib/src/_managers_for_classes.dart';
5+
import 'package:flutter_ble_lib/src/util/_transaction_id_generator.dart';
56
import 'package:mockito/mockito.dart';
67
import 'package:test/test.dart';
78

89
class ManagerForPeripheralMock extends Mock implements ManagerForPeripheral {}
910
class ServiceMock extends Mock implements Service {}
1011
class CharacteristicMock extends Mock implements Characteristic {}
12+
class TransactionIdGeneratorMock extends Mock implements TransactionIdGenerator {}
1113

1214
void main() {
1315

1416
const PERIPHERAL_NAME = 'Peripheral name';
1517
const PERIPHERAL_ID = 'peripheral id';
1618

17-
ManagerForPeripheralMock managerForPeripheral = ManagerForPeripheralMock();
19+
ManagerForPeripheralMock managerForPeripheral;
20+
TransactionIdGeneratorMock transactionIdGeneratorMock;
1821
Peripheral peripheral;
1922

2023
setUp(() {
2124
Map<String, dynamic> json = {
2225
'name': PERIPHERAL_NAME,
2326
'id' : PERIPHERAL_ID
2427
};
25-
peripheral = Peripheral.fromJson(json, managerForPeripheral);
28+
managerForPeripheral = ManagerForPeripheralMock();
29+
transactionIdGeneratorMock = TransactionIdGeneratorMock();
30+
peripheral = Peripheral.fromJson(json, managerForPeripheral, transactionIdGenerator: transactionIdGeneratorMock);
2631
});
2732

2833
group("Connect", () {
@@ -126,13 +131,17 @@ void main() {
126131
});
127132

128133
test("use generated transactionId", () async {
134+
//given
135+
var ids = ["1", "5"];
136+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
137+
129138
//when
130139
await peripheral.discoverAllServicesAndCharacteristics();
131140
await peripheral.discoverAllServicesAndCharacteristics();
132141

133142
//then
134143
verify(managerForPeripheral.discoverAllServicesAndCharacteristics(any, '1'));
135-
verify(managerForPeripheral.discoverAllServicesAndCharacteristics(any, '2'));
144+
verify(managerForPeripheral.discoverAllServicesAndCharacteristics(any, '5'));
136145
});
137146
});
138147

@@ -181,13 +190,17 @@ void main() {
181190
});
182191

183192
test("use generated transactionId", () async {
193+
//given
194+
var ids = ["4", "9"];
195+
when(transactionIdGeneratorMock.getNextId()).thenAnswer((_) => ids.removeAt(0));
196+
184197
//when
185198
await peripheral.rssi();
186199
await peripheral.rssi();
187200

188201
//then
189-
verify(managerForPeripheral.rssi(any, '1'));
190-
verify(managerForPeripheral.rssi(any, '2'));
202+
verify(managerForPeripheral.rssi(any, '4'));
203+
verify(managerForPeripheral.rssi(any, '9'));
191204
});
192205
});
193206

test/src/util/transcation_id_generator_test.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import 'package:flutter_test/flutter_test.dart';
22
import 'package:flutter_ble_lib/src/util/_transaction_id_generator.dart';
33

44
void main() {
5+
6+
TransactionIdGenerator transactionIdGenerator = TransactionIdGenerator();
7+
58
test("should be able to generate an id", () {
6-
expect(TransactionIdGenerator.getNextId(), isNotNull);
9+
expect(transactionIdGenerator.getNextId(), isNotNull);
710
});
811

912
test("should always return unique values", () {
1013
List<String> generatedIds = [];
1114
for (var i = 0; i < 1000; i++) {
12-
var generatedId = TransactionIdGenerator.getNextId();
15+
var generatedId = transactionIdGenerator.getNextId();
1316
expect(generatedIds, isNot(contains(generatedId)));
1417
generatedIds.add(generatedId);
1518
}

0 commit comments

Comments
 (0)