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/logging.h" | |
9 #include "base/process_util.h" | |
10 | |
11 #include "chrome/test/browser/browser_test_runner.h" | |
12 #include "chrome/test/unit/chrome_test_suite.h" | |
13 | |
14 // This version of the browser test launcher forks a new process for each test | |
15 // it runs. | |
16 | |
17 namespace { | |
18 | |
19 const wchar_t* const kGTestListTestsFlag = L"gtest_list_tests"; | |
20 const wchar_t* const kChildProcessFlag = L"child"; | |
21 | |
22 class OutOfProcBrowserTestRunner : public browser_tests::BrowserTestRunner { | |
23 public: | |
24 OutOfProcBrowserTestRunner() { | |
25 } | |
26 | |
27 virtual ~OutOfProcBrowserTestRunner() { | |
28 } | |
29 | |
30 bool Init() { | |
31 return true; | |
32 } | |
33 | |
34 // Returns true if the test succeeded, false if it failed. | |
35 bool RunTest(const std::string& test_name) { | |
36 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
37 CommandLine new_cmd_line(cmd_line->argv()); | |
38 // Always enable disabled tests. This method is not called with disabled | |
39 // tests unless this flag was specified to the browser test executable. | |
40 new_cmd_line.AppendSwitch(L"gtest_also_run_disabled_tests"); | |
41 new_cmd_line.AppendSwitchWithValue(L"gtest_filter", ASCIIToWide(test_name)); | |
42 new_cmd_line.AppendSwitch(kChildProcessFlag); | |
43 | |
44 base::ProcessHandle process_handle; | |
45 bool r = base::LaunchApp(new_cmd_line, false, false, &process_handle); | |
46 if (!r) | |
47 return false; | |
48 | |
49 int exit_code = 0; | |
50 r = base::WaitForExitCode(process_handle, &exit_code); | |
51 if (!r) | |
52 return false; | |
53 | |
54 return exit_code == 0; | |
55 } | |
56 | |
57 private: | |
58 DISALLOW_COPY_AND_ASSIGN(OutOfProcBrowserTestRunner); | |
59 }; | |
60 | |
61 class OutOfProcBrowserTestRunnerFactory | |
62 : public browser_tests::BrowserTestRunnerFactory { | |
63 public: | |
64 OutOfProcBrowserTestRunnerFactory() { } | |
65 | |
66 virtual browser_tests::BrowserTestRunner* CreateBrowserTestRunner() const { | |
67 return new OutOfProcBrowserTestRunner(); | |
68 } | |
69 | |
70 private: | |
71 DISALLOW_COPY_AND_ASSIGN(OutOfProcBrowserTestRunnerFactory); | |
72 }; | |
73 | |
74 } // namespace | |
75 | |
76 int main(int argc, char** argv) { | |
77 CommandLine::Init(argc, argv); | |
78 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
79 | |
80 if (command_line->HasSwitch(kChildProcessFlag)) | |
81 return ChromeTestSuite(argc, argv).Run(); | |
82 | |
83 if (command_line->HasSwitch(kGTestListTestsFlag)) | |
84 return ChromeTestSuite(argc, argv).Run(); | |
85 | |
86 OutOfProcBrowserTestRunnerFactory test_runner_factory; | |
87 return browser_tests::RunTests(test_runner_factory) ? 0 : 1; | |
88 } | |
OLD | NEW |