Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2690)

Unified Diff: base/test/main_hook_ios.mm

Issue 10690161: Make it possible to run gtests on iOS. (Closed) Base URL: http://git.chromium.org/chromium/src.git@presubmit_change
Patch Set: Code review feedback addressed. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..7ca4fe9ef8ecdc6c2992d6a90ca26777a76a22dd
--- /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/logging.h"
+#include "base/mac/scoped_nsautorelease_pool.h"
+#include "base/debug/debugger.h"
stuartmorgan 2012/07/12 10:59:23 Move this up a line so the headers are in order.
leng 2012/07/12 11:17:31 Two lines. :)
+
+// 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(),
+// 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.
+
+// 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
+// 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.
+ 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): b/6810854 Figure out how much time is actually needed.
stuartmorgan 2012/07/12 10:59:23 Sorry, I meant a crbug.com bug; I should have said
leng 2012/07/12 11:17:31 Done.
+ [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
+
+ // 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:)
+ 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,
+ @"ChromeUnitTestDelegate");
+ exit(exitStatus);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698