-
Notifications
You must be signed in to change notification settings - Fork 17
/
LittleBlueToothTests.xctest.coverage.txt
2500 lines (2489 loc) · 123 KB
/
LittleBlueToothTests.xctest.coverage.txt
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/Users/Andrea/Documents/GitHub/LittleBlueTooth/Tests/LittleBlueToothTests/ConnectionTest.swift:
1| |//
2| |// ConnectionTest.swift
3| |// LittleBlueToothTests
4| |//
5| |// Created by Andrea Finollo on 29/06/2020.
6| |// Copyright © 2020 Andrea Finollo. All rights reserved.
7| |//
8| |
9| |import XCTest
10| |import CoreBluetoothMock
11| |import Combine
12| |@testable import LittleBlueToothForTest
13| |
14| |class ConnectionTest: LittleBlueToothTests {
15| |
16| 9| override func setUpWithError() throws {
17| 9| // Put setup code here. This method is called before the invocation of each test method in the class.
18| 9| try super.setUpWithError()
19| 9| var configuration = LittleBluetoothConfiguration()
20| 9| configuration.isLogEnabled = true
21| 9| littleBT = LittleBlueTooth(with: configuration)
22| 9| }
23| |
24| 9| override func tearDownWithError() throws {
25| 9| // Put teardown code here. This method is called after the invocation of each test method in the class.
26| 9| }
27| |
28| 1| func testPeripheralConnectionSuccess() {
29| 1| disposeBag.removeAll()
30| 1|
31| 1| blinky.simulateProximityChange(.immediate)
32| 1| let connectionExpectation = expectation(description: "Connection expectation")
33| 1|
34| 1| var connectedPeripheral: Peripheral?
35| 1|
36| 1| littleBT.startDiscovery(withServices: nil)
37| 1| .flatMap { discovery in
38| 1| self.littleBT.connect(to: discovery)
39| 1| }
40| 1| .sink(receiveCompletion: { completion in
41| 0| print("Completion \(completion)")
42| 1| }) { (connectedPeriph) in
43| 1| print("Discovery \(connectedPeriph)")
44| 1| connectedPeripheral = connectedPeriph
45| 1| self.littleBT.disconnect().sink(receiveCompletion: { _ in
46| 1| }) { _ in
47| 1| connectionExpectation.fulfill()
48| 1| }
49| 1| .store(in: &self.disposeBag)
50| 1| }
51| 1| .store(in: &disposeBag)
52| 1|
53| 1| waitForExpectations(timeout: 15)
54| 1| XCTAssertNotNil(connectedPeripheral)
55| 1| XCTAssertEqual(connectedPeripheral!.cbPeripheral.identifier, blinky.identifier)
56| 1|
57| 1| }
58| |
59| 1| func testPeripheralConnectionReadRSSI() {
60| 1| disposeBag.removeAll()
61| 1|
62| 1| blinky.simulateProximityChange(.near)
63| 1| let readRSSIExpectation = expectation(description: "Read RSSI expectation")
64| 1|
65| 1| var rssiRead: Int?
66| 1|
67| 1| littleBT.startDiscovery(withServices: nil)
68| 1| .flatMap { discovery in
69| 1| self.littleBT.connect(to: discovery)
70| 1| }
71| 1| .flatMap{ _ in
72| 1| self.littleBT.readRSSI()
73| 1| }
74| 1| .sink(receiveCompletion: { completion in
75| 0| print("Completion \(completion)")
76| 1| }) { (rssi) in
77| 1| print("RSSI \(rssi)")
78| 1| rssiRead = rssi
79| 1| self.littleBT.disconnect().sink(receiveCompletion: { _ in
80| 1| }) { _ in
81| 1| readRSSIExpectation.fulfill()
82| 1| }
83| 1| .store(in: &self.disposeBag)
84| 1| }
85| 1| .store(in: &disposeBag)
86| 1|
87| 1| waitForExpectations(timeout: 15)
88| 1| XCTAssertNotNil(rssiRead)
89| 1| XCTAssert(rssiRead! < 70)
90| 1|
91| 1| }
92| |
93| |
94| 1| func testMultipleConnection() {
95| 1| disposeBag.removeAll()
96| 1|
97| 1| blinky.simulateProximityChange(.immediate)
98| 1| let connectionExpectation = expectation(description: "Multiple connection")
99| 1|
100| 1| var isAlreadyConnected = false
101| 1|
102| 1| littleBT.startDiscovery(withServices: nil)
103| 1| .flatMap { discovery in
104| 1| self.littleBT.connect(to: discovery).map {_ in discovery}
105| 1| }
106| 1| .flatMap { discovery in
107| 1| self.littleBT.connect(to: discovery)
108| 1| }
109| 1| .sink(receiveCompletion: { completion in
110| 1| print("Completion \(completion)")
111| 1| switch completion {
112| 1| case .finished:
113| 0| break
114| 1| case let .failure(error):
115| 1| if case LittleBluetoothError.peripheralAlreadyConnectedOrConnecting(_) = error {
116| 1| isAlreadyConnected = true
117| 1| connectionExpectation.fulfill()
118| 1| self.littleBT.disconnect()
119| 1| }
120| 1| }
121| 1| }) { (connectedPeriph) in
122| 0| print("Discovery \(connectedPeriph)")
123| 0| }
124| 1| .store(in: &disposeBag)
125| 1| waitForExpectations(timeout: 10)
126| 1| XCTAssert(isAlreadyConnected)
127| 1| }
128| |
129| 1| func testConnectionDisconnectionObserving() {
130| 1| disposeBag.removeAll()
131| 1|
132| 1| blinky.simulateProximityChange(.immediate)
133| 1| var connectionEvent = [ConnectionEvent]()
134| 1| var peripheralState = [PeripheralState]()
135| 1| let connectionExpectation = expectation(description: "Connection test")
136| 1|
137| 1| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4)) {
138| 1| blinky.simulateDisconnection()
139| 1| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
140| 1| connectionExpectation.fulfill()
141| 1| }
142| 1| }
143| 1|
144| 1| littleBT.startDiscovery(withServices: nil)
145| 1| .flatMap { discovery in
146| 1| self.littleBT.connect(to: discovery)
147| 1| }
148| 1| .sink(receiveCompletion: { completion in
149| 0| print("Completion \(completion)")
150| 1| }) { (connectedPeriph) in
151| 1| print("Discovery \(connectedPeriph)")
152| 1| }
153| 1| .store(in: &disposeBag)
154| 1|
155| 1| littleBT.connectionEventPublisher
156| 3| .sink { (event) in
157| 3| print("ConnectionEvent \(event)")
158| 3| connectionEvent.append(event)
159| 3| }
160| 1| .store(in: &disposeBag)
161| 1|
162| 1| littleBT.peripheralStatePublisher
163| 677| .sink { (state) in
164| 677| print("Peripheral state: \(state)")
165| 677| peripheralState.append(state)
166| 677| }
167| 1| .store(in: &disposeBag)
168| 1|
169| 1| waitForExpectations(timeout: 30)
170| 1| print("Connection disconnection event \(connectionEvent.count)")
171| 1| XCTAssert(connectionEvent.count == 3)
172| 1|
173| 1| }
174| |
175| |
176| 1| func testChangeDeviceName() {
177| 1| disposeBag.removeAll()
178| 1|
179| 1| blinky.simulateProximityChange(.immediate)
180| 1|
181| 1| let changeNameAndServiceExpectation = expectation(description: "Change name test")
182| 1|
183| 1| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4)) {
184| 1| blinky.simulateServiceChange(newName: "pippo",
185| 1| newServices: [.blinkyService])
186| 1| }
187| 1| let charateristic = LittleBlueToothCharacteristic(characteristic: CBMUUID.ledCharacteristic.uuidString, for: CBMUUID.nordicBlinkyService.uuidString, properties: [.notify, .read, .write])
188| 1|
189| 1|
190| 1| littleBT.startDiscovery(withServices: nil)
191| 1| .flatMap { discovery in
192| 1| self.littleBT.connect(to: discovery)
193| 1| }
194| 1| .flatMap { _ -> AnyPublisher<LedState, LittleBluetoothError> in
195| 1| self.littleBT.read(from: charateristic)
196| 1| }
197| 1| .sink(receiveCompletion: { completion in
198| 0| print("Completion \(completion)")
199| 1| }) { (connectedPeriph) in
200| 1| print("Discovery \(connectedPeriph)")
201| 1| }
202| 1| .store(in: &disposeBag)
203| 1|
204| 1| littleBT.changesStatePublisher
205| 2| .sink { (state) in
206| 2| print("Peripheral state: \(state)")
207| 2| switch state {
208| 2| case let .invalidatedServices(services):
209| 1| print("Invalidated services \(services)")
210| 2| case let .name(newName):
211| 1| print("New Name: \(String(describing: newName))")
212| 1| self.littleBT.disconnect()
213| 1| changeNameAndServiceExpectation.fulfill()
214| 2| }
215| 2| }
216| 1| .store(in: &disposeBag)
217| 1|
218| 1| waitForExpectations(timeout: 30)
219| 1| }
220| |
221| |
222| 1| func testDisconnection() {
223| 1| disposeBag.removeAll()
224| 1|
225| 1| blinky.simulateProximityChange(.immediate)
226| 1|
227| 1| let disconnectionExpectation = expectation(description: "Disconnection expectation")
228| 1|
229| 1| var isDisconnected = false
230| 1|
231| 1|
232| 1| littleBT.startDiscovery(withServices: nil)
233| 1| .flatMap { discovery in
234| 1| self.littleBT.connect(to: discovery)
235| 1| }
236| 1| .flatMap { _ in
237| 1| self.littleBT.disconnect()
238| 1| }
239| 1| .sink(receiveCompletion: { completion in
240| 0| print("Completion \(completion)")
241| 1| }) { (disconnectedPeriph) in
242| 1| print("Disconnection \(disconnectedPeriph)")
243| 1| isDisconnected = true
244| 1| disconnectionExpectation.fulfill()
245| 1| }
246| 1| .store(in: &disposeBag)
247| 1|
248| 1| waitForExpectations(timeout: 10)
249| 1| XCTAssert(isDisconnected)
250| 1| }
251| |
252| |
253| 1| func testAutoConnection() {
254| 1| disposeBag.removeAll()
255| 1|
256| 1| blinky.simulateProximityChange(.immediate)
257| 1| var connectionEvent = [ConnectionEvent]()
258| 1| var peripheralState = [PeripheralState]()
259| 1| let connectionExpectation = expectation(description: "Connection test")
260| 1| littleBT.autoconnectionHandler = { (peripheral, error) -> Bool in
261| 1| return true
262| 1| }
263| 1|
264| 1| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
265| 1| blinky.simulateReset()
266| 1| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(10)) {
267| 1| connectionExpectation.fulfill()
268| 1| }
269| 1| }
270| 1|
271| 1| littleBT.startDiscovery(withServices: nil)
272| 1| .flatMap { discovery in
273| 1| self.littleBT.connect(to: discovery)
274| 1| }
275| 1| .sink(receiveCompletion: { completion in
276| 0| print("Completion \(completion)")
277| 1| }) { (connectedPeriph) in
278| 1| print("Discovery \(connectedPeriph)")
279| 1| }
280| 1| .store(in: &disposeBag)
281| 1|
282| 1| littleBT.connectionEventPublisher
283| 6| .sink { (event) in
284| 6| print("ConnectionEvent \(event)")
285| 6| connectionEvent.append(event)
286| 6| }
287| 1| .store(in: &disposeBag)
288| 1|
289| 1| littleBT.peripheralStatePublisher
290| 758| .sink { (state) in
291| 758| print("Peripheral state: \(state)")
292| 758| peripheralState.append(state)
293| 758| }
294| 1| .store(in: &disposeBag)
295| 1|
296| 1| waitForExpectations(timeout: 100)
297| 1| self.littleBT.autoconnectionHandler = nil
298| 1| self.littleBT.disconnect()
299| 1| print("Autoconn event \(connectionEvent.count)")
300| 1| XCTAssert(connectionEvent.count == 5)
301| 1| }
302| |
303| 1| func testPeripheralConnectionInitializationSuccess() {
304| 1| disposeBag.removeAll()
305| 1|
306| 1| blinky.simulateProximityChange(.immediate)
307| 1| let connectionExpectation = expectation(description: "Connection expectation")
308| 1| let charateristic = LittleBlueToothCharacteristic(characteristic: CBMUUID.ledCharacteristic.uuidString, for: CBMUUID.nordicBlinkyService.uuidString, properties: [.notify, .read, .write])
309| 1|
310| 1| var ledState: LedState?
311| 1|
312| 1| littleBT.connectionTasks = Just(()).setFailureType(to: LittleBluetoothError.self)
313| 1| .flatMap{ _ -> AnyPublisher<LedState, LittleBluetoothError> in
314| 1| self.littleBT.read(from: charateristic)
315| 1| }.map { state in
316| 1| ledState = state
317| 1| return ()
318| 1| }.eraseToAnyPublisher()
319| 1|
320| 1|
321| 1| var connectedPeripheral: Peripheral?
322| 1|
323| 1| littleBT.startDiscovery(withServices: nil)
324| 1| .flatMap { discovery in
325| 1| self.littleBT.connect(to: discovery)
326| 1| }
327| 1| .sink(receiveCompletion: { completion in
328| 0| print("Completion \(completion)")
329| 1| }) { (connectedPeriph) in
330| 1| print("Discovery \(connectedPeriph)")
331| 1| connectedPeripheral = connectedPeriph
332| 1| self.littleBT.disconnect().sink(receiveCompletion: { _ in
333| 1| }) { _ in
334| 1| connectionExpectation.fulfill()
335| 1| }
336| 1| .store(in: &self.disposeBag)
337| 1| }
338| 1| .store(in: &disposeBag)
339| 1|
340| 1| waitForExpectations(timeout: 15)
341| 1| littleBT.connectionTasks = nil
342| 1| XCTAssertNotNil(connectedPeripheral)
343| 1| XCTAssertNotNil(ledState)
344| 1| XCTAssert(!ledState!.isOn)
345| 1| XCTAssertEqual(connectedPeripheral!.cbPeripheral.identifier, blinky.identifier)
346| 1| }
347| |
348| 1| func testConnectionFailed() {
349| 1| disposeBag.removeAll()
350| 1|
351| 1| blinky.simulateProximityChange(.immediate)
352| 1| blinky.simulateReset()
353| 1|
354| 1| let foundExpectation = XCTestExpectation(description: "Device found expectation")
355| 1|
356| 1| var discovery: PeripheralDiscovery?
357| 1|
358| 1| littleBT.startDiscovery(withServices: nil)
359| 1| .sink(receiveCompletion: { completion in
360| 0| print("Completion \(completion)")
361| 1| }) { (disc) in
362| 1| print("Discovery \(disc)")
363| 1| discovery = disc
364| 1| foundExpectation.fulfill()
365| 1| }
366| 1| .store(in: &disposeBag)
367| 1| wait(for: [foundExpectation], timeout: 3)
368| 1| XCTAssertNotNil(discovery)
369| 1|
370| 1| blinky.simulateProximityChange(.outOfRange)
371| 1| // Should never happen
372| 1| let connected = XCTestExpectation(description: "Connected expectation")
373| 1| connected.isInverted = true
374| 1|
375| 1| littleBT.connect(to: discovery!)
376| 1| .sink(receiveCompletion: { (completion) in
377| 1| print("Completion \(completion)")
378| 1| }) { (periph) in
379| 0| print("Peripheral \(periph)")
380| 0| connected.fulfill()
381| 0| }
382| 1| .store(in: &disposeBag)
383| 1|
384| 1| wait(for: [connected], timeout: 3)
385| 1| littleBT.disconnect()
386| 1| }
387| |}
/Users/Andrea/Documents/GitHub/LittleBlueTooth/Tests/LittleBlueToothTests/CustomOperator.swift:
1| |//
2| |// CustomOperator.swift
3| |// LittleBlueToothTests
4| |//
5| |// Created by Andrea Finollo on 27/08/2020.
6| |//
7| |
8| |import XCTest
9| |import CoreBluetoothMock
10| |import Combine
11| |@testable import LittleBlueToothForTest
12| |
13| |class CustomOperator: LittleBlueToothTests {
14| |
15| 11| override func setUpWithError() throws {
16| 11| // Put setup code here. This method is called before the invocation of each test method in the class.
17| 11| try super.setUpWithError()
18| 11| var configuration = LittleBluetoothConfiguration()
19| 11| configuration.isLogEnabled = true
20| 11| littleBT = LittleBlueTooth(with: configuration)
21| 11| }
22| |
23| 11| override func tearDownWithError() throws {
24| 11| // Put teardown code here. This method is called after the invocation of each test method in the class.
25| 11| }
26| |
27| | /// Connection custom operator test
28| 1| func testConnectionOperatorFromScanDiscovery() {
29| 1| disposeBag.removeAll()
30| 1|
31| 1| blinky.simulateProximityChange(.immediate)
32| 1| let connectionExpectation = expectation(description: "Connection expectation")
33| 1|
34| 1| var connectedPeripheral: Peripheral?
35| 1|
36| 1| StartLittleBlueTooth
37| 1| .startDiscovery(for: self.littleBT, withServices: nil)
38| 1| .connect(for: self.littleBT)
39| 1| .sink(receiveCompletion: { completion in
40| 0| print("Completion \(completion)")
41| 1| }) { (connectedPeriph) in
42| 1| print("Discovery \(connectedPeriph)")
43| 1| connectedPeripheral = connectedPeriph
44| 1| self.littleBT.disconnect()
45| 1| .delay(for: .seconds(5), scheduler: DispatchQueue.global())
46| 1| .sink(receiveCompletion: { _ in
47| 1| }) { _ in
48| 1| connectionExpectation.fulfill()
49| 1| }
50| 1| .store(in: &self.disposeBag)
51| 1| }
52| 1| .store(in: &disposeBag)
53| 1|
54| 1| waitForExpectations(timeout: 15)
55| 1| XCTAssertNotNil(connectedPeripheral)
56| 1| XCTAssertEqual(connectedPeripheral!.cbPeripheral.identifier, blinky.identifier)
57| 1| }
58| |
59| | /// Connection custom operator test
60| 1| func testConnectionOperatorFromPeriphIdentifier() {
61| 1| disposeBag.removeAll()
62| 1| blinkyWOR.simulateProximityChange(.outOfRange)
63| 1| blinky.simulateProximityChange(.immediate)
64| 1| let connectionExpectation = expectation(description: "Connection identifier expectation")
65| 1|
66| 1| var connectedPeripheral: Peripheral?
67| 1|
68| 1| StartLittleBlueTooth
69| 1| .startDiscovery(for: self.littleBT, withServices: nil)
70| 1| .prefix(1)
71| 1| .map{ PeripheralIdentifier(peripheral: $0.cbPeripheral)}
72| 1| .connect(for: self.littleBT)
73| 1| .sink(receiveCompletion: { completion in
74| 1| print("Completion \(completion)")
75| 1| }) { (connectedPeriph) in
76| 1| print("Discovery \(connectedPeriph)")
77| 1| connectedPeripheral = connectedPeriph
78| 1| self.littleBT.disconnect()
79| 1| .delay(for: .seconds(5), scheduler: DispatchQueue.global())
80| 1| .sink(receiveCompletion: { _ in
81| 1| }) { _ in
82| 1| connectionExpectation.fulfill()
83| 1| }
84| 1| .store(in: &self.disposeBag)
85| 1| }
86| 1| .store(in: &disposeBag)
87| 1|
88| 1| waitForExpectations(timeout: 15)
89| 1| XCTAssertNotNil(connectedPeripheral)
90| 1| XCTAssertEqual(connectedPeripheral!.cbPeripheral.identifier, blinky.identifier)
91| 1| }
92| |
93| | /// Scan e Stop custom operator test
94| 1| func testScanStopOperatorFromScanDiscovery() {
95| 1| disposeBag.removeAll()
96| 1| blinkyWOR.simulateProximityChange(.immediate)
97| 1| blinky.simulateProximityChange(.immediate)
98| 1| let scanExpectation = expectation(description: "Scanning expectation")
99| 1| scanExpectation.expectedFulfillmentCount = 2
100| 1| var isStopped = false
101| 1| var periphCounter = 0
102| 1|
103| 1| StartLittleBlueTooth
104| 1| .startDiscovery(for: self.littleBT, withServices: nil)
105| 2| .map { _ in
106| 2| periphCounter += 1
107| 2| }
108| 1| .delay(for: .seconds(5), scheduler: DispatchQueue.global())
109| 1| .stopDiscovery(for: self.littleBT)
110| 2| .map {
111| 2| isStopped = true
112| 2| }
113| 1| .sink(receiveCompletion: { completion in
114| 0| print("Completion \(completion)")
115| 2| }) { (_) in
116| 2| print("Stopped")
117| 2| blinkyWOR.simulateProximityChange(.outOfRange)
118| 2| scanExpectation.fulfill()
119| 2| }
120| 1| .store(in: &disposeBag)
121| 1|
122| 1| waitForExpectations(timeout: 15)
123| 1| XCTAssertTrue(isStopped)
124| 1| XCTAssertTrue(periphCounter == 2)
125| 1| }
126| |
127| | /// ReadRSSI custom operator test
128| 1| func testPeripheralConnectionReadRSSIOperator() {
129| 1| disposeBag.removeAll()
130| 1|
131| 1| blinky.simulateProximityChange(.near)
132| 1| let readRSSIExpectation = expectation(description: "Read RSSI expectation")
133| 1|
134| 1| var rssiRead: Int?
135| 1|
136| 1| StartLittleBlueTooth
137| 1| .startDiscovery(for: self.littleBT, withServices: nil)
138| 1| .connect(for: self.littleBT)
139| 1| .readRSSI(for: self.littleBT)
140| 1| .sink(receiveCompletion: { completion in
141| 0| print("Completion \(completion)")
142| 1| }) { (rssi) in
143| 1| print("RSSI \(rssi)")
144| 1| rssiRead = rssi
145| 1| self.littleBT.disconnect().sink(receiveCompletion: { _ in
146| 1| }) { _ in
147| 1| readRSSIExpectation.fulfill()
148| 1| }
149| 1| .store(in: &self.disposeBag)
150| 1| }
151| 1| .store(in: &disposeBag)
152| 1|
153| 1| waitForExpectations(timeout: 15)
154| 1| XCTAssertNotNil(rssiRead)
155| 1| XCTAssert(rssiRead! < 70)
156| 1|
157| 1| }
158| |
159| | /// Disconnection custom operator test
160| 1| func testDisconnectionOperator() {
161| 1| disposeBag.removeAll()
162| 1|
163| 1| blinky.simulateProximityChange(.immediate)
164| 1|
165| 1| let disconnectionExpectation = expectation(description: "Disconnection expectation")
166| 1| var isDisconnected = false
167| 1|
168| 1| StartLittleBlueTooth
169| 1| .startDiscovery(for: self.littleBT, withServices: nil)
170| 1| .connect(for: littleBT)
171| 1| .delay(for: .seconds(5), scheduler: DispatchQueue.global())
172| 1| .disconnect(for: littleBT)
173| 1| .sink(receiveCompletion: { completion in
174| 0| print("Completion \(completion)")
175| 1| }) { (disconnectedPeriph) in
176| 1| print("Disconnection \(disconnectedPeriph)")
177| 1| isDisconnected = true
178| 1| disconnectionExpectation.fulfill()
179| 1| }
180| 1| .store(in: &disposeBag)
181| 1|
182| 1| waitForExpectations(timeout: 30)
183| 1| XCTAssert(isDisconnected)
184| 1| }
185| |
186| |
187| | /// Read custom operator test
188| 1| func testReadLedOFFOperator() {
189| 1| disposeBag.removeAll()
190| 1|
191| 1| blinky.simulateProximityChange(.immediate)
192| 1| let charateristic = LittleBlueToothCharacteristic(characteristic: CBUUID.ledCharacteristic.uuidString, for: CBUUID.nordicBlinkyService.uuidString, properties: [.read, .notify, .write])
193| 1| let readExpectation = expectation(description: "Read expectation")
194| 1|
195| 1| var ledState: LedState?
196| 1|
197| 1| StartLittleBlueTooth
198| 1| .startDiscovery(for: self.littleBT, withServices: nil)
199| 1| .connect(for: self.littleBT)
200| 1| .read(for: self.littleBT, from: charateristic)
201| 1| .sink(receiveCompletion: { completion in
202| 0| print("Completion \(completion)")
203| 1| }) { (answer: LedState) in
204| 1| print("Answer \(answer)")
205| 1| ledState = answer
206| 1| self.littleBT.disconnect().sink(receiveCompletion: {_ in
207| 1| }) { (_) in
208| 1| readExpectation.fulfill()
209| 1| }
210| 1| .store(in: &self.disposeBag)
211| 1| }
212| 1| .store(in: &disposeBag)
213| 1|
214| 1| waitForExpectations(timeout: 10)
215| 1| XCTAssertNotNil(ledState)
216| 1| XCTAssert(ledState!.isOn == false)
217| 1| }
218| |
219| | /// Read custom operator test fail for wrong charcteriscti
220| 1| func testWrongCharacteristicErrorOperator() {
221| 1| disposeBag.removeAll()
222| 1|
223| 1| blinky.simulateProximityChange(.immediate)
224| 1| let charateristic = LittleBlueToothCharacteristic(characteristic: "00001525-1212-EFDE-1523-785FEABCD133", for: CBUUID.nordicBlinkyService.uuidString, properties: [.read, .notify, .write])
225| 1| let wrongCharacteristicExpectation = expectation(description: "Wrong characteristic expectation")
226| 1|
227| 1| var isWrong = false
228| 1|
229| 1| StartLittleBlueTooth
230| 1| .startDiscovery(for: self.littleBT, withServices: nil)
231| 1| .connect(for: self.littleBT)
232| 1| .read(for: self.littleBT, from: charateristic)
233| 1| .sink(receiveCompletion: { completion in
234| 1| print("Completion \(completion)")
235| 1| switch completion {
236| 1| case .finished:
237| 0| break
238| 1| case let .failure(error):
239| 1| if case LittleBluetoothError.characteristicNotFound(_) = error {
240| 1| isWrong = true
241| 1| self.littleBT.disconnect().sink(receiveCompletion: {_ in
242| 1| }) { (_) in
243| 1| wrongCharacteristicExpectation.fulfill()
244| 1| }
245| 1| .store(in: &self.disposeBag)
246| 1| }
247| 1| }
248| 1| }) { (answer: LedState) in
249| 0| print("Answer \(answer)")
250| 0| }
251| 1| .store(in: &disposeBag)
252| 1|
253| 1| waitForExpectations(timeout: 10)
254| 1| XCTAssert(isWrong)
255| 1| }
256| |
257| | /// Write custom operator test
258| 1| func testWriteLedOnReadLedONOperator() {
259| 1| disposeBag.removeAll()
260| 1|
261| 1| blinky.simulateProximityChange(.immediate)
262| 1| let charateristic = LittleBlueToothCharacteristic(characteristic: CBUUID.ledCharacteristic.uuidString, for: CBUUID.nordicBlinkyService.uuidString, properties: [.read, .notify, .write])
263| 1| let readExpectation = expectation(description: "Read expectation")
264| 1|
265| 1| var ledState: LedState?
266| 1|
267| 1| StartLittleBlueTooth
268| 1| .startDiscovery(for: self.littleBT, withServices: nil)
269| 1| .connect(for: self.littleBT)
270| 1| .write(for: self.littleBT, to: charateristic, value: Data([0x01]))
271| 1| .read(for: self.littleBT, from: charateristic)
272| 1| .sink(receiveCompletion: { completion in
273| 0| print("Completion \(completion)")
274| 1| }) { (answer: LedState) in
275| 1| print("Answer \(answer)")
276| 1| ledState = answer
277| 1| self.littleBT.disconnect().sink(receiveCompletion: {_ in
278| 1| }) { (_) in
279| 1| readExpectation.fulfill()
280| 1| }
281| 1| .store(in: &self.disposeBag)
282| 1|
283| 1| }
284| 1| .store(in: &disposeBag)
285| 1| waitForExpectations(timeout: 10)
286| 1| XCTAssertNotNil(ledState)
287| 1| XCTAssert(ledState!.isOn == true)
288| 1| }
289| |
290| | /// Write and listen custom operator test
291| 1| func testWriteAndListenOperator() {
292| 1| disposeBag.removeAll()
293| 1|
294| 1| blinky.simulateProximityChange(.immediate)
295| 1| let charateristic = LittleBlueToothCharacteristic(characteristic: CBUUID.ledCharacteristic.uuidString, for: CBUUID.nordicBlinkyService.uuidString, properties: [.read, .notify, .write])
296| 1| let writeAndListenExpectation = expectation(description: "Write and Listen")
297| 1|
298| 1| var ledState: LedState?
299| 1|
300| 1| Timer.publish(every: 0.5, on: .main, in: .common)
301| 1| .autoconnect()
302| 130| .map {_ in
303| 130| blinky.simulateValueUpdate(Data([0x01]),
304| 130| for: CBMCharacteristicMock.ledCharacteristic)
305| 130| }.sink { value in
306| 130| print("Led value:\(value)")
307| 130| }
308| 1| .store(in: &self.disposeBag)
309| 1|
310| 1| StartLittleBlueTooth
311| 1| .startDiscovery(for: self.littleBT, withServices: nil)
312| 1| .prefix(1)
313| 1| .connect(for: self.littleBT)
314| 1| .writeAndListen(for: self.littleBT, from: charateristic, value: Data([0x01]))
315| 1| .sink(receiveCompletion: { completion in
316| 1| print("Completion \(completion)")
317| 1| }) { (answer: LedState) in
318| 1| print("Answer \(answer)")
319| 1| ledState = answer
320| 1| self.littleBT.disconnect().sink(receiveCompletion: {_ in
321| 1| }) { (_) in
322| 1| writeAndListenExpectation.fulfill()
323| 1| }
324| 1| .store(in: &self.disposeBag)
325| 1|
326| 1| }
327| 1| .store(in: &disposeBag)
328| 1| waitForExpectations(timeout: 10)
329| 1| XCTAssertNotNil(ledState)
330| 1| XCTAssert(ledState!.isOn == true)
331| 1| }
332| | /// Listen custom operator test
333| 1| func testListenOperator() {
334| 1| disposeBag.removeAll()
335| 1|
336| 1| blinky.simulateProximityChange(.immediate)
337| 1| let charateristic = LittleBlueToothCharacteristic(characteristic: CBMUUID.buttonCharacteristic.uuidString, for: CBMUUID.nordicBlinkyService.uuidString, properties: [.read, .notify])
338| 1| let listenExpectation = expectation(description: "Listen expectation")
339| 1|
340| 1| var listenCounter = 0
341| 1| var timerCounter = 0
342| 1| let timer = Timer.publish(every: 1, on: .main, in: .common)
343| 1| let scheduler: AnyCancellable =
344| 1| timer
345| 11| .map {_ in
346| 11| blinky.simulateValueUpdate(Data([0x01]), for: CBMCharacteristicMock.buttonCharacteristic)
347| 11| timerCounter += 1
348| 11| }.sink { value in
349| 11| print("Led value:\(value)")
350| 11| }
351| 1|
352| 1| StartLittleBlueTooth
353| 1| .startDiscovery(for: self.littleBT, withServices: nil)
354| 1| .connect(for: self.littleBT)
355| 1| .startListen(for: self.littleBT, from: charateristic)
356| 1| .sink(receiveCompletion: { completion in
357| 1| print("Completion \(completion)")
358| 11| }) { (answer: LedState) in
359| 11| listenCounter += 1
360| 11| print("Answer \(answer)")
361| 11| if listenCounter > 10 {
362| 1| scheduler.cancel()
363| 1| self.littleBT.disconnect().sink(receiveCompletion: {_ in
364| 1| }) { (_) in
365| 1| listenExpectation.fulfill()
366| 1| }
367| 1| .store(in: &self.disposeBag)
368| 11| }
369| 11| }
370| 1| .store(in: &disposeBag)
371| 1| _ = timer.connect()
372| 1| waitForExpectations(timeout: 20)
373| 1| let contingencyRange = (timerCounter - 2)...timerCounter
374| 1| print("Timer counter: \(timerCounter) Listen counter \(listenCounter) ")
375| 1| XCTAssert(contingencyRange.contains(listenCounter))
376| 1| }
377| | /// Enable Listen custom operator test
378| 1| func testListenToMoreCharacteristicOperator() {
379| 1| disposeBag.removeAll()
380| 1|
381| 1| blinky.simulateProximityChange(.immediate)
382| 1| let charateristicOne = LittleBlueToothCharacteristic(characteristic: CBMUUID.buttonCharacteristic.uuidString, for: CBMUUID.nordicBlinkyService.uuidString, properties: [.read, .notify])
383| 1| let charateristicTwo = LittleBlueToothCharacteristic(characteristic: CBMUUID.ledCharacteristic.uuidString, for: CBMUUID.nordicBlinkyService.uuidString, properties: [.read, .notify, .write])
384| 1| // Expectation
385| 1| let firstListenExpectation = XCTestExpectation(description: "First sub more expectation")
386| 1| let secondListenExpectation = XCTestExpectation(description: "Second sub more expectation")
387| 1| var sub1Event = [Bool]()
388| 1| var sub2Event = [Bool]()
389| 1| var firstCounter = 0
390| 1| var secondCounter = 0
391| 1| // Simulate notification
392| 1| var timerCounter = 0
393| 1| let timer = Timer.publish(every: 1, on: .main, in: .common)
394| 1| let scheduler: AnyCancellable = timer
395| 11| .map {_ -> UInt8 in
396| 11| var data = UInt8.random(in: 0...1)
397| 11| blinky.simulateValueUpdate(Data([data]), for: CBMCharacteristicMock.buttonCharacteristic)
398| 11| data = UInt8.random(in: 0...1)
399| 11| blinky.simulateValueUpdate(Data([data]), for: CBMCharacteristicMock.ledCharacteristic)
400| 11| timerCounter += 1
401| 11| return data
402| 11| }.sink { value in
403| 11| print("Sink from timer value:\(value)")
404| 11| }
405| 1|
406| 1| // First publisher
407| 1| littleBT.listenPublisher
408| 21| .filter { charact -> Bool in
409| 21| charact.id == charateristicOne.id
410| 21| }
411| 11| .tryMap { (characteristic) -> ButtonState in
412| 11| try characteristic.value()
413| 11| }
414| 1| .mapError { (error) -> LittleBluetoothError in
415| 1| if let er = error as? LittleBluetoothError {
416| 1| return er
417| 1| }
418| 0| return .emptyData
419| 1| }
420| 1| .sink(receiveCompletion: { completion in
421| 1| print("Completion \(completion)")
422| 11| }) { (answer) in
423| 11| print("Sub1: \(answer)")
424| 11| if firstCounter == 10 {
425| 1| scheduler.cancel()
426| 1| return
427| 10| } else {
428| 10| sub1Event.append(answer.isOn)
429| 10| firstCounter += 1
430| 10| }
431| 10| }
432| 1| .store(in: &self.disposeBag)
433| 1|
434| 1| // Second publisher
435| 1| littleBT.listenPublisher
436| 21| .filter { charact -> Bool in
437| 21| charact.id == charateristicTwo.id
438| 21| }
439| 10| .tryMap { (characteristic) -> LedState in
440| 10| try characteristic.value()
441| 10| }.mapError { (error) -> LittleBluetoothError in
442| 1| if let er = error as? LittleBluetoothError {
443| 1| return er
444| 1| }
445| 0| return .emptyData
446| 1| }
447| 1| .sink(receiveCompletion: { completion in
448| 1| print("Completion \(completion)")
449| 10| }) { (answer) in
450| 10| print("Sub2: \(answer)")
451| 10| if secondCounter == 10 {
452| 0| return
453| 10| } else {
454| 10| sub2Event.append(answer.isOn)
455| 10| secondCounter += 1
456| 10| }
457| 10| }
458| 1| .store(in: &self.disposeBag)
459| 1|
460| 1| StartLittleBlueTooth
461| 1| .startDiscovery(for: self.littleBT, withServices: nil)
462| 1| .connect(for: self.littleBT)
463| 1| .enableListen(for: self.littleBT, from: charateristicOne)
464| 1| .enableListen(for: self.littleBT, from: charateristicTwo)
465| 1| .delay(for: .seconds(20), scheduler: DispatchQueue.global())
466| 1| .disableListen(for: self.littleBT, from: charateristicOne)
467| 1| .disableListen(for: self.littleBT, from: charateristicTwo)
468| 1| .sink(receiveCompletion: { completion in
469| 0| print("Completion \(completion)")
470| 1| }) { (_) in
471| 1| secondListenExpectation.fulfill()
472| 1| firstListenExpectation.fulfill()
473| 1| }
474| 1| .store(in: &disposeBag)
475| 1| _ = timer.connect()
476| 1|
477| 1| wait(for: [firstListenExpectation, secondListenExpectation], timeout: 30)
478| 1| littleBT.disconnect()
479| 1| XCTAssert(sub1Event.count == sub2Event.count)
480| 1| let contingencyRange = (timerCounter - 2)...timerCounter
481| 1| print("Timer counter: \(timerCounter) Event counter \(sub2Event.count) ")
482| 1| XCTAssert(contingencyRange.contains(sub2Event.count))
483| 1| }
484| |
485| |}
/Users/Andrea/Documents/GitHub/LittleBlueTooth/Tests/LittleBlueToothTests/Extraction.swift:
1| |//
2| |// Extraction.swift
3| |// LittleBlueToothTests
4| |//
5| |// Created by Andrea Finollo on 09/08/2020.
6| |//
7| |
8| |import XCTest
9| |import CoreBluetoothMock
10| |import Combine
11| |@testable import LittleBlueToothForTest
12| |
13| |class Extraction: LittleBlueToothTests {
14| |
15| 3| override func setUpWithError() throws {
16| 3| // Put setup code here. This method is called before the invocation of each test method in the class.
17| 3| try super.setUpWithError()
18| 3| var configuration = LittleBluetoothConfiguration()
19| 3| configuration.isLogEnabled = true
20| 3| littleBT = LittleBlueTooth(with: configuration)
21| 3|
22| 3| }
23| |
24| 3| override func tearDownWithError() throws {
25| 3| // Put teardown code here. This method is called after the invocation of each test method in the class.
26| 3| }
27| |
28| 1| func testExtractionWithPeriph() {
29| 1| disposeBag.removeAll()
30| 1| var configuration = LittleBluetoothConfiguration()
31| 1| configuration.isLogEnabled = true
32| 1| littleBT = LittleBlueTooth(with: configuration)
33| 1|
34| 1| blinky.simulateProximityChange(.immediate)
35| 1| let extractionExpectation = expectation(description: "Extraction expectation")
36| 1|
37| 1| var connectedPeripheral: Peripheral?
38| 1| var extractedState: (central: CBCentralManager, peripheral: CBPeripheral?)?
39| 1|
40| 1| littleBT.startDiscovery(withServices: nil)
41| 1| .flatMap { discovery in
42| 1| self.littleBT.connect(to: discovery)
43| 1| }
44| 1| .sink(receiveCompletion: { completion in
45| 0| print("Completion \(completion)")
46| 1| }) { (connectedPeriph) in
47| 1| print("Discovery \(connectedPeriph)")
48| 1| connectedPeripheral = connectedPeriph
49| 1| // Extract state
50| 1| extractedState = self.littleBT.extract()
51| 1| extractionExpectation.fulfill()
52| 1| }
53| 1| .store(in: &disposeBag)
54| 1|
55| 1| waitForExpectations(timeout: 15)
56| 1| XCTAssertNotNil(connectedPeripheral)
57| 1| XCTAssertEqual(connectedPeripheral!.cbPeripheral.identifier, blinky.identifier)
58| 1| XCTAssertNotNil(extractedState)
59| 1| XCTAssertEqual(extractedState!.peripheral!.identifier, blinky.identifier)
60| 1| XCTAssertEqual(extractedState!.peripheral!.state, CBPeripheralState.connected)
61| 1| self.littleBT.disconnect()
62| 1|
63| 1| }
64| |
65| 1| func testExtractionWithoutPeriph() {
66| 1| disposeBag.removeAll()
67| 1| var configuration = LittleBluetoothConfiguration()
68| 1| configuration.isLogEnabled = true
69| 1| littleBT = LittleBlueTooth(with: configuration)
70| 1|
71| 1| let extractedState = self.littleBT.extract()
72| 1|
73| 1| XCTAssertNil(extractedState.peripheral)
74| 1| XCTAssertNotNil(extractedState.central)
75| 1| }
76| |
77| 1| func testRestart() {
78| 1| disposeBag.removeAll()
79| 1| var configuration = LittleBluetoothConfiguration()
80| 1| configuration.isLogEnabled = true
81| 1| littleBT = LittleBlueTooth(with: configuration)
82| 1| blinky.simulateDisconnection()
83| 1| blinky.simulateProximityChange(.immediate)
84| 1| let restartExpectation = expectation(description: "Restart expectation")
85| 1|
86| 1| var connectedPeripheral: Peripheral?
87| 1| var extractedState: (central: CBCentralManager, peripheral: CBPeripheral?)?
88| 1|
89| 1| littleBT.startDiscovery(withServices: nil)
90| 1| .flatMap { discovery in
91| 1| self.littleBT.connect(to: discovery)
92| 1| }
93| 1| .sink(receiveCompletion: { completion in
94| 0| print("Completion \(completion)")
95| 1| }) { (connectedPeriph) in
96| 1| print("Discovery \(connectedPeriph)")
97| 1| connectedPeripheral = connectedPeriph
98| 1| // Extract state
99| 1| extractedState = self.littleBT.extract()
100| 1| XCTAssertNotNil(connectedPeripheral)
101| 1| XCTAssertEqual(connectedPeripheral!.cbPeripheral.identifier, blinky.identifier)
102| 1| XCTAssertNotNil(extractedState)
103| 1| XCTAssertEqual(extractedState!.peripheral!.identifier, blinky.identifier)
104| 1| self.littleBT.restart(with: extractedState!.central, peripheral: extractedState!.peripheral!)
105| 1| restartExpectation.fulfill()
106| 1| }
107| 1| .store(in: &disposeBag)
108| 1|
109| 1| waitForExpectations(timeout: 10)
110| 1| XCTAssertNotNil(littleBT.peripheral)
111| 1| self.littleBT.disconnect()
112| 1|
113| 1| }
114| |}
/Users/Andrea/Documents/GitHub/LittleBlueTooth/Tests/LittleBlueToothTests/ListenTest.swift:
1| |//
2| |// ListenTest.swift
3| |// LittleBlueToothTests
4| |//
5| |// Created by Andrea Finollo on 04/07/2020.
6| |// Copyright © 2020 Andrea Finollo. All rights reserved.
7| |//