Home | History | Annotate | Download | only in test
      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 #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/mac/scoped_nsobject.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "base/message_loop/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   base::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 = NSTextAlignmentCenter;
     59   [window_ addSubview:label];
     60 
     61   if ([self shouldRedirectOutputToFile])
     62     [self redirectOutput];
     63 
     64   // Queue up the test run.
     65   [self performSelector:@selector(runTests)
     66              withObject:nil
     67              afterDelay:0.1];
     68   return YES;
     69 }
     70 
     71 // Returns true if the gtest output should be redirected to a file, then sent
     72 // to NSLog when compleete. This redirection is used because gtest only writes
     73 // output to stdout, but results must be written to NSLog in order to show up in
     74 // the device log that is retrieved from the device by the host.
     75 - (BOOL)shouldRedirectOutputToFile {
     76 #if !TARGET_IPHONE_SIMULATOR
     77   return !base::debug::BeingDebugged();
     78 #endif  // TARGET_IPHONE_SIMULATOR
     79   return NO;
     80 }
     81 
     82 // Returns the path to the directory to store gtest output files.
     83 - (NSString*)outputPath {
     84   NSArray* searchPath =
     85       NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
     86                                           NSUserDomainMask,
     87                                           YES);
     88   CHECK([searchPath count] > 0) << "Failed to get the Documents folder";
     89   return [searchPath objectAtIndex:0];
     90 }
     91 
     92 // Returns the path to file that stdout is redirected to.
     93 - (NSString*)stdoutPath {
     94   return [[self outputPath] stringByAppendingPathComponent:@"stdout.log"];
     95 }
     96 
     97 // Returns the path to file that stderr is redirected to.
     98 - (NSString*)stderrPath {
     99   return [[self outputPath] stringByAppendingPathComponent:@"stderr.log"];
    100 }
    101 
    102 // Redirects stdout and stderr to files in the Documents folder in the app's
    103 // sandbox.
    104 - (void)redirectOutput {
    105   freopen([[self stdoutPath] UTF8String], "w+", stdout);
    106   freopen([[self stderrPath] UTF8String], "w+", stderr);
    107 }
    108 
    109 // Reads the redirected gtest output from a file and writes it to NSLog.
    110 - (void)writeOutputToNSLog {
    111   // Close the redirected stdout and stderr files so that the content written to
    112   // NSLog doesn't end up in these files.
    113   fclose(stdout);
    114   fclose(stderr);
    115   for (NSString* path in @[ [self stdoutPath], [self stderrPath]]) {
    116     NSString* content = [NSString stringWithContentsOfFile:path
    117                                                   encoding:NSUTF8StringEncoding
    118                                                      error:NULL];
    119     NSArray* lines = [content componentsSeparatedByCharactersInSet:
    120         [NSCharacterSet newlineCharacterSet]];
    121 
    122     NSLog(@"Writing contents of %@ to NSLog", path);
    123     for (NSString* line in lines) {
    124       NSLog(@"%@", line);
    125     }
    126   }
    127 }
    128 
    129 - (void)runTests {
    130   int exitStatus = g_test_suite->Run();
    131 
    132   if ([self shouldRedirectOutputToFile])
    133     [self writeOutputToNSLog];
    134 
    135   // If a test app is too fast, it will exit before Instruments has has a
    136   // a chance to initialize and no test results will be seen.
    137   // TODO(ios): crbug.com/137010 Figure out how much time is actually needed,
    138   // and sleep only to make sure that much time has elapsed since launch.
    139   [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
    140   window_.reset();
    141 
    142   // Use the hidden selector to try and cleanly take down the app (otherwise
    143   // things can think the app crashed even on a zero exit status).
    144   UIApplication* application = [UIApplication sharedApplication];
    145   [application _terminateWithStatus:exitStatus];
    146 
    147   exit(exitStatus);
    148 }
    149 
    150 @end
    151 
    152 namespace {
    153 
    154 scoped_ptr<base::MessagePump> CreateMessagePumpForUIForTests() {
    155   // A default MessagePump will do quite nicely in tests.
    156   return scoped_ptr<base::MessagePump>(new base::MessagePumpDefault());
    157 }
    158 
    159 }  // namespace
    160 
    161 namespace base {
    162 
    163 void InitIOSTestMessageLoop() {
    164   MessageLoop::InitMessagePumpForUIFactory(&CreateMessagePumpForUIForTests);
    165 }
    166 
    167 void InitIOSRunHook(TestSuite* suite, int argc, char* argv[]) {
    168   g_test_suite = suite;
    169   g_argc = argc;
    170   g_argv = argv;
    171 }
    172 
    173 void RunTestsFromIOSApp() {
    174   // When TestSuite::Run is invoked it calls RunTestsFromIOSApp(). On the first
    175   // invocation, this method fires up an iOS app via UIApplicationMain. Since
    176   // UIApplicationMain does not return until the app exits, control does not
    177   // return to the initial TestSuite::Run invocation, so the app invokes
    178   // TestSuite::Run a second time and since |ran_hook| is true at this point,
    179   // this method is a no-op and control returns to TestSuite:Run so that test
    180   // are executed. Once the app exits, RunTestsFromIOSApp calls exit() so that
    181   // control is not returned to the initial invocation of TestSuite::Run.
    182   static bool ran_hook = false;
    183   if (!ran_hook) {
    184     ran_hook = true;
    185     mac::ScopedNSAutoreleasePool pool;
    186     int exit_status = UIApplicationMain(g_argc, g_argv, nil,
    187                                         @"ChromeUnitTestDelegate");
    188     exit(exit_status);
    189   }
    190 }
    191 
    192 }  // namespace base
    193