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_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" |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); | 213 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); |
214 } | 214 } |
215 | 215 |
216 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); | 216 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); |
217 } | 217 } |
218 | 218 |
219 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) { | 219 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) { |
220 const int64 kFakeInstallationDate = 12345; | 220 const int64 kFakeInstallationDate = 12345; |
221 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate); | 221 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate); |
222 | 222 |
| 223 const int64 test_begin_time = base::Time::Now().ToTimeT(); |
| 224 |
223 // Holds ClientInfo from previous scoped test for extra checks. | 225 // Holds ClientInfo from previous scoped test for extra checks. |
224 scoped_ptr<ClientInfo> previous_client_info; | 226 scoped_ptr<ClientInfo> previous_client_info; |
225 | 227 |
226 { | 228 { |
227 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 229 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
228 | 230 |
229 // client_id shouldn't be auto-generated if metrics reporting is not | 231 // client_id shouldn't be auto-generated if metrics reporting is not |
230 // enabled. | 232 // enabled. |
231 EXPECT_EQ(std::string(), state_manager->client_id()); | 233 EXPECT_EQ(std::string(), state_manager->client_id()); |
232 EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp)); | 234 EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp)); |
233 | 235 |
234 // Confirm that the initial ForceClientIdCreation call creates the client id | 236 // Confirm that the initial ForceClientIdCreation call creates the client id |
235 // and backs it up via MockStoreClientInfoBackup. | 237 // and backs it up via MockStoreClientInfoBackup. |
236 EXPECT_FALSE(stored_client_info_backup_); | 238 EXPECT_FALSE(stored_client_info_backup_); |
237 state_manager->ForceClientIdCreation(); | 239 state_manager->ForceClientIdCreation(); |
238 EXPECT_NE(std::string(), state_manager->client_id()); | 240 EXPECT_NE(std::string(), state_manager->client_id()); |
239 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0); | 241 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
| 242 test_begin_time); |
240 | 243 |
241 ASSERT_TRUE(stored_client_info_backup_); | 244 ASSERT_TRUE(stored_client_info_backup_); |
242 EXPECT_EQ(state_manager->client_id(), | 245 EXPECT_EQ(state_manager->client_id(), |
243 stored_client_info_backup_->client_id); | 246 stored_client_info_backup_->client_id); |
244 EXPECT_EQ(kFakeInstallationDate, | 247 EXPECT_EQ(kFakeInstallationDate, |
245 stored_client_info_backup_->installation_date); | 248 stored_client_info_backup_->installation_date); |
246 EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), | 249 EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
247 stored_client_info_backup_->reporting_enabled_date); | 250 stored_client_info_backup_->reporting_enabled_date); |
248 | 251 |
249 previous_client_info = stored_client_info_backup_.Pass(); | 252 previous_client_info = stored_client_info_backup_.Pass(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp); | 337 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp); |
335 | 338 |
336 { | 339 { |
337 // When running the backup from old-style client ids, dashes should be | 340 // When running the backup from old-style client ids, dashes should be |
338 // re-added. And missing dates in backup should be replaced by Time::Now(). | 341 // re-added. And missing dates in backup should be replaced by Time::Now(). |
339 | 342 |
340 EXPECT_FALSE(stored_client_info_backup_); | 343 EXPECT_FALSE(stored_client_info_backup_); |
341 | 344 |
342 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 345 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
343 EXPECT_EQ(kBackupClientId, state_manager->client_id()); | 346 EXPECT_EQ(kBackupClientId, state_manager->client_id()); |
344 EXPECT_GT(prefs_.GetInt64(prefs::kInstallDate), 0); | 347 EXPECT_GE(prefs_.GetInt64(prefs::kInstallDate), test_begin_time); |
345 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0); | 348 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
| 349 test_begin_time); |
346 | 350 |
347 EXPECT_TRUE(stored_client_info_backup_); | 351 EXPECT_TRUE(stored_client_info_backup_); |
348 previous_client_info = stored_client_info_backup_.Pass(); | 352 previous_client_info = stored_client_info_backup_.Pass(); |
349 } | 353 } |
350 | 354 |
351 prefs_.SetBoolean(prefs::kMetricsResetIds, true); | 355 prefs_.SetBoolean(prefs::kMetricsResetIds, true); |
352 | 356 |
353 { | 357 { |
354 // Upon request to reset metrics ids, the existing backup should not be | 358 // Upon request to reset metrics ids, the existing backup should not be |
355 // restored. | 359 // restored. |
356 | 360 |
357 EXPECT_FALSE(stored_client_info_backup_); | 361 EXPECT_FALSE(stored_client_info_backup_); |
358 | 362 |
359 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 363 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
360 | 364 |
361 // A brand new client id should have been generated. | 365 // A brand new client id should have been generated. |
362 EXPECT_NE(std::string(), state_manager->client_id()); | 366 EXPECT_NE(std::string(), state_manager->client_id()); |
363 EXPECT_NE(previous_client_info->client_id, state_manager->client_id()); | 367 EXPECT_NE(previous_client_info->client_id, state_manager->client_id()); |
364 | 368 |
365 // Dates should not have been affected. | 369 // The installation date should not have been affected. |
366 EXPECT_EQ(previous_client_info->installation_date, | 370 EXPECT_EQ(previous_client_info->installation_date, |
367 prefs_.GetInt64(prefs::kInstallDate)); | 371 prefs_.GetInt64(prefs::kInstallDate)); |
368 EXPECT_EQ(previous_client_info->reporting_enabled_date, | 372 |
369 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp)); | 373 // The metrics-reporting-enabled date will be reset to Now(). |
| 374 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
| 375 previous_client_info->reporting_enabled_date); |
370 | 376 |
371 stored_client_info_backup_.reset(); | 377 stored_client_info_backup_.reset(); |
372 } | 378 } |
373 } | 379 } |
374 | 380 |
375 } // namespace metrics | 381 } // namespace metrics |
OLD | NEW |