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