summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/SDLSoftButtonObjectSpec.m
blob: 201b08dd3b1fe33ad87c853a4a30353cb60285c3 (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
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>

#import "SDLArtwork.h"
#import "SDLSoftButton.h"
#import "SDLSoftButtonObject.h"
#import "SDLSoftButtonState.h"

QuickSpecBegin(SDLSoftButtonObjectSpec)

describe(@"a soft button object", ^{
    __block SDLSoftButtonObject *testObject = nil;
    __block NSString *testObjectName = @"Test Object Name";

    context(@"with a single state", ^{
        __block SDLSoftButtonState *testSingleState = OCMClassMock([SDLSoftButtonState class]);
        __block NSString *testSingleStateName = @"Test state name";
        __block SDLSoftButton *testSingleStateSoftButton = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"Some Text" image:nil highlighted:NO buttonId:0 systemAction:SDLSystemActionDefaultAction handler:nil];

        beforeEach(^{
            OCMStub(testSingleState.name).andReturn(testSingleStateName);
            OCMStub(testSingleState.softButton).andReturn(testSingleStateSoftButton);

            testObject = [[SDLSoftButtonObject alloc] initWithName:testObjectName state:testSingleState handler:nil];
        });

        it(@"should create correctly", ^{
            expect(testObject.name).to(equal(testObjectName));
            expect(testObject.currentState.name).to(equal(testSingleState.name));
            expect(testObject.currentStateSoftButton).to(equal(testSingleStateSoftButton));
            expect(testObject.states).to(haveCount(1));
        });

        it(@"should not allow transitioning to another state", ^{
            BOOL performedTransition = [testObject transitionToStateNamed:@"Some other state"];
            expect(performedTransition).to(beFalsy());
        });

        it(@"should return a state when asked and not when incorrect", ^{
            SDLSoftButtonState *returnedState = [testObject stateWithName:testSingleStateName];
            expect(returnedState).toNot(beNil());

            returnedState = [testObject stateWithName:@"Some other state"];
            expect(returnedState).to(beNil());
        });
    });

    context(@"with a single state implicitly created", ^{
        NSString *testText = @"Hello";
        SDLArtwork *testArt = [[SDLArtwork alloc] initWithStaticIcon:SDLStaticIconNameKey];

        beforeEach(^{
            testObject = [[SDLSoftButtonObject alloc] initWithName:testObjectName text:testText artwork:testArt handler:nil];
        });

        it(@"should create correctly", ^{
            expect(testObject.name).to(equal(testObjectName));
            expect(testObject.currentState.name).to(equal(testObjectName));
            expect(testObject.currentState.text).to(equal(testText));
            expect(testObject.currentState.artwork).to(equal(testArt));
            expect(testObject.states).to(haveCount(1));
        });

        it(@"should not allow transitioning to another state", ^{
            BOOL performedTransition = [testObject transitionToStateNamed:@"Some other state"];
            expect(performedTransition).to(beFalse());
        });

        it(@"should return a state when asked and not when incorrect", ^{
            SDLSoftButtonState *returnedState = [testObject stateWithName:testObjectName];
            expect(returnedState).toNot(beNil());

            returnedState = [testObject stateWithName:@"Some other state"];
            expect(returnedState).to(beNil());
        });
    });

    context(@"with multiple states", ^{
        __block SDLSoftButtonState *testFirstState = OCMClassMock([SDLSoftButtonState class]);
        __block NSString *testFirstStateName = @"Test First Name";
        __block SDLSoftButton *testFirstStateSoftButton = [[SDLSoftButton alloc] initWithType:SDLSoftButtonTypeText text:@"Some Text" image:nil highlighted:NO buttonId:0 systemAction:SDLSystemActionDefaultAction handler:nil];

        __block SDLSoftButtonState *testSecondState = OCMClassMock([SDLSoftButtonState class]);
        __block NSString *testSecondStateName = @"Test Second Name";

        beforeEach(^{
            OCMStub(testFirstState.name).andReturn(testFirstStateName);
            OCMStub(testFirstState.softButton).andReturn(testFirstStateSoftButton);

            OCMStub(testSecondState.name).andReturn(testSecondStateName);

            testObject = [[SDLSoftButtonObject alloc] initWithName:testObjectName states:@[testFirstState, testSecondState] initialStateName:testFirstStateName handler:nil];
        });

        it(@"should create correctly", ^{
            expect(testObject.name).to(equal(testObjectName));
            expect(testObject.currentState.name).to(equal(testFirstState.name));
            expect(testObject.currentStateSoftButton).to(equal(testFirstStateSoftButton));
            expect(testObject.states).to(haveCount(2));
        });

        it(@"should transitioning to the second state", ^{
            BOOL performedTransition = [testObject transitionToStateNamed:testSecondStateName];
            expect(performedTransition).to(beTruthy());
            expect(testObject.currentState.name).to(equal(testSecondStateName));
        });

        it(@"should return a current when asked and not when incorrect", ^{
            SDLSoftButtonState *returnedState = [testObject stateWithName:testFirstStateName];
            expect(returnedState).toNot(beNil());

            returnedState = [testObject stateWithName:testSecondStateName];
            expect(returnedState).toNot(beNil());

            returnedState = [testObject stateWithName:@"Some other state"];
            expect(returnedState).to(beNil());
        });
    });
});

QuickSpecEnd