blob: 7576b5576373117b2337fd677bc5630b7c448d80 [file] [log] [blame]
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +00001/*
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
mflodman@webrtc.orgeae79242014-06-05 09:32:51 +000013#include "webrtc/test/run_test.h"
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000014
mflodman@webrtc.orgeae79242014-06-05 09:32:51 +000015// 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.
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000022@interface TestRunner : NSObject {
23 BOOL running_;
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000024}
mflodman@webrtc.orgeae79242014-06-05 09:32:51 +000025- (void)runAllTests:(TestBlock)ignored;
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000026- (BOOL)running;
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000027@end
28
29@implementation TestRunner
30- (id)init {
31 self = [super init];
32 if (self) {
33 running_ = YES;
34 }
35 return self;
36}
37
mflodman@webrtc.orgeae79242014-06-05 09:32:51 +000038- (void)runAllTests:(TestBlock)testBlock {
kthelgasonc0977102017-04-24 00:57:16 -070039 @autoreleasepool {
40 testBlock();
41 running_ = NO;
42 }
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000043}
44
45- (BOOL)running {
46 return running_;
47}
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000048@end
49
50namespace webrtc {
51namespace test {
52
mflodman@webrtc.orgeae79242014-06-05 09:32:51 +000053void RunTest(void(*test)()) {
kthelgasonc0977102017-04-24 00:57:16 -070054 @autoreleasepool {
55 [NSApplication sharedApplication];
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000056
kthelgasonc0977102017-04-24 00:57:16 -070057 // 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];
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000064
kthelgasonc0977102017-04-24 00:57:16 -070065 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
66 while ([testRunner running] && [runLoop runMode:NSDefaultRunLoopMode
67 beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]])
68 ;
69 }
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000070}
71
72} // namespace test
73} // namespace webrtc