blob: 38c6c8f8c1b61636d87035181d363c50d2dfea99 [file] [log] [blame]
mflodman@webrtc.org7f944f32013-05-27 15:52:381/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "test/run_test.h"
mflodman@webrtc.org7f944f32013-05-27 15:52:3814
mflodman@webrtc.orgeae79242014-06-05 09:32:5115// 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:3822@interface TestRunner : NSObject {
23 BOOL running_;
mflodman@webrtc.org7f944f32013-05-27 15:52:3824}
mflodman@webrtc.orgeae79242014-06-05 09:32:5125- (void)runAllTests:(TestBlock)ignored;
mflodman@webrtc.org7f944f32013-05-27 15:52:3826- (BOOL)running;
mflodman@webrtc.org7f944f32013-05-27 15:52:3827@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:5138- (void)runAllTests:(TestBlock)testBlock {
kthelgasonc0977102017-04-24 07:57:1639 @autoreleasepool {
40 testBlock();
41 running_ = NO;
42 }
mflodman@webrtc.org7f944f32013-05-27 15:52:3843}
44
45- (BOOL)running {
46 return running_;
47}
mflodman@webrtc.org7f944f32013-05-27 15:52:3848@end
49
50namespace webrtc {
51namespace test {
52
mflodman@webrtc.orgeae79242014-06-05 09:32:5153void RunTest(void(*test)()) {
kthelgasonc0977102017-04-24 07:57:1654 @autoreleasepool {
55 [NSApplication sharedApplication];
mflodman@webrtc.org7f944f32013-05-27 15:52:3856
kthelgasonc0977102017-04-24 07:57:1657 // 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:3864
kthelgasonc0977102017-04-24 07:57:1665 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:3870}
71
72} // namespace test
73} // namespace webrtc