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

#import "SDLAddCommand.h"
#import "SDLAddCommandResponse.h"
#import "SDLDeleteCommand.h"
#import "SDLFileManager.h"
#import "SDLHMILevel.h"
#import "SDLOnHMIStatus.h"
#import "SDLPredefinedWindows.h"
#import "SDLRPCNotificationNotification.h"
#import "SDLVoiceCommand.h"
#import "SDLVoiceCommandManager.h"
#import "SDLVoiceCommandUpdateOperation.h"
#import "TestConnectionManager.h"

@interface SDLVoiceCommandUpdateOperation ()

@property (strong, nonatomic) NSMutableArray<SDLVoiceCommand *> *currentVoiceCommands;

@end

@interface SDLVoiceCommand()

@property (assign, nonatomic) UInt32 commandId;

@end

@interface SDLVoiceCommandManager()

@property (weak, nonatomic) id<SDLConnectionManagerType> connectionManager;

@property (strong, nonatomic) NSOperationQueue *transactionQueue;
@property (copy, nonatomic, nullable) SDLHMILevel currentLevel;

@property (assign, nonatomic) UInt32 lastVoiceCommandId;
@property (copy, nonatomic) NSArray<SDLVoiceCommand *> *currentVoiceCommands;

@end

UInt32 const VoiceCommandIdMin = 1900000000;

QuickSpecBegin(SDLVoiceCommandManagerSpec)

describe(@"voice command manager", ^{
    __block SDLVoiceCommandManager *testManager = nil;
    __block TestConnectionManager *mockConnectionManager = nil;

    __block SDLVoiceCommand *testVoiceCommand = [[SDLVoiceCommand alloc] initWithVoiceCommands:@[@"Test 1"] handler:^{}];
    __block SDLVoiceCommand *testVoiceCommand2 = [[SDLVoiceCommand alloc] initWithVoiceCommands:@[@"Test 2"] handler:^{}];
    __block SDLOnHMIStatus *newHMIStatus = [[SDLOnHMIStatus alloc] init];
    __block NSArray<SDLVoiceCommand *> *testVCArray = nil;

    beforeEach(^{
        testVCArray = @[testVoiceCommand];
        mockConnectionManager = [[TestConnectionManager alloc] init];
        testManager = [[SDLVoiceCommandManager alloc] initWithConnectionManager:mockConnectionManager];
    });

    // should instantiate correctly
    it(@"should instantiate correctly", ^{
        expect(testManager.connectionManager).to(equal(mockConnectionManager));
        expect(testManager.currentLevel).to(beNil());
        expect(testManager.voiceCommands).to(beEmpty());
        expect(testManager.lastVoiceCommandId).to(equal(VoiceCommandIdMin));
        expect(testManager.currentVoiceCommands).to(beEmpty());
        expect(testManager.transactionQueue).toNot(beNil());
    });

    // updating voice commands before HMI is ready
    describe(@"updating voice commands before HMI is ready", ^{

        // when in HMI NONE
        context(@"when in HMI NONE", ^{
            beforeEach(^{
                newHMIStatus.hmiLevel = SDLHMILevelNone;
                newHMIStatus.windowID = @(SDLPredefinedWindowsDefaultWindow);

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

            it(@"should not update", ^{
                testManager.voiceCommands = @[testVoiceCommand];
                expect(testManager.transactionQueue.isSuspended).to(beTrue());
                expect(testManager.transactionQueue.operationCount).to(equal(1));
            });
        });

        // when no HMI level has been received
        context(@"when no HMI level has been received", ^{
            it(@"should not update", ^{
                testManager.voiceCommands = @[testVoiceCommand];
                expect(testManager.transactionQueue.isSuspended).to(beTrue());
                expect(testManager.transactionQueue.operationCount).to(equal(1));
            });
        });
    });

    // updating voice commands
    describe(@"when voice commands are set", ^{
        beforeEach(^{
            newHMIStatus.hmiLevel = SDLHMILevelFull;
            newHMIStatus.windowID = @(SDLPredefinedWindowsDefaultWindow);

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

            testManager.voiceCommands = testVCArray;
        });

        // should properly update a command
        it(@"should properly update a command", ^{
            expect(testManager.voiceCommands.firstObject.commandId).to(equal(VoiceCommandIdMin));
            expect(testManager.transactionQueue.isSuspended).to(beFalse());
            expect(testManager.transactionQueue.operations).to(haveCount(1));
        });

        // when new voice commands is identical to the existing ones
        describe(@"when new voice commands is identical to the existing ones", ^{
            beforeEach(^{
                testManager.voiceCommands = testVCArray;
            });

            // should only have one operation
            it(@"should only have one operation", ^{
                expect(testManager.transactionQueue.operations).to(haveCount(1));
            });
        });

        // when new voice commands are set
        describe(@"when new voice commands are set", ^{
            beforeEach(^{
                testManager.voiceCommands = @[testVoiceCommand2];
            });

            // should queue another operation
            it(@"should queue another operation", ^{
                expect(testManager.transactionQueue.operations).to(haveCount(2));
            });

            // when the first operation finishes and updates the current voice commands
            describe(@"when the first operation finishes and updates the current voice commands", ^{
                beforeEach(^{
                    SDLVoiceCommandUpdateOperation *firstOp = testManager.transactionQueue.operations[0];
                    firstOp.currentVoiceCommands = [@[testVoiceCommand2] mutableCopy];
                    [firstOp finishOperation];

                    [NSThread sleepForTimeInterval:0.5];
                });

                it(@"should update the second operation", ^{
                    expect(((SDLVoiceCommandUpdateOperation *)testManager.transactionQueue.operations.firstObject).oldVoiceCommands.firstObject).withTimeout(3.0).toEventually(equal(testVoiceCommand2));
                });
            });
        });
    });

    // on disconnect
    context(@"on disconnect", ^{
        beforeEach(^{
            [testManager stop];
        });

        it(@"should reset correctly", ^{
            expect(testManager.connectionManager).to(equal(mockConnectionManager));
            expect(testManager.voiceCommands).to(beEmpty());
            expect(testManager.currentLevel).to(beNil());
            expect(testManager.lastVoiceCommandId).to(equal(VoiceCommandIdMin));
            expect(testManager.currentVoiceCommands).to(beEmpty());
        });
    });
});

QuickSpecEnd