| 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/browser/browser_test_runner.h" | |
| 18 | |
| 19 // This version of the browser test launcher loads a dynamic library containing | |
| 20 // the tests and executes the them in that library. When the test has been run | |
| 21 // the 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 kBrowserTesLibBaseName = L"browser_tests"; | |
| 27 const wchar_t* const kGTestListTestsFlag = L"gtest_list_tests"; | |
| 28 | |
| 29 class InProcBrowserTestRunner : public browser_tests::BrowserTestRunner { | |
| 30 public: | |
| 31 InProcBrowserTestRunner() : dynamic_lib_(NULL), run_test_proc_(NULL) { | |
| 32 } | |
| 33 | |
| 34 ~InProcBrowserTestRunner() { | |
| 35 if (!dynamic_lib_) | |
| 36 return; | |
| 37 base::UnloadNativeLibrary(dynamic_lib_); | |
| 38 LOG(INFO) << "Unloaded " << | |
| 39 base::GetNativeLibraryName(kBrowserTesLibBaseName); | |
| 40 } | |
| 41 | |
| 42 bool Init() { | |
| 43 FilePath lib_path; | |
| 44 CHECK(PathService::Get(base::FILE_EXE, &lib_path)); | |
| 45 lib_path = lib_path.DirName().Append( | |
| 46 base::GetNativeLibraryName(kBrowserTesLibBaseName)); | |
| 47 | |
| 48 LOG(INFO) << "Loading '" << lib_path.value() << "'"; | |
| 49 | |
| 50 dynamic_lib_ = base::LoadNativeLibrary(lib_path); | |
| 51 if (!dynamic_lib_) { | |
| 52 LOG(ERROR) << "Failed to load " << lib_path.value(); | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 run_test_proc_ = reinterpret_cast<RunTestProc>( | |
| 57 base::GetFunctionPointerFromNativeLibrary(dynamic_lib_, "RunTests")); | |
| 58 if (!run_test_proc_) { | |
| 59 LOG(ERROR) << | |
| 60 "Failed to find RunTest function in " << lib_path.value(); | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 // Returns true if the test succeeded, false if it failed. | |
| 68 bool RunTest(const std::string& test_name) { | |
| 69 std::string filter_flag = StringPrintf("--gtest_filter=%s", | |
| 70 test_name.c_str()); | |
| 71 char* argv[3]; | |
| 72 argv[0] = const_cast<char*>(""); | |
| 73 argv[1] = const_cast<char*>(filter_flag.c_str()); | |
| 74 // Always enable disabled tests. This method is not called with disabled | |
| 75 // tests unless this flag was specified to the browser test executable. | |
| 76 argv[2] = "--gtest_also_run_disabled_tests"; | |
| 77 return RunAsIs(3, argv) == 0; | |
| 78 } | |
| 79 | |
| 80 // Calls-in to GTest with the arguments we were started with. | |
| 81 int RunAsIs(int argc, char** argv) { | |
| 82 return (run_test_proc_)(argc, argv); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 typedef int (CDECL *RunTestProc)(int, char**); | |
| 87 | |
| 88 base::NativeLibrary dynamic_lib_; | |
| 89 RunTestProc run_test_proc_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(InProcBrowserTestRunner); | |
| 92 }; | |
| 93 | |
| 94 class InProcBrowserTestRunnerFactory | |
| 95 : public browser_tests::BrowserTestRunnerFactory { | |
| 96 public: | |
| 97 InProcBrowserTestRunnerFactory() { } | |
| 98 | |
| 99 virtual browser_tests::BrowserTestRunner* CreateBrowserTestRunner() const { | |
| 100 return new InProcBrowserTestRunner(); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 DISALLOW_COPY_AND_ASSIGN(InProcBrowserTestRunnerFactory); | |
| 105 }; | |
| 106 | |
| 107 } // namespace | |
| 108 | |
| 109 int main(int argc, char** argv) { | |
| 110 base::AtExitManager at_exit_manager; | |
| 111 | |
| 112 CommandLine::Init(argc, argv); | |
| 113 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 114 | |
| 115 if (command_line->HasSwitch(kGTestListTestsFlag)) { | |
| 116 InProcBrowserTestRunner test_runner; | |
| 117 if (!test_runner.Init()) | |
| 118 return 1; | |
| 119 return test_runner.RunAsIs(argc, argv); | |
| 120 } | |
| 121 | |
| 122 InProcBrowserTestRunnerFactory test_runner_factory; | |
| 123 return browser_tests::RunTests(test_runner_factory) ? 0 : 1; | |
| 124 } | |
| OLD | NEW |