OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #import <UIKit/UIKit.h> |
| 6 |
| 7 #include "base/debug/debugger.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/mac/scoped_nsautorelease_pool.h" |
| 10 #include "base/memory/scoped_nsobject.h" |
5 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
6 #include "base/message_pump_default.h" | 12 #include "base/message_pump_default.h" |
| 13 #include "base/test/test_suite.h" |
| 14 |
| 15 // Springboard will kill any iOS app that fails to check in after launch within |
| 16 // a given time. Starting a UIApplication before invoking TestSuite::Run |
| 17 // prevents this from happening. |
| 18 |
| 19 // InitIOSRunHook saves the TestSuite and argc/argv, then invoking |
| 20 // RunTestsFromIOSApp calls UIApplicationMain(), providing an application |
| 21 // delegate class: ChromeUnitTestDelegate. The delegate implements |
| 22 // application:didFinishLaunchingWithOptions: to invoke the TestSuite's Run |
| 23 // method. |
| 24 |
| 25 // Since the executable isn't likely to be a real iOS UI, the delegate puts up a |
| 26 // window displaying the app name. If a bunch of apps using MainHook are being |
| 27 // run in a row, this provides an indication of which one is currently running. |
| 28 |
| 29 static base::TestSuite* g_test_suite = NULL; |
| 30 static int g_argc; |
| 31 static char** g_argv; |
| 32 |
| 33 @interface UIApplication (Testing) |
| 34 - (void) _terminateWithStatus:(int)status; |
| 35 @end |
| 36 |
| 37 @interface ChromeUnitTestDelegate : NSObject { |
| 38 @private |
| 39 scoped_nsobject<UIWindow> window_; |
| 40 } |
| 41 - (void)runTests; |
| 42 @end |
| 43 |
| 44 @implementation ChromeUnitTestDelegate |
| 45 |
| 46 - (BOOL)application:(UIApplication *)application |
| 47 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
| 48 |
| 49 CGRect bounds = [[UIScreen mainScreen] bounds]; |
| 50 |
| 51 // Yes, this is leaked, it's just to make what's running visible. |
| 52 window_.reset([[UIWindow alloc] initWithFrame:bounds]); |
| 53 [window_ makeKeyAndVisible]; |
| 54 |
| 55 // Add a label with the app name. |
| 56 UILabel* label = [[[UILabel alloc] initWithFrame:bounds] autorelease]; |
| 57 label.text = [[NSProcessInfo processInfo] processName]; |
| 58 label.textAlignment = UITextAlignmentCenter; |
| 59 [window_ addSubview:label]; |
| 60 |
| 61 // Queue up the test run. |
| 62 [self performSelector:@selector(runTests) |
| 63 withObject:nil |
| 64 afterDelay:0.1]; |
| 65 return YES; |
| 66 } |
| 67 |
| 68 - (void)runTests { |
| 69 int exitStatus = g_test_suite->Run(); |
| 70 |
| 71 // If a test app is too fast, it will exit before Instruments has has a |
| 72 // a chance to initialize and no test results will be seen. |
| 73 // TODO(ios): crbug.com/137010 Figure out how much time is actually needed, |
| 74 // and sleep only to make sure that much time has elapsed since launch. |
| 75 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]]; |
| 76 window_.reset(); |
| 77 |
| 78 // Use the hidden selector to try and cleanly take down the app (otherwise |
| 79 // things can think the app crashed even on a zero exit status). |
| 80 UIApplication* application = [UIApplication sharedApplication]; |
| 81 [application _terminateWithStatus:exitStatus]; |
| 82 |
| 83 exit(exitStatus); |
| 84 } |
| 85 |
| 86 @end |
7 | 87 |
8 namespace { | 88 namespace { |
9 | 89 |
10 base::MessagePump* CreateMessagePumpForUIForTests() { | 90 base::MessagePump* CreateMessagePumpForUIForTests() { |
11 // A default MessagePump will do quite nicely in tests. | 91 // A default MessagePump will do quite nicely in tests. |
12 return new base::MessagePumpDefault(); | 92 return new base::MessagePumpDefault(); |
13 } | 93 } |
14 | 94 |
15 } // namespace | 95 } // namespace |
16 | 96 |
17 namespace base { | 97 namespace base { |
18 | 98 |
19 void InitIOSTestMessageLoop() { | 99 void InitIOSTestMessageLoop() { |
20 MessageLoop::InitMessagePumpForUIFactory(&CreateMessagePumpForUIForTests); | 100 MessageLoop::InitMessagePumpForUIFactory(&CreateMessagePumpForUIForTests); |
21 } | 101 } |
22 | 102 |
| 103 void InitIOSRunHook(TestSuite* suite, int argc, char* argv[]) { |
| 104 g_test_suite = suite; |
| 105 g_argc = argc; |
| 106 g_argv = argv; |
| 107 } |
| 108 |
| 109 void RunTestsFromIOSApp() { |
| 110 // When TestSuite::Run is invoked it calls RunTestsFromIOSApp(). On the first |
| 111 // invocation, this method fires up an iOS app via UIApplicationMain. Since |
| 112 // UIApplicationMain does not return until the app exits, control does not |
| 113 // return to the initial TestSuite::Run invocation, so the app invokes |
| 114 // TestSuite::Run a second time and since |ran_hook| is true at this point, |
| 115 // this method is a no-op and control returns to TestSuite:Run so that test |
| 116 // are executed. Once the app exits, RunTestsFromIOSApp calls exit() so that |
| 117 // control is not returned to the initial invocation of TestSuite::Run. |
| 118 static bool ran_hook = false; |
| 119 if (!ran_hook) { |
| 120 ran_hook = true; |
| 121 mac::ScopedNSAutoreleasePool pool; |
| 122 int exit_status = UIApplicationMain(g_argc, g_argv, nil, |
| 123 @"ChromeUnitTestDelegate"); |
| 124 exit(exit_status); |
| 125 } |
| 126 } |
| 127 |
23 } // namespace base | 128 } // namespace base |
OLD | NEW |