سؤال

I'm writing a Cocoa program that interacts with other programs via the Accessibility API. To test these interactions, I want to launch a small tester program and have my tests act upon it. For verification of test results, I thought about querying the tester via distributed objects. For example, I launch the tester, it's main window appears, my test runs code that moves the window to a specific location, then queries the tester program via a distributed object to verify that the window was moved to the correct location.

I have working code that launches the tester via NSTask, and also I have some working code that communicates the results I need via distributed objects, but it only works if I manually launch the tester before the testing code.

I'm stuck with automating the whole test with XCTest. In the setUp method, I launch the tester program via NSTask, but then I have to wait for it to finish launching before running the tests. So from within the tester, I thought I'd throw a notification via NSDistributedNotificationCenter, and wait for the notification in the test code to arrive before starting the testing. But I'm not sure how I should block the test until the notification arrived.

I thought about launching a thread to wait for the notification and busy-wait on a flag from the main thread, but NSDistributedNotificationCenter posts notifications only on the main thread.

Maybe there's an easier way to synchronize my test code and the tester process, and I'm thinking much too complicated?

هل كانت مفيدة؟

المحلول

I succeeded with the first part, that is receiving the distributed notification from within the setUp method, by letting the currentRunLoop run for a while:

- (void)setUp
{
    [super setUp];

    [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(testerRunningNotification:) name:@"TesterReady" object:nil];

    _t = [NSTask launchedTaskWithLaunchPath:[NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"../Tester.app/Contents/MacOS/Tester"] arguments:@[]];

    while(!_testerRunning)
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
}

- (void)testerRunningNotification:(NSNotification *)notification
{
    _testerRunning = YES;
}

Now I only need to figure out what's necessary to get distributed objects working from within a test case.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top