mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "test/run_test.h" |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 14 | |
mflodman@webrtc.org | eae7924 | 2014-06-05 09:32:51 | [diff] [blame] | 15 | // Converting a C++ function pointer to an Objective-C block. |
| 16 | typedef void(^TestBlock)(); |
| 17 | TestBlock functionToBlock(void(*function)()) { |
| 18 | return [^(void) { function(); } copy]; |
| 19 | } |
| 20 | |
| 21 | // Class calling the test function on the platform specific thread. |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 22 | @interface TestRunner : NSObject { |
| 23 | BOOL running_; |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 24 | } |
mflodman@webrtc.org | eae7924 | 2014-06-05 09:32:51 | [diff] [blame] | 25 | - (void)runAllTests:(TestBlock)ignored; |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 26 | - (BOOL)running; |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 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 | |
mflodman@webrtc.org | eae7924 | 2014-06-05 09:32:51 | [diff] [blame] | 38 | - (void)runAllTests:(TestBlock)testBlock { |
kthelgason | c097710 | 2017-04-24 07:57:16 | [diff] [blame] | 39 | @autoreleasepool { |
| 40 | testBlock(); |
| 41 | running_ = NO; |
| 42 | } |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | - (BOOL)running { |
| 46 | return running_; |
| 47 | } |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 48 | @end |
| 49 | |
| 50 | namespace webrtc { |
| 51 | namespace test { |
| 52 | |
mflodman@webrtc.org | eae7924 | 2014-06-05 09:32:51 | [diff] [blame] | 53 | void RunTest(void(*test)()) { |
kthelgason | c097710 | 2017-04-24 07:57:16 | [diff] [blame] | 54 | @autoreleasepool { |
| 55 | [NSApplication sharedApplication]; |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 56 | |
kthelgason | c097710 | 2017-04-24 07:57:16 | [diff] [blame] | 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]; |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 64 | |
kthelgason | c097710 | 2017-04-24 07:57:16 | [diff] [blame] | 65 | NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; |
| 66 | while ([testRunner running] && [runLoop runMode:NSDefaultRunLoopMode |
| 67 | beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]) |
| 68 | ; |
| 69 | } |
mflodman@webrtc.org | 7f944f3 | 2013-05-27 15:52:38 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace test |
| 73 | } // namespace webrtc |