Adoption Curve Dot Net

Unit Testing UITabBarController Actions

| Comments

Another outboard brain post: this time, testing tab bar controllers.

Firstly, testing that the tabs are changed after the triggering method:

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
it(@"should update the tabs after the logout button is tapped", ^{

    // The array of tabs BEFORE the logout button was tapped
    NSArray *beforeTabs = [tbc viewControllers];

    // Trigger the method that should swap the tabs around
    [tbc didLogoutSuccessfully];

    // The array of tabs AFTER the logout action was triggered
    NSArray *afterTabs = [tbc viewControllers];

    // The two arrays should be different
    [[beforeTabs shouldNot] equal:afterTabs];

    // There SHOULD NOT be a status view controller present in the array
    NSIndexSet *indexesOfStatusVC = [afterTabs indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj isKindOfClass:[CAStatusViewController class]];
    }];

    [[theValue([indexesOfStatusVC count]) should] equal:theValue(0)];

    // There SHOULD be a login view controller present
    NSIndexSet *indexesOfLoginVC = [afterTabs indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj isKindOfClass:[CALoginViewController class]];
    }];

    [[theValue([indexesOfLoginVC count]) should] equal:theValue(1)];

    // The login view controller should be the first tab
    [[[[afterTabs objectAtIndex:0] class] should] beKindOfClass:[CALoginViewController class]];
});

And testing that the tabs are locked before and after two methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
it(@"should only allow selection of the first tab before login", ^{
    // iterate across all the view controllers in the viewControllers array
    for (int i=1; i < [[tbc viewControllers] count]; i++) {
        id viewController = [[tbc viewControllers] objectAtIndex:i];

        // Test that NO gets returned in response to a selection attempt
        BOOL response = [tbc tabBarController:tbc shouldSelectViewController:viewController];
        [[theValue(response) should] equal:theValue(NO)];
    }
});

it(@"should allow selection of tabs after login", ^{
    // Trigger the login method, which should unlock the tabs
    [tbc didLoginSuccessfully];

    // iterate across all the view controllers in the viewControllers array
    for (int i=0; i < [[tbc viewControllers] count]; i++) {
        id viewController = [[tbc viewControllers] objectAtIndex:i];

        // Test that YES gets returned in response to a selection attempt
        BOOL response = [tbc tabBarController:tbc shouldSelectViewController:viewController];
        [[theValue(response) should] equal:theValue(YES)];
    }
});

Comments