summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/DevAPISpecs/SDLMenuManagerSpec.m
blob: 296c4c9335b337a990bb2155eb0a0eb2b5c66f1f (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
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>

#import <SmartDeviceLink/SmartDeviceLink.h>

#import "SDLGlobals.h"
#import "SDLMenuManager.h"
#import "SDLMenuReplaceOperation.h"
#import "TestConnectionManager.h"


@interface SDLMenuCell()

@property (assign, nonatomic) UInt32 parentCellId;
@property (assign, nonatomic) UInt32 cellId;

@end

@interface SDLMenuManager()

@property (weak, nonatomic) id<SDLConnectionManagerType> connectionManager;
@property (weak, nonatomic) SDLFileManager *fileManager;
@property (weak, nonatomic) SDLSystemCapabilityManager *systemCapabilityManager;

@property (strong, nonatomic) NSOperationQueue *transactionQueue;
@property (strong, nonatomic, nullable) SDLWindowCapability *windowCapability;

@property (copy, nonatomic, nullable) SDLHMILevel currentHMILevel;
@property (copy, nonatomic, nullable) SDLSystemContext currentSystemContext;
@property (copy, nonatomic) NSArray<SDLMenuCell *> *currentMenuCells;
@property (strong, nonatomic, nullable) SDLMenuConfiguration *currentMenuConfiguration;

@end

QuickSpecBegin(SDLMenuManagerSpec)

describe(@"menu manager", ^{
    __block SDLMenuManager *testManager = nil;
    __block TestConnectionManager *mockConnectionManager = nil;
    __block SDLFileManager *mockFileManager = nil;
    __block SDLSystemCapabilityManager *mockSystemCapabilityManager = nil;

    __block SDLMenuConfiguration *testMenuConfiguration = nil;

    __block SDLMenuCell *textOnlyCell = nil;
    __block SDLMenuCell *submenuCell = nil;

    beforeEach(^{
        textOnlyCell = [[SDLMenuCell alloc] initWithTitle:@"Test 1" secondaryText:nil tertiaryText:nil icon:nil secondaryArtwork:nil voiceCommands:nil handler:^(SDLTriggerSource  _Nonnull triggerSource) {}];
        submenuCell = [[SDLMenuCell alloc] initWithTitle:@"Test 3" secondaryText:nil tertiaryText:nil icon:nil secondaryArtwork:nil submenuLayout:nil subCells:@[textOnlyCell]];

        testMenuConfiguration = [[SDLMenuConfiguration alloc] initWithMainMenuLayout:SDLMenuLayoutTiles defaultSubmenuLayout:SDLMenuLayoutList];

        mockConnectionManager = [[TestConnectionManager alloc] init];
        mockFileManager = OCMClassMock([SDLFileManager class]);
        mockSystemCapabilityManager = OCMClassMock([SDLSystemCapabilityManager class]);
        testManager = [[SDLMenuManager alloc] initWithConnectionManager:mockConnectionManager fileManager:mockFileManager systemCapabilityManager:mockSystemCapabilityManager];

        SDLImageField *commandIconField = [[SDLImageField alloc] init];
        commandIconField.name = SDLImageFieldNameCommandIcon;
        SDLWindowCapability *windowCapability = [[SDLWindowCapability alloc] init];
        windowCapability.windowID = @(SDLPredefinedWindowsDefaultWindow);
        windowCapability.imageFields = @[commandIconField];
        windowCapability.imageTypeSupported = @[SDLImageTypeDynamic, SDLImageTypeStatic];
        windowCapability.menuLayoutsAvailable = @[SDLMenuLayoutList, SDLMenuLayoutTiles];
        testManager.windowCapability = windowCapability;
    });

    it(@"should instantiate correctly", ^{
        expect(testManager.menuCells).to(beEmpty());

        expect(@(testManager.dynamicMenuUpdatesMode)).to(equal(@(SDLDynamicMenuUpdatesModeOnWithCompatibility)));
        expect(testManager.connectionManager).to(equal(mockConnectionManager));
        expect(testManager.fileManager).to(equal(mockFileManager));
        expect(testManager.systemCapabilityManager).to(equal(mockSystemCapabilityManager));
        expect(testManager.transactionQueue).toNot(beNil());
        expect(testManager.windowCapability).toNot(beNil());
        expect(testManager.currentHMILevel).to(beNil());
        expect(testManager.currentSystemContext).to(beNil());
        expect(testManager.currentMenuCells).to(beEmpty());
        expect(testManager.currentMenuConfiguration).to(beNil());
    });

    describe(@"when the manager stops", ^{
        beforeEach(^{
            [testManager stop];
        });

        it(@"should reset correctly", ^{
            expect(testManager.menuCells).to(beEmpty());

            expect(@(testManager.dynamicMenuUpdatesMode)).to(equal(@(SDLDynamicMenuUpdatesModeOnWithCompatibility)));
            expect(testManager.connectionManager).to(equal(mockConnectionManager));
            expect(testManager.fileManager).to(equal(mockFileManager));
            expect(testManager.systemCapabilityManager).to(equal(mockSystemCapabilityManager));
            expect(testManager.transactionQueue).toNot(beNil());
            expect(testManager.windowCapability).to(beNil());
            expect(testManager.currentHMILevel).to(beNil());
            expect(testManager.currentSystemContext).to(beNil());
            expect(testManager.currentMenuCells).to(beEmpty());
            expect(testManager.currentMenuConfiguration).to(beNil());
        });
    });

    context(@"when in HMI NONE", ^{
        beforeEach(^{
            SDLOnHMIStatus *noneStatus = [[SDLOnHMIStatus alloc] initWithHMILevel:SDLHMILevelNone systemContext:SDLSystemContextMain audioStreamingState:SDLAudioStreamingStateNotAudible videoStreamingState:nil windowID:nil];
            [[NSNotificationCenter defaultCenter] postNotification:[[SDLRPCNotificationNotification alloc] initWithName:SDLDidChangeHMIStatusNotification object:nil rpcNotification:noneStatus]];
        });

        it(@"should not suspend the transaction queue", ^{
            expect(testManager.transactionQueue.isSuspended).to(beTrue());
        });

        // when entering HMI FULL
        describe(@"when entering HMI FULL", ^{
            beforeEach(^{
                SDLOnHMIStatus *onHMIStatus = [[SDLOnHMIStatus alloc] init];
                onHMIStatus.hmiLevel = SDLHMILevelFull;
                onHMIStatus.systemContext = SDLSystemContextMain;

                SDLRPCNotificationNotification *testSystemContextNotification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidChangeHMIStatusNotification object:nil rpcNotification:onHMIStatus];
                [[NSNotificationCenter defaultCenter] postNotification:testSystemContextNotification];
            });

            it(@"should run the transaction queue", ^{
                expect(testManager.transactionQueue.isSuspended).to(beFalse());
            });
        });
    });

    context(@"when the HMI is ready", ^{
        beforeEach(^{
            testManager.currentHMILevel = SDLHMILevelFull;
            testManager.currentSystemContext = SDLSystemContextMain;
        });

        describe(@"setting new menu cells", ^{
            context(@"containing duplicate titles", ^{
                it(@"should not start an operation", ^{
                    testManager.menuCells = @[textOnlyCell, textOnlyCell];
                    expect(testManager.menuCells).to(beEmpty());
                    expect(testManager.transactionQueue.operationCount).to(equal(0));
                });
            });

            context(@"containing duplicate VR commands", ^{
                SDLMenuCell *textAndVRCell1 = [[SDLMenuCell alloc] initWithTitle:@"Test 1" secondaryText:nil tertiaryText:nil icon:nil secondaryArtwork:nil voiceCommands:@[@"Cat", @"Turtle"] handler:^(SDLTriggerSource  _Nonnull triggerSource) {}];
                SDLMenuCell *textAndVRCell2 = [[SDLMenuCell alloc] initWithTitle:@"Test 3" secondaryText:nil tertiaryText:nil icon:nil secondaryArtwork:nil voiceCommands:@[@"Cat", @"Dog"] handler:^(SDLTriggerSource  _Nonnull triggerSource) {}];

                it(@"should not start an operation", ^{
                    testManager.menuCells = @[textAndVRCell1, textAndVRCell2];
                    expect(testManager.menuCells).to(beEmpty());
                    expect(testManager.transactionQueue.operationCount).to(equal(0));
                });
            });

            context(@"if the new menu cells are identical to the old menu cells", ^{
                it(@"should only queue one transaction", ^{
                    testManager.menuCells = @[textOnlyCell];
                    testManager.menuCells = @[textOnlyCell];

                    expect(testManager.menuCells).to(equal(@[textOnlyCell]));
                    expect(testManager.transactionQueue.operationCount).to(equal(1));
                });
            });

            context(@"when a second menu cells update is queued before the first is done", ^{
                it(@"should cancel the first operation", ^{
                    testManager.menuCells = @[textOnlyCell];
                    testManager.menuCells = @[submenuCell];

                    expect(testManager.menuCells).to(equal(@[submenuCell]));
                    expect(testManager.transactionQueue.operationCount).to(equal(2));
                    expect(testManager.transactionQueue.operations[0].isCancelled).to(beTrue());
                });
            });

            context(@"if cells are formed properly", ^{
                it(@"should properly prepare and queue the transaction", ^{
                    testManager.menuCells = @[textOnlyCell];

                    expect(testManager.transactionQueue.operationCount).to(equal(1));
                    expect(testManager.transactionQueue.operations[0]).to(beAnInstanceOf([SDLMenuReplaceOperation class]));

                    // Assign proper current menu
                    SDLMenuReplaceOperation *testOp = testManager.transactionQueue.operations[0];
                    expect(testOp.currentMenu).to(haveCount(0));

                    // Callback proper current menu
                    testOp.currentMenu = @[textOnlyCell];
                    [testOp finishOperation];
                    expect(testManager.currentMenuCells).to(haveCount(1));
                });
            });
        });

        describe(@"updating the menu configuration", ^{
            beforeEach(^{
                testManager.currentHMILevel = SDLHMILevelFull;
                testManager.currentSystemContext = SDLSystemContextMain;
            });

            context(@"if the connection RPC version is less than 6.0.0", ^{
                beforeEach(^{
                    [SDLGlobals sharedGlobals].rpcVersion = [SDLVersion versionWithString:@"5.0.0"];
                });

                it(@"should not queue a menu configuration update", ^{
                    testManager.menuConfiguration = testMenuConfiguration;

                    expect(testManager.menuConfiguration).toNot(equal(testMenuConfiguration));
                    expect(testManager.transactionQueue.operationCount).to(equal(0));
                });
            });

            context(@"if the connection RPC version is greater than or equal to 6.0.0", ^{
                beforeEach(^{
                    [SDLGlobals sharedGlobals].rpcVersion = [SDLVersion versionWithString:@"6.0.0"];
                });

                it(@"should should queue a menu configuration update", ^{
                    testManager.menuConfiguration = testMenuConfiguration;

                    expect(testManager.menuConfiguration).to(equal(testMenuConfiguration));
                    expect(testManager.transactionQueue.operationCount).to(equal(1));
                });

                context(@"when queueing a second task after the first", ^{
                    it(@"should cancel the first task", ^{
                        testManager.menuConfiguration = testMenuConfiguration;
                        testManager.menuConfiguration = [[SDLMenuConfiguration alloc] initWithMainMenuLayout:SDLMenuLayoutList defaultSubmenuLayout:SDLMenuLayoutList];

                        expect(testManager.transactionQueue.operationCount).to(equal(2));
                        expect(testManager.transactionQueue.operations[0].isCancelled).to(beTrue());
                    });
                });
            });
        });

        describe(@"opening the menu", ^{
            beforeEach(^{
                testManager.currentHMILevel = SDLHMILevelFull;
                testManager.currentSystemContext = SDLSystemContextMain;
            });

            context(@"when open menu RPC can be sent", ^{
                beforeEach(^{
                    SDLVersion *oldVersion = [SDLVersion versionWithMajor:6 minor:0 patch:0];
                    id globalMock = OCMPartialMock([SDLGlobals sharedGlobals]);
                    OCMStub([globalMock rpcVersion]).andReturn(oldVersion);
                });

                // should queue an open menu operation for the main menu
                it(@"should queue an open menu operation for the main menu", ^{
                    BOOL canSendRPC = [testManager openMenu:nil];

                    expect(testManager.transactionQueue.operationCount).to(equal(1));
                    expect(canSendRPC).to(equal(YES));
                });

                // should queue an open menu operation for a submenu cell
                it(@"should queue an open menu operation for a submenu cell", ^ {
                    testManager.menuCells = @[submenuCell];
                    BOOL canSendRPC = [testManager openMenu:submenuCell];

                    expect(testManager.transactionQueue.operationCount).to(equal(2));
                    expect(canSendRPC).to(equal(YES));
                });

                it(@"should cancel the first task if a second is queued", ^{
                    testManager.menuCells = @[submenuCell];
                    [testManager openMenu:nil];
                    [testManager openMenu:submenuCell];

                    expect(testManager.transactionQueue.operationCount).to(equal(3));
                    expect(testManager.transactionQueue.operations[1].isCancelled).to(beTrue());
                });
            });

            context(@"when the open menu RPC can not be sent", ^{
                it(@"should not queue an open menu operation when cell has no subcells", ^ {
                    BOOL canSendRPC = [testManager openMenu:textOnlyCell];

                    expect(testManager.transactionQueue.operationCount).to(equal(0));
                    expect(canSendRPC).to(equal(NO));
                });

                it(@"should not queue an open menu operation when RPC version is not at least 6.0.0", ^ {
                    SDLVersion *oldVersion = [SDLVersion versionWithMajor:5 minor:0 patch:0];
                    id globalMock = OCMPartialMock([SDLGlobals sharedGlobals]);
                    OCMStub([globalMock rpcVersion]).andReturn(oldVersion);

                    BOOL canSendRPC = [testManager openMenu:submenuCell];

                    expect(testManager.transactionQueue.operationCount).to(equal(0));
                    expect(canSendRPC).to(equal(NO));
                });

                it(@"should not queue an open menu operation when the cell is not in the menu array", ^ {
                    SDLVersion *oldVersion = [SDLVersion versionWithMajor:6 minor:0 patch:0];
                    id globalMock = OCMPartialMock([SDLGlobals sharedGlobals]);
                    OCMStub([globalMock rpcVersion]).andReturn(oldVersion);

                    BOOL canSendRPC = [testManager openMenu:submenuCell];

                    expect(testManager.transactionQueue.operationCount).to(equal(0));
                    expect(canSendRPC).to(equal(NO));
                });
            });
        });
    });

    describe(@"running menu cell handlers", ^{
        __block SDLMenuCell *cellWithHandler = nil;
        __block BOOL cellCalled = NO;
        __block SDLTriggerSource testTriggerSource = nil;

        beforeEach(^{
            testManager.currentHMILevel = SDLHMILevelFull;
            testManager.currentSystemContext = SDLSystemContextMain;

            cellCalled = NO;
            testTriggerSource = nil;
        });

        context(@"on a main menu cell", ^{
            beforeEach(^{
                cellWithHandler = [[SDLMenuCell alloc] initWithTitle:@"Hello" secondaryText:nil tertiaryText:nil icon:nil secondaryArtwork:nil voiceCommands:nil handler:^(SDLTriggerSource  _Nonnull triggerSource) {
                    cellCalled = YES;
                    testTriggerSource = triggerSource;
                }];
                cellWithHandler.cellId = 1;

                testManager.menuCells = @[cellWithHandler];
            });

            it(@"should call the cell handler", ^{
                SDLOnCommand *onCommand = [[SDLOnCommand alloc] init];
                onCommand.cmdID = @1;
                onCommand.triggerSource = SDLTriggerSourceMenu;

                SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveCommandNotification object:nil rpcNotification:onCommand];
                [[NSNotificationCenter defaultCenter] postNotification:notification];

                expect(cellCalled).to(beTrue());
                expect(testTriggerSource).to(equal(SDLTriggerSourceMenu));
            });
        });

        context(@"on a submenu menu cell", ^{
            beforeEach(^{
                cellWithHandler = [[SDLMenuCell alloc] initWithTitle:@"Hello" secondaryText:nil tertiaryText:nil icon:nil secondaryArtwork:nil voiceCommands:nil handler:^(SDLTriggerSource  _Nonnull triggerSource) {
                    cellCalled = YES;
                    testTriggerSource = triggerSource;
                }];
                cellWithHandler.cellId = 2;

                SDLMenuCell *submenuCell = [[SDLMenuCell alloc] initWithTitle:@"Submenu" secondaryText:nil tertiaryText:nil icon:nil secondaryArtwork:nil submenuLayout:SDLMenuLayoutTiles subCells:@[cellWithHandler]];
                submenuCell.cellId = 1;

                cellWithHandler.parentCellId = 1;

                testManager.menuCells = @[submenuCell];
            });

            it(@"should call the cell handler", ^{
                SDLOnCommand *onCommand = [[SDLOnCommand alloc] init];
                onCommand.cmdID = @2;
                onCommand.triggerSource = SDLTriggerSourceMenu;

                SDLRPCNotificationNotification *notification = [[SDLRPCNotificationNotification alloc] initWithName:SDLDidReceiveCommandNotification object:nil rpcNotification:onCommand];
                [[NSNotificationCenter defaultCenter] postNotification:notification];

                expect(cellCalled).to(beTrue());
                expect(testTriggerSource).to(equal(SDLTriggerSourceMenu));
            });
        });
    });
});

QuickSpecEnd