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

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

Issue 2732713003: Delete old client info migration behavior. (Closed)
Patch Set: Update tests Created 3 years, 9 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
« no previous file with comments | « components/metrics/metrics_state_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <string> 10 #include <string>
(...skipping 10 matching lines...) Expand all
21 #include "components/prefs/testing_pref_service.h" 21 #include "components/prefs/testing_pref_service.h"
22 #include "components/variations/caching_permuted_entropy_provider.h" 22 #include "components/variations/caching_permuted_entropy_provider.h"
23 #include "components/variations/pref_names.h" 23 #include "components/variations/pref_names.h"
24 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 25
26 namespace metrics { 26 namespace metrics {
27 27
28 class MetricsStateManagerTest : public testing::Test { 28 class MetricsStateManagerTest : public testing::Test {
29 public: 29 public:
30 MetricsStateManagerTest() 30 MetricsStateManagerTest()
31 : enabled_state_provider_(new TestEnabledStateProvider(false, false)) { 31 : test_begin_time_(base::Time::Now().ToTimeT()),
32 enabled_state_provider_(new TestEnabledStateProvider(false, false)) {
32 MetricsService::RegisterPrefs(prefs_.registry()); 33 MetricsService::RegisterPrefs(prefs_.registry());
33 } 34 }
34 35
35 std::unique_ptr<MetricsStateManager> CreateStateManager() { 36 std::unique_ptr<MetricsStateManager> CreateStateManager() {
36 return MetricsStateManager::Create( 37 return MetricsStateManager::Create(
37 &prefs_, enabled_state_provider_.get(), 38 &prefs_, enabled_state_provider_.get(),
38 base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup, 39 base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup,
39 base::Unretained(this)), 40 base::Unretained(this)),
40 base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup, 41 base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup,
41 base::Unretained(this))); 42 base::Unretained(this)));
42 } 43 }
43 44
44 // Sets metrics reporting as enabled for testing. 45 // Sets metrics reporting as enabled for testing.
45 void EnableMetricsReporting() { 46 void EnableMetricsReporting() {
46 enabled_state_provider_->set_consent(true); 47 enabled_state_provider_->set_consent(true);
47 enabled_state_provider_->set_enabled(true); 48 enabled_state_provider_->set_enabled(true);
48 } 49 }
49 50
51 void SetClientInfoPrefs(const ClientInfo& client_info) {
52 prefs_.SetString(prefs::kMetricsClientID, client_info.client_id);
53 prefs_.SetInt64(prefs::kInstallDate, client_info.installation_date);
54 prefs_.SetInt64(prefs::kMetricsReportingEnabledTimestamp,
55 client_info.reporting_enabled_date);
56 }
57
58 void SetFakeClientInfoBackup(const ClientInfo& client_info) {
59 fake_client_info_backup_.reset(new ClientInfo);
60 fake_client_info_backup_->client_id = client_info.client_id;
61 fake_client_info_backup_->installation_date = client_info.installation_date;
62 fake_client_info_backup_->reporting_enabled_date =
63 client_info.reporting_enabled_date;
64 }
65
50 protected: 66 protected:
51 TestingPrefServiceSimple prefs_; 67 TestingPrefServiceSimple prefs_;
52 68
53 // Last ClientInfo stored by the MetricsStateManager via 69 // Last ClientInfo stored by the MetricsStateManager via
54 // MockStoreClientInfoBackup. 70 // MockStoreClientInfoBackup.
55 std::unique_ptr<ClientInfo> stored_client_info_backup_; 71 std::unique_ptr<ClientInfo> stored_client_info_backup_;
56 72
57 // If set, will be returned via LoadFakeClientInfoBackup if requested by the 73 // If set, will be returned via LoadFakeClientInfoBackup if requested by the
58 // MetricsStateManager. 74 // MetricsStateManager.
59 std::unique_ptr<ClientInfo> fake_client_info_backup_; 75 std::unique_ptr<ClientInfo> fake_client_info_backup_;
60 76
77 const int64_t test_begin_time_;
78
61 private: 79 private:
62 // Stores the |client_info| in |stored_client_info_backup_| for verification 80 // Stores the |client_info| in |stored_client_info_backup_| for verification
63 // by the tests later. 81 // by the tests later.
64 void MockStoreClientInfoBackup(const ClientInfo& client_info) { 82 void MockStoreClientInfoBackup(const ClientInfo& client_info) {
65 stored_client_info_backup_.reset(new ClientInfo); 83 stored_client_info_backup_.reset(new ClientInfo);
66 stored_client_info_backup_->client_id = client_info.client_id; 84 stored_client_info_backup_->client_id = client_info.client_id;
67 stored_client_info_backup_->installation_date = 85 stored_client_info_backup_->installation_date =
68 client_info.installation_date; 86 client_info.installation_date;
69 stored_client_info_backup_->reporting_enabled_date = 87 stored_client_info_backup_->reporting_enabled_date =
70 client_info.reporting_enabled_date; 88 client_info.reporting_enabled_date;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); 233 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));
216 } 234 }
217 235
218 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); 236 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID));
219 } 237 }
220 238
221 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) { 239 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
222 const int64_t kFakeInstallationDate = 12345; 240 const int64_t kFakeInstallationDate = 12345;
223 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate); 241 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);
224 242
225 const int64_t test_begin_time = base::Time::Now().ToTimeT();
226
227 // Holds ClientInfo from previous scoped test for extra checks.
228 std::unique_ptr<ClientInfo> previous_client_info;
229
230 { 243 {
231 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); 244 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
232 245
233 // client_id shouldn't be auto-generated if metrics reporting is not 246 // client_id shouldn't be auto-generated if metrics reporting is not
234 // enabled. 247 // enabled.
235 EXPECT_EQ(std::string(), state_manager->client_id()); 248 EXPECT_EQ(std::string(), state_manager->client_id());
236 EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp)); 249 EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
237 250
238 // Confirm that the initial ForceClientIdCreation call creates the client id 251 // Confirm that the initial ForceClientIdCreation call creates the client id
239 // and backs it up via MockStoreClientInfoBackup. 252 // and backs it up via MockStoreClientInfoBackup.
240 EXPECT_FALSE(stored_client_info_backup_); 253 EXPECT_FALSE(stored_client_info_backup_);
241 state_manager->ForceClientIdCreation(); 254 state_manager->ForceClientIdCreation();
242 EXPECT_NE(std::string(), state_manager->client_id()); 255 EXPECT_NE(std::string(), state_manager->client_id());
243 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 256 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
244 test_begin_time); 257 test_begin_time_);
245 258
246 ASSERT_TRUE(stored_client_info_backup_); 259 ASSERT_TRUE(stored_client_info_backup_);
247 EXPECT_EQ(state_manager->client_id(), 260 EXPECT_EQ(state_manager->client_id(),
248 stored_client_info_backup_->client_id); 261 stored_client_info_backup_->client_id);
249 EXPECT_EQ(kFakeInstallationDate, 262 EXPECT_EQ(kFakeInstallationDate,
250 stored_client_info_backup_->installation_date); 263 stored_client_info_backup_->installation_date);
251 EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 264 EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
252 stored_client_info_backup_->reporting_enabled_date); 265 stored_client_info_backup_->reporting_enabled_date);
266 }
267 }
253 268
254 previous_client_info = std::move(stored_client_info_backup_); 269 TEST_F(MetricsStateManagerTest, LoadPrefs) {
255 } 270 ClientInfo client_info;
271 client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
272 client_info.installation_date = 1112;
273 client_info.reporting_enabled_date = 2223;
274 SetClientInfoPrefs(client_info);
256 275
257 EnableMetricsReporting(); 276 EnableMetricsReporting();
258
259 { 277 {
278 EXPECT_FALSE(fake_client_info_backup_);
260 EXPECT_FALSE(stored_client_info_backup_); 279 EXPECT_FALSE(stored_client_info_backup_);
261 280
262 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); 281 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
263 282
264 // client_id should be auto-obtained from the constructor when metrics 283 // client_id should be auto-obtained from the constructor when metrics
265 // reporting is enabled. 284 // reporting is enabled.
266 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id()); 285 EXPECT_EQ(client_info.client_id, state_manager->client_id());
267 286
268 // The backup should also be refreshed when the client id re-initialized. 287 // The backup should not be modified.
269 ASSERT_TRUE(stored_client_info_backup_); 288 ASSERT_FALSE(stored_client_info_backup_);
270 EXPECT_EQ(previous_client_info->client_id,
271 stored_client_info_backup_->client_id);
272 EXPECT_EQ(kFakeInstallationDate,
273 stored_client_info_backup_->installation_date);
274 EXPECT_EQ(previous_client_info->reporting_enabled_date,
275 stored_client_info_backup_->reporting_enabled_date);
276 289
277 // Re-forcing client id creation shouldn't cause another backup and 290 // Re-forcing client id creation shouldn't cause another backup and
278 // shouldn't affect the existing client id. 291 // shouldn't affect the existing client id.
279 stored_client_info_backup_.reset();
280 state_manager->ForceClientIdCreation(); 292 state_manager->ForceClientIdCreation();
281 EXPECT_FALSE(stored_client_info_backup_); 293 EXPECT_FALSE(stored_client_info_backup_);
282 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id()); 294 EXPECT_EQ(client_info.client_id, state_manager->client_id());
283 } 295 }
296 }
284 297
285 const int64_t kBackupInstallationDate = 1111; 298 TEST_F(MetricsStateManagerTest, PreferPrefs) {
286 const int64_t kBackupReportingEnabledDate = 2222; 299 ClientInfo client_info;
287 const char kBackupClientId[] = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"; 300 client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
288 fake_client_info_backup_.reset(new ClientInfo); 301 client_info.installation_date = 1112;
289 fake_client_info_backup_->client_id = kBackupClientId; 302 client_info.reporting_enabled_date = 2223;
290 fake_client_info_backup_->installation_date = kBackupInstallationDate; 303 SetClientInfoPrefs(client_info);
291 fake_client_info_backup_->reporting_enabled_date =
292 kBackupReportingEnabledDate;
293 304
305 ClientInfo client_info2;
306 client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
307 client_info2.installation_date = 1111;
308 client_info2.reporting_enabled_date = 2222;
309 SetFakeClientInfoBackup(client_info2);
310
311 EnableMetricsReporting();
294 { 312 {
295 // The existence of a backup should result in the same behaviour as 313 // The backup should be ignored if we already have a client id.
296 // before if we already have a client id.
297 314
298 EXPECT_FALSE(stored_client_info_backup_); 315 EXPECT_FALSE(stored_client_info_backup_);
299 316
300 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); 317 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
301 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id()); 318 EXPECT_EQ(client_info.client_id, state_manager->client_id());
302 319
303 // The backup should also be refreshed when the client id re-initialized. 320 // The backup should not be modified.
304 ASSERT_TRUE(stored_client_info_backup_); 321 ASSERT_FALSE(stored_client_info_backup_);
305 EXPECT_EQ(previous_client_info->client_id,
306 stored_client_info_backup_->client_id);
307 EXPECT_EQ(kFakeInstallationDate,
308 stored_client_info_backup_->installation_date);
309 EXPECT_EQ(previous_client_info->reporting_enabled_date,
310 stored_client_info_backup_->reporting_enabled_date);
311 stored_client_info_backup_.reset();
312 } 322 }
323 }
324
325 TEST_F(MetricsStateManagerTest, RestoreBackup) {
326 ClientInfo client_info;
327 client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
328 client_info.installation_date = 1112;
329 client_info.reporting_enabled_date = 2223;
330 SetClientInfoPrefs(client_info);
331
332 ClientInfo client_info2;
333 client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
334 client_info2.installation_date = 1111;
335 client_info2.reporting_enabled_date = 2222;
336 SetFakeClientInfoBackup(client_info2);
313 337
314 prefs_.ClearPref(prefs::kMetricsClientID); 338 prefs_.ClearPref(prefs::kMetricsClientID);
315 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp); 339 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
316 340
341 EnableMetricsReporting();
317 { 342 {
318 // The backup should kick in if the client id has gone missing. It should 343 // The backup should kick in if the client id has gone missing. It should
319 // replace remaining and missing dates as well. 344 // replace remaining and missing dates as well.
320 345
321 EXPECT_FALSE(stored_client_info_backup_); 346 EXPECT_FALSE(stored_client_info_backup_);
322 347
323 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); 348 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
324 EXPECT_EQ(kBackupClientId, state_manager->client_id()); 349 EXPECT_EQ(client_info2.client_id, state_manager->client_id());
325 EXPECT_EQ(kBackupInstallationDate, prefs_.GetInt64(prefs::kInstallDate)); 350 EXPECT_EQ(client_info2.installation_date,
326 EXPECT_EQ(kBackupReportingEnabledDate, 351 prefs_.GetInt64(prefs::kInstallDate));
352 EXPECT_EQ(client_info2.reporting_enabled_date,
327 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp)); 353 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
328 354
329 EXPECT_TRUE(stored_client_info_backup_); 355 EXPECT_TRUE(stored_client_info_backup_);
330 stored_client_info_backup_.reset();
331 } 356 }
357 }
332 358
333 const char kNoDashesBackupClientId[] = "AAAAAAAABBBBCCCCDDDDEEEEEEEEEEEE"; 359 TEST_F(MetricsStateManagerTest, ResetBackup) {
334 fake_client_info_backup_.reset(new ClientInfo); 360 ClientInfo client_info;
335 fake_client_info_backup_->client_id = kNoDashesBackupClientId; 361 client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
362 client_info.installation_date = 1111;
363 client_info.reporting_enabled_date = 2222;
336 364
337 prefs_.ClearPref(prefs::kInstallDate); 365 SetFakeClientInfoBackup(client_info);
338 prefs_.ClearPref(prefs::kMetricsClientID); 366 SetClientInfoPrefs(client_info);
339 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
340
341 {
342 // When running the backup from old-style client ids, dashes should be
343 // re-added. And missing dates in backup should be replaced by Time::Now().
344
345 EXPECT_FALSE(stored_client_info_backup_);
346
347 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
348 EXPECT_EQ(kBackupClientId, state_manager->client_id());
349 EXPECT_GE(prefs_.GetInt64(prefs::kInstallDate), test_begin_time);
350 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
351 test_begin_time);
352
353 EXPECT_TRUE(stored_client_info_backup_);
354 previous_client_info = std::move(stored_client_info_backup_);
355 }
356 367
357 prefs_.SetBoolean(prefs::kMetricsResetIds, true); 368 prefs_.SetBoolean(prefs::kMetricsResetIds, true);
358 369
370 EnableMetricsReporting();
359 { 371 {
360 // Upon request to reset metrics ids, the existing backup should not be 372 // Upon request to reset metrics ids, the existing backup should not be
361 // restored. 373 // restored.
362 374
363 EXPECT_FALSE(stored_client_info_backup_);
364
365 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); 375 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
366 376
367 // A brand new client id should have been generated. 377 // A brand new client id should have been generated.
368 EXPECT_NE(std::string(), state_manager->client_id()); 378 EXPECT_NE(std::string(), state_manager->client_id());
369 EXPECT_NE(previous_client_info->client_id, state_manager->client_id()); 379 EXPECT_NE(client_info.client_id, state_manager->client_id());
380 EXPECT_TRUE(stored_client_info_backup_);
370 381
371 // The installation date should not have been affected. 382 // The installation date should not have been affected.
372 EXPECT_EQ(previous_client_info->installation_date, 383 EXPECT_EQ(client_info.installation_date,
373 prefs_.GetInt64(prefs::kInstallDate)); 384 prefs_.GetInt64(prefs::kInstallDate));
374 385
375 // The metrics-reporting-enabled date will be reset to Now(). 386 // The metrics-reporting-enabled date will be reset to Now().
376 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 387 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
377 previous_client_info->reporting_enabled_date); 388 test_begin_time_);
378
379 stored_client_info_backup_.reset();
380 } 389 }
381 } 390 }
382 391
383 } // namespace metrics 392 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/metrics_state_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698