OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/metrics/metrics_service.h" | 5 #include "components/metrics/metrics_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
14 #include "components/metrics/client_info.h" | 14 #include "components/metrics/client_info.h" |
15 #include "components/metrics/compression_utils.h" | 15 #include "components/metrics/compression_utils.h" |
16 #include "components/metrics/metrics_log.h" | 16 #include "components/metrics/metrics_log.h" |
17 #include "components/metrics/metrics_pref_names.h" | 17 #include "components/metrics/metrics_pref_names.h" |
18 #include "components/metrics/metrics_service_observer.h" | 18 #include "components/metrics/metrics_service_observer.h" |
19 #include "components/metrics/metrics_state_manager.h" | 19 #include "components/metrics/metrics_state_manager.h" |
20 #include "components/metrics/test_metrics_service_client.h" | 20 #include "components/metrics/test_metrics_service_client.h" |
21 #include "components/variations/metrics_util.h" | 21 #include "components/variations/metrics_util.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
| 24 namespace metrics { |
| 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 using metrics::MetricsLogManager; | 28 void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) { |
27 | |
28 void StoreNoClientInfoBackup(const metrics::ClientInfo& /* client_info */) { | |
29 } | 29 } |
30 | 30 |
31 scoped_ptr<metrics::ClientInfo> ReturnNoBackup() { | 31 scoped_ptr<ClientInfo> ReturnNoBackup() { |
32 return scoped_ptr<metrics::ClientInfo>(); | 32 return scoped_ptr<ClientInfo>(); |
33 } | 33 } |
34 | 34 |
35 class TestMetricsService : public MetricsService { | 35 class TestMetricsService : public MetricsService { |
36 public: | 36 public: |
37 TestMetricsService(metrics::MetricsStateManager* state_manager, | 37 TestMetricsService(MetricsStateManager* state_manager, |
38 metrics::MetricsServiceClient* client, | 38 MetricsServiceClient* client, |
39 PrefService* local_state) | 39 PrefService* local_state) |
40 : MetricsService(state_manager, client, local_state) {} | 40 : MetricsService(state_manager, client, local_state) {} |
41 virtual ~TestMetricsService() {} | 41 virtual ~TestMetricsService() {} |
42 | 42 |
43 using MetricsService::log_manager; | 43 using MetricsService::log_manager; |
44 | 44 |
45 private: | 45 private: |
46 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); | 46 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); |
47 }; | 47 }; |
48 | 48 |
49 class TestMetricsLog : public MetricsLog { | 49 class TestMetricsLog : public MetricsLog { |
50 public: | 50 public: |
51 TestMetricsLog(const std::string& client_id, | 51 TestMetricsLog(const std::string& client_id, |
52 int session_id, | 52 int session_id, |
53 metrics::MetricsServiceClient* client, | 53 MetricsServiceClient* client, |
54 PrefService* local_state) | 54 PrefService* local_state) |
55 : MetricsLog(client_id, | 55 : MetricsLog(client_id, |
56 session_id, | 56 session_id, |
57 MetricsLog::ONGOING_LOG, | 57 MetricsLog::ONGOING_LOG, |
58 client, | 58 client, |
59 local_state) {} | 59 local_state) {} |
60 | 60 |
61 virtual ~TestMetricsLog() {} | 61 virtual ~TestMetricsLog() {} |
62 | 62 |
63 private: | 63 private: |
64 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); | 64 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
65 }; | 65 }; |
66 | 66 |
67 class MetricsServiceTest : public testing::Test { | 67 class MetricsServiceTest : public testing::Test { |
68 public: | 68 public: |
69 MetricsServiceTest() : is_metrics_reporting_enabled_(false) { | 69 MetricsServiceTest() : is_metrics_reporting_enabled_(false) { |
70 MetricsService::RegisterPrefs(testing_local_state_.registry()); | 70 MetricsService::RegisterPrefs(testing_local_state_.registry()); |
71 metrics_state_manager_ = metrics::MetricsStateManager::Create( | 71 metrics_state_manager_ = MetricsStateManager::Create( |
72 GetLocalState(), | 72 GetLocalState(), |
73 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled, | 73 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled, |
74 base::Unretained(this)), | 74 base::Unretained(this)), |
75 base::Bind(&StoreNoClientInfoBackup), | 75 base::Bind(&StoreNoClientInfoBackup), |
76 base::Bind(&ReturnNoBackup)); | 76 base::Bind(&ReturnNoBackup)); |
77 } | 77 } |
78 | 78 |
79 virtual ~MetricsServiceTest() { | 79 virtual ~MetricsServiceTest() { |
80 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE, | 80 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE, |
81 GetLocalState()); | 81 GetLocalState()); |
82 } | 82 } |
83 | 83 |
84 metrics::MetricsStateManager* GetMetricsStateManager() { | 84 MetricsStateManager* GetMetricsStateManager() { |
85 return metrics_state_manager_.get(); | 85 return metrics_state_manager_.get(); |
86 } | 86 } |
87 | 87 |
88 PrefService* GetLocalState() { return &testing_local_state_; } | 88 PrefService* GetLocalState() { return &testing_local_state_; } |
89 | 89 |
90 // Sets metrics reporting as enabled for testing. | 90 // Sets metrics reporting as enabled for testing. |
91 void EnableMetricsReporting() { | 91 void EnableMetricsReporting() { |
92 is_metrics_reporting_enabled_ = true; | 92 is_metrics_reporting_enabled_ = true; |
93 } | 93 } |
94 | 94 |
95 // Waits until base::TimeTicks::Now() no longer equals |value|. This should | 95 // Waits until base::TimeTicks::Now() no longer equals |value|. This should |
96 // take between 1-15ms per the documented resolution of base::TimeTicks. | 96 // take between 1-15ms per the documented resolution of base::TimeTicks. |
97 void WaitUntilTimeChanges(const base::TimeTicks& value) { | 97 void WaitUntilTimeChanges(const base::TimeTicks& value) { |
98 while (base::TimeTicks::Now() == value) { | 98 while (base::TimeTicks::Now() == value) { |
99 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 99 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 // Returns true if there is a synthetic trial in the given vector that matches | 103 // Returns true if there is a synthetic trial in the given vector that matches |
104 // the given trial name and trial group; returns false otherwise. | 104 // the given trial name and trial group; returns false otherwise. |
105 bool HasSyntheticTrial( | 105 bool HasSyntheticTrial( |
106 const std::vector<variations::ActiveGroupId>& synthetic_trials, | 106 const std::vector<variations::ActiveGroupId>& synthetic_trials, |
107 const std::string& trial_name, | 107 const std::string& trial_name, |
108 const std::string& trial_group) { | 108 const std::string& trial_group) { |
109 uint32 trial_name_hash = metrics::HashName(trial_name); | 109 uint32 trial_name_hash = HashName(trial_name); |
110 uint32 trial_group_hash = metrics::HashName(trial_group); | 110 uint32 trial_group_hash = HashName(trial_group); |
111 for (std::vector<variations::ActiveGroupId>::const_iterator it = | 111 for (std::vector<variations::ActiveGroupId>::const_iterator it = |
112 synthetic_trials.begin(); | 112 synthetic_trials.begin(); |
113 it != synthetic_trials.end(); ++it) { | 113 it != synthetic_trials.end(); ++it) { |
114 if ((*it).name == trial_name_hash && (*it).group == trial_group_hash) | 114 if ((*it).name == trial_name_hash && (*it).group == trial_group_hash) |
115 return true; | 115 return true; |
116 } | 116 } |
117 return false; | 117 return false; |
118 } | 118 } |
119 | 119 |
120 private: | 120 private: |
121 bool is_metrics_reporting_enabled() const { | 121 bool is_metrics_reporting_enabled() const { |
122 return is_metrics_reporting_enabled_; | 122 return is_metrics_reporting_enabled_; |
123 } | 123 } |
124 | 124 |
125 bool is_metrics_reporting_enabled_; | 125 bool is_metrics_reporting_enabled_; |
126 TestingPrefServiceSimple testing_local_state_; | 126 TestingPrefServiceSimple testing_local_state_; |
127 scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_; | 127 scoped_ptr<MetricsStateManager> metrics_state_manager_; |
128 base::MessageLoop message_loop; | 128 base::MessageLoop message_loop; |
129 | 129 |
130 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); | 130 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); |
131 }; | 131 }; |
132 | 132 |
133 class TestMetricsServiceObserver : public MetricsServiceObserver { | 133 class TestMetricsServiceObserver : public MetricsServiceObserver { |
134 public: | 134 public: |
135 TestMetricsServiceObserver(): observed_(0) {} | 135 TestMetricsServiceObserver(): observed_(0) {} |
136 virtual ~TestMetricsServiceObserver() {} | 136 virtual ~TestMetricsServiceObserver() {} |
137 | 137 |
138 virtual void OnDidCreateMetricsLog() OVERRIDE { | 138 virtual void OnDidCreateMetricsLog() OVERRIDE { |
139 ++observed_; | 139 ++observed_; |
140 } | 140 } |
141 int observed() const { return observed_; } | 141 int observed() const { return observed_; } |
142 | 142 |
143 private: | 143 private: |
144 int observed_; | 144 int observed_; |
145 | 145 |
146 DISALLOW_COPY_AND_ASSIGN(TestMetricsServiceObserver); | 146 DISALLOW_COPY_AND_ASSIGN(TestMetricsServiceObserver); |
147 }; | 147 }; |
148 | 148 |
149 } // namespace | 149 } // namespace |
150 | 150 |
151 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { | 151 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { |
152 EnableMetricsReporting(); | 152 EnableMetricsReporting(); |
153 GetLocalState()->SetBoolean(metrics::prefs::kStabilityExitedCleanly, true); | 153 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); |
154 | 154 |
155 metrics::TestMetricsServiceClient client; | 155 TestMetricsServiceClient client; |
156 TestMetricsService service( | 156 TestMetricsService service( |
157 GetMetricsStateManager(), &client, GetLocalState()); | 157 GetMetricsStateManager(), &client, GetLocalState()); |
158 service.InitializeMetricsRecordingState(); | 158 service.InitializeMetricsRecordingState(); |
159 // No initial stability log should be generated. | 159 // No initial stability log should be generated. |
160 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); | 160 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); |
161 EXPECT_FALSE(service.log_manager()->has_staged_log()); | 161 EXPECT_FALSE(service.log_manager()->has_staged_log()); |
162 } | 162 } |
163 | 163 |
164 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { | 164 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { |
165 EnableMetricsReporting(); | 165 EnableMetricsReporting(); |
166 GetLocalState()->ClearPref(metrics::prefs::kStabilityExitedCleanly); | 166 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); |
167 | 167 |
168 // Set up prefs to simulate restarting after a crash. | 168 // Set up prefs to simulate restarting after a crash. |
169 | 169 |
170 // Save an existing system profile to prefs, to correspond to what would be | 170 // Save an existing system profile to prefs, to correspond to what would be |
171 // saved from a previous session. | 171 // saved from a previous session. |
172 metrics::TestMetricsServiceClient client; | 172 TestMetricsServiceClient client; |
173 TestMetricsLog log("client", 1, &client, GetLocalState()); | 173 TestMetricsLog log("client", 1, &client, GetLocalState()); |
174 log.RecordEnvironment(std::vector<metrics::MetricsProvider*>(), | 174 log.RecordEnvironment(std::vector<MetricsProvider*>(), |
175 std::vector<variations::ActiveGroupId>(), | 175 std::vector<variations::ActiveGroupId>(), |
176 0); | 176 0); |
177 | 177 |
178 // Record stability build time and version from previous session, so that | 178 // Record stability build time and version from previous session, so that |
179 // stability metrics (including exited cleanly flag) won't be cleared. | 179 // stability metrics (including exited cleanly flag) won't be cleared. |
180 GetLocalState()->SetInt64(metrics::prefs::kStabilityStatsBuildTime, | 180 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, |
181 MetricsLog::GetBuildTime()); | 181 MetricsLog::GetBuildTime()); |
182 GetLocalState()->SetString(metrics::prefs::kStabilityStatsVersion, | 182 GetLocalState()->SetString(prefs::kStabilityStatsVersion, |
183 client.GetVersionString()); | 183 client.GetVersionString()); |
184 | 184 |
185 GetLocalState()->SetBoolean(metrics::prefs::kStabilityExitedCleanly, false); | 185 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); |
186 | 186 |
187 TestMetricsService service( | 187 TestMetricsService service( |
188 GetMetricsStateManager(), &client, GetLocalState()); | 188 GetMetricsStateManager(), &client, GetLocalState()); |
189 service.InitializeMetricsRecordingState(); | 189 service.InitializeMetricsRecordingState(); |
190 | 190 |
191 // The initial stability log should be generated and persisted in unsent logs. | 191 // The initial stability log should be generated and persisted in unsent logs. |
192 MetricsLogManager* log_manager = service.log_manager(); | 192 MetricsLogManager* log_manager = service.log_manager(); |
193 EXPECT_TRUE(log_manager->has_unsent_logs()); | 193 EXPECT_TRUE(log_manager->has_unsent_logs()); |
194 EXPECT_FALSE(log_manager->has_staged_log()); | 194 EXPECT_FALSE(log_manager->has_staged_log()); |
195 | 195 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 | 307 |
308 service.RemoveObserver(&observer1); | 308 service.RemoveObserver(&observer1); |
309 | 309 |
310 service.OpenNewLog(); | 310 service.OpenNewLog(); |
311 EXPECT_EQ(2, observer1.observed()); | 311 EXPECT_EQ(2, observer1.observed()); |
312 EXPECT_EQ(2, observer2.observed()); | 312 EXPECT_EQ(2, observer2.observed()); |
313 service.log_manager_.FinishCurrentLog(); | 313 service.log_manager_.FinishCurrentLog(); |
314 | 314 |
315 service.RemoveObserver(&observer2); | 315 service.RemoveObserver(&observer2); |
316 } | 316 } |
| 317 |
| 318 } // namespace metrics |
OLD | NEW |