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

#import "SDLStateMachine.h"
#import "TestStateMachineTarget.h"

QuickSpecBegin(SDLStateMachineSpec)

describe(@"A state machine", ^{
    __block SDLStateMachine *testStateMachine = nil;
    __block TestStateMachineTarget *testTarget = nil;
    
    __block SDLState *initialState = @"Initial";
    __block SDLState *secondState = @"Second";
    __block SDLState *thirdState = @"Third";
    
    __block NSDictionary *allowableStateTransitions = @{
                                                        initialState: @[secondState],
                                                        secondState: @[thirdState],
                                                        thirdState: @[initialState]
                                                        };
    
    beforeEach(^{
        testTarget = [[TestStateMachineTarget alloc] init];
        
        testStateMachine = [[SDLStateMachine alloc] initWithTarget:testTarget initialState:initialState states:allowableStateTransitions];
    });
    
    describe(@"when the state machine initializes", ^{
        __block BOOL isInitialState = NO;
        
        beforeEach(^{
            isInitialState = [testStateMachine isCurrentState:initialState];
        });
        
        it(@"should be in a correct initial state", ^{
            expect(@(isInitialState)).to(equal(@YES));
            expect(testStateMachine.states).to(equal(allowableStateTransitions));
            expect(testStateMachine.target).to(equal(testTarget));
        });
        
        describe(@"when transitioning correctly", ^{
            __block BOOL didTransitionNotificationCalled = NO;
            
            __block NSDictionary<NSString *, NSString *> *willLeaveMethodInfo = nil;
            __block NSDictionary<NSString *, NSString *> *willTransitionMethodInfo = nil;
            __block NSDictionary<NSString *, NSString *> *didTransitionMethodInfo = nil;
            __block NSDictionary<NSString *, NSString *> *didEnterMethodInfo = nil;
            
            beforeEach(^{
                [[NSNotificationCenter defaultCenter] addObserverForName:testStateMachine.transitionNotificationName object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
                    didTransitionNotificationCalled = YES;
                }];
                
                testTarget.callback = ^(NSString * _Nonnull type, NSString * _Nullable oldState, NSString * _Nullable newState) {
                    if ([type isEqualToString:SDLStateMachineTransitionTypeWillLeave]) {
                        willLeaveMethodInfo = @{ SDLStateMachineNotificationInfoKeyOldState: oldState };
                    } else if ([type isEqualToString:SDLStateMachineTransitionTypeWillTransition]) {
                        willTransitionMethodInfo = @{ SDLStateMachineNotificationInfoKeyOldState: oldState, SDLStateMachineNotificationInfoKeyNewState: newState };
                    } else if ([type isEqualToString:SDLStateMachineTransitionTypeDidTransition]) {
                        didTransitionMethodInfo = @{ SDLStateMachineNotificationInfoKeyOldState: oldState, SDLStateMachineNotificationInfoKeyNewState: newState };
                    } else if ([type isEqualToString:SDLStateMachineTransitionTypeDidEnter]) {
                        didEnterMethodInfo = @{ SDLStateMachineNotificationInfoKeyNewState: newState };
                    }
                };
                
                // Valid transition
                [testStateMachine transitionToState:secondState];
            });
            
            it(@"should end up in the correct state", ^{
                expect(@([testStateMachine isCurrentState:secondState])).to(equal(@YES));
            });
            
            it(@"should call all appropriate notifications", ^{
                expect(@(didTransitionNotificationCalled)).to(equal(@YES));
            });
            
            it(@"should call all the appropriate state methods", ^{
                expect(willLeaveMethodInfo[SDLStateMachineNotificationInfoKeyOldState]).to(match(initialState));
                expect(willLeaveMethodInfo[SDLStateMachineNotificationInfoKeyNewState]).to(beNil());
                expect(willTransitionMethodInfo[SDLStateMachineNotificationInfoKeyOldState]).to(match(initialState));
                expect(willTransitionMethodInfo[SDLStateMachineNotificationInfoKeyNewState]).to(match(secondState));
                expect(didTransitionMethodInfo[SDLStateMachineNotificationInfoKeyOldState]).to(match(initialState));
                expect(didTransitionMethodInfo[SDLStateMachineNotificationInfoKeyNewState]).to(match(secondState));
                expect(didEnterMethodInfo[SDLStateMachineNotificationInfoKeyOldState]).to(beNil());
                expect(didEnterMethodInfo[SDLStateMachineNotificationInfoKeyNewState]).to(match(secondState));
            });
        });
        
        describe(@"when transitioning incorrectly", ^{
            __block BOOL willLeaveNotificationCalled = NO;
            __block BOOL willTransitionNotificationCalled = NO;
            __block BOOL didTransitionNotificationCalled = NO;
            __block BOOL didEnterNotificationCalled = NO;
            
            __block NSDictionary<NSString *, NSString *> *willLeaveMethodInfo = nil;
            __block NSDictionary<NSString *, NSString *> *willTransitionMethodInfo = nil;
            __block NSDictionary<NSString *, NSString *> *didTransitionMethodInfo = nil;
            __block NSDictionary<NSString *, NSString *> *didEnterMethodInfo = nil;
            
            beforeEach(^{
                [[NSNotificationCenter defaultCenter] addObserverForName:testStateMachine.transitionNotificationName object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
                    if ([note.userInfo[@"type"] isEqualToString:@"willLeave"]) { willLeaveNotificationCalled = YES; }
                    else if ([note.userInfo[@"type"] isEqualToString:@"willTransition"]) { willTransitionNotificationCalled = YES; }
                    else if ([note.userInfo[@"type"] isEqualToString:@"didTransition"]) { didTransitionNotificationCalled = YES; }
                    else if ([note.userInfo[@"type"] isEqualToString:@"didEnter"]) { didEnterNotificationCalled = YES; }
                }];
                
                testTarget.callback = ^(NSString * _Nonnull type, NSString * _Nullable oldState, NSString * _Nullable newState) {
                    if ([type isEqualToString:SDLStateMachineTransitionTypeWillLeave]) {
                        willLeaveMethodInfo = @{ SDLStateMachineNotificationInfoKeyOldState: oldState, SDLStateMachineNotificationInfoKeyNewState: newState };
                    } else if ([type isEqualToString:SDLStateMachineTransitionTypeWillTransition]) {
                        willTransitionMethodInfo = @{ SDLStateMachineNotificationInfoKeyOldState: oldState, SDLStateMachineNotificationInfoKeyNewState: newState };
                    } else if ([type isEqualToString:SDLStateMachineTransitionTypeDidTransition]) {
                        didTransitionMethodInfo = @{ SDLStateMachineNotificationInfoKeyOldState: oldState, SDLStateMachineNotificationInfoKeyNewState: newState };
                    } else if ([type isEqualToString:SDLStateMachineTransitionTypeDidEnter]) {
                        didEnterMethodInfo = @{ SDLStateMachineNotificationInfoKeyOldState: oldState, SDLStateMachineNotificationInfoKeyNewState: newState };
                    }
                };
            });
            
            it(@"should throw an exception trying to transition incorrectly and nothing should be called", ^{
                // Side effects, but I can't think of any other way around it.
                expectAction(^{
                    [testStateMachine transitionToState:thirdState];
                }).to(raiseException().named(NSInternalInconsistencyException));
                
                expect(@([testStateMachine isCurrentState:initialState])).to(equal(@YES));
                expect(@([testStateMachine isCurrentState:thirdState])).to(equal(@NO));
                expect(@(willLeaveNotificationCalled)).to(equal(@NO));
                expect(@(willTransitionNotificationCalled)).to(equal(@NO));
                expect(@(didTransitionNotificationCalled)).to(equal(@NO));
                expect(@(didEnterNotificationCalled)).to(equal(@NO));
                expect(willLeaveMethodInfo).to(beNil());
                expect(willTransitionMethodInfo).to(beNil());
                expect(didTransitionMethodInfo).to(beNil());
                expect(didEnterMethodInfo).to(beNil());
            });
        });
    });
});

QuickSpecEnd