1/*
2 *  Copyright (c) 2012 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 "webrtc/test/testsupport/mac/run_threaded_main_mac.h"
12
13#import <Cocoa/Cocoa.h>
14
15// This class passes parameter from main to the worked thread and back.
16@interface AutoTestInWorkerThread : NSObject {
17  int    argc_;
18  char** argv_;
19  int    result_;
20  bool   done_;
21}
22
23- (void)setDone:(bool)done;
24- (bool)done;
25- (void)setArgc:(int)argc argv:(char**)argv;
26- (int) result;
27- (void)runTest:(NSObject*)ignored;
28
29@end
30
31@implementation AutoTestInWorkerThread
32
33- (void)setDone:(bool)done {
34  done_ = done;
35}
36
37- (bool)done {
38  return done_;
39}
40
41- (void)setArgc:(int)argc argv:(char**)argv {
42  argc_ = argc;
43  argv_ = argv;
44}
45
46- (void)runTest:(NSObject*)ignored {
47    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
48
49    result_ = ImplementThisToRunYourTest(argc_, argv_);
50    done_ = true;
51
52    [pool release];
53    return;
54}
55
56- (int)result {
57  return result_;
58}
59
60@end
61
62int main(int argc, char * argv[]) {
63    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
64
65    [NSApplication sharedApplication];
66
67    int result = 0;
68    AutoTestInWorkerThread* tests = [[AutoTestInWorkerThread alloc] init];
69
70    [tests setArgc:argc argv:argv];
71    [tests setDone:false];
72
73    [NSThread detachNewThreadSelector:@selector(runTest:)
74                             toTarget:tests
75                           withObject:nil];
76
77    NSRunLoop* main_run_loop = [NSRunLoop mainRunLoop];
78    NSDate *loop_until = [NSDate dateWithTimeIntervalSinceNow:0.1];
79    bool runloop_ok = true;
80    while (![tests done] && runloop_ok) {
81      runloop_ok = [main_run_loop runMode:NSDefaultRunLoopMode
82                               beforeDate:loop_until];
83      loop_until = [NSDate dateWithTimeIntervalSinceNow:0.1];
84    }
85
86    result = [tests result];
87
88    [pool release];
89    return result;
90}
91