You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #import <Cocoa/Cocoa.h>
  11. #include "test/run_test.h"
  12. // Converting a C++ function pointer to an Objective-C block.
  13. typedef void(^TestBlock)();
  14. TestBlock functionToBlock(void(*function)()) {
  15. return [^(void) { function(); } copy];
  16. }
  17. // Class calling the test function on the platform specific thread.
  18. @interface TestRunner : NSObject {
  19. BOOL running_;
  20. }
  21. - (void)runAllTests:(TestBlock)ignored;
  22. - (BOOL)running;
  23. @end
  24. @implementation TestRunner
  25. - (id)init {
  26. self = [super init];
  27. if (self) {
  28. running_ = YES;
  29. }
  30. return self;
  31. }
  32. - (void)runAllTests:(TestBlock)testBlock {
  33. @autoreleasepool {
  34. testBlock();
  35. running_ = NO;
  36. }
  37. }
  38. - (BOOL)running {
  39. return running_;
  40. }
  41. @end
  42. namespace webrtc {
  43. namespace test {
  44. void RunTest(void(*test)()) {
  45. @autoreleasepool {
  46. [NSApplication sharedApplication];
  47. // Convert the function pointer to an Objective-C block and call on a
  48. // separate thread, to avoid blocking the main thread.
  49. TestRunner *testRunner = [[TestRunner alloc] init];
  50. TestBlock testBlock = functionToBlock(test);
  51. [NSThread detachNewThreadSelector:@selector(runAllTests:)
  52. toTarget:testRunner
  53. withObject:testBlock];
  54. NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
  55. while ([testRunner running] && [runLoop runMode:NSDefaultRunLoopMode
  56. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]])
  57. ;
  58. }
  59. }
  60. } // namespace test
  61. } // namespace webrtc