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/command_line.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/native_library.h" |
| 12 #include "base/process_util.h" |
| 13 #include "base/string_util.h" |
| 14 |
| 15 #include "chrome/test/browser/browser_test_runner.h" |
| 16 |
| 17 // This version of the browser test launcher loads a dynamic library containing |
| 18 // the tests and executes the them in that library. When the test has been run |
| 19 // the library is unloaded, to ensure atexit handlers are run and static |
| 20 // initializers will be run again for the next test. |
| 21 |
| 22 namespace { |
| 23 |
| 24 const wchar_t* const kBrowserTesLibBaseName = L"browser_tests"; |
| 25 const wchar_t* const kGTestListTestsFlag = L"gtest_list_tests"; |
| 26 |
| 27 class InProcBrowserTestRunner : public browser_tests::BrowserTestRunner { |
| 28 public: |
| 29 InProcBrowserTestRunner() : dynamic_lib_(NULL), run_test_proc_(NULL) { |
| 30 } |
| 31 |
| 32 ~InProcBrowserTestRunner() { |
| 33 if (!dynamic_lib_) |
| 34 return; |
| 35 base::UnloadNativeLibrary(dynamic_lib_); |
| 36 LOG(INFO) << "Unloaded " << |
| 37 base::GetNativeLibraryName(kBrowserTesLibBaseName); |
| 38 } |
| 39 |
| 40 bool Init() { |
| 41 FilePath lib_path; |
| 42 if (!file_util::GetCurrentDirectory(&lib_path)) { |
| 43 LOG(ERROR) << "Failed to retrieve curret directory."; |
| 44 return false; |
| 45 } |
| 46 |
| 47 string16 lib_name = base::GetNativeLibraryName(kBrowserTesLibBaseName); |
| 48 #if defined(OS_WIN) |
| 49 lib_path = lib_path.Append(lib_name); |
| 50 #else |
| 51 lib_path = lib_path.Append(WideToUTF8(lib_name)); |
| 52 #endif |
| 53 LOG(INFO) << "Loading '" << lib_path.value() << "'"; |
| 54 |
| 55 dynamic_lib_ = base::LoadNativeLibrary(lib_path); |
| 56 if (!dynamic_lib_) { |
| 57 LOG(ERROR) << "Failed to find " << lib_name; |
| 58 return false; |
| 59 } |
| 60 |
| 61 run_test_proc_ = reinterpret_cast<RunTestProc>( |
| 62 base::GetFunctionPointerFromNativeLibrary(dynamic_lib_, "RunTests")); |
| 63 if (!run_test_proc_) { |
| 64 LOG(ERROR) << |
| 65 "Failed to find RunTest function in " << lib_name; |
| 66 return false; |
| 67 } |
| 68 |
| 69 return true; |
| 70 } |
| 71 |
| 72 // Returns true if the test succeeded, false if it failed. |
| 73 bool RunTest(const std::string& test_name) { |
| 74 std::string filter_flag = StringPrintf("--gtest_filter=%s", |
| 75 test_name.c_str()); |
| 76 char* argv[2]; |
| 77 argv[0] = const_cast<char*>(""); |
| 78 argv[1] = const_cast<char*>(filter_flag.c_str()); |
| 79 return RunAsIs(2, argv) == 0; |
| 80 } |
| 81 |
| 82 // Calls-in to GTest with the arguments we were started with. |
| 83 int RunAsIs(int argc, char** argv) { |
| 84 return (run_test_proc_)(argc, argv); |
| 85 } |
| 86 |
| 87 private: |
| 88 typedef int (CDECL *RunTestProc)(int, char**); |
| 89 |
| 90 base::NativeLibrary dynamic_lib_; |
| 91 RunTestProc run_test_proc_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(InProcBrowserTestRunner); |
| 94 }; |
| 95 |
| 96 class InProcBrowserTestRunnerFactory |
| 97 : public browser_tests::BrowserTestRunnerFactory { |
| 98 public: |
| 99 InProcBrowserTestRunnerFactory() { } |
| 100 |
| 101 virtual browser_tests::BrowserTestRunner* CreateBrowserTestRunner() const { |
| 102 return new InProcBrowserTestRunner(); |
| 103 } |
| 104 |
| 105 private: |
| 106 DISALLOW_COPY_AND_ASSIGN(InProcBrowserTestRunnerFactory); |
| 107 }; |
| 108 |
| 109 } // namespace |
| 110 |
| 111 int main(int argc, char** argv) { |
| 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 |