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