1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/test/main_hook.h"
7 #import <UIKit/UIKit.h>
9 #include "base/debug/debugger.h"
10 #include "base/logging.h"
11 #include "base/mac/scoped_nsautorelease_pool.h"
12 #include "base/memory/scoped_nsobject.h"
14 // Springboard will kill any iOS app that fails to check in after launch within
15 // a given time. These two classes prevent this from happening.
17 // MainHook saves the chrome main() and calls UIApplicationMain(),
18 // providing an application delegate class: ChromeUnitTestDelegate. The delegate
19 // listens for UIApplicationDidFinishLaunchingNotification. When the
20 // notification is received, it fires main() again to have the real work done.
23 // int main(int argc, char** argv) {
24 // MainHook hook(main, argc, argv);
25 // // Testing code goes here. There should be no code above MainHook. If
26 // // there is, it will be run twice.
29 // Since the executable isn't likely to be a real iOS UI, the delegate puts up a
30 // window displaying the app name. If a bunch of apps using MainHook are being
31 // run in a row, this provides an indication of which one is currently running.
33 static MainHook::MainType g_main_func = NULL;
37 @interface UIApplication (Testing)
38 - (void) _terminateWithStatus:(int)status;
41 @interface ChromeUnitTestDelegate : NSObject {
43 scoped_nsobject<UIWindow> window_;
48 @implementation ChromeUnitTestDelegate
50 - (BOOL)application:(UIApplication *)application
51 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
53 CGRect bounds = [[UIScreen mainScreen] bounds];
55 // Yes, this is leaked, it's just to make what's running visible.
56 window_.reset([[UIWindow alloc] initWithFrame:bounds]);
57 [window_ makeKeyAndVisible];
59 // Add a label with the app name.
60 UILabel* label = [[[UILabel alloc] initWithFrame:bounds] autorelease];
61 label.text = [[NSProcessInfo processInfo] processName];
62 label.textAlignment = UITextAlignmentCenter;
63 [window_ addSubview:label];
65 // Queue up the test run.
66 [self performSelector:@selector(runTests)
73 int exitStatus = g_main_func(g_argc, g_argv);
75 // If a test app is too fast, it will exit before Instruments has has a
76 // a chance to initialize and no test results will be seen.
77 // TODO(ios): crbug.com/137010 Figure out how much time is actually needed,
78 // and sleep only to make sure that much time has elapsed since launch.
79 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
82 // Use the hidden selector to try and cleanly take down the app (otherwise
83 // things can think the app crashed even on a zero exit status).
84 UIApplication* application = [UIApplication sharedApplication];
85 [application _terminateWithStatus:exitStatus];
94 MainHook::MainHook(MainType main_func, int argc, char* argv[]) {
95 static bool ran_hook = false;
99 g_main_func = main_func;
103 base::mac::ScopedNSAutoreleasePool pool;
104 int exit_status = UIApplicationMain(argc, argv, nil,
105 @"ChromeUnitTestDelegate");