OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/test/test_launcher.h" | 5 #include "base/test/test_launcher.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 // Called when test named |name| is scheduled to be started. | 121 // Called when test named |name| is scheduled to be started. |
122 void OnTestStarted(const std::string& name); | 122 void OnTestStarted(const std::string& name); |
123 | 123 |
124 // Called when all tests that were to be started have been scheduled | 124 // Called when all tests that were to be started have been scheduled |
125 // to be started. | 125 // to be started. |
126 void OnAllTestsStarted(); | 126 void OnAllTestsStarted(); |
127 | 127 |
128 // Adds |result| to the stored test results. | 128 // Adds |result| to the stored test results. |
129 void AddTestResult(const TestResult& result); | 129 void AddTestResult(const TestResult& result); |
130 | 130 |
| 131 WeakPtr<ResultsPrinter> GetWeakPtr(); |
| 132 |
131 private: | 133 private: |
132 // Prints a list of tests that finished with |status|. | 134 // Prints a list of tests that finished with |status|. |
133 void PrintTestsByStatus(TestResult::Status status, | 135 void PrintTestsByStatus(TestResult::Status status, |
134 const std::string& description); | 136 const std::string& description); |
135 | 137 |
136 // Test results grouped by test case name. | 138 // Test results grouped by test case name. |
137 typedef std::map<std::string, std::vector<TestResult> > ResultsMap; | 139 typedef std::map<std::string, std::vector<TestResult> > ResultsMap; |
138 ResultsMap results_; | 140 ResultsMap results_; |
139 | 141 |
140 // List of full names of failed tests. | 142 // List of full names of failed tests. |
141 typedef std::map<TestResult::Status, std::vector<std::string> > StatusMap; | 143 typedef std::map<TestResult::Status, std::vector<std::string> > StatusMap; |
142 StatusMap tests_by_status_; | 144 StatusMap tests_by_status_; |
143 | 145 |
144 size_t test_started_count_; | 146 size_t test_started_count_; |
145 | 147 |
146 // Total number of tests run. | 148 // Total number of tests run. |
147 size_t test_run_count_; | 149 size_t test_run_count_; |
148 | 150 |
149 // File handle of output file (can be NULL if no file). | 151 // File handle of output file (can be NULL if no file). |
150 FILE* out_; | 152 FILE* out_; |
151 | 153 |
152 RunTestsCallback callback_; | 154 RunTestsCallback callback_; |
153 | 155 |
154 ThreadChecker thread_checker_; | 156 ThreadChecker thread_checker_; |
155 | 157 |
| 158 WeakPtrFactory<ResultsPrinter> weak_ptr_; |
| 159 |
156 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); | 160 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); |
157 }; | 161 }; |
158 | 162 |
159 ResultsPrinter::ResultsPrinter(const CommandLine& command_line, | 163 ResultsPrinter::ResultsPrinter(const CommandLine& command_line, |
160 const RunTestsCallback& callback) | 164 const RunTestsCallback& callback) |
161 : test_started_count_(0), | 165 : test_started_count_(0), |
162 test_run_count_(0), | 166 test_run_count_(0), |
163 out_(NULL), | 167 out_(NULL), |
164 callback_(callback) { | 168 callback_(callback), |
| 169 weak_ptr_(this) { |
165 if (!command_line.HasSwitch(kGTestOutputFlag)) | 170 if (!command_line.HasSwitch(kGTestOutputFlag)) |
166 return; | 171 return; |
167 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); | 172 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); |
168 size_t colon_pos = flag.find(':'); | 173 size_t colon_pos = flag.find(':'); |
169 FilePath path; | 174 FilePath path; |
170 if (colon_pos != std::string::npos) { | 175 if (colon_pos != std::string::npos) { |
171 FilePath flag_path = | 176 FilePath flag_path = |
172 command_line.GetSwitchValuePath(kGTestOutputFlag); | 177 command_line.GetSwitchValuePath(kGTestOutputFlag); |
173 FilePath::StringType path_string = flag_path.value(); | 178 FilePath::StringType path_string = flag_path.value(); |
174 path = FilePath(path_string.substr(colon_pos + 1)); | 179 path = FilePath(path_string.substr(colon_pos + 1)); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 PrintTestsByStatus(TestResult::TEST_CRASH, "crashed"); | 289 PrintTestsByStatus(TestResult::TEST_CRASH, "crashed"); |
285 PrintTestsByStatus(TestResult::TEST_UNKNOWN, "had unknown result"); | 290 PrintTestsByStatus(TestResult::TEST_UNKNOWN, "had unknown result"); |
286 | 291 |
287 callback_.Run( | 292 callback_.Run( |
288 tests_by_status_[TestResult::TEST_SUCCESS].size() == test_run_count_); | 293 tests_by_status_[TestResult::TEST_SUCCESS].size() == test_run_count_); |
289 | 294 |
290 delete this; | 295 delete this; |
291 } | 296 } |
292 } | 297 } |
293 | 298 |
| 299 WeakPtr<ResultsPrinter> ResultsPrinter::GetWeakPtr() { |
| 300 return weak_ptr_.GetWeakPtr(); |
| 301 } |
| 302 |
294 void ResultsPrinter::PrintTestsByStatus(TestResult::Status status, | 303 void ResultsPrinter::PrintTestsByStatus(TestResult::Status status, |
295 const std::string& description) { | 304 const std::string& description) { |
296 DCHECK(thread_checker_.CalledOnValidThread()); | 305 DCHECK(thread_checker_.CalledOnValidThread()); |
297 | 306 |
298 const std::vector<std::string>& tests = tests_by_status_[status]; | 307 const std::vector<std::string>& tests = tests_by_status_[status]; |
299 if (tests.empty()) | 308 if (tests.empty()) |
300 return; | 309 return; |
301 fprintf(stdout, | 310 fprintf(stdout, |
302 "%" PRIuS " test%s %s:\n", | 311 "%" PRIuS " test%s %s:\n", |
303 tests.size(), | 312 tests.size(), |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 426 } |
418 } | 427 } |
419 | 428 |
420 MessageLoop::current()->PostTask( | 429 MessageLoop::current()->PostTask( |
421 FROM_HERE, | 430 FROM_HERE, |
422 Bind(&TestLauncherDelegate::RunRemainingTests, | 431 Bind(&TestLauncherDelegate::RunRemainingTests, |
423 Unretained(launcher_delegate))); | 432 Unretained(launcher_delegate))); |
424 | 433 |
425 MessageLoop::current()->PostTask( | 434 MessageLoop::current()->PostTask( |
426 FROM_HERE, | 435 FROM_HERE, |
427 Bind(&ResultsPrinter::OnAllTestsStarted, Unretained(printer))); | 436 Bind(&ResultsPrinter::OnAllTestsStarted, printer->GetWeakPtr())); |
428 } | 437 } |
429 | 438 |
430 void RunTestIteration(TestLauncherDelegate* launcher_delegate, | 439 void RunTestIteration(TestLauncherDelegate* launcher_delegate, |
431 int32 total_shards, | 440 int32 total_shards, |
432 int32 shard_index, | 441 int32 shard_index, |
433 int cycles, | 442 int cycles, |
434 int* exit_code, | 443 int* exit_code, |
435 bool run_tests_success) { | 444 bool run_tests_success) { |
436 if (!run_tests_success) { | 445 if (!run_tests_success) { |
437 *exit_code = 1; | 446 *exit_code = 1; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 cycles, | 595 cycles, |
587 &exit_code, | 596 &exit_code, |
588 true)); | 597 true)); |
589 | 598 |
590 MessageLoop::current()->Run(); | 599 MessageLoop::current()->Run(); |
591 | 600 |
592 return exit_code; | 601 return exit_code; |
593 } | 602 } |
594 | 603 |
595 } // namespace base | 604 } // namespace base |
OLD | NEW |