|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'package:flutter_ble_lib/flutter_ble_lib.dart'; |
| 5 | +import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; |
| 6 | +import 'package:mockito/mockito.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +import 'mock/mocks.dart'; |
| 10 | +import 'test_util/descriptor_generator.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + ManagerForDescriptor managerForDescriptor = ManagerForDescriptorMock(); |
| 14 | + DescriptorGenerator descriptorGenerator = |
| 15 | + DescriptorGenerator(managerForDescriptor); |
| 16 | + |
| 17 | + DescriptorWithValue createDescriptor(int seed) => |
| 18 | + descriptorGenerator.create(seed, CharacteristicMock()); |
| 19 | + |
| 20 | + Descriptor descriptor = createDescriptor(123); |
| 21 | + |
| 22 | + tearDown(() { |
| 23 | + clearInteractions(managerForDescriptor); |
| 24 | + }); |
| 25 | + |
| 26 | + test("read returns expected value", () async { |
| 27 | + //given |
| 28 | + when(managerForDescriptor.readDescriptorForIdentifier(descriptor, "456")) |
| 29 | + .thenAnswer((_) => Future.value(Uint8List.fromList([1, 2, 3, 4]))); |
| 30 | + |
| 31 | + //when |
| 32 | + var value = await descriptor.read(transactionId: "456"); |
| 33 | + |
| 34 | + //then |
| 35 | + expect(value, equals(Uint8List.fromList([1, 2, 3, 4]))); |
| 36 | + }); |
| 37 | + |
| 38 | + test( |
| 39 | + "read invokes manager with expected params when transactionId is specified", |
| 40 | + () { |
| 41 | + //when |
| 42 | + descriptor.read(transactionId: "456"); |
| 43 | + |
| 44 | + //then |
| 45 | + verify( |
| 46 | + managerForDescriptor.readDescriptorForIdentifier(descriptor, "456"), |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + test( |
| 51 | + "read invokes manager with expected params when transactionId is not specified", |
| 52 | + () { |
| 53 | + //when |
| 54 | + descriptor.read(); |
| 55 | + |
| 56 | + //then |
| 57 | + verify( |
| 58 | + managerForDescriptor.readDescriptorForIdentifier( |
| 59 | + descriptor, argThat(isNotNull)), |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + test( |
| 64 | + "read invokes manager with unique transactionId when transactionId is not specified", |
| 65 | + () { |
| 66 | + //when |
| 67 | + descriptor.read(); |
| 68 | + descriptor.read(); |
| 69 | + |
| 70 | + //then |
| 71 | + var transactionIds = verify( |
| 72 | + managerForDescriptor.readDescriptorForIdentifier( |
| 73 | + descriptor, captureThat(isNotNull)), |
| 74 | + ).captured; |
| 75 | + expect(transactionIds[0], isNot(equals(transactionIds[1]))); |
| 76 | + }); |
| 77 | + |
| 78 | + test( |
| 79 | + "write invokes manager with expected params when transactionId is specified", |
| 80 | + () { |
| 81 | + //when |
| 82 | + descriptor.write(Uint8List.fromList([1, 2, 3, 4]), transactionId: "456"); |
| 83 | + |
| 84 | + //then |
| 85 | + verify( |
| 86 | + managerForDescriptor.writeDescriptorForIdentifier( |
| 87 | + descriptor, Uint8List.fromList([1, 2, 3, 4]), "456"), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + test( |
| 92 | + "write invokes manager with expected params when transactionId is not specified", |
| 93 | + () { |
| 94 | + //when |
| 95 | + descriptor.write(Uint8List.fromList([1, 2, 3, 4])); |
| 96 | + |
| 97 | + //then |
| 98 | + verify( |
| 99 | + managerForDescriptor.writeDescriptorForIdentifier( |
| 100 | + descriptor, Uint8List.fromList([1, 2, 3, 4]), argThat(isNotNull)), |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + test( |
| 105 | + "write invokes manager with unique transactionId when transactionId is not specified", |
| 106 | + () { |
| 107 | + //when |
| 108 | + descriptor.write(Uint8List.fromList([1, 2, 3, 4])); |
| 109 | + descriptor.write(Uint8List.fromList([1, 2, 3, 4])); |
| 110 | + |
| 111 | + //then |
| 112 | + var transactionIds = verify( |
| 113 | + managerForDescriptor.writeDescriptorForIdentifier(descriptor, |
| 114 | + Uint8List.fromList([1, 2, 3, 4]), captureThat(isNotNull))) |
| 115 | + .captured; |
| 116 | + expect(transactionIds[0], isNot(equals(transactionIds[1]))); |
| 117 | + }); |
| 118 | +} |
0 commit comments