/
SDLChoiceSetManagerSpec.m
391 lines (307 loc) 路 18.3 KB
/
SDLChoiceSetManagerSpec.m
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
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>
#import "SDLChoiceSetManager.h"
#import "SDLCheckChoiceVROptionalOperation.h"
#import "SDLChoiceCell.h"
#import "SDLChoiceSet.h"
#import "SDLChoiceSetDelegate.h"
#import "SDLDeleteChoicesOperation.h"
#import "SDLFileManager.h"
#import "SDLHMILevel.h"
#import "SDLKeyboardDelegate.h"
#import "SDLKeyboardProperties.h"
#import "SDLPreloadChoicesOperation.h"
#import "SDLPresentChoiceSetOperation.h"
#import "SDLPresentKeyboardOperation.h"
#import "SDLGlobals.h"
#import "SDLStateMachine.h"
#import "SDLSystemContext.h"
#import "SDLSystemCapabilityManager.h"
#import "TestConnectionManager.h"
#import "SDLVersion.h"
@interface SDLPresentChoiceSetOperation()
@property (copy, nonatomic, nullable) NSError *internalError;
@end
@interface SDLCheckChoiceVROptionalOperation()
@property (copy, nonatomic, nullable) NSError *internalError;
@end
@interface SDLChoiceSetManager()
@property (strong, nonatomic, readonly) SDLStateMachine *stateMachine;
@property (strong, nonatomic) NSOperationQueue *transactionQueue;
@property (copy, nonatomic, nullable) SDLHMILevel currentHMILevel;
@property (copy, nonatomic, nullable) SDLSystemContext currentSystemContext;
@property (strong, nonatomic) NSMutableSet<SDLChoiceCell *> *preloadedMutableChoices;
@property (strong, nonatomic, readonly) NSSet<SDLChoiceCell *> *pendingPreloadChoices;
@property (strong, nonatomic) NSMutableSet<SDLChoiceCell *> *pendingMutablePreloadChoices;
@property (strong, nonatomic, nullable) SDLChoiceSet *pendingPresentationSet;
@property (strong, nonatomic, nullable) SDLAsynchronousOperation *pendingPresentOperation;
@property (assign, nonatomic, getter=isVROptional) BOOL vrOptional;
@end
QuickSpecBegin(SDLChoiceSetManagerSpec)
describe(@"choice set manager tests", ^{
__block SDLChoiceSetManager *testManager = nil;
__block TestConnectionManager *testConnectionManager = nil;
__block SDLFileManager *testFileManager = nil;
__block SDLSystemCapabilityManager *testSystemCapabilityManager = nil;
__block SDLChoiceCell *testCell1 = nil;
__block SDLChoiceCell *testCell2 = nil;
__block SDLChoiceCell *testCell3 = nil;
beforeEach(^{
testConnectionManager = [[TestConnectionManager alloc] init];
testFileManager = OCMClassMock([SDLFileManager class]);
testSystemCapabilityManager = OCMClassMock([SDLSystemCapabilityManager class]);
testManager = [[SDLChoiceSetManager alloc] initWithConnectionManager:testConnectionManager fileManager:testFileManager systemCapabilityManager:testSystemCapabilityManager];
testCell1 = [[SDLChoiceCell alloc] initWithText:@"test1"];
testCell2 = [[SDLChoiceCell alloc] initWithText:@"test2"];
testCell3 = [[SDLChoiceCell alloc] initWithText:@"test3"];
});
it(@"should be in the correct startup state", ^{
expect(testManager.currentState).to(equal(SDLChoiceManagerStateShutdown));
SDLKeyboardProperties *defaultProperties = [[SDLKeyboardProperties alloc] initWithLanguage:SDLLanguageEnUs layout:SDLKeyboardLayoutQWERTY keypressMode:SDLKeypressModeResendCurrentEntry limitedCharacterList:nil autoCompleteText:nil autoCompleteList:nil];
expect(testManager.keyboardConfiguration).to(equal(defaultProperties));
});
describe(@"once started", ^{
beforeEach(^{
[testManager start];
testManager.transactionQueue.suspended = YES;
});
it(@"should start checking for VR Optional", ^{
expect(testManager.currentState).to(equal(SDLChoiceManagerStateCheckingVoiceOptional));
expect(testManager.transactionQueue.operationCount).to(equal(1)); expect(testManager.transactionQueue.operations.lastObject).to(beAnInstanceOf([SDLCheckChoiceVROptionalOperation class]));
});
describe(@"after the bad vr optional response", ^{
beforeEach(^{
SDLCheckChoiceVROptionalOperation *vrOptionalOp = testManager.transactionQueue.operations.lastObject;
vrOptionalOp.vrOptional = NO;
vrOptionalOp.internalError = [NSError errorWithDomain:@"test" code:0 userInfo:nil];
vrOptionalOp.completionBlock();
});
it(@"should be ready", ^{
expect(testManager.currentState).to(equal(SDLChoiceManagerStateStartupError));
});
});
describe(@"after the vr optional response", ^{
beforeEach(^{
SDLCheckChoiceVROptionalOperation *vrOptionalOp = testManager.transactionQueue.operations.lastObject;
vrOptionalOp.vrOptional = YES;
vrOptionalOp.completionBlock();
});
it(@"should be ready", ^{
expect(testManager.currentState).to(equal(SDLChoiceManagerStateReady));
});
});
describe(@"once shutdown", ^{
beforeEach(^{
[testManager stop];
});
it(@"should shutdown", ^{
expect(testManager.currentState).to(equal(SDLChoiceManagerStateShutdown));
});
});
});
describe(@"once ready", ^{
beforeEach(^{
[testManager.stateMachine setToState:SDLChoiceManagerStateReady fromOldState:SDLChoiceManagerStateCheckingVoiceOptional callEnterTransition:NO];
});
describe(@"preloading choices", ^{
context(@"when some choices are already uploaded", ^{
beforeEach(^{
testManager.preloadedMutableChoices = [NSMutableSet setWithArray:@[testCell1]];
[testManager preloadChoices:@[testCell1, testCell2, testCell3] withCompletionHandler:^(NSError * _Nullable error) {
}];
});
it(@"should properly start the preload", ^{
expect(testManager.pendingPreloadChoices).toNot(contain(testCell1));
expect(testManager.pendingPreloadChoices).to(contain(testCell2));
expect(testManager.pendingPreloadChoices).to(contain(testCell3));
expect(testManager.transactionQueue.operations.firstObject).to(beAnInstanceOf([SDLPreloadChoicesOperation class]));
SDLPreloadChoicesOperation *testOp = testManager.transactionQueue.operations.firstObject;
testOp.completionBlock();
expect(testManager.preloadedChoices).to(contain(testCell1));
expect(testManager.preloadedChoices).to(contain(testCell2));
expect(testManager.preloadedChoices).to(contain(testCell3));
expect(testManager.pendingPreloadChoices).to(haveCount(0));
});
});
context(@"when some choices are already pending", ^{
beforeEach(^{
testManager.pendingMutablePreloadChoices = [NSMutableSet setWithArray:@[testCell1]];
[testManager preloadChoices:@[testCell1, testCell2, testCell3] withCompletionHandler:^(NSError * _Nullable error) {
}];
});
it(@"should properly start the preload", ^{
expect(testManager.pendingPreloadChoices).to(contain(testCell1));
expect(testManager.pendingPreloadChoices).to(contain(testCell2));
expect(testManager.pendingPreloadChoices).to(contain(testCell3));
expect(testManager.transactionQueue.operations.firstObject).to(beAnInstanceOf([SDLPreloadChoicesOperation class]));
SDLPreloadChoicesOperation *testOp = testManager.transactionQueue.operations.firstObject;
testOp.completionBlock();
expect(testManager.preloadedChoices).toNot(contain(testCell1));
expect(testManager.preloadedChoices).to(contain(testCell2));
expect(testManager.preloadedChoices).to(contain(testCell3));
expect(testManager.pendingPreloadChoices).to(haveCount(1));
});
});
});
describe(@"deleting choices", ^{
context(@"used in a pending presentation", ^{
__block SDLPresentChoiceSetOperation *pendingPresentOp = nil;
__block id<SDLChoiceSetDelegate> choiceDelegate = nil;
beforeEach(^{
choiceDelegate = OCMProtocolMock(@protocol(SDLChoiceSetDelegate));
pendingPresentOp = OCMClassMock([SDLPresentChoiceSetOperation class]);
OCMStub(pendingPresentOp.choiceSet.choices).andReturn([NSSet setWithArray:@[testCell1]]);
testManager.pendingPresentOperation = pendingPresentOp;
testManager.pendingPresentationSet = [[SDLChoiceSet alloc] initWithTitle:@"Test" delegate:choiceDelegate choices:@[testCell1]];
testManager.preloadedMutableChoices = [NSMutableSet setWithObject:testCell1];
[testManager deleteChoices:@[testCell1, testCell2, testCell3]];
});
it(@"should properly start the deletion", ^{
expect(testManager.transactionQueue.operations.lastObject).to(beAnInstanceOf([SDLDeleteChoicesOperation class]));
expect(testManager.pendingPresentationSet).to(beNil());
OCMVerify([pendingPresentOp cancel]);
OCMVerify([choiceDelegate choiceSet:[OCMArg any] didReceiveError:[OCMArg any]]);
testManager.transactionQueue.operations.lastObject.completionBlock();
expect(testManager.preloadedChoices).to(beEmpty());
});
});
context(@"used in pending preloads", ^{
__block SDLPreloadChoicesOperation *pendingPreloadOp = nil;
beforeEach(^{
pendingPreloadOp = [[SDLPreloadChoicesOperation alloc] init];
[testManager.transactionQueue addOperation:pendingPreloadOp];
testManager.pendingMutablePreloadChoices = [NSMutableSet setWithObject:testCell1];
[testManager deleteChoices:@[testCell1, testCell2, testCell3]];
});
it(@"should properly start the deletion", ^{
expect(testManager.pendingPreloadChoices).to(beEmpty());
expect(testManager.transactionQueue.operationCount).to(equal(1)); // No delete operation
});
});
});
describe(@"presenting a choice set", ^{
__block SDLChoiceSet *testChoiceSet = nil;
__block NSString *testTitle = @"test title";
__block id<SDLChoiceSetDelegate> choiceDelegate = nil;
__block id<SDLKeyboardDelegate> keyboardDelegate = nil;
__block SDLInteractionMode testMode = SDLInteractionModeBoth;
__block SDLPresentKeyboardOperation *pendingPresentOp = nil;
beforeEach(^{
keyboardDelegate = OCMProtocolMock(@protocol(SDLKeyboardDelegate));
choiceDelegate = OCMProtocolMock(@protocol(SDLChoiceSetDelegate));
testChoiceSet = [[SDLChoiceSet alloc] initWithTitle:testTitle delegate:choiceDelegate choices:@[testCell1, testCell2, testCell3]];
pendingPresentOp = OCMClassMock([SDLPresentKeyboardOperation class]);
testManager.pendingPresentOperation = pendingPresentOp;
testManager.pendingPresentationSet = [[SDLChoiceSet alloc] init];
});
context(@"searchable", ^{
beforeEach(^{
[testManager presentChoiceSet:testChoiceSet mode:testMode withKeyboardDelegate:keyboardDelegate];
});
it(@"should properly start the presentation", ^{
OCMVerify([pendingPresentOp cancel]);
expect(testManager.pendingPresentationSet).to(equal(testChoiceSet));
expect(testManager.transactionQueue.operations).to(haveCount(2));
expect(testManager.transactionQueue.operations.firstObject).to(beAnInstanceOf([SDLPreloadChoicesOperation class]));
expect(testManager.transactionQueue.operations.lastObject).to(beAnInstanceOf([SDLPresentChoiceSetOperation class]));
});
describe(@"after the completion handler is called", ^{
context(@"with an error", ^{
beforeEach(^{
SDLPresentChoiceSetOperation *op = testManager.transactionQueue.operations.lastObject;
op.internalError = [[NSError alloc] init];
op.completionBlock();
});
it(@"should call the error handler", ^{
OCMVerify([choiceDelegate choiceSet:[OCMArg any] didReceiveError:[OCMArg isNotNil]]);
expect(testManager.pendingPresentationSet).to(beNil());
expect(testManager.pendingPresentOperation).to(beNil());
});
});
});
});
context(@"non-searchable", ^{
beforeEach(^{
[testManager presentChoiceSet:testChoiceSet mode:testMode withKeyboardDelegate:nil];
});
it(@"should properly start the presentation", ^{
OCMVerify([pendingPresentOp cancel]);
expect(testManager.pendingPresentationSet).to(equal(testChoiceSet));
expect(testManager.transactionQueue.operations).to(haveCount(2));
expect(testManager.transactionQueue.operations.firstObject).to(beAnInstanceOf([SDLPreloadChoicesOperation class]));
expect(testManager.transactionQueue.operations.lastObject).to(beAnInstanceOf([SDLPresentChoiceSetOperation class]));
});
describe(@"after the completion handler is called", ^{
context(@"with an error", ^{
beforeEach(^{
SDLPresentChoiceSetOperation *op = testManager.transactionQueue.operations.lastObject;
op.internalError = [[NSError alloc] init];
op.completionBlock();
});
it(@"should call the error handler", ^{
OCMVerify([choiceDelegate choiceSet:[OCMArg any] didReceiveError:[OCMArg isNotNil]]);
expect(testManager.pendingPresentationSet).to(beNil());
expect(testManager.pendingPresentOperation).to(beNil());
});
});
});
});
});
describe(@"presenting a keyboard", ^{
__block SDLPresentChoiceSetOperation *pendingPresentOp = nil;
__block NSString *testInitialText = @"Test text";
__block id<SDLKeyboardDelegate> testKeyboardDelegate = nil;
beforeEach(^{
testKeyboardDelegate = OCMProtocolMock(@protocol(SDLKeyboardDelegate));
pendingPresentOp = OCMClassMock([SDLPresentChoiceSetOperation class]);
testManager.pendingPresentOperation = pendingPresentOp;
testManager.pendingPresentationSet = [[SDLChoiceSet alloc] init];
});
it(@"should return a cancelID and should properly start the keyboard presentation with presentKeyboardWithInitialText:keyboardDelegate:", ^{
NSNumber *cancelID = [testManager presentKeyboardWithInitialText:testInitialText delegate:testKeyboardDelegate];
expect(cancelID).toNot(beNil());
OCMVerify([pendingPresentOp cancel]);
expect(testManager.transactionQueue.operations).to(haveCount(1));
expect(testManager.pendingPresentOperation).to(beAnInstanceOf([SDLPresentKeyboardOperation class]));
});
it(@"should return nil and should not start the keyboard presentation if the the keyboard can not be sent to Core", ^{
[testManager.stateMachine setToState:SDLChoiceManagerStateCheckingVoiceOptional fromOldState:SDLChoiceManagerStateShutdown callEnterTransition:NO];
NSNumber *cancelID = [testManager presentKeyboardWithInitialText:testInitialText delegate:testKeyboardDelegate];
expect(cancelID).to(beNil());
OCMReject([pendingPresentOp cancel]);
expect(testManager.transactionQueue.operations).to(haveCount(0));
expect(testManager.pendingPresentOperation).toNot(beAnInstanceOf([SDLPresentKeyboardOperation class]));
});
});
describe(@"dismissing the keyboard", ^{
__block SDLPresentKeyboardOperation *mockKeyboardOp1 = nil;
__block SDLPresentKeyboardOperation *mockKeyboardOp2 = nil;
__block NSOperationQueue *mockQueue = nil;
__block UInt16 testCancelId = 387;
beforeEach(^{
mockKeyboardOp1 = OCMPartialMock([[SDLPresentKeyboardOperation alloc] init]);
OCMStub([mockKeyboardOp1 cancelId]).andReturn(88);
mockKeyboardOp2 = OCMPartialMock([[SDLPresentKeyboardOperation alloc] init]);
OCMStub([mockKeyboardOp2 cancelId]).andReturn(testCancelId);
mockQueue = OCMPartialMock([[NSOperationQueue alloc] init]);
NSArray<SDLAsynchronousOperation *> *keyboardOps = @[mockKeyboardOp1, mockKeyboardOp2];
OCMStub([mockQueue operations]).andReturn(keyboardOps);
testManager.transactionQueue = mockQueue;
});
it(@"should dismiss the keyboard operation with the matching cancelID if it is executing", ^{
OCMStub([mockKeyboardOp2 isExecuting]).andReturn(true);
[testManager dismissKeyboardWithCancelID:@(testCancelId)];
OCMReject([mockKeyboardOp1 dismissKeyboard]);
OCMVerify([mockKeyboardOp2 dismissKeyboard]);
});
it(@"should dismiss the keyboard operation with the matching cancelID if it is not executing", ^{
OCMStub([mockKeyboardOp2 isExecuting]).andReturn(false);
[testManager dismissKeyboardWithCancelID:@(testCancelId)];
OCMReject([mockKeyboardOp1 dismissKeyboard]);
OCMVerify([mockKeyboardOp2 dismissKeyboard]);
});
});
});
});
QuickSpecEnd