0

I am trying to test if a presented view controller exists when I have automated the tapping of a table row cell. When I attempt to test if the controller's presentedViewController is of a given type of class it always results to nil. I'm assuming the new presented view controller is transitioning to being the presented view controller and that is why the [controller presentedViewController] is nil.

I am using the Cedar BDD testing framework. I have installed the PivotalCore libs to provide the automated 'tap' functionality.

Here is the Spec code:

#import <Cedar-iOS/Cedar-iOS.h>
#import "UITableViewCell+Spec.h"

#import "FMNavigatorViewController.h"

using namespace Cedar::Matchers;
using namespace Cedar::Doubles;

SPEC_BEGIN(FMNavigatorViewControllerSpec)

describe(@"FMNavigatorViewController", ^{
    __block UINavigationController *nav;
    __block FMNavigatorViewController *controller;

    beforeEach(^{
        FSHandle *documents = [FSHandle handleAtUrl:[[BasicFileManager sharedManager] documentsUrl] isDirectory:YES];
        // @todo Remove all files from Recent Files and Local Files.
        // Remove all configured remote connections.
        NSArray *contents = [[BasicFileManager sharedManager] contentsOfDirectoryAtURL:documents.url];
        for (NSURL *url in contents) {
            if (! [url.lastPathComponent isEqualToString:@"Local Files"] && ! [url.lastPathComponent isEqualToString:@"Recent Files"]) {
                NSLog(@"WARNING: Deleting Manager: %@", url.lastPathComponent);
                FileManager *manager = [FileManager fileManagerWithName:url.lastPathComponent];
                [manager deleteFileManager];
            }
        }
        // Create view.
        controller = [[FMNavigatorViewController alloc] initWithDirectory:documents];
        nav = [[UINavigationController alloc] initWithRootViewController:controller];
        // Initiates view lifecycle. Accessing the 'view' will automatically
        // create it.
        nav.view should_not be_nil;
        // Doesn't get called unless properly added to a heirarchy -- which I
        // haven't found the correct process for yet.
        [controller viewWillAppear:NO];
    });

    it(@"should contain Local and Recent Files with no other connections", ^{
        controller should be_instance_of([FMNavigatorViewController class]);
        // Local and Remote Connection Groups
        [controller.tableView.dataSource numberOfSectionsInTableView:controller.tableView] should equal(2);
        // Recent Files and Local Files
        [controller.tableView.dataSource tableView:controller.tableView numberOfRowsInSection:0] should equal(2);
        // Enforce order: Local Files then Recent Files.
        [[[controller.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] textLabel] text] should equal(@"Local Files");
        [[[controller.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] textLabel] text] should equal(@"Recent Files");
        // The second group should have one row with description.
        [controller.tableView.dataSource tableView:controller.tableView numberOfRowsInSection:1] should equal(1);
        [[[controller.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] textLabel] text] should equal(NSLocalizedString(@"CreateRemoteConnection", @""));
    });

    it(@"should display the FM wizard view", ^{
        [[controller.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] tap];
        controller.presentedViewController should_not be_nil;
        //[nav presentedViewController] should be_instance_of([UINavigationController class]);
        //[controller presentedViewController] should be_instance_of([UINavigationController class]);
    });

});

SPEC_END

The very last tests contain the code in question. My question is: do I need to wait a second or two before testing if the presentedViewController is not nil? If so, how do I do this?

Here is the code that should execute once the cell has been tapped:

FMWizardViewController *controller = [FMWizardViewController new];
[controller setDelegate:self];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
nav.navigationBar.tintColor = UIColorFromRGB(BNAV_TINT_COLOR);
[self presentViewController:nav animated:YES completion:nil];

I double checked to make sure that this code actually gets ran after the cell is tapped; it does.

Thank you!

PeqNP
  • 1,363
  • 15
  • 26

1 Answers1

0

With the help of the Google cedar-discuss group (https://groups.google.com/forum/#!forum/cedar-discuss) I was able to figure it out.

What needed to happen was:

  1. The main run loop needed to be advanced for the presentedViewController to be instantiated and associated to the respective view controller
  2. The entire view hierarchy needed to be created (window, navigation controller, FMNavigationController)

#import <Cedar-iOS/Cedar-iOS.h>

#import "UITableViewCell+Spec.h"

#import "FMNavigatorViewController.h"

// << -- Add this
#define tickRunLoop (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, false))

using namespace Cedar::Matchers;
using namespace Cedar::Doubles;

SPEC_BEGIN(FMNavigatorViewControllerSpec)

describe(@"FMNavigatorViewController", ^{
    __block UIWindow *window; // <<-- and this,
    __block UINavigationController *nav;
    __block FMNavigatorViewController *controller;

    beforeEach(^{
        window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        FSHandle *documents = [FSHandle handleAtUrl:[[BasicFileManager sharedManager] documentsUrl] isDirectory:YES];
        // @todo Remove all files from Recent Files and Local Files.
        // Remove all configured remote connections.
        NSArray *contents = [[BasicFileManager sharedManager] contentsOfDirectoryAtURL:documents.url];
        for (NSURL *url in contents) {
            if (! [url.lastPathComponent isEqualToString:@"Local Files"] && ! [url.lastPathComponent isEqualToString:@"Recent Files"]) {
                NSLog(@"WARNING: Deleting Manager: %@", url.lastPathComponent);
                FileManager *manager = [FileManager fileManagerWithName:url.lastPathComponent];
                [manager deleteFileManager];
            }
        }
        // Create view.
        controller = [[FMNavigatorViewController alloc] initWithDirectory:documents];
        nav = [[UINavigationController alloc] initWithRootViewController:controller];
        window.rootViewController = nav;
        [window makeKeyAndVisible];
        // Initiates view lifecycle. Accessing the 'view' will automatically
        // create it.
        nav.view should_not be_nil;
        // Doesn't get called unless properly added to a heirarchy -- which I
        // haven't found the correct process for yet.
        [controller viewWillAppear:NO];
    });

    it(@"should contain Local and Recent Files with no other connections", ^{
        controller should be_instance_of([FMNavigatorViewController class]);
        // Local and Remote Connection Groups
        [controller.tableView.dataSource numberOfSectionsInTableView:controller.tableView] should equal(2);
        // Recent Files and Local Files
        [controller.tableView.dataSource tableView:controller.tableView numberOfRowsInSection:0] should equal(2);
        // Enforce order: Local Files then Recent Files.
        [[[controller.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] textLabel] text] should equal(@"Local Files");
        [[[controller.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] textLabel] text] should equal(@"Recent Files");
        // The second group should have one row with description.
        [controller.tableView.dataSource tableView:controller.tableView numberOfRowsInSection:1] should equal(1);
        [[[controller.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] textLabel] text] should equal(NSLocalizedString(@"CreateRemoteConnection", @""));
    });

    it(@"should display the FM wizard view", ^{
        // Sanity to ensure we are tapping the right cell.
        [[controller.tableView.visibleCells[2] textLabel] text] should equal(NSLocalizedString(@"CreateRemoteConnection", @""));
        [controller.tableView.visibleCells[2] tap];
        tickRunLoop; // <<-- and this.
        controller.presentedViewController should_not be_nil;
        controller.presentedViewController should be_instance_of([UINavigationController class]);
    });

});

SPEC_END

All tests pass now.

One of the maintainers said that the issue is most likely related to some internal changes made in iOS 8; where it doesn't associate the presentedViewController value as soon as presentViewController:animated:completion: is called. It should be addressed in the future.

I hope this helps someone!

UPDATE

I forgot to add that advancing the run loop is not best practice. This should be considered a stop-gap until the issue is fixed.

PeqNP
  • 1,363
  • 15
  • 26