Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 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 | |
| 11 | #import <UIKit/UIKit.h> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "test/ios/test_support.h" |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #import "sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.h" |
denicija | d207a39 | 2017-09-11 06:43:28 -0700 | [diff] [blame] | 16 | |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 17 | // Springboard will kill any iOS app that fails to check in after launch within |
| 18 | // a given time. Starting a UIApplication before invoking TestSuite::Run |
| 19 | // prevents this from happening. |
| 20 | |
| 21 | // InitIOSRunHook saves the TestSuite and argc/argv, then invoking |
| 22 | // RunTestsFromIOSApp calls UIApplicationMain(), providing an application |
| 23 | // delegate class: WebRtcUnitTestDelegate. The delegate implements |
| 24 | // application:didFinishLaunchingWithOptions: to invoke the TestSuite's Run |
| 25 | // method. |
| 26 | |
| 27 | // Since the executable isn't likely to be a real iOS UI, the delegate puts up a |
| 28 | // window displaying the app name. If a bunch of apps using MainHook are being |
| 29 | // run in a row, this provides an indication of which one is currently running. |
| 30 | |
| 31 | static int (*g_test_suite)(void) = NULL; |
| 32 | static int g_argc; |
| 33 | static char **g_argv; |
| 34 | |
| 35 | @interface UIApplication (Testing) |
| 36 | - (void)_terminateWithStatus:(int)status; |
| 37 | @end |
| 38 | |
| 39 | @interface WebRtcUnitTestDelegate : NSObject { |
| 40 | UIWindow *_window; |
| 41 | } |
| 42 | - (void)runTests; |
| 43 | @end |
| 44 | |
| 45 | @implementation WebRtcUnitTestDelegate |
| 46 | |
| 47 | - (BOOL)application:(UIApplication *)application |
| 48 | didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
| 49 | CGRect bounds = [[UIScreen mainScreen] bounds]; |
| 50 | |
| 51 | _window = [[UIWindow alloc] initWithFrame:bounds]; |
| 52 | [_window setBackgroundColor:[UIColor whiteColor]]; |
| 53 | [_window makeKeyAndVisible]; |
| 54 | |
| 55 | // Add a label with the app name. |
| 56 | UILabel *label = [[UILabel alloc] initWithFrame:bounds]; |
| 57 | label.text = [[NSProcessInfo processInfo] processName]; |
| 58 | label.textAlignment = NSTextAlignmentCenter; |
| 59 | [_window addSubview:label]; |
| 60 | |
| 61 | // An NSInternalInconsistencyException is thrown if the app doesn't have a |
| 62 | // root view controller. Set an empty one here. |
| 63 | [_window setRootViewController:[[UIViewController alloc] init]]; |
| 64 | |
denicija | d207a39 | 2017-09-11 06:43:28 -0700 | [diff] [blame] | 65 | // We want to call `RTCUIApplicationStatusObserver sharedInstance` as early as |
| 66 | // possible in the application lifecycle to set observation properly. |
| 67 | __unused RTCUIApplicationStatusObserver *observer = |
| 68 | [RTCUIApplicationStatusObserver sharedInstance]; |
| 69 | |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 70 | // Queue up the test run. |
| 71 | [self performSelector:@selector(runTests) withObject:nil afterDelay:0.1]; |
| 72 | return YES; |
| 73 | } |
| 74 | |
| 75 | - (void)runTests { |
| 76 | int exitStatus = g_test_suite(); |
| 77 | |
| 78 | // If a test app is too fast, it will exit before Instruments has has a |
| 79 | // a chance to initialize and no test results will be seen. |
| 80 | // TODO(crbug.com/137010): Figure out how much time is actually needed, and |
| 81 | // sleep only to make sure that much time has elapsed since launch. |
| 82 | [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]]; |
| 83 | |
| 84 | // Use the hidden selector to try and cleanly take down the app (otherwise |
| 85 | // things can think the app crashed even on a zero exit status). |
| 86 | UIApplication *application = [UIApplication sharedApplication]; |
| 87 | [application _terminateWithStatus:exitStatus]; |
| 88 | |
| 89 | exit(exitStatus); |
| 90 | } |
| 91 | |
| 92 | @end |
| 93 | namespace rtc { |
| 94 | namespace test { |
| 95 | |
| 96 | void InitTestSuite(int (*test_suite)(void), int argc, char *argv[]) { |
| 97 | g_test_suite = test_suite; |
| 98 | g_argc = argc; |
| 99 | g_argv = argv; |
| 100 | } |
| 101 | |
| 102 | void RunTestsFromIOSApp() { |
| 103 | @autoreleasepool { |
| 104 | exit(UIApplicationMain(g_argc, g_argv, nil, @"WebRtcUnitTestDelegate")); |
| 105 | } |
| 106 | } |
| 107 | } // namespace test |
| 108 | } // namespace rtc |