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

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: review:isherman 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
45
46
Ilya Sherman 2014/07/11 23:25:00 nit: Spurious blank lines.
gab 2014/07/14 19:17:15 Done.
39 protected: 47 protected:
40 TestingPrefServiceSimple prefs_; 48 TestingPrefServiceSimple prefs_;
41 49
50 // Last ClientInfo stored by the MetricsStateManager via
51 // MockStoreClientInfoBackup.
52 scoped_ptr<ClientInfo> stored_client_info_backup_;
53
54 // If set, will be returned via LoadFakeClientInfoBackup if requested by the
55 // MetricsStateManager.
56 scoped_ptr<ClientInfo> fake_client_info_backup_;
57
42 private: 58 private:
43 bool is_metrics_reporting_enabled() const { 59 bool is_metrics_reporting_enabled() const {
44 return is_metrics_reporting_enabled_; 60 return is_metrics_reporting_enabled_;
45 } 61 }
46 62
63 // Stores the |client_info| in |stored_client_info_backup_| for verification
64 // by the tests later.
65 void MockStoreClientInfoBackup(const ClientInfo& client_info) {
66 stored_client_info_backup_.reset(new ClientInfo);
67 stored_client_info_backup_->client_id = client_info.client_id;
68 stored_client_info_backup_->installation_date =
69 client_info.installation_date;
70 stored_client_info_backup_->reporting_enabled_date =
71 client_info.reporting_enabled_date;
72 }
73
74 // Hands out a copy of |fake_client_info_backup_| if it is set.
75 scoped_ptr<ClientInfo> LoadFakeClientInfoBackup() {
76 if (!fake_client_info_backup_)
77 return scoped_ptr<ClientInfo>();
78
79 scoped_ptr<ClientInfo> backup_copy(new ClientInfo);
80 backup_copy->client_id = fake_client_info_backup_->client_id;
81 backup_copy->installation_date =
82 fake_client_info_backup_->installation_date;
83 backup_copy->reporting_enabled_date =
84 fake_client_info_backup_->reporting_enabled_date;
85 return backup_copy.Pass();
Ilya Sherman 2014/07/11 23:25:00 Wouldn't this be easier if copying the struct were
gab 2014/07/14 19:17:14 Yes, except that by [1] this means I would need an
grt (UTC plus 2) 2014/07/14 19:40:21 Is this true for structs? I thought structs were p
gab 2014/07/14 19:47:21 I think they are, but I don't see a good reason to
grt (UTC plus 2) 2014/07/15 16:04:27 The reason we disallow copy and assign by default
gab 2014/07/15 20:50:05 I see, makes sense, if you insist I'm happy to rem
grt (UTC plus 2) 2014/07/16 13:52:31 I don't feel strongly that you must change it in t
86 }
87
47 bool is_metrics_reporting_enabled_; 88 bool is_metrics_reporting_enabled_;
48 89
49 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest); 90 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest);
50 }; 91 };
51 92
52 // Ensure the ClientId is formatted as expected. 93 // Ensure the ClientId is formatted as expected.
53 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { 94 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) {
54 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); 95 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
55 state_manager->ForceClientIdCreation(); 96 state_manager->ForceClientIdCreation();
56 97
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 EXPECT_NE(kInitialClientId, state_manager->client_id()); 205 EXPECT_NE(kInitialClientId, state_manager->client_id());
165 206
166 state_manager->GetLowEntropySource(); 207 state_manager->GetLowEntropySource();
167 208
168 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); 209 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));
169 } 210 }
170 211
171 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); 212 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID));
172 } 213 }
173 214
215 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
216 const int64 kFakeInstallationDate = 12345;
217 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);
218
219 // Holds ClientInfo from previous scoped test for extra checks.
220 scoped_ptr<ClientInfo> tmp_client_info;
Ilya Sherman 2014/07/11 23:25:00 nit: Perhaps s/tmp/previous/?
gab 2014/07/14 19:17:15 Done.
221
222 {
223 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
224
225 // client_id shouldn't be auto-generated if metrics reporting is not
226 // enabled.
227 EXPECT_EQ(std::string(), state_manager->client_id());
228 EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
229
230 // Confirm that the initial ForceClientIdCreation call creates the client id
231 // and backs it up via MockStoreClientInfoBackup.
232 EXPECT_FALSE(stored_client_info_backup_);
233 state_manager->ForceClientIdCreation();
234 EXPECT_NE(std::string(), state_manager->client_id());
235 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);
236
237 ASSERT_TRUE(stored_client_info_backup_);
238 EXPECT_EQ(state_manager->client_id(),
239 stored_client_info_backup_->client_id);
240 EXPECT_EQ(kFakeInstallationDate,
241 stored_client_info_backup_->installation_date);
242 EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
243 stored_client_info_backup_->reporting_enabled_date);
244
245 tmp_client_info = stored_client_info_backup_.Pass();
246 }
247
248 EnableMetricsReporting();
249
250 {
251 EXPECT_FALSE(stored_client_info_backup_);
252
253 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
254
255 // client_id should be auto-obtained from the constructor when metrics
256 // reporting is enabled.
257 EXPECT_EQ(tmp_client_info->client_id, state_manager->client_id());
258
259 // The backup should also be refreshed when the client id re-initialized.
260 ASSERT_TRUE(stored_client_info_backup_);
261 EXPECT_EQ(tmp_client_info->client_id,
262 stored_client_info_backup_->client_id);
263 EXPECT_EQ(kFakeInstallationDate,
264 stored_client_info_backup_->installation_date);
265 EXPECT_EQ(tmp_client_info->reporting_enabled_date,
266 stored_client_info_backup_->reporting_enabled_date);
267
268 // Re-forcing client id creation shouldn't cause another backup and
269 // shouldn't affect the existing client id.
270 stored_client_info_backup_.reset();
271 state_manager->ForceClientIdCreation();
272 EXPECT_FALSE(stored_client_info_backup_);
273 EXPECT_EQ(tmp_client_info->client_id, state_manager->client_id());
274 }
275
276 const int64 kBackupInstallationDate = 1111;
277 const int64 kBackupReportingEnabledDate = 2222;
278 const char kBackupClientId[] = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
279 fake_client_info_backup_.reset(new ClientInfo);
280 fake_client_info_backup_->client_id = kBackupClientId;
281 fake_client_info_backup_->installation_date = kBackupInstallationDate;
282 fake_client_info_backup_->reporting_enabled_date =
283 kBackupReportingEnabledDate;
284
285 {
286 // The existence of a backup should result in the same behaviour as
287 // before if we already have a client id.
288
289 EXPECT_FALSE(stored_client_info_backup_);
290
291 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
292 EXPECT_EQ(tmp_client_info->client_id, state_manager->client_id());
293
294 // The backup should also be refreshed when the client id re-initialized.
295 ASSERT_TRUE(stored_client_info_backup_);
296 EXPECT_EQ(tmp_client_info->client_id,
297 stored_client_info_backup_->client_id);
298 EXPECT_EQ(kFakeInstallationDate,
299 stored_client_info_backup_->installation_date);
300 EXPECT_EQ(tmp_client_info->reporting_enabled_date,
301 stored_client_info_backup_->reporting_enabled_date);
302 stored_client_info_backup_.reset();
303 }
304
305 prefs_.ClearPref(prefs::kMetricsClientID);
306 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
307
308 {
309 // The backup should kick in if the client id has gone missing. It should
310 // replace remaining and missing dates as well.
311
312 EXPECT_FALSE(stored_client_info_backup_);
313
314 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
315 EXPECT_EQ(kBackupClientId, state_manager->client_id());
316 EXPECT_EQ(kBackupInstallationDate, prefs_.GetInt64(prefs::kInstallDate));
317 EXPECT_EQ(kBackupReportingEnabledDate,
318 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
319
320 EXPECT_TRUE(stored_client_info_backup_);
321 stored_client_info_backup_.reset();
322 }
323
324 const char kNoDashesBackupClientId[] = "AAAAAAAABBBBCCCCDDDDEEEEEEEEEEEE";
325 fake_client_info_backup_.reset(new ClientInfo);
326 fake_client_info_backup_->client_id = kNoDashesBackupClientId;
327
328 prefs_.ClearPref(prefs::kInstallDate);
329 prefs_.ClearPref(prefs::kMetricsClientID);
330 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
331
332 {
333 // When running the backup from old-style client ids, dashes should be
334 // re-added. And missing dates in backup should be replaced by Time::Now().
335
336 EXPECT_FALSE(stored_client_info_backup_);
337
338 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager());
339 EXPECT_EQ(kBackupClientId, state_manager->client_id());
340 EXPECT_GT(prefs_.GetInt64(prefs::kInstallDate), 0);
341 EXPECT_GT(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);
342
343 EXPECT_TRUE(stored_client_info_backup_);
344 stored_client_info_backup_.reset();
345 }
346 }
347
174 } // namespace metrics 348 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698