-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_test.dart
111 lines (99 loc) · 2.76 KB
/
example_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'dart:async';
import 'package:rpi_gpio/src/rpi_gpio_impl.dart';
import 'package:test/test.dart';
import '../example/example_app.dart';
import 'mock_isolate.dart';
void main() {
RpiGpio? gpio;
late AckHandler ackHandler;
test('setup', () async {
ackHandler = AckHandler();
gpio = await RpiGpio.init(
isolateEntryPoint: isolateMockMain,
testSendPort: ackHandler.receivePort.sendPort,
);
ackHandler.gpio = gpio;
await ackHandler.isSetup.future;
});
test('example start', () async {
await runExample(gpio!, blink: Duration(milliseconds: 1), debounce: 1);
await ackHandler.is18Setup.future;
await ackHandler.is22Setup.future;
});
test('example blink', () async {
await ackHandler.is17Setup.future;
expect(ackHandler.write22Count, greaterThan(42)); // fixed blinking LED
expect(ackHandler.write18Count, greaterThan(6)); // PWM LED
});
test('example button', () async {
await ackHandler.is17Setup.future;
await ackHandler.isDisposed.future;
expect(ackHandler.buttonPolledCount, greaterThan(5));
expect(ackHandler.buttonToggledCount, greaterThan(5));
});
test('dispose', () async {
await ackHandler.isDisposed.future;
});
test('unexpected ack', () async {
// Allow time for unexpected gpio ack to propagate
await Future.delayed(const Duration(milliseconds: 4));
expect(ackHandler.unexpectedAck, isEmpty);
});
tearDownAll(() async {
await ackHandler.subscription.cancel();
ackHandler.receivePort.close();
});
}
class AckHandler extends BaseAckHandler {
final is17Setup = Completer();
final is18Setup = Completer();
final is22Setup = Completer();
var write18Count = 0;
var write22Count = 0;
var buttonState = false;
var buttonPolledCount = 0;
var buttonToggledCount = 0;
@override
void handleAck(List ack) {
switch (ack[0] as int) {
case setInputAck:
switch (ack[1] as int) {
case 17:
is17Setup.complete();
return;
}
break;
case readAck:
switch (ack[1] as int) {
case 17:
++buttonPolledCount;
if (gpio == null) return;
buttonState = !buttonState;
++buttonToggledCount;
gpio!.testCmd([17, buttonState]);
return;
}
break;
case setOutputAck:
switch (ack[1] as int) {
case 18:
is18Setup.complete();
return;
case 22:
is22Setup.complete();
return;
}
break;
case writeAck:
switch (ack[1] as int) {
case 18:
++write18Count;
return;
case 22:
++write22Count;
return;
}
}
super.handleAck(ack);
}
}