summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/DevAPISpecs/SDLCheckChoiceVROptionalOperationSpec.m
blob: 265dad6e47cf5ec52c6619050b33c77abd174719 (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
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>

#import "SDLCheckChoiceVROptionalOperation.h"

#import "SDLChoice.h"
#import "SDLCreateInteractionChoiceSet.h"
#import "SDLCreateInteractionChoiceSetResponse.h"
#import "SDLDeleteInteractionChoiceSet.h"
#import "SDLDeleteInteractionChoiceSetResponse.h"
#import "TestConnectionManager.h"

QuickSpecBegin(SDLCheckChoiceVROptionalOperationSpec)

describe(@"check choice VR optional operation", ^{
    __block TestConnectionManager *testConnectionManager = nil;
    __block SDLCheckChoiceVROptionalOperation *testOp = nil;

    __block BOOL resultVROptional = NO;
    __block BOOL hasCalledOperationCompletionHandler = NO;
    __block NSError *resultError = nil;

    beforeEach(^{
        resultVROptional = NO;
        hasCalledOperationCompletionHandler = NO;

        testConnectionManager = [[TestConnectionManager alloc] init];
        testOp = [[SDLCheckChoiceVROptionalOperation alloc] initWithConnectionManager:testConnectionManager];
        testOp.completionBlock = ^{
            hasCalledOperationCompletionHandler = YES;
            resultVROptional = testOp.vrOptional;
            resultError = testOp.error;
        };
    });

    it(@"should have priority of 'very high'", ^{
        expect(@(testOp.queuePriority)).to(equal(@(NSOperationQueuePriorityVeryHigh)));
    });

    describe(@"running the operation", ^{
        __block SDLCreateInteractionChoiceSetResponse *testResponse;

        beforeEach(^{
            [testOp start];
        });

        it(@"should send a create choice set request with no VR", ^{
            expect(testConnectionManager.receivedRequests.lastObject).to(beAnInstanceOf([SDLCreateInteractionChoiceSet class]));

            SDLCreateInteractionChoiceSet *receivedRequest = testConnectionManager.receivedRequests.lastObject;
            expect(receivedRequest.interactionChoiceSetID).to(equal(@0));
            expect(receivedRequest.choiceSet).to(haveCount(1));

            SDLChoice *receivedChoice = receivedRequest.choiceSet.firstObject;
            expect(receivedChoice.choiceID).to(equal(@0));
            expect(receivedChoice.menuName).to(equal(@"Test Cell"));
            expect(receivedChoice.vrCommands).to(beNil());
        });

        context(@"when a good response comes back", ^{
            beforeEach(^{
                testResponse = [[SDLCreateInteractionChoiceSetResponse alloc] init];
                testResponse.success = @YES;
                testResponse.resultCode = SDLResultSuccess;

                [testConnectionManager respondToLastRequestWithResponse:testResponse];
            });

            it(@"should have sent a DeleteChoiceSet request", ^{
                expect(testConnectionManager.receivedRequests.lastObject).to(beAnInstanceOf([SDLDeleteInteractionChoiceSet class]));
            });

            describe(@"after the DeleteChoiceSet response", ^{
                beforeEach(^{
                    SDLDeleteInteractionChoiceSetResponse *testDeleteResponse = [[SDLDeleteInteractionChoiceSetResponse alloc] init];
                    testDeleteResponse.success = @YES;
                    testDeleteResponse.resultCode = SDLResultSuccess;

                    [testConnectionManager respondToLastRequestWithResponse:testDeleteResponse];
                });

                it(@"should have called the completion handler with proper data and finish", ^{
                    expect(hasCalledOperationCompletionHandler).toEventually(beTrue());
                    expect(resultVROptional).to(beTrue());
                    expect(resultError).to(beNil());
                    expect(@(testOp.finished)).to(equal(@YES));
                    expect(@(testOp.executing)).to(equal(@NO));
                });
            });
        });

        context(@"when a bad response comes back", ^{
            beforeEach(^{
                testResponse = [[SDLCreateInteractionChoiceSetResponse alloc] init];
                testResponse.success = @NO;
                testResponse.resultCode = SDLResultRejected;

                [testConnectionManager respondToLastRequestWithResponse:testResponse];
            });

            it(@"should have sent out a new request", ^{
                expect(hasCalledOperationCompletionHandler).to(beFalse());

                expect(testConnectionManager.receivedRequests.lastObject).to(beAnInstanceOf([SDLCreateInteractionChoiceSet class]));

                SDLCreateInteractionChoiceSet *receivedRequest = testConnectionManager.receivedRequests.lastObject;
                expect(receivedRequest.interactionChoiceSetID).to(equal(@0));
                expect(receivedRequest.choiceSet).to(haveCount(1));

                SDLChoice *receivedChoice = receivedRequest.choiceSet.firstObject;
                expect(receivedChoice.choiceID).to(equal(@0));
                expect(receivedChoice.menuName).to(equal(@"Test Cell"));
                expect(receivedChoice.vrCommands).to(equal(@[@"Test VR"]));
            });

            context(@"when a good response comes back", ^{
                beforeEach(^{
                    testResponse = [[SDLCreateInteractionChoiceSetResponse alloc] init];
                    testResponse.success = @YES;
                    testResponse.resultCode = SDLResultSuccess;

                    [testConnectionManager respondToLastRequestWithResponse:testResponse];
                });

                describe(@"after the DeleteChoiceSet response", ^{
                    beforeEach(^{
                        SDLDeleteInteractionChoiceSetResponse *testDeleteResponse = [[SDLDeleteInteractionChoiceSetResponse alloc] init];
                        testDeleteResponse.success = @YES;
                        testDeleteResponse.resultCode = SDLResultSuccess;

                        [testConnectionManager respondToLastRequestWithResponse:testDeleteResponse];
                    });

                    it(@"should have called the completion handler with proper data and finish", ^{
                        expect(hasCalledOperationCompletionHandler).toEventually(beTrue());
                        expect(resultVROptional).to(beFalse());
                        expect(resultError).to(beNil());
                        expect(@(testOp.finished)).to(equal(@YES));
                        expect(@(testOp.executing)).to(equal(@NO));
                    });
                });
            });

            context(@"when a bad response comes back", ^{
                beforeEach(^{
                    testResponse = [[SDLCreateInteractionChoiceSetResponse alloc] init];
                    testResponse.success = @NO;
                    testResponse.resultCode = SDLResultRejected;

                    [testConnectionManager respondToLastRequestWithResponse:testResponse];
                });

                it(@"should return a failure", ^{
                    expect(hasCalledOperationCompletionHandler).toEventually(beTrue());
                    expect(resultVROptional).to(beFalse());
                    expect(resultError).toNot(beNil());
                });
            });
        });
    });
});

QuickSpecEnd