| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 <string> | |
| 6 | |
| 7 #include "base/at_exit.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/file_path.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/native_library.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/process_util.h" | |
| 15 #include "base/string_util.h" | |
| 16 | |
| 17 #include "chrome/test/test_launcher/test_runner.h" | |
| 18 | |
| 19 // This version of the test launcher loads a dynamic library containing the | |
| 20 // tests and executes the them in that library. When the test has been run the | |
| 21 // library is unloaded, to ensure atexit handlers are run and static | |
| 22 // initializers will be run again for the next test. | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const wchar_t* const kLibNameFlag = L"lib"; | |
| 27 const wchar_t* const kGTestListTestsFlag = L"gtest_list_tests"; | |
| 28 | |
| 29 class InProcTestRunner : public tests::TestRunner { | |
| 30 public: | |
| 31 explicit InProcTestRunner(const std::wstring& lib_name) | |
| 32 : lib_name_(lib_name), | |
| 33 dynamic_lib_(NULL), | |
| 34 run_test_proc_(NULL), | |
| 35 uninitialize_proc_(NULL) { | |
| 36 } | |
| 37 | |
| 38 ~InProcTestRunner() { | |
| 39 if (!dynamic_lib_) | |
| 40 return; | |
| 41 if (uninitialize_proc_ && (uninitialize_proc_)()) { | |
| 42 LOG(ERROR) << "Uninitialization of " << | |
| 43 base::GetNativeLibraryName(lib_name_) << " failed."; | |
| 44 } | |
| 45 base::UnloadNativeLibrary(dynamic_lib_); | |
| 46 LOG(INFO) << "Unloaded " << base::GetNativeLibraryName(lib_name_); | |
| 47 } | |
| 48 | |
| 49 bool Init() { | |
| 50 FilePath lib_path; | |
| 51 CHECK(PathService::Get(base::FILE_EXE, &lib_path)); | |
| 52 lib_path = lib_path.DirName().Append(base::GetNativeLibraryName(lib_name_)); | |
| 53 | |
| 54 LOG(INFO) << "Loading '" << lib_path.value() << "'"; | |
| 55 | |
| 56 dynamic_lib_ = base::LoadNativeLibrary(lib_path); | |
| 57 if (!dynamic_lib_) { | |
| 58 LOG(ERROR) << "Failed to load " << lib_path.value(); | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 run_test_proc_ = reinterpret_cast<RunTestProc>( | |
| 63 base::GetFunctionPointerFromNativeLibrary(dynamic_lib_, "RunTests")); | |
| 64 if (!run_test_proc_) { | |
| 65 LOG(ERROR) << | |
| 66 "Failed to find RunTest function in " << lib_path.value(); | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 uninitialize_proc_ = reinterpret_cast<UninitializeProc>( | |
| 71 base::GetFunctionPointerFromNativeLibrary(dynamic_lib_, | |
| 72 "UninitializeTest")); | |
| 73 | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 // Returns true if the test succeeded, false if it failed. | |
| 78 bool RunTest(const std::string& test_name) { | |
| 79 std::string filter_flag = StringPrintf("--gtest_filter=%s", | |
| 80 test_name.c_str()); | |
| 81 char* argv[3]; | |
| 82 argv[0] = const_cast<char*>(""); | |
| 83 argv[1] = const_cast<char*>(filter_flag.c_str()); | |
| 84 // Always enable disabled tests. This method is not called with disabled | |
| 85 // tests unless this flag was specified to the test launcher. | |
| 86 argv[2] = "--gtest_also_run_disabled_tests"; | |
| 87 return RunAsIs(3, argv) == 0; | |
| 88 } | |
| 89 | |
| 90 // Calls-in to GTest with the arguments we were started with. | |
| 91 int RunAsIs(int argc, char** argv) { | |
| 92 return (run_test_proc_)(argc, argv); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 typedef int (CDECL *RunTestProc)(int, char**); | |
| 97 typedef int (CDECL *UninitializeProc)(); | |
| 98 | |
| 99 std::wstring lib_name_; | |
| 100 base::NativeLibrary dynamic_lib_; | |
| 101 RunTestProc run_test_proc_; | |
| 102 | |
| 103 // An optional UnitializeTest method called before the library containing the | |
| 104 // test is unloaded. | |
| 105 UninitializeProc uninitialize_proc_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(InProcTestRunner); | |
| 108 }; | |
| 109 | |
| 110 class InProcTestRunnerFactory : public tests::TestRunnerFactory { | |
| 111 public: | |
| 112 explicit InProcTestRunnerFactory(const std::wstring& lib_name) | |
| 113 : lib_name_(lib_name) { | |
| 114 } | |
| 115 | |
| 116 virtual tests::TestRunner* CreateTestRunner() const { | |
| 117 return new InProcTestRunner(lib_name_); | |
| 118 } | |
| 119 | |
| 120 private: | |
| 121 std::wstring lib_name_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(InProcTestRunnerFactory); | |
| 124 }; | |
| 125 | |
| 126 } // namespace | |
| 127 | |
| 128 int main(int argc, char** argv) { | |
| 129 base::AtExitManager at_exit_manager; | |
| 130 | |
| 131 CommandLine::Init(argc, argv); | |
| 132 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 133 std::wstring lib_name = command_line->GetSwitchValue(kLibNameFlag); | |
| 134 if (lib_name.empty()) { | |
| 135 LOG(ERROR) << "No dynamic library name specified. You must specify one with" | |
| 136 " the --lib=<lib_name> option."; | |
| 137 return 1; | |
| 138 } | |
| 139 | |
| 140 if (command_line->HasSwitch(kGTestListTestsFlag)) { | |
| 141 InProcTestRunner test_runner(lib_name); | |
| 142 if (!test_runner.Init()) | |
| 143 return 1; | |
| 144 return test_runner.RunAsIs(argc, argv); | |
| 145 } | |
| 146 | |
| 147 InProcTestRunnerFactory test_runner_factory(lib_name); | |
| 148 return tests::RunTests(test_runner_factory) ? 0 : 1; | |
| 149 } | |
| OLD | NEW |