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.

126 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2017 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 <UIKit/UIKit.h>
  11. #include "test/ios/coverage_util_ios.h"
  12. #include "test/ios/test_support.h"
  13. #include "test/testsupport/perf_test.h"
  14. #import "sdk/objc/helpers/NSString+StdString.h"
  15. // Springboard will kill any iOS app that fails to check in after launch within
  16. // a given time. Starting a UIApplication before invoking TestSuite::Run
  17. // prevents this from happening.
  18. // InitIOSRunHook saves the TestSuite and argc/argv, then invoking
  19. // RunTestsFromIOSApp calls UIApplicationMain(), providing an application
  20. // delegate class: WebRtcUnitTestDelegate. The delegate implements
  21. // application:didFinishLaunchingWithOptions: to invoke the TestSuite's Run
  22. // method.
  23. // Since the executable isn't likely to be a real iOS UI, the delegate puts up a
  24. // window displaying the app name. If a bunch of apps using MainHook are being
  25. // run in a row, this provides an indication of which one is currently running.
  26. static int (*g_test_suite)(void) = NULL;
  27. static int g_argc;
  28. static char **g_argv;
  29. static bool g_save_chartjson_result;
  30. @interface UIApplication (Testing)
  31. - (void)_terminateWithStatus:(int)status;
  32. @end
  33. @interface WebRtcUnitTestDelegate : NSObject {
  34. UIWindow *_window;
  35. }
  36. - (void)runTests;
  37. @end
  38. @implementation WebRtcUnitTestDelegate
  39. - (BOOL)application:(UIApplication *)application
  40. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  41. CGRect bounds = [[UIScreen mainScreen] bounds];
  42. _window = [[UIWindow alloc] initWithFrame:bounds];
  43. [_window setBackgroundColor:[UIColor whiteColor]];
  44. [_window makeKeyAndVisible];
  45. // Add a label with the app name.
  46. UILabel *label = [[UILabel alloc] initWithFrame:bounds];
  47. label.text = [[NSProcessInfo processInfo] processName];
  48. label.textAlignment = NSTextAlignmentCenter;
  49. [_window addSubview:label];
  50. // An NSInternalInconsistencyException is thrown if the app doesn't have a
  51. // root view controller. Set an empty one here.
  52. [_window setRootViewController:[[UIViewController alloc] init]];
  53. // Queue up the test run.
  54. [self performSelector:@selector(runTests) withObject:nil afterDelay:0.1];
  55. return YES;
  56. }
  57. - (void)runTests {
  58. rtc::test::ConfigureCoverageReportPath();
  59. int exitStatus = g_test_suite();
  60. if (g_save_chartjson_result) {
  61. // Stores data into a json file under the app's document directory.
  62. NSString* fileName = @"perf_result.json";
  63. NSArray<NSString*>* outputDirectories = NSSearchPathForDirectoriesInDomains(
  64. NSDocumentDirectory, NSUserDomainMask, YES);
  65. if ([outputDirectories count] != 0) {
  66. NSString* outputPath =
  67. [outputDirectories[0] stringByAppendingPathComponent:fileName];
  68. webrtc::test::WritePerfResults(
  69. [NSString stdStringForString:outputPath]);
  70. }
  71. }
  72. // If a test app is too fast, it will exit before Instruments has has a
  73. // a chance to initialize and no test results will be seen.
  74. // TODO(crbug.com/137010): Figure out how much time is actually needed, and
  75. // sleep only to make sure that much time has elapsed since launch.
  76. [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
  77. // Use the hidden selector to try and cleanly take down the app (otherwise
  78. // things can think the app crashed even on a zero exit status).
  79. UIApplication *application = [UIApplication sharedApplication];
  80. [application _terminateWithStatus:exitStatus];
  81. exit(exitStatus);
  82. }
  83. @end
  84. namespace rtc {
  85. namespace test {
  86. // Note: This is not thread safe, and must be called from the same thread as
  87. // runTests above.
  88. void InitTestSuite(int (*test_suite)(void), int argc, char *argv[],
  89. bool save_chartjson_result) {
  90. g_test_suite = test_suite;
  91. g_argc = argc;
  92. g_argv = argv;
  93. g_save_chartjson_result = save_chartjson_result;
  94. }
  95. void RunTestsFromIOSApp() {
  96. @autoreleasepool {
  97. exit(UIApplicationMain(g_argc, g_argv, nil, @"WebRtcUnitTestDelegate"));
  98. }
  99. }
  100. } // namespace test
  101. } // namespace rtc