Skip to content

Commit

Permalink
Invoke the iOS hook from TestSuite so each run_all_unittests.cc file …
Browse files Browse the repository at this point in the history
…does not

have to remember to install MainHook.


BUG=None


Review URL: https://chromiumcodereview.appspot.com/12321117

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@188376 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
lliabraa@chromium.org committed Mar 15, 2013
1 parent 7b5173b commit 5136ba2
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 156 deletions.
3 changes: 0 additions & 3 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,6 @@
'test/expectations/expectation.h',
'test/expectations/parser.cc',
'test/expectations/parser.h',
'test/main_hook.cc',
'test/main_hook.h',
'test/main_hook_ios.mm',
'test/mock_chrome_application_mac.h',
'test/mock_chrome_application_mac.mm',
'test/mock_devices_changed_observer.cc',
Expand Down
9 changes: 0 additions & 9 deletions base/test/main_hook.cc

This file was deleted.

17 changes: 0 additions & 17 deletions base/test/main_hook.h

This file was deleted.

108 changes: 0 additions & 108 deletions base/test/main_hook_ios.mm

This file was deleted.

2 changes: 0 additions & 2 deletions base/test/run_all_perftests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// 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"
#include "base/test/perf_test_suite.h"

int main(int argc, char** argv) {
MainHook hook(main, argc, argv);
return base::PerfTestSuite(argc, argv).Run();
}
2 changes: 0 additions & 2 deletions base/test/run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// 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"
#include "base/test/test_suite.h"

int main(int argc, char** argv) {
MainHook hook(main, argc, argv);
return base::TestSuite(argc, argv).Run();
}
8 changes: 8 additions & 0 deletions base/test/test_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void TestSuite::PreInitialize(int argc, char** argv,
at_exit_manager_.reset(new base::AtExitManager);
#endif

#if defined(OS_IOS)
InitIOSRunHook(this, argc, argv);
#endif

// Don't add additional code to this function. Instead add it to
// Initialize(). See bug 6436.
}
Expand All @@ -141,6 +145,10 @@ void TestSuite::ResetCommandLine() {
// Don't add additional code to this method. Instead add it to
// Initialize(). See bug 6436.
int TestSuite::Run() {
#if defined(OS_IOS)
RunTestsFromIOSApp();
#endif

#if defined(OS_MACOSX)
base::mac::ScopedNSAutoreleasePool scoped_pool;
#endif
Expand Down
11 changes: 10 additions & 1 deletion base/test/test_support_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
#ifndef BASE_TEST_TEST_SUPPORT_IOS_H_
#define BASE_TEST_TEST_SUPPORT_IOS_H_

#include "base/test/test_suite.h"

namespace base {

// Init the message loop for tests on iOS.
// Inits the message loop for tests on iOS.
void InitIOSTestMessageLoop();

// Inits the run hook for tests on iOS.
void InitIOSRunHook(TestSuite* suite, int argc, char* argv[]);

// Launches an iOS app that runs the tests in the suite passed to
// InitIOSRunHook.
void RunTestsFromIOSApp();

} // namespace base

#endif // BASE_TEST_TEST_SUPPORT_IOS_H_
105 changes: 105 additions & 0 deletions base/test/test_support_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,88 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <UIKit/UIKit.h>

#include "base/debug/debugger.h"
#include "base/logging.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "base/memory/scoped_nsobject.h"
#include "base/message_loop.h"
#include "base/message_pump_default.h"
#include "base/test/test_suite.h"

// Springboard will kill any iOS app that fails to check in after launch within
// a given time. Starting a UIApplication before invoking TestSuite::Run
// prevents this from happening.

// InitIOSRunHook saves the TestSuite and argc/argv, then invoking
// RunTestsFromIOSApp calls UIApplicationMain(), providing an application
// delegate class: ChromeUnitTestDelegate. The delegate implements
// application:didFinishLaunchingWithOptions: to invoke the TestSuite's Run
// method.

// 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 MainHook are being
// run in a row, this provides an indication of which one is currently running.

static base::TestSuite* g_test_suite = NULL;
static int g_argc;
static char** g_argv;

@interface UIApplication (Testing)
- (void) _terminateWithStatus:(int)status;
@end

@interface ChromeUnitTestDelegate : NSObject {
@private
scoped_nsobject<UIWindow> window_;
}
- (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.
window_.reset([[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_test_suite->Run();

// 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,
// and sleep only to make sure that much time has elapsed since launch.
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
window_.reset();

// 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];
[application _terminateWithStatus:exitStatus];

exit(exitStatus);
}

@end

namespace {

Expand All @@ -20,4 +100,29 @@ void InitIOSTestMessageLoop() {
MessageLoop::InitMessagePumpForUIFactory(&CreateMessagePumpForUIForTests);
}

void InitIOSRunHook(TestSuite* suite, int argc, char* argv[]) {
g_test_suite = suite;
g_argc = argc;
g_argv = argv;
}

void RunTestsFromIOSApp() {
// When TestSuite::Run is invoked it calls RunTestsFromIOSApp(). On the first
// invocation, this method fires up an iOS app via UIApplicationMain. Since
// UIApplicationMain does not return until the app exits, control does not
// return to the initial TestSuite::Run invocation, so the app invokes
// TestSuite::Run a second time and since |ran_hook| is true at this point,
// this method is a no-op and control returns to TestSuite:Run so that test
// are executed. Once the app exits, RunTestsFromIOSApp calls exit() so that
// control is not returned to the initial invocation of TestSuite::Run.
static bool ran_hook = false;
if (!ran_hook) {
ran_hook = true;
mac::ScopedNSAutoreleasePool pool;
int exit_status = UIApplicationMain(g_argc, g_argv, nil,
@"ChromeUnitTestDelegate");
exit(exit_status);
}
}

} // namespace base
2 changes: 0 additions & 2 deletions chrome/test/base/run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// 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"
#include "chrome/test/base/chrome_test_suite.h"
#include "content/public/test/unittest_test_suite.h"

int main(int argc, char **argv) {
MainHook hook(main, argc, argv);
return content::UnitTestTestSuite(new ChromeTestSuite(argc, argv)).Run();
}
3 changes: 0 additions & 3 deletions crypto/run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
// 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"
#include "base/test/test_suite.h"
#include "crypto/nss_util.h"

int main(int argc, char** argv) {
MainHook hook(main, argc, argv);

#if defined(USE_NSS)
// This is most likely not needed, but it basically replaces a similar call
// that was performed on test_support_base.
Expand Down
2 changes: 0 additions & 2 deletions media/base/run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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"
#include "base/test/test_suite.h"
#include "media/base/media.h"

Expand All @@ -23,6 +22,5 @@ void TestSuiteNoAtExit::Initialize() {
}

int main(int argc, char** argv) {
MainHook hook(main, argc, argv);
return TestSuiteNoAtExit(argc, argv).Run();
}
3 changes: 0 additions & 3 deletions net/base/run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

#include "base/metrics/statistics_recorder.h"
#include "base/test/main_hook.h"
#include "build/build_config.h"
#include "crypto/nss_util.h"
#include "net/base/net_test_suite.h"
Expand All @@ -24,8 +23,6 @@ using net::internal::ClientSocketPoolBaseHelper;
using net::SpdySession;

int main(int argc, char** argv) {
MainHook hook(main, argc, argv);

// Record histograms, so we can get histograms data in tests.
base::StatisticsRecorder::Initialize();

Expand Down
Loading

0 comments on commit 5136ba2

Please sign in to comment.