• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 "chrome/browser/diagnostics/diagnostics_test.h"
6 
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "base/path_service.h"
11 #include "chrome/common/chrome_constants.h"
12 #include "chrome/common/chrome_paths.h"
13 
14 namespace diagnostics {
15 
DiagnosticsTest(DiagnosticsTestId id)16 DiagnosticsTest::DiagnosticsTest(DiagnosticsTestId id)
17     : id_(id), outcome_code_(-1), result_(DiagnosticsModel::TEST_NOT_RUN) {}
18 
~DiagnosticsTest()19 DiagnosticsTest::~DiagnosticsTest() {}
20 
Execute(DiagnosticsModel::Observer * observer,DiagnosticsModel * model,size_t index)21 bool DiagnosticsTest::Execute(DiagnosticsModel::Observer* observer,
22                               DiagnosticsModel* model,
23                               size_t index) {
24   start_time_ = base::Time::Now();
25   result_ = DiagnosticsModel::TEST_RUNNING;
26   bool keep_going = ExecuteImpl(observer);
27   if (observer)
28     observer->OnTestFinished(index, model);
29   return keep_going;
30 }
31 
Recover(DiagnosticsModel::Observer * observer,DiagnosticsModel * model,size_t index)32 bool DiagnosticsTest::Recover(DiagnosticsModel::Observer* observer,
33                               DiagnosticsModel* model,
34                               size_t index) {
35   result_ = DiagnosticsModel::RECOVERY_RUNNING;
36   bool keep_going = RecoveryImpl(observer);
37   result_ = keep_going ? DiagnosticsModel::RECOVERY_OK
38                        : DiagnosticsModel::RECOVERY_FAIL_STOP;
39 #if defined(OS_CHROMEOS)  // Only collecting UMA stats on ChromeOS
40   if (result_ == DiagnosticsModel::RECOVERY_OK) {
41     RecordUMARecoveryResult(static_cast<DiagnosticsTestId>(GetId()),
42                             RESULT_SUCCESS);
43   } else {
44     RecordUMARecoveryResult(static_cast<DiagnosticsTestId>(GetId()),
45                             RESULT_FAILURE);
46   }
47 #endif
48   if (observer)
49     observer->OnRecoveryFinished(index, model);
50   return keep_going;
51 }
52 
RecordOutcome(int outcome_code,const std::string & additional_info,DiagnosticsModel::TestResult result)53 void DiagnosticsTest::RecordOutcome(int outcome_code,
54                                     const std::string& additional_info,
55                                     DiagnosticsModel::TestResult result) {
56   end_time_ = base::Time::Now();
57   outcome_code_ = outcome_code;
58   additional_info_ = additional_info;
59   result_ = result;
60 #if defined(OS_CHROMEOS)  // Only collecting UMA stats on ChromeOS
61   if (result_ == DiagnosticsModel::TEST_OK) {
62     // Record individual test success.
63     RecordUMATestResult(static_cast<DiagnosticsTestId>(GetId()),
64                         RESULT_SUCCESS);
65   } else if (result_ == DiagnosticsModel::TEST_FAIL_CONTINUE ||
66              result_ == DiagnosticsModel::TEST_FAIL_STOP) {
67     // Record test failure in summary histogram.
68     UMA_HISTOGRAM_ENUMERATION(
69         "Diagnostics.TestFailures", GetId(), DIAGNOSTICS_TEST_ID_COUNT);
70     // Record individual test failure.
71     RecordUMATestResult(static_cast<DiagnosticsTestId>(GetId()),
72                         RESULT_FAILURE);
73   }
74 #endif
75 }
76 
77 // static
GetUserDefaultProfileDir()78 base::FilePath DiagnosticsTest::GetUserDefaultProfileDir() {
79   base::FilePath path;
80   if (!PathService::Get(chrome::DIR_USER_DATA, &path))
81     return base::FilePath();
82   return path.AppendASCII(chrome::kInitialProfile);
83 }
84 
GetId() const85 int DiagnosticsTest::GetId() const { return id_; }
86 
GetName() const87 std::string DiagnosticsTest::GetName() const { return GetTestName(id_); }
88 
GetTitle() const89 std::string DiagnosticsTest::GetTitle() const {
90   return GetTestDescription(id_);
91 }
92 
GetResult() const93 DiagnosticsModel::TestResult DiagnosticsTest::GetResult() const {
94   return result_;
95 }
96 
GetOutcomeCode() const97 int DiagnosticsTest::GetOutcomeCode() const { return outcome_code_; }
98 
GetAdditionalInfo() const99 std::string DiagnosticsTest::GetAdditionalInfo() const {
100   return additional_info_;
101 }
102 
GetStartTime() const103 base::Time DiagnosticsTest::GetStartTime() const { return start_time_; }
104 
GetEndTime() const105 base::Time DiagnosticsTest::GetEndTime() const { return end_time_; }
106 
RecoveryImpl(DiagnosticsModel::Observer * observer)107 bool DiagnosticsTest::RecoveryImpl(DiagnosticsModel::Observer* observer) {
108   return true;
109 }
110 
111 }  // namespace diagnostics
112