summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/SDLAlertManagerSpec.m
blob: c84b824561c95643f80437c7cb2c28dd9d1bd75e (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
//
//  SDLAlertManagerSpec.m
//  SmartDeviceLinkTests
//
//  Created by Nicole on 11/18/20.
//  Copyright © 2020 smartdevicelink. All rights reserved.
//

#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>

#import "SDLAlertManager.h"
#import "SDLAlertView.h"
#import "SDLFileManager.h"
#import "SDLPermissionElement.h"
#import "SDLPermissionManager.h"
#import "SDLPresentAlertOperation.h"
#import "SDLSystemCapabilityManager.h"
#import "SDLWindowCapability.h"
#import "TestConnectionManager.h"

@interface SDLAlertManager()

@property (strong, nonatomic) NSOperationQueue *transactionQueue;
@property (assign, nonatomic) UInt16 nextCancelId;
@property (copy, nonatomic, nullable) SDLWindowCapability *currentWindowCapability;

- (void)sdl_displayCapabilityDidUpdate;

@end

@interface SDLPresentAlertOperation()

@property (copy, nonatomic, nullable) NSError *internalError;
@property (assign, nonatomic) UInt16 cancelId;

@end

QuickSpecBegin(SDLAlertManagerSpec)

describe(@"alert manager tests", ^{
    __block SDLAlertManager *testAlertManager = nil;
    __block id mockConnectionManager = nil;
    __block id mockFileManager = nil;
    __block id mockSystemCapabilityManager = nil;
    __block id mockCurrentWindowCapability = nil;
    __block id mockPermissionManager = nil;
    __block SDLWindowCapability *testWindowCapability = nil;

    beforeEach(^{
        mockConnectionManager = OCMProtocolMock(@protocol(SDLConnectionManagerType));
        mockFileManager = OCMClassMock([SDLFileManager class]);
        mockSystemCapabilityManager = OCMClassMock([SDLSystemCapabilityManager class]);
        mockCurrentWindowCapability = OCMClassMock([SDLWindowCapability class]);
        mockPermissionManager = OCMClassMock([SDLPermissionManager class]);

        testWindowCapability = [[SDLWindowCapability alloc] init];
        testWindowCapability.numCustomPresetsAvailable = @10;
        testWindowCapability.windowID = @(SDLPredefinedWindowsDefaultWindow);
    });

    describe(@"when initialized", ^{
        it(@"the transaction queue should be suspended", ^{
            OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(testWindowCapability);

            testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];

            expect(testAlertManager.transactionQueue.suspended).toEventually(beTrue());
        });
    });

    describe(@"when started", ^{
        it(@"should subscribe to capability and permission updates", ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
            SEL testSelector = @selector(sdl_displayCapabilityDidUpdate);
#pragma clang diagnostic pop

            OCMExpect([mockSystemCapabilityManager subscribeToCapabilityType:SDLSystemCapabilityTypeDisplays withObserver:[OCMArg any] selector:testSelector]);
            OCMExpect([mockPermissionManager subscribeToRPCPermissions:[OCMArg checkWithBlock:^BOOL(id value) {
                NSArray<SDLPermissionElement *> *permissionElements = (NSArray<SDLPermissionElement *> *)value;
                SDLPermissionElement *permissionElement = permissionElements[0];
                expect(permissionElement.rpcName).to(equal(SDLRPCFunctionNameAlert));
                return [permissionElement isKindOfClass:[SDLPermissionElement class]];
            }] groupType:SDLPermissionGroupTypeAny withHandler:[OCMArg any]]);

            testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
            [testAlertManager start];

            OCMVerifyAll(mockSystemCapabilityManager);
            OCMVerifyAll(mockPermissionManager);
        });

        describe(@"transaction queue state", ^{
            it(@"should not start the transaction queue until the alert rpc has the correct permissions to be sent", ^{
                OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(testWindowCapability);
                OCMExpect([mockPermissionManager subscribeToRPCPermissions:[OCMArg any] groupType:SDLPermissionGroupTypeAny withHandler:([OCMArg invokeBlockWithArgs:[OCMArg any], @(SDLPermissionGroupStatusDisallowed), nil])]);

                testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
                [testAlertManager start];

                expect(testAlertManager.transactionQueue.suspended).to(beTrue());
            });

            it(@"should start the transaction queue if the alert rpc has the correct permissions and the currentWindowCapability has been set", ^{
                OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(testWindowCapability);
                OCMExpect([mockPermissionManager subscribeToRPCPermissions:[OCMArg any] groupType:SDLPermissionGroupTypeAny withHandler:([OCMArg invokeBlockWithArgs:[OCMArg any], @(SDLPermissionGroupStatusAllowed), nil])]);

                testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
                [testAlertManager start];

                expect(testAlertManager.transactionQueue.suspended).toEventually(beFalse());
            });

            it(@"should not start the transaction queue until the currentWindowCapability has been set", ^{
                OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(nil);
                OCMExpect([mockPermissionManager subscribeToRPCPermissions:[OCMArg any] groupType:SDLPermissionGroupTypeAny withHandler:([OCMArg invokeBlockWithArgs:[OCMArg any], @(SDLPermissionGroupStatusAllowed), nil])]);

                testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
                [testAlertManager start];

                expect(testAlertManager.transactionQueue.suspended).toEventually(beTrue());
            });
        });
    });

    describe(@"When the display capability updates", ^{
        __block SDLAlertView *testAlertView = nil;
        __block SDLAlertView *testAlertView2 = nil;

        beforeEach(^{
            testAlertView = [[SDLAlertView alloc] initWithText:@"alert text" secondaryText:nil tertiaryText:nil timeout:@(5.0) showWaitIndicator:@(NO) audioIndication:nil buttons:nil icon:nil];
            testAlertView2 = [[SDLAlertView alloc] initWithText:@"alert 2 text" secondaryText:nil tertiaryText:nil timeout:@(5.0) showWaitIndicator:@(NO) audioIndication:nil buttons:nil icon:nil];
        });

        it(@"should suspend the queue if the new capability is nil and it should not update operations that are currently excecuting with the new capability", ^{
            OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(testWindowCapability);
            OCMExpect([mockPermissionManager subscribeToRPCPermissions:[OCMArg any] groupType:SDLPermissionGroupTypeAny withHandler:([OCMArg invokeBlockWithArgs:[OCMArg any], @(SDLPermissionGroupStatusAllowed), nil])]);
            testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
            [testAlertManager start];

            expect(testAlertManager.transactionQueue.suspended).to(beFalse());

            [testAlertManager presentAlert:testAlertView withCompletionHandler:nil];
            [testAlertManager presentAlert:testAlertView2 withCompletionHandler:nil];
            expect(testAlertManager.transactionQueue.operationCount).to(equal(2));

            OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(nil);
            [testAlertManager sdl_displayCapabilityDidUpdate];

            OCMVerifyAllWithDelay(mockSystemCapabilityManager, 0.5);

            expect(testAlertManager.transactionQueue.suspended).to(beTrue());
            expect(testAlertManager.transactionQueue.operationCount).to(equal(2));

            SDLPresentAlertOperation *presentAlertOp1 = testAlertManager.transactionQueue.operations[0];
            SDLPresentAlertOperation *presentAlertOp2 = testAlertManager.transactionQueue.operations[1];
            expect(presentAlertOp1.isExecuting).to(beTrue());
            expect(presentAlertOp2.isExecuting).to(beFalse());
            expect(presentAlertOp1.currentWindowCapability).to(equal(testWindowCapability));
            expect(presentAlertOp2.currentWindowCapability).to(beNil());
        });

        it(@"should start the queue if the new capability is not nil and update the pending operations with the new capability", ^{
            OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(nil);
            OCMExpect([mockPermissionManager subscribeToRPCPermissions:[OCMArg any] groupType:SDLPermissionGroupTypeAny withHandler:([OCMArg invokeBlockWithArgs:[OCMArg any], @(SDLPermissionGroupStatusAllowed), nil])]);
            testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
            [testAlertManager start];

            expect(testAlertManager.transactionQueue.suspended).to(beTrue());

            [testAlertManager presentAlert:testAlertView withCompletionHandler:nil];
            [testAlertManager presentAlert:testAlertView2 withCompletionHandler:nil];
            expect(testAlertManager.transactionQueue.operationCount).to(equal(2));

            OCMExpect([mockSystemCapabilityManager defaultMainWindowCapability]).andReturn(testWindowCapability);
            [testAlertManager sdl_displayCapabilityDidUpdate];

            OCMVerifyAllWithDelay(mockSystemCapabilityManager, 0.5);

            expect(testAlertManager.transactionQueue.suspended).toEventually(beFalse());
            expect(testAlertManager.transactionQueue.operationCount).to(equal(2));

            SDLPresentAlertOperation *presentAlertOp1 = testAlertManager.transactionQueue.operations[0];
            SDLPresentAlertOperation *presentAlertOp2 = testAlertManager.transactionQueue.operations[1];
            expect(presentAlertOp1.isExecuting).to(beTrue());
            expect(presentAlertOp2.isExecuting).to(beFalse());
            expect(presentAlertOp2.currentWindowCapability).toEventually(equal(testWindowCapability));
            expect(presentAlertOp1.currentWindowCapability).toEventually(equal(testWindowCapability));
        });
    });

    describe(@"generating a cancel id", ^{
        __block SDLAlertView *testAlertView = nil;
        beforeEach(^{
            testAlertView = [[SDLAlertView alloc] initWithText:@"alert text" secondaryText:nil tertiaryText:nil timeout:@(5.0) showWaitIndicator:@(NO) audioIndication:nil buttons:nil icon:nil];
            testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
            [testAlertManager start];
        });

        it(@"should set the first cancelID correctly", ^{
            [testAlertManager presentAlert:testAlertView withCompletionHandler:nil];

            expect(testAlertManager.transactionQueue.operations.count).to(equal(1));

            SDLPresentAlertOperation *testPresentOp = (SDLPresentAlertOperation *)testAlertManager.transactionQueue.operations.firstObject;
            expect(@(testPresentOp.cancelId)).to(equal(1));
        });

        it(@"should reset the cancelID correctly once the max has been reached", ^{
            testAlertManager.nextCancelId = 100;
            [testAlertManager presentAlert:testAlertView withCompletionHandler:nil];

            expect(testAlertManager.transactionQueue.operations.count).to(equal(1));

            SDLPresentAlertOperation *testPresentOp = (SDLPresentAlertOperation *)testAlertManager.transactionQueue.operations[0];
            expect(@(testPresentOp.cancelId)).to(equal(100));

            [testAlertManager presentAlert:testAlertView withCompletionHandler:nil];

            expect(testAlertManager.transactionQueue.operations.count).to(equal(2));

            SDLPresentAlertOperation *testPresentOp2 = (SDLPresentAlertOperation *)testAlertManager.transactionQueue.operations[1];
            expect(@(testPresentOp2.cancelId)).to(equal(1));
        });
    });

    describe(@"presenting an alert", ^{
        __block SDLAlertView *testAlertView = nil;
        __block SDLAlertView *testAlertView2 = nil;

        beforeEach(^{
            testAlertView = [[SDLAlertView alloc] initWithText:@"alert text" secondaryText:nil tertiaryText:nil timeout:@(5.0) showWaitIndicator:@(NO) audioIndication:nil buttons:nil icon:nil];
            testAlertView2 = [[SDLAlertView alloc] initWithText:@"alert 2 text" secondaryText:nil tertiaryText:nil timeout:@(5.0) showWaitIndicator:@(NO) audioIndication:nil buttons:nil icon:nil];
            testAlertManager = [[SDLAlertManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager permissionManager:mockPermissionManager];
            [testAlertManager start];
        });

        it(@"should add the alert operation to the queue", ^{
            [testAlertManager presentAlert:testAlertView withCompletionHandler:nil];

            expect(testAlertManager.transactionQueue.operations.count).to(equal(1));
            expect(testAlertManager.transactionQueue.operations.firstObject).to(beAKindOf([SDLPresentAlertOperation class]));
        });

        describe(@"when the completion handler is called", ^{
            __block BOOL completionHandlerCalled = NO;
            __block NSError *completionHandlerError = nil;

            beforeEach(^{
                completionHandlerCalled = NO;
                completionHandlerError = nil;

                [testAlertManager presentAlert:testAlertView withCompletionHandler:^(NSError * _Nullable error) {
                    completionHandlerCalled = YES;
                    completionHandlerError = error;
                }];
            });

            context(@"without an error", ^{
                it(@"should call the handler", ^{
                    SDLPresentAlertOperation *op = testAlertManager.transactionQueue.operations.lastObject;
                    op.internalError = nil;
                    op.completionBlock();

                    expect(completionHandlerCalled).to(beTrue());
                    expect(completionHandlerError).to(beNil());
                });
            });

            context(@"with an error", ^{
                __block NSError *testError;

                beforeEach(^{
                    testError = [NSError errorWithDomain:@"com.sdl.testConnectionManager" code:-1 userInfo:nil];
                });

                it(@"should call the handler with the error", ^{
                    SDLPresentAlertOperation *op = testAlertManager.transactionQueue.operations.lastObject;
                    op.internalError = testError;
                    op.completionBlock();

                    expect(completionHandlerCalled).to(beTrue());
                    expect(completionHandlerError).to(equal(testError));
                });
            });
        });

        describe(@"when the manager shuts down during presentation", ^{
            beforeEach(^{
                [testAlertManager presentAlert:testAlertView withCompletionHandler:nil];
                [testAlertManager presentAlert:testAlertView2 withCompletionHandler:nil];
            });

            it(@"should cancel any pending operations and unsubscribe to capability and permission updates", ^{
                SDLPresentAlertOperation *presentAlertOp1 = testAlertManager.transactionQueue.operations[0];
                SDLPresentAlertOperation *presentAlertOp2 = testAlertManager.transactionQueue.operations[1];

                OCMExpect([mockSystemCapabilityManager unsubscribeFromCapabilityType:SDLSystemCapabilityTypeDisplays withObserver:[OCMArg any]]);
                OCMExpect([mockPermissionManager removeAllObservers]);

                [testAlertManager stop];

                expect(presentAlertOp1.isCancelled).to(beTrue());
                expect(presentAlertOp2.isCancelled).to(beTrue());
                expect(testAlertManager.transactionQueue.operationCount).to(equal(0));

                OCMVerifyAll(mockSystemCapabilityManager);
                OCMVerifyAll(mockPermissionManager);
            });
        });
    });
});

QuickSpecEnd