Index: base/test/main_hook_ios.mm |
diff --git a/base/test/main_hook_ios.mm b/base/test/main_hook_ios.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..271804e0021782c926046a3d711f97cdf1a78ecc |
--- /dev/null |
+++ b/base/test/main_hook_ios.mm |
@@ -0,0 +1,93 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/test/main_hook.h" |
+ |
+#import <UIKit/UIKit.h> |
+ |
+#include "base/debug/debugger.h" |
+#include "base/logging.h" |
+#include "base/mac/scoped_nsautorelease_pool.h" |
+ |
+// Springboard will kill any iOS app that fails to check in after launch within |
+// a given time. These two classes prevent this from happening. |
+ |
+// MainHook saves the chrome main() and calls UIApplicationMain(), |
Mark Mentovai
2012/07/12 13:37:50
Oh, patch-cest!
leng
2012/07/12 15:18:06
Stuart had to explain that to me. :)
|
+// providing an application delegate class: ChromeUnitTestDelegate. The delegate |
+// listens for UIApplicationDidFinishLaunchingNotification. When the |
+// notification is received, it fires main() again to have the real work done. |
Mark Mentovai
2012/07/12 13:37:50
Give an example of usage, showing that the MainHoo
leng
2012/07/12 15:18:06
Done.
|
+ |
+// Since the executable isn't likely to be a real iOS UI, the delegate puts up a |
+// window displaying the app name. If a bunch of apps using this are being run |
Mark Mentovai
2012/07/12 13:37:50
“using this” isn’t clear. You mean MainHook, but I
leng
2012/07/12 15:18:06
Done.
|
+// in a row, this provides an indication of which one is currently running. |
+ |
+static MainHook::MainType g_main_func = NULL; |
+static int g_argc; |
+static char** g_argv; |
+ |
+@interface ChromeUnitTestDelegate : NSObject |
+- (void)runTests; |
+@end |
+ |
+@implementation ChromeUnitTestDelegate |
+ |
+- (BOOL)application:(UIApplication *)application |
+ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
+ |
+ CGRect bounds = [[UIScreen mainScreen] bounds]; |
+ |
+ // Yes, this is leaked, it's just to make what's running visible. |
Mark Mentovai
2012/07/12 13:37:50
You don’t have to leak it, and it’s easy enough to
leng
2012/07/12 15:18:06
Done.
|
+ UIWindow* window = [[UIWindow alloc] initWithFrame:bounds]; |
+ [window makeKeyAndVisible]; |
+ |
+ // Add a label with the app name. |
+ UILabel* label = [[[UILabel alloc] initWithFrame:bounds] autorelease]; |
+ label.text = [[NSProcessInfo processInfo] processName]; |
+ label.textAlignment = UITextAlignmentCenter; |
+ [window addSubview:label]; |
+ |
+ // Queue up the test run. |
+ [self performSelector:@selector(runTests) |
+ withObject:nil |
+ afterDelay:0.1]; |
+ return YES; |
+} |
+ |
+- (void)runTests { |
+ int exitStatus = g_main_func(g_argc, g_argv); |
+ |
+ // If a test app is too fast, it will exit before Instruments has has a |
+ // a chance to initialize and no test results will be seen. |
+ // TODO(ios): crbug.com/137010 Figure out how much time is actually needed. |
+ [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]]; |
Mark Mentovai
2012/07/12 13:37:50
This also adds a delay to test apps that weren’t t
stuartmorgan
2012/07/12 13:41:35
That's exactly what the TODO is for; I think we sh
Mark Mentovai
2012/07/12 14:04:09
stuartmorgan wrote:
leng
2012/07/12 15:18:06
I hope the comment is clearer now.
|
+ |
+ // Use the hidden selector to try and cleanly take down the app (otherwise |
+ // things can think the app crashed even on a zero exit status). |
+ UIApplication* application = [UIApplication sharedApplication]; |
+ if ([application respondsToSelector:@selector(_terminateWithStatus:)]) { |
+ [application performSelector:@selector(_terminateWithStatus:) |
Mark Mentovai
2012/07/12 13:37:50
Rather than using -performSelector: and this ugly
leng
2012/07/12 15:18:06
Done.
|
+ withObject:(id)exitStatus]; |
+ } |
+ exit(exitStatus); |
+} |
+ |
+@end |
+ |
+#pragma mark - |
+ |
+MainHook::MainHook(MainType main_func, int argc, char* argv[]) { |
+ static bool ran_hook = false; |
+ if (!ran_hook) { |
+ ran_hook = true; |
+ |
+ g_main_func = main_func; |
+ g_argc = argc; |
+ g_argv = argv; |
+ |
+ base::mac::ScopedNSAutoreleasePool pool; |
+ int exitStatus = UIApplicationMain(argc, argv, nil, |
Mark Mentovai
2012/07/12 13:37:50
Outside of an Objective-C section, name_variables_
leng
2012/07/12 15:18:06
Done.
|
+ @"ChromeUnitTestDelegate"); |
+ exit(exitStatus); |
+ } |
+} |