Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: components/metrics/metrics_state_manager_unittest.cc

Issue 372473004: Retrieve client_id from GoogleUpdateSettings when its missing from Local State. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_state_manager.h" 5 #include "components/metrics/metrics_state_manager.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/prefs/testing_pref_service.h" 12 #include "base/prefs/testing_pref_service.h"
13 #include "components/metrics/client_info.h"
13 #include "components/metrics/metrics_pref_names.h" 14 #include "components/metrics/metrics_pref_names.h"
15 #include "components/metrics/metrics_service.h"
14 #include "components/metrics/metrics_switches.h" 16 #include "components/metrics/metrics_switches.h"
15 #include "components/variations/caching_permuted_entropy_provider.h" 17 #include "components/variations/caching_permuted_entropy_provider.h"
16 #include "components/variations/pref_names.h" 18 #include "components/variations/pref_names.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 namespace metrics { 21 namespace metrics {
20 22
21 class MetricsStateManagerTest : public testing::Test { 23 class MetricsStateManagerTest : public testing::Test {
22 public: 24 public:
23 MetricsStateManagerTest() : is_metrics_reporting_enabled_(false) { 25 MetricsStateManagerTest() : is_metrics_reporting_enabled_(false) {
24 MetricsStateManager::RegisterPrefs(prefs_.registry()); 26 MetricsService::RegisterPrefs(prefs_.registry());
25 } 27 }
26 28
27 scoped_ptr<MetricsStateManager> CreateStateManager() { 29 scoped_ptr<MetricsStateManager> CreateStateManager() {
28 return MetricsStateManager::Create( 30 return MetricsStateManager::Create(
29 &prefs_, 31 &prefs_,
30 base::Bind(&MetricsStateManagerTest::is_metrics_reporting_enabled, 32 base::Bind(&MetricsStateManagerTest::is_metrics_reporting_enabled,
33 base::Unretained(this)),
34 base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup,
35 base::Unretained(this)),
36 base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup,
31 base::Unretained(this))).Pass(); 37 base::Unretained(this))).Pass();
32 } 38 }
33 39
34 // Sets metrics reporting as enabled for testing. 40 // Sets metrics reporting as enabled for testing.
35 void EnableMetricsReporting() { 41 void EnableMetricsReporting() {
36 is_metrics_reporting_enabled_ = true; 42 is_metrics_reporting_enabled_ = true;
37 } 43 }
38 44
39 protected: 45 protected:
40 TestingPrefServiceSimple prefs_; 46 TestingPrefServiceSimple prefs_;
41 47
48 // Last ClientInfo stored by the MetricsStateManager via
49 // MockStoreClientInfoBackup.
50 scoped_ptr<ClientInfo> stored_client_info_backup_;
51
52 // If set, will be returned via LoadFakeClientInfoBackup if requested by the
53 // MetricsStateManager.
54 scoped_ptr<ClientInfo> fake_client_info_backup_;
55
42 private: 56 private:
43 bool is_metrics_reporting_enabled() const { 57 bool is_metrics_reporting_enabled() const {
44 return is_metrics_reporting_enabled_; 58 return is_metrics_reporting_enabled_;
45 } 59 }
46 60
61 // Stores the |client_info| in |stored_client_info_backup_| for verification
62 // by the tests later.
63 void MockStoreClientInfoBackup(const ClientInfo& client_info) {
64 stored_client_info_backup_.reset(new ClientInfo);
65 stored_client_info_backup_->client_id = client_info.client_id;
66 stored_client_info_backup_->installation_date =
67 client_info.installation_date;
68 stored_client_info_backup_->reporting_enabled_date =
69 client_info.reporting_enabled_date;
70 }
71
72 // Hands out a copy of |fake_client_info_backup_| if it is set.
73 scoped_ptr<ClientInfo> LoadFakeClientInfoBackup() {
74 if (!fake_client_info_backup_)
75 return scoped_ptr<ClientInfo>();
76
77 scoped_ptr<ClientInfo> backup_copy(new ClientInfo);
78 backup_copy->client_id = fake_client_info_backup_->client_id;
79 backup_copy->installation_date =
80 fake_client_info_backup_->installation_date;
81 backup_copy->reporting_enabled_date =
82 fake_client_info_backup_->reporting_enabled_date;
83 return backup_copy.Pass();
84 }
85
47 bool is_metrics_reporting_enabled_; 86 bool is_metrics_reporting_enabled_;
48 87
49 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest); 88 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest);
50 }; 89 };
51 90
52 // Ensure the ClientId is formatted as expected. 91 // Ensure the ClientId is formatted as expected.
53 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { 92 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) {
54 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); 93 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
55 state_manager->ForceClientIdCreation(); 94 state_manager->ForceClientIdCreation();
56 95
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 EXPECT_NE(kInitialClientId, state_manager->client_id()); 203 EXPECT_NE(kInitialClientId, state_manager->client_id());
165 204
166 state_manager->GetLowEntropySource(); 205 state_manager->GetLowEntropySource();
167 206
168 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); 207 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));
169 } 208 }
170 209
171 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); 210 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID));
172 } 211 }
173 212
213 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
214 const int64 kFakeInstallationDate = 12345;
215 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);
216
217 // Holds ClientInfo from previous scoped test for extra checks.
218 scoped_ptr<ClientInfo> previous_client_info;
219
220 {
221 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
222
223 // client_id shouldn't be auto-generated if metrics reporting is not
224 // enabled.
225 EXPECT_EQ(std::string(), state_manager->client_id());
226 EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
227
228 // Confirm that the initial ForceClientIdCreation call creates the client id
229 // and backs it up via MockStoreClientInfoBackup.
230 EXPECT_FALSE(stored_client_info_backup_);
231 state_manager->ForceClientIdCreation();
232 EXPECT_NE(std::string(), state_manager->client_id());
233 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);
234
235 ASSERT_TRUE(stored_client_info_backup_);
236 EXPECT_EQ(state_manager->client_id(),
237 stored_client_info_backup_->client_id);
238 EXPECT_EQ(kFakeInstallationDate,
239 stored_client_info_backup_->installation_date);
240 EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
241 stored_client_info_backup_->reporting_enabled_date);
242
243 previous_client_info = stored_client_info_backup_.Pass();
244 }
245
246 EnableMetricsReporting();
247
248 {
249 EXPECT_FALSE(stored_client_info_backup_);
250
251 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
252
253 // client_id should be auto-obtained from the constructor when metrics
254 // reporting is enabled.
255 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id());
256
257 // The backup should also be refreshed when the client id re-initialized.
258 ASSERT_TRUE(stored_client_info_backup_);
259 EXPECT_EQ(previous_client_info->client_id,
260 stored_client_info_backup_->client_id);
261 EXPECT_EQ(kFakeInstallationDate,
262 stored_client_info_backup_->installation_date);
263 EXPECT_EQ(previous_client_info->reporting_enabled_date,
264 stored_client_info_backup_->reporting_enabled_date);
265
266 // Re-forcing client id creation shouldn't cause another backup and
267 // shouldn't affect the existing client id.
268 stored_client_info_backup_.reset();
269 state_manager->ForceClientIdCreation();
270 EXPECT_FALSE(stored_client_info_backup_);
271 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id());
272 }
273
274 const int64 kBackupInstallationDate = 1111;
275 const int64 kBackupReportingEnabledDate = 2222;
276 const char kBackupClientId[] = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
277 fake_client_info_backup_.reset(new ClientInfo);
278 fake_client_info_backup_->client_id = kBackupClientId;
279 fake_client_info_backup_->installation_date = kBackupInstallationDate;
280 fake_client_info_backup_->reporting_enabled_date =
281 kBackupReportingEnabledDate;
282
283 {
284 // The existence of a backup should result in the same behaviour as
285 // before if we already have a client id.
286
287 EXPECT_FALSE(stored_client_info_backup_);
288
289 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
290 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id());
291
292 // The backup should also be refreshed when the client id re-initialized.
293 ASSERT_TRUE(stored_client_info_backup_);
294 EXPECT_EQ(previous_client_info->client_id,
295 stored_client_info_backup_->client_id);
296 EXPECT_EQ(kFakeInstallationDate,
297 stored_client_info_backup_->installation_date);
298 EXPECT_EQ(previous_client_info->reporting_enabled_date,
299 stored_client_info_backup_->reporting_enabled_date);
300 stored_client_info_backup_.reset();
301 }
302
303 prefs_.ClearPref(prefs::kMetricsClientID);
304 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
305
306 {
307 // The backup should kick in if the client id has gone missing. It should
308 // replace remaining and missing dates as well.
309
310 EXPECT_FALSE(stored_client_info_backup_);
311
312 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
313 EXPECT_EQ(kBackupClientId, state_manager->client_id());
314 EXPECT_EQ(kBackupInstallationDate, prefs_.GetInt64(prefs::kInstallDate));
315 EXPECT_EQ(kBackupReportingEnabledDate,
316 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
317
318 EXPECT_TRUE(stored_client_info_backup_);
319 stored_client_info_backup_.reset();
320 }
321
322 const char kNoDashesBackupClientId[] = "AAAAAAAABBBBCCCCDDDDEEEEEEEEEEEE";
323 fake_client_info_backup_.reset(new ClientInfo);
324 fake_client_info_backup_->client_id = kNoDashesBackupClientId;
325
326 prefs_.ClearPref(prefs::kInstallDate);
327 prefs_.ClearPref(prefs::kMetricsClientID);
328 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
329
330 {
331 // When running the backup from old-style client ids, dashes should be
332 // re-added. And missing dates in backup should be replaced by Time::Now().
333
334 EXPECT_FALSE(stored_client_info_backup_);
335
336 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
337 EXPECT_EQ(kBackupClientId, state_manager->client_id());
338 EXPECT_GT(prefs_.GetInt64(prefs::kInstallDate), 0);
339 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);
340
341 EXPECT_TRUE(stored_client_info_backup_);
342 stored_client_info_backup_.reset();
343 }
344 }
345
174 } // namespace metrics 346 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698