1/* 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#import <Cocoa/Cocoa.h> 12 13#include "webrtc/test/run_test.h" 14 15// Converting a C++ function pointer to an Objective-C block. 16typedef void(^TestBlock)(); 17TestBlock functionToBlock(void(*function)()) { 18 return [^(void) { function(); } copy]; 19} 20 21// Class calling the test function on the platform specific thread. 22@interface TestRunner : NSObject { 23 BOOL running_; 24} 25- (void)runAllTests:(TestBlock)ignored; 26- (BOOL)running; 27@end 28 29@implementation TestRunner 30- (id)init { 31 self = [super init]; 32 if (self) { 33 running_ = YES; 34 } 35 return self; 36} 37 38- (void)runAllTests:(TestBlock)testBlock { 39 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 40 testBlock(); 41 running_ = NO; 42 [pool release]; 43} 44 45- (BOOL)running { 46 return running_; 47} 48@end 49 50namespace webrtc { 51namespace test { 52 53void RunTest(void(*test)()) { 54 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 55 [NSApplication sharedApplication]; 56 57 // Convert the function pointer to an Objective-C block and call on a 58 // separate thread, to avoid blocking the main thread. 59 TestRunner *testRunner = [[TestRunner alloc] init]; 60 TestBlock testBlock = functionToBlock(test); 61 [NSThread detachNewThreadSelector:@selector(runAllTests:) 62 toTarget:testRunner 63 withObject:testBlock]; 64 65 NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 66 while ([testRunner running] && 67 [runLoop runMode:NSDefaultRunLoopMode 68 beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]); 69 70 [testRunner release]; 71 [pool release]; 72} 73 74} // namespace test 75} // namespace webrtc 76