OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/diagnostics/diagnostics_main.h" | 5 #include "chrome/browser/diagnostics/diagnostics_main.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 208 |
209 // How many tests reported failure. | 209 // How many tests reported failure. |
210 int failures() { return failures_; } | 210 int failures() { return failures_; } |
211 | 211 |
212 // Write an informational line of text in white over black. | 212 // Write an informational line of text in white over black. |
213 bool WriteInfoText(const std::wstring& txt) { | 213 bool WriteInfoText(const std::wstring& txt) { |
214 console_->SetColor(SimpleConsole::DEFAULT); | 214 console_->SetColor(SimpleConsole::DEFAULT); |
215 return console_->Write(txt); | 215 return console_->Write(txt); |
216 } | 216 } |
217 | 217 |
| 218 bool WriteInfoText(const std::string& txt) { |
| 219 return WriteInfoText(UTF8ToWide(txt)); |
| 220 } |
| 221 |
218 // Write a result block. It consist of two lines. The first line | 222 // Write a result block. It consist of two lines. The first line |
219 // has [PASS] or [FAIL] with |name| and the second line has | 223 // has [PASS] or [FAIL] with |name| and the second line has |
220 // the text in |extra|. | 224 // the text in |extra|. |
221 bool WriteResult(bool success, const std::wstring& name, | 225 bool WriteResult(bool success, const std::wstring& name, |
222 const std::wstring& extra) { | 226 const std::wstring& extra) { |
223 if (success) { | 227 if (success) { |
224 console_->SetColor(SimpleConsole::GREEN); | 228 console_->SetColor(SimpleConsole::GREEN); |
225 console_->Write(L"[PASS] "); | 229 console_->Write(L"[PASS] "); |
226 } else { | 230 } else { |
227 console_->SetColor(SimpleConsole::RED); | 231 console_->SetColor(SimpleConsole::RED); |
228 console_->Write(L"[FAIL] "); | 232 console_->Write(L"[FAIL] "); |
229 failures_++; | 233 failures_++; |
230 } | 234 } |
231 WriteInfoText(name + L"\n"); | 235 WriteInfoText(name + L"\n"); |
232 std::wstring second_line(L" "); | 236 std::wstring second_line(L" "); |
233 second_line.append(extra); | 237 second_line.append(extra); |
234 return WriteInfoText(second_line + L"\n\n"); | 238 return WriteInfoText(second_line + L"\n\n"); |
235 } | 239 } |
236 | 240 |
237 private: | 241 private: |
238 SimpleConsole* console_; | 242 SimpleConsole* console_; |
239 | 243 |
240 // Keeps track of how many tests reported failure. | 244 // Keeps track of how many tests reported failure. |
241 int failures_; | 245 int failures_; |
242 | 246 |
243 DISALLOW_COPY_AND_ASSIGN(TestWriter); | 247 DISALLOW_COPY_AND_ASSIGN(TestWriter); |
244 }; | 248 }; |
245 | 249 |
246 std::wstring PrintableUSCurrentTime() { | 250 std::string PrintableUSCurrentTime() { |
247 base::Time::Exploded exploded = {0}; | 251 base::Time::Exploded exploded = {0}; |
248 base::Time::Now().UTCExplode(&exploded); | 252 base::Time::Now().UTCExplode(&exploded); |
249 return base::StringPrintf(L"%d:%d:%d.%d:%d:%d", | 253 return base::StringPrintf("%d:%d:%d.%d:%d:%d", |
250 exploded.year, | 254 exploded.year, |
251 exploded.month, | 255 exploded.month, |
252 exploded.day_of_month, | 256 exploded.day_of_month, |
253 exploded.hour, | 257 exploded.hour, |
254 exploded.minute, | 258 exploded.minute, |
255 exploded.second); | 259 exploded.second); |
256 } | 260 } |
257 | 261 |
258 // This class is a basic test controller. In this design the view (TestWriter) | 262 // This class is a basic test controller. In this design the view (TestWriter) |
259 // and the model (DiagnosticsModel) do not talk to each other directly but they | 263 // and the model (DiagnosticsModel) do not talk to each other directly but they |
260 // are mediated by the controller. This has a name: 'passive view'. | 264 // are mediated by the controller. This has a name: 'passive view'. |
261 // More info at http://martinfowler.com/eaaDev/PassiveScreen.html | 265 // More info at http://martinfowler.com/eaaDev/PassiveScreen.html |
262 class TestController : public DiagnosticsModel::Observer { | 266 class TestController : public DiagnosticsModel::Observer { |
263 public: | 267 public: |
264 explicit TestController(TestWriter* writer) | 268 explicit TestController(TestWriter* writer) |
265 : model_(NULL), | 269 : model_(NULL), |
266 writer_(writer) { | 270 writer_(writer) { |
267 } | 271 } |
268 | 272 |
269 // Run all the diagnostics of |model| and invoke the view as the model | 273 // Run all the diagnostics of |model| and invoke the view as the model |
270 // callbacks arrive. | 274 // callbacks arrive. |
271 void Run(DiagnosticsModel* model) { | 275 void Run(DiagnosticsModel* model) { |
272 std::wstring title(L"Chrome Diagnostics Mode ("); | 276 writer_->WriteInfoText(L"Chrome Diagnostics Mode ("); |
273 writer_->WriteInfoText(title.append(PrintableUSCurrentTime()) + L")\n"); | 277 writer_->WriteInfoText(PrintableUSCurrentTime() + ")\n"); |
274 if (!model) { | 278 if (!model) { |
275 writer_->WriteResult(false, L"Diagnostics start", L"model is null"); | 279 writer_->WriteResult(false, L"Diagnostics start", L"model is null"); |
276 return; | 280 return; |
277 } | 281 } |
278 bool icu_result = icu_util::Initialize(); | 282 bool icu_result = icu_util::Initialize(); |
279 if (!icu_result) { | 283 if (!icu_result) { |
280 writer_->WriteResult(false, L"Diagnostics start", L"ICU failure"); | 284 writer_->WriteResult(false, L"Diagnostics start", L"ICU failure"); |
281 return; | 285 return; |
282 } | 286 } |
283 ResourceBundle::InitSharedInstanceWithLocale(std::string(), NULL); | 287 ResourceBundle::InitSharedInstanceWithLocale(std::string(), NULL); |
284 int count = model->GetTestAvailableCount(); | 288 int count = model->GetTestAvailableCount(); |
285 writer_->WriteInfoText(base::StringPrintf( | 289 writer_->WriteInfoText(base::StringPrintf( |
286 L"%d available test(s)\n\n", count)); | 290 "%d available test(s)\n\n", count)); |
287 model->RunAll(this); | 291 model->RunAll(this); |
288 } | 292 } |
289 | 293 |
290 // Next four are overridden from DiagnosticsModel::Observer. | 294 // Next four are overridden from DiagnosticsModel::Observer. |
291 virtual void OnProgress(int id, int percent, DiagnosticsModel* model) { | 295 virtual void OnProgress(int id, int percent, DiagnosticsModel* model) { |
292 } | 296 } |
293 | 297 |
294 virtual void OnSkipped(int id, DiagnosticsModel* model) { | 298 virtual void OnSkipped(int id, DiagnosticsModel* model) { |
295 // TODO(cpu): display skipped tests. | 299 // TODO(cpu): display skipped tests. |
296 } | 300 } |
297 | 301 |
298 virtual void OnFinished(int id, DiagnosticsModel* model) { | 302 virtual void OnFinished(int id, DiagnosticsModel* model) { |
299 // As each test completes we output the results. | 303 // As each test completes we output the results. |
300 ShowResult(&model->GetTest(id)); | 304 ShowResult(&model->GetTest(id)); |
301 } | 305 } |
302 | 306 |
303 virtual void OnDoneAll(DiagnosticsModel* model) { | 307 virtual void OnDoneAll(DiagnosticsModel* model) { |
304 if (writer_->failures() > 0) { | 308 if (writer_->failures() > 0) { |
305 writer_->WriteInfoText(base::StringPrintf( | 309 writer_->WriteInfoText(base::StringPrintf( |
306 L"DONE. %d failure(s)\n\n", writer_->failures())); | 310 "DONE. %d failure(s)\n\n", writer_->failures())); |
307 } else { | 311 } else { |
308 writer_->WriteInfoText(L"DONE\n\n"); | 312 writer_->WriteInfoText(L"DONE\n\n"); |
309 } | 313 } |
310 } | 314 } |
311 | 315 |
312 private: | 316 private: |
313 void ShowResult(DiagnosticsModel::TestInfo* test_info) { | 317 void ShowResult(DiagnosticsModel::TestInfo* test_info) { |
314 bool success = (DiagnosticsModel::TEST_OK == test_info->GetResult()); | 318 bool success = (DiagnosticsModel::TEST_OK == test_info->GetResult()); |
315 writer_->WriteResult(success, UTF16ToWide(test_info->GetTitle()), | 319 writer_->WriteResult(success, UTF16ToWide(test_info->GetTitle()), |
316 UTF16ToWide(test_info->GetAdditionalInfo())); | 320 UTF16ToWide(test_info->GetAdditionalInfo())); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 TestController controller(&writer); | 356 TestController controller(&writer); |
353 | 357 |
354 // Run all the diagnostic tests. | 358 // Run all the diagnostic tests. |
355 controller.Run(model); | 359 controller.Run(model); |
356 delete model; | 360 delete model; |
357 | 361 |
358 console->OnQuit(); | 362 console->OnQuit(); |
359 delete console; | 363 delete console; |
360 return 0; | 364 return 0; |
361 } | 365 } |
OLD | NEW |