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

Side by Side 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: Empty line added. 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 unified diff | Download patch
« no previous file with comments | « base/test/main_hook.cc ('k') | base/test/run_all_perftests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/test/main_hook.h"
6
7 #import <UIKit/UIKit.h>
8
9 #include "base/debug/debugger.h"
10 #include "base/logging.h"
11 #include "base/mac/scoped_nsautorelease_pool.h"
12
13 // Springboard will kill any iOS app that fails to check in after launch within
14 // a given time. These two classes prevent this from happening.
15
16 // 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. :)
17 // providing an application delegate class: ChromeUnitTestDelegate. The delegate
18 // listens for UIApplicationDidFinishLaunchingNotification. When the
19 // 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.
20
21 // Since the executable isn't likely to be a real iOS UI, the delegate puts up a
22 // 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.
23 // in a row, this provides an indication of which one is currently running.
24
25 static MainHook::MainType g_main_func = NULL;
26 static int g_argc;
27 static char** g_argv;
28
29 @interface ChromeUnitTestDelegate : NSObject
30 - (void)runTests;
31 @end
32
33 @implementation ChromeUnitTestDelegate
34
35 - (BOOL)application:(UIApplication *)application
36 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
37
38 CGRect bounds = [[UIScreen mainScreen] bounds];
39
40 // 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.
41 UIWindow* window = [[UIWindow alloc] initWithFrame:bounds];
42 [window makeKeyAndVisible];
43
44 // Add a label with the app name.
45 UILabel* label = [[[UILabel alloc] initWithFrame:bounds] autorelease];
46 label.text = [[NSProcessInfo processInfo] processName];
47 label.textAlignment = UITextAlignmentCenter;
48 [window addSubview:label];
49
50 // Queue up the test run.
51 [self performSelector:@selector(runTests)
52 withObject:nil
53 afterDelay:0.1];
54 return YES;
55 }
56
57 - (void)runTests {
58 int exitStatus = g_main_func(g_argc, g_argv);
59
60 // If a test app is too fast, it will exit before Instruments has has a
61 // a chance to initialize and no test results will be seen.
62 // TODO(ios): crbug.com/137010 Figure out how much time is actually needed.
63 [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.
64
65 // Use the hidden selector to try and cleanly take down the app (otherwise
66 // things can think the app crashed even on a zero exit status).
67 UIApplication* application = [UIApplication sharedApplication];
68 if ([application respondsToSelector:@selector(_terminateWithStatus:)]) {
69 [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.
70 withObject:(id)exitStatus];
71 }
72 exit(exitStatus);
73 }
74
75 @end
76
77 #pragma mark -
78
79 MainHook::MainHook(MainType main_func, int argc, char* argv[]) {
80 static bool ran_hook = false;
81 if (!ran_hook) {
82 ran_hook = true;
83
84 g_main_func = main_func;
85 g_argc = argc;
86 g_argv = argv;
87
88 base::mac::ScopedNSAutoreleasePool pool;
89 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.
90 @"ChromeUnitTestDelegate");
91 exit(exitStatus);
92 }
93 }
OLDNEW
« no previous file with comments | « base/test/main_hook.cc ('k') | base/test/run_all_perftests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698