summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/DevAPISpecs/SDLResponseDispatcherSpec.m
blob: 410d243862a74248be17054718472ebb6a05bfb2 (plain)
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
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>

#import "SDLAddCommand.h"
#import "SDLAlert.h"
#import "SDLButtonName.h"
#import "SDLDeleteCommand.h"
#import "SDLDeleteCommandResponse.h"
#import "SDLNotificationConstants.h"
#import "SDLOnAudioPassThru.h"
#import "SDLOnButtonEvent.h"
#import "SDLOnButtonPress.h"
#import "SDLOnCommand.h"
#import "SDLPerformAudioPassThru.h"
#import "SDLPerformAudioPassThruResponse.h"
#import "SDLReadDID.h"
#import "SDLReadDIDResponse.h"
#import "SDLResponseDispatcher.h"
#import "SDLRPCNotificationNotification.h"
#import "SDLRPCResponseNotification.h"
#import "SDLScrollableMessage.h"
#import "SDLShow.h"
#import "SDLSoftButton.h"
#import "SDLSoftButtonType.h"
#import "SDLSubscribeButton.h"
#import "SDLSystemAction.h"
#import "SDLTextAlignment.h"
#import "SDLUnsubscribeButton.h"
#import "SDLUnsubscribeButtonResponse.h"


QuickSpecBegin(SDLResponseDispatcherSpec)

describe(@"a response dispatcher", ^{
    __block SDLResponseDispatcher *testDispatcher = nil;
    
    beforeEach(^{
        testDispatcher = [[SDLResponseDispatcher alloc] init];
    });
    
    it(@"should initialize the NSMapTable properties", ^{
        expect(testDispatcher.rpcResponseHandlerMap).toNot(beNil());
        expect(testDispatcher.rpcRequestDictionary).toNot(beNil());
        expect(testDispatcher.commandHandlerMap).toNot(beNil());
        expect(testDispatcher.buttonHandlerMap).toNot(beNil());
        expect(testDispatcher.customButtonHandlerMap).toNot(beNil());
//        expect(testDispatcher.audioPassThruHandler).to(beNil());

        expect(testDispatcher.rpcResponseHandlerMap).to(haveCount(@0));
        expect(testDispatcher.rpcRequestDictionary).to(haveCount(@0));
        expect(testDispatcher.commandHandlerMap).to(haveCount(@0));
        expect(testDispatcher.buttonHandlerMap).to(haveCount(@0));
        expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
    });
    
    context(@"storing a request without a handler", ^{
        __block SDLReadDID *testRPC = nil;
        __block NSNumber *testCorrelationId = nil;
        
        beforeEach(^{
            testRPC = [[SDLReadDID alloc] init];
            testCorrelationId = @111;
            testRPC.correlationID = testCorrelationId;
        });
        
        it(@"should not store the request", ^{
            [testDispatcher storeRequest:testRPC handler:nil];
            
            expect(testDispatcher.rpcResponseHandlerMap).to(haveCount(@0));
            expect(testDispatcher.rpcRequestDictionary).to(haveCount(@1));
            expect(testDispatcher.commandHandlerMap).to(haveCount(@0));
            expect(testDispatcher.buttonHandlerMap).to(haveCount(@0));
            expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
        });
    });
    
    context(@"storing a request with a handler", ^{
        __block SDLRPCRequest *testRequest = nil;
        __block NSNumber *testCorrelationId = nil;
        __block BOOL handlerCalled = NO;
        
        beforeEach(^{
            testRequest = [[SDLReadDID alloc] init];
            testCorrelationId = @42;
            
            testRequest.correlationID = testCorrelationId;
            [testDispatcher storeRequest:testRequest handler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
                handlerCalled = YES;
            }];
        });
        
        it(@"should store the request and response", ^{
            expect(testDispatcher.rpcRequestDictionary[testCorrelationId]).toNot(beNil());
            expect(testDispatcher.rpcRequestDictionary).to(haveCount(@1));
            
            expect(testDispatcher.rpcResponseHandlerMap[testCorrelationId]).toNot(beNil());
            expect(testDispatcher.rpcResponseHandlerMap).to(haveCount(@1));
        });
        
        describe(@"when a response arrives", ^{
            __block SDLRPCResponse *testResponse = nil;
            
            beforeEach(^{
                testResponse = [[SDLReadDIDResponse alloc] init];
                testResponse.correlationID = testCorrelationId;
                
                SDLRPCResponseNotification *responseNotification = [[SDLRPCResponseNotification alloc] initWithName:SDLDidReceiveReadDIDResponse object:nil rpcResponse:testResponse];
                [[NSNotificationCenter defaultCenter] postNotification:responseNotification];
            });
            
            it(@"should run the handler", ^{
                expect(@(handlerCalled)).to(beTruthy());
                expect(testDispatcher.rpcRequestDictionary).to(haveCount(@0));
                expect(testDispatcher.rpcResponseHandlerMap).to(haveCount(@0));
            });
        });
    });
    
    context(@"storing a show request", ^{
        __block SDLShow *testShow = nil;
        __block SDLSoftButton *testSoftButton1 = nil;
        __block NSUInteger numTimesHandlerCalled = 0;
        
        beforeEach(^{
            testShow = [[SDLShow alloc] initWithMainField1:@"Test Show" mainField2:nil alignment:SDLTextAlignmentCenter];
            testShow.correlationID = @1;
        });
        
        context(@"with a correct soft button and handler", ^{
            beforeEach(^{
                numTimesHandlerCalled = 0;
                
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:^(SDLOnButtonPress * _Nullable buttonPressNotification, SDLOnButtonEvent * _Nullable buttonEventNotification) {
                    numTimesHandlerCalled++;
                }];
                testShow.softButtons = [@[testSoftButton1] mutableCopy];
                testShow.correlationID = @1;
                [testDispatcher storeRequest:testShow handler:nil];
            });
            
            it(@"should add the soft button to the map", ^{
                expect(testDispatcher.customButtonHandlerMap[testSoftButton1.softButtonID]).toNot(beNil());
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@1));
            });
            
            describe(@"when button press and button event notifications arrive", ^{
                __block SDLOnButtonEvent *testButtonEvent = nil;
                __block SDLOnButtonPress *testButtonPress = nil;
                
                context(@"that correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.customButtonID = testSoftButton1.softButtonID;
                        testButtonEvent.buttonName = SDLButtonNameCustomButton;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.customButtonID = testSoftButton1.softButtonID;
                        testButtonPress.buttonName = SDLButtonNameCustomButton;
                        
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should run the handler for each", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@2));
                    });
                });
                
                context(@"that do not correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.buttonName = SDLButtonNameOk;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.buttonName = SDLButtonNameOk;
                        
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should not run the handler", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@0));
                    });
                });
            });
        });
        
        context(@"with a correct soft button but no handler", ^{
            beforeEach(^{
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:nil];
            });
            
            it(@"should not add the soft button", ^{
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
            });
        });
        
        context(@"with a malformed soft button", ^{
            beforeEach(^{
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:nil];
            });
            
            it(@"should throw an exception if there's no button id", ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
                testSoftButton1.softButtonID = nil;
#pragma clang diagnostic pop
                testShow.softButtons = [@[testSoftButton1] mutableCopy];
                
                expectAction(^{ [testDispatcher storeRequest:testShow handler:nil]; }).to(raiseException().named(@"MissingIdException"));
            });
        });
        
        context(@"without soft buttons", ^{
            it(@"should not store the request", ^{
                [testDispatcher storeRequest:testShow handler:nil];
                
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
            });
        });
    });
    
    context(@"storing a command request", ^{
        __block SDLAddCommand *testAddCommand = nil;
        __block UInt32 testCommandId = 0;
        __block NSUInteger numTimesHandlerCalled = 0;
        
        __block NSNumber *testAddCommandCorrelationId = nil;
        __block NSNumber *testDeleteCommandCorrelationId = nil;
        
        context(@"with a handler", ^{
            beforeEach(^{
                testCommandId = 1;
                testAddCommandCorrelationId = @42;
                numTimesHandlerCalled = 0;
                
                testAddCommand = [[SDLAddCommand alloc] initWithId:testCommandId vrCommands:nil handler:^(__kindof SDLRPCNotification * _Nonnull notification) {
                    numTimesHandlerCalled++;
                }];
                testAddCommand.correlationID = testAddCommandCorrelationId;
            });
            
            it(@"should add the command to the map", ^{
                [testDispatcher storeRequest:testAddCommand handler:nil];
                
                expect(testDispatcher.commandHandlerMap[testAddCommand.cmdID]).toNot(beNil());
                expect(testDispatcher.commandHandlerMap).to(haveCount(@1));
            });
            
            it(@"should throw an exception if there's no command id", ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
                testAddCommand.cmdID = nil;
                
#pragma clang diagnostic pop
                expectAction(^{ [testDispatcher storeRequest:testAddCommand handler:nil]; }).to(raiseException().named(@"MissingIdException"));
            });
            
            describe(@"when button press and button event notifications arrive", ^{
                __block SDLOnCommand *testOnCommand = nil;
                
                beforeEach(^{
                    [testDispatcher storeRequest:testAddCommand handler:nil];
                });
                
                context(@"that correspond to the created button", ^{
                    beforeEach(^{
                        testOnCommand = [[SDLOnCommand alloc] init];
                        testOnCommand.cmdID = @(testCommandId);
                        testOnCommand.triggerSource = SDLTriggerSourceMenu;
                        
                        SDLRPCNotificationNotification *commandNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveCommandNotification object:nil rpcNotification:testOnCommand];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:commandNotification];
                    });
                    
                    it(@"should run the handler for each", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@1));
                    });
                });
                
                context(@"that do not correspond to the created button", ^{
                    beforeEach(^{
                        testOnCommand = [[SDLOnCommand alloc] init];
                        testOnCommand.cmdID = @999;
                        
                        SDLRPCNotificationNotification *commandNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveCommandNotification object:nil rpcNotification:testOnCommand];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:commandNotification];
                    });
                    
                    it(@"should not run the handler", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@0));
                    });
                });
            });
            
            describe(@"then deleting the command", ^{
                __block SDLDeleteCommand *testDeleteCommand = nil;
                __block SDLDeleteCommandResponse *testDeleteResponse = nil;
                
                beforeEach(^{
                    [testDispatcher storeRequest:testAddCommand handler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {}];
                    
                    // We need both the delete command and the response for this to work
                    testDeleteCommandCorrelationId = @43;
                    
                    testDeleteCommand = [[SDLDeleteCommand alloc] init];
                    testDeleteCommand.correlationID = testDeleteCommandCorrelationId;
                    testDeleteCommand.cmdID = @(testCommandId);
                    
                    [testDispatcher storeRequest:testDeleteCommand handler:nil];
                    
                    testDeleteResponse = [[SDLDeleteCommandResponse alloc] init];
                    testDeleteResponse.correlationID = testDeleteCommandCorrelationId;
                    testDeleteResponse.success = @YES;
                    
                    SDLRPCResponseNotification *deleteCommandNotification = [[SDLRPCResponseNotification alloc] initWithName:SDLDidReceiveDeleteCommandResponse object:nil rpcResponse:testDeleteResponse];
                    
                    [[NSNotificationCenter defaultCenter] postNotification:deleteCommandNotification];
                });
                
                it(@"should have removed all the handlers", ^{
                    // There should still be the add command request & handler in the dictionaries since we never responded
                    expect(testDispatcher.commandHandlerMap).to(haveCount(@0));
                    expect(testDispatcher.rpcRequestDictionary).to(haveCount(@1));
                    expect(testDispatcher.rpcResponseHandlerMap).to(haveCount(@1));
                });
            });
        });
        
        context(@"without a handler", ^{
            beforeEach(^{
                testAddCommand = [[SDLAddCommand alloc] initWithId:1 vrCommands:nil handler:nil];
            });
                                  
            it(@"should not add the command", ^{
                expect(testDispatcher.commandHandlerMap).to(haveCount(@0));
            });
        });
    });
    
    context(@"storing a subscribe button request", ^{
        __block SDLSubscribeButton *testSubscribeButton = nil;
        __block SDLButtonName testButtonName = nil;
        __block NSUInteger numTimesHandlerCalled = 0;
        
        __block NSNumber *testSubscribeCorrelationId = nil;
        __block NSNumber *testUnsubscribeCorrelationId = nil;
        
        context(@"with a handler", ^{
            beforeEach(^{
                testButtonName = SDLButtonNameOk;
                testSubscribeCorrelationId = @42;
                numTimesHandlerCalled = 0;
                
                testSubscribeButton = [[SDLSubscribeButton alloc] initWithButtonName:testButtonName handler:^(SDLOnButtonPress * _Nullable buttonPressNotification, SDLOnButtonEvent * _Nullable buttonEventNotification) {
                    numTimesHandlerCalled++;
                }];
                testSubscribeButton.correlationID = testSubscribeCorrelationId;
            });
            
            it(@"should add the subscription to the map", ^{
                [testDispatcher storeRequest:testSubscribeButton handler:nil];
                
                expect(testDispatcher.buttonHandlerMap[testSubscribeButton.buttonName]).toNot(beNil());
                expect(testDispatcher.buttonHandlerMap).to(haveCount(@1));
            });
            
            it(@"should throw an exception if there's no button name", ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
                testSubscribeButton.buttonName = nil;
#pragma clang diagnostic pop
                
                expectAction(^{ [testDispatcher storeRequest:testSubscribeButton handler:nil]; }).to(raiseException().named(@"MissingIdException"));
            });
            
            describe(@"when button press and button event notifications arrive", ^{
                __block SDLOnButtonEvent *testButtonEvent = nil;
                __block SDLOnButtonPress *testButtonPress = nil;
                
                beforeEach(^{
                    [testDispatcher storeRequest:testSubscribeButton handler:nil];
                });
                
                context(@"that correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.buttonName = testButtonName;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.buttonName = testButtonName;
                        
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should run the handler for each", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@2));
                    });
                });
                
                context(@"that do not correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.buttonName = SDLButtonNamePreset0;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.buttonName = SDLButtonNamePreset0;
                        
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should not run the handler", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@0));
                    });
                });
            });
            
            describe(@"then unsubscribing", ^{
                __block SDLUnsubscribeButton *testUnsubscribe = nil;
                __block SDLUnsubscribeButtonResponse *testUnsubscribeResponse = nil;
                
                beforeEach(^{
                    [testDispatcher storeRequest:testSubscribeButton handler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {}];
                    
                    // We need both the delete command and the response for this to work
                    testUnsubscribeCorrelationId = @43;
                    
                    testUnsubscribe = [[SDLUnsubscribeButton alloc] init];
                    testUnsubscribe.correlationID = testUnsubscribeCorrelationId;
                    testUnsubscribe.buttonName = testButtonName;
                    
                    [testDispatcher storeRequest:testUnsubscribe handler:nil];
                    
                    testUnsubscribeResponse = [[SDLUnsubscribeButtonResponse alloc] init];
                    testUnsubscribeResponse.correlationID = testUnsubscribeCorrelationId;
                    testUnsubscribeResponse.success = @YES;
                    
                    SDLRPCResponseNotification *unsubscribeNotification = [[SDLRPCResponseNotification alloc] initWithName:SDLDidReceiveUnsubscribeButtonResponse object:nil rpcResponse:testUnsubscribeResponse];
                    [[NSNotificationCenter defaultCenter] postNotification:unsubscribeNotification];
                });
                
                it(@"should have removed all the handlers", ^{
                    // There should still be the add command request & handler in the dictionaries since we never responded
                    expect(testDispatcher.commandHandlerMap).to(haveCount(@0));
                    expect(testDispatcher.rpcRequestDictionary).to(haveCount(@1));
                    expect(testDispatcher.rpcResponseHandlerMap).to(haveCount(@1));
                });
            });
        });
    
        context(@"without a handler", ^{
            beforeEach(^{
                testSubscribeButton = [[SDLSubscribeButton alloc] initWithButtonName:SDLButtonNameOk handler:nil];
            });
            
            it(@"should not add the subscription", ^{
                expect(testDispatcher.buttonHandlerMap).to(haveCount(@0));
            });
        });
    });
    
    context(@"storing an alert request", ^{
        __block SDLAlert *testAlert = nil;
        __block SDLSoftButton *testSoftButton1 = nil;
        
        beforeEach(^{
            testAlert = [[SDLAlert alloc] initWithAlertText1:@"test 1" alertText2:@"test 1" alertText3:nil duration:1 softButtons:nil];
            testAlert.correlationID = @1;
        });
        
        context(@"with a correct soft button and handler", ^{
            __block NSUInteger numTimesHandlerCalled = 0;
            
            beforeEach(^{
                numTimesHandlerCalled = 0;
                
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:^(SDLOnButtonPress * _Nullable buttonPressNotification, SDLOnButtonEvent * _Nullable buttonEventNotification) {
                    numTimesHandlerCalled++;
                }];
                
                testAlert.softButtons = [@[testSoftButton1] mutableCopy];
                [testDispatcher storeRequest:testAlert handler:nil];
            });
            
            it(@"should add the soft button to the map", ^{
                expect(testDispatcher.customButtonHandlerMap[testSoftButton1.softButtonID]).toNot(beNil());
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@1));
            });
            
            describe(@"when button press and button event notifications arrive", ^{
                __block SDLOnButtonEvent *testButtonEvent = nil;
                __block SDLOnButtonPress *testButtonPress = nil;
                
                context(@"that correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.buttonName = SDLButtonNameCustomButton;
                        testButtonEvent.customButtonID = testSoftButton1.softButtonID;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.buttonName = SDLButtonNameCustomButton;
                        testButtonPress.customButtonID = testSoftButton1.softButtonID;
                        
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should run the handler for each", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@2));
                    });
                });
                
                context(@"that do not correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.buttonName = SDLButtonNameOk;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.buttonName = SDLButtonNameOk;
                    
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should not run the handler", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@0));
                    });
                });
            });
        });
        
        context(@"with a correct soft button but no handler", ^{
            beforeEach(^{
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:nil];
            });
            
            it(@"should not add the soft button", ^{
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
            });
        });
        
        context(@"with a malformed soft button", ^{
            beforeEach(^{
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:nil];
            });
            
            it(@"should throw an exception if there's no button id", ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
                testSoftButton1.softButtonID = nil;
#pragma clang diagnostic pop
                testAlert.softButtons = [@[testSoftButton1] mutableCopy];
                
                expectAction(^{ [testDispatcher storeRequest:testAlert handler:nil]; }).to(raiseException().named(@"MissingIdException"));
            });
        });
        
        context(@"without soft buttons", ^{
            it(@"should not store the request", ^{
                [testDispatcher storeRequest:testAlert handler:nil];
                
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
            });
        });
    });
    
    context(@"storing a scrollable message request", ^{
        __block SDLScrollableMessage *testScrollableMessage = nil;
        __block SDLSoftButton *testSoftButton1 = nil;
        
        beforeEach(^{
            testScrollableMessage = [[SDLScrollableMessage alloc] initWithMessage:@"test" timeout:1 softButtons:nil];
            testScrollableMessage.correlationID = @1;
        });
        
        context(@"with a correct soft button and handler", ^{
            __block NSUInteger numTimesHandlerCalled = 0;
            
            beforeEach(^{
                numTimesHandlerCalled = 0;
                
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:^(SDLOnButtonPress * _Nullable buttonPressNotification, SDLOnButtonEvent * _Nullable buttonEventNotification) {
                    numTimesHandlerCalled++;
                }];
                
                testScrollableMessage.softButtons = [@[testSoftButton1] mutableCopy];
                [testDispatcher storeRequest:testScrollableMessage handler:nil];
            });
            
            it(@"should add the soft button to the map", ^{
                expect(testDispatcher.customButtonHandlerMap[testSoftButton1.softButtonID]).toNot(beNil());
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@1));
            });
            
            describe(@"when button press and button event notifications arrive", ^{
                __block SDLOnButtonEvent *testButtonEvent = nil;
                __block SDLOnButtonPress *testButtonPress = nil;
                
                context(@"that correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.buttonName = SDLButtonNameCustomButton;
                        testButtonEvent.customButtonID = testSoftButton1.softButtonID;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.buttonName = SDLButtonNameCustomButton;
                        testButtonPress.customButtonID = testSoftButton1.softButtonID;
                        
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should run the handler for each", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@2));
                    });
                });
                
                context(@"that do not correspond to the created button", ^{
                    beforeEach(^{
                        testButtonEvent = [[SDLOnButtonEvent alloc] init];
                        testButtonEvent.buttonName = SDLButtonNameOk;
                        
                        testButtonPress = [[SDLOnButtonPress alloc] init];
                        testButtonPress.buttonName = SDLButtonNameOk;
                        
                        SDLRPCNotificationNotification *buttonEventNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonEventNotification object:nil rpcNotification:testButtonEvent];
                        SDLRPCNotificationNotification *buttonPressNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveButtonPressNotification object:nil rpcNotification:testButtonPress];
                        
                        [[NSNotificationCenter defaultCenter] postNotification:buttonEventNotification];
                        [[NSNotificationCenter defaultCenter] postNotification:buttonPressNotification];
                    });
                    
                    it(@"should not run the handler", ^{
                        expect(@(numTimesHandlerCalled)).to(equal(@0));
                    });
                });
            });
        });
        
        context(@"with a correct soft button but no handler", ^{
            beforeEach(^{
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:nil];
            });
            
            it(@"should not add the soft button", ^{
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
            });
        });
        
        context(@"with a malformed soft button", ^{
            beforeEach(^{
                testSoftButton1 = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"test" image:nil highlighted:NO buttonId:1 systemAction:SDLSystemActionDefaultAction handler:nil];
            });
            
            it(@"should throw an exception if there's no button id", ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
                testSoftButton1.softButtonID = nil;
#pragma clang diagnostic pop
                testScrollableMessage.softButtons = [@[testSoftButton1] mutableCopy];
                
                expectAction(^{ [testDispatcher storeRequest:testScrollableMessage handler:nil]; }).to(raiseException().named(@"MissingIdException"));
            });
        });
        
        context(@"without soft buttons", ^{
            it(@"should not store the request", ^{
                [testDispatcher storeRequest:testScrollableMessage handler:nil];
                
                expect(testDispatcher.customButtonHandlerMap).to(haveCount(@0));
            });
        });
    });
    
    context(@"storing an audio pass thru handler", ^{
        __block SDLPerformAudioPassThru* testPerformAudioPassThru = nil;
        __block NSUInteger numTimesHandlerCalled = 0;
        
        context(@"with a handler", ^{
            beforeEach(^{
                testPerformAudioPassThru = [[SDLPerformAudioPassThru alloc] initWithSamplingRate:SDLSamplingRate8KHZ bitsPerSample:SDLBitsPerSample8Bit audioType:SDLAudioTypePCM maxDuration:1000 audioDataHandler:^(NSData * _Nullable audioData) {
                    numTimesHandlerCalled++;
                }];
                
                testPerformAudioPassThru.correlationID = @1;
                [testDispatcher storeRequest:testPerformAudioPassThru handler:nil];
            });
            
            it(@"should store the handler" ,^{
//                expect(testDispatcher.audioPassThruHandler).toNot(beNil());
//                expect(testDispatcher.audioPassThruHandler).to(equal(testPerformAudioPassThru.audioDataHandler));
            });
            
            describe(@"when an on audio data notification arrives", ^{
                beforeEach(^{
                    SDLOnAudioPassThru *testOnAudioPassThru = [[SDLOnAudioPassThru alloc] init];

                    SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveAudioPassThruNotification object:nil rpcNotification:testOnAudioPassThru];
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                });
                
                it(@"should run the handler", ^{
                    expect(@(numTimesHandlerCalled)).to(equal(@1));
                });
            });
            
            describe(@"when an on audio data response arrives", ^{
                beforeEach(^{
                    SDLPerformAudioPassThruResponse *performAudioPassThruResponse = [[SDLPerformAudioPassThruResponse alloc] init];
                    performAudioPassThruResponse.success = @YES;
                    
                    SDLRPCResponseNotification *notification = [[SDLRPCResponseNotification alloc] initWithName:SDLDidReceivePerformAudioPassThruResponse object:nil rpcResponse:performAudioPassThruResponse];
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                });
                
                it(@"should clear the handler", ^{
//                    expect(testDispatcher.audioPassThruHandler).to(beNil());
                });
            });
        });
        
        context(@"without a handler", ^{
            beforeEach(^{
                numTimesHandlerCalled = 0;
                
                testPerformAudioPassThru = [[SDLPerformAudioPassThru alloc] initWithSamplingRate:SDLSamplingRate8KHZ bitsPerSample:SDLBitsPerSample8Bit audioType:SDLAudioTypePCM maxDuration:1000];
                
                testPerformAudioPassThru.correlationID = @1;
                [testDispatcher storeRequest:testPerformAudioPassThru handler:nil];
            });
            
            describe(@"when an on audio data notification arrives", ^{
               beforeEach(^{
                   SDLOnAudioPassThru *testOnAudioPassThru = [[SDLOnAudioPassThru alloc] init];
                   
                   SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveAudioPassThruNotification object:nil rpcNotification:testOnAudioPassThru];
                   [[NSNotificationCenter defaultCenter] postNotification:notification];
               });
               
               it(@"should not run a handler", ^{
                   expect(@(numTimesHandlerCalled)).to(equal(@0));
               });
            });
        
            describe(@"when an on audio data response arrives", ^{
                beforeEach(^{
                    SDLPerformAudioPassThruResponse *performAudioPassThruResponse = [[SDLPerformAudioPassThruResponse alloc] init];
                    performAudioPassThruResponse.success = @YES;
                    
                    SDLRPCResponseNotification *notification = [[SDLRPCResponseNotification alloc] initWithName:SDLDidReceivePerformAudioPassThruResponse object:nil rpcResponse:performAudioPassThruResponse];
                    [[NSNotificationCenter defaultCenter] postNotification:notification];
                });
                
                it(@"should clear the handler", ^{
//                    expect(testDispatcher.audioPassThruHandler).to(beNil());
                });
            });
            
        });
    });
});

QuickSpecEnd