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

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: put dependency back for installer_util_nacl_win64 fixes compile?! 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 // Respect the contract that storing an empty client_id voids the existing
72 // backup (required for the last section of the ForceClientIdCreation test
73 // below).
74 if (client_info.client_id.empty())
75 fake_client_info_backup_.reset();
76 }
77
78 // Hands out a copy of |fake_client_info_backup_| if it is set.
79 scoped_ptr<ClientInfo> LoadFakeClientInfoBackup() {
80 if (!fake_client_info_backup_)
81 return scoped_ptr<ClientInfo>();
82
83 scoped_ptr<ClientInfo> backup_copy(new ClientInfo);
84 backup_copy->client_id = fake_client_info_backup_->client_id;
85 backup_copy->installation_date =
86 fake_client_info_backup_->installation_date;
87 backup_copy->reporting_enabled_date =
88 fake_client_info_backup_->reporting_enabled_date;
89 return backup_copy.Pass();
90 }
91
47 bool is_metrics_reporting_enabled_; 92 bool is_metrics_reporting_enabled_;
48 93
49 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest); 94 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest);
50 }; 95 };
51 96
52 // Ensure the ClientId is formatted as expected. 97 // Ensure the ClientId is formatted as expected.
53 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { 98 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) {
54 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); 99 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
55 state_manager->ForceClientIdCreation(); 100 state_manager->ForceClientIdCreation();
56 101
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 EXPECT_NE(kInitialClientId, state_manager->client_id()); 209 EXPECT_NE(kInitialClientId, state_manager->client_id());
165 210
166 state_manager->GetLowEntropySource(); 211 state_manager->GetLowEntropySource();
167 212
168 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); 213 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));
169 } 214 }
170 215
171 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); 216 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID));
172 } 217 }
173 218
219 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
220 const int64 kFakeInstallationDate = 12345;
221 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);
222
223 // Holds ClientInfo from previous scoped test for extra checks.
224 scoped_ptr<ClientInfo> previous_client_info;
225
226 {
227 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
228
229 // client_id shouldn't be auto-generated if metrics reporting is not
230 // enabled.
231 EXPECT_EQ(std::string(), state_manager->client_id());
232 EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
233
234 // Confirm that the initial ForceClientIdCreation call creates the client id
235 // and backs it up via MockStoreClientInfoBackup.
236 EXPECT_FALSE(stored_client_info_backup_);
237 state_manager->ForceClientIdCreation();
238 EXPECT_NE(std::string(), state_manager->client_id());
239 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);
240
241 ASSERT_TRUE(stored_client_info_backup_);
242 EXPECT_EQ(state_manager->client_id(),
243 stored_client_info_backup_->client_id);
244 EXPECT_EQ(kFakeInstallationDate,
245 stored_client_info_backup_->installation_date);
246 EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
247 stored_client_info_backup_->reporting_enabled_date);
248
249 previous_client_info = stored_client_info_backup_.Pass();
250 }
251
252 EnableMetricsReporting();
253
254 {
255 EXPECT_FALSE(stored_client_info_backup_);
256
257 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
258
259 // client_id should be auto-obtained from the constructor when metrics
260 // reporting is enabled.
261 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id());
262
263 // The backup should also be refreshed when the client id re-initialized.
264 ASSERT_TRUE(stored_client_info_backup_);
265 EXPECT_EQ(previous_client_info->client_id,
266 stored_client_info_backup_->client_id);
267 EXPECT_EQ(kFakeInstallationDate,
268 stored_client_info_backup_->installation_date);
269 EXPECT_EQ(previous_client_info->reporting_enabled_date,
270 stored_client_info_backup_->reporting_enabled_date);
271
272 // Re-forcing client id creation shouldn't cause another backup and
273 // shouldn't affect the existing client id.
274 stored_client_info_backup_.reset();
275 state_manager->ForceClientIdCreation();
276 EXPECT_FALSE(stored_client_info_backup_);
277 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id());
278 }
279
280 const int64 kBackupInstallationDate = 1111;
281 const int64 kBackupReportingEnabledDate = 2222;
282 const char kBackupClientId[] = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
283 fake_client_info_backup_.reset(new ClientInfo);
284 fake_client_info_backup_->client_id = kBackupClientId;
285 fake_client_info_backup_->installation_date = kBackupInstallationDate;
286 fake_client_info_backup_->reporting_enabled_date =
287 kBackupReportingEnabledDate;
288
289 {
290 // The existence of a backup should result in the same behaviour as
291 // before if we already have a client id.
292
293 EXPECT_FALSE(stored_client_info_backup_);
294
295 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
296 EXPECT_EQ(previous_client_info->client_id, state_manager->client_id());
297
298 // The backup should also be refreshed when the client id re-initialized.
299 ASSERT_TRUE(stored_client_info_backup_);
300 EXPECT_EQ(previous_client_info->client_id,
301 stored_client_info_backup_->client_id);
302 EXPECT_EQ(kFakeInstallationDate,
303 stored_client_info_backup_->installation_date);
304 EXPECT_EQ(previous_client_info->reporting_enabled_date,
305 stored_client_info_backup_->reporting_enabled_date);
306 stored_client_info_backup_.reset();
307 }
308
309 prefs_.ClearPref(prefs::kMetricsClientID);
310 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
311
312 {
313 // The backup should kick in if the client id has gone missing. It should
314 // replace remaining and missing dates as well.
315
316 EXPECT_FALSE(stored_client_info_backup_);
317
318 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
319 EXPECT_EQ(kBackupClientId, state_manager->client_id());
320 EXPECT_EQ(kBackupInstallationDate, prefs_.GetInt64(prefs::kInstallDate));
321 EXPECT_EQ(kBackupReportingEnabledDate,
322 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
323
324 EXPECT_TRUE(stored_client_info_backup_);
325 stored_client_info_backup_.reset();
326 }
327
328 const char kNoDashesBackupClientId[] = "AAAAAAAABBBBCCCCDDDDEEEEEEEEEEEE";
329 fake_client_info_backup_.reset(new ClientInfo);
330 fake_client_info_backup_->client_id = kNoDashesBackupClientId;
331
332 prefs_.ClearPref(prefs::kInstallDate);
333 prefs_.ClearPref(prefs::kMetricsClientID);
334 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
335
336 {
337 // 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().
339
340 EXPECT_FALSE(stored_client_info_backup_);
341
342 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
343 EXPECT_EQ(kBackupClientId, state_manager->client_id());
344 EXPECT_GT(prefs_.GetInt64(prefs::kInstallDate), 0);
345 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);
346
347 EXPECT_TRUE(stored_client_info_backup_);
348 previous_client_info = stored_client_info_backup_.Pass();
349 }
350
351 prefs_.SetBoolean(prefs::kMetricsResetIds, true);
352
353 {
354 // Upon request to reset metrics ids, the existing backup should not be
355 // restored.
356
357 EXPECT_FALSE(stored_client_info_backup_);
358
359 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
360
361 // A brand new client id should have been generated.
362 EXPECT_NE(std::string(), state_manager->client_id());
363 EXPECT_NE(previous_client_info->client_id, state_manager->client_id());
364
365 // Dates should not have been affected.
366 EXPECT_EQ(previous_client_info->installation_date,
367 prefs_.GetInt64(prefs::kInstallDate));
368 EXPECT_EQ(previous_client_info->reporting_enabled_date,
369 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
370
371 stored_client_info_backup_.reset();
372 }
373 }
374
174 } // namespace metrics 375 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698