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

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

Issue 2687393004: Gather stability prefs into managing objects. (Closed)
Patch Set: Incorporate Feedback Created 3 years, 10 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_log.cc ('k') | components/metrics/metrics_pref_names.h » ('j') | 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_log.h" 5 #include "components/metrics/metrics_log.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/base64.h" 12 #include "base/base64.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/metrics/bucket_ranges.h" 15 #include "base/metrics/bucket_ranges.h"
16 #include "base/metrics/sample_vector.h" 16 #include "base/metrics/sample_vector.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/sys_info.h" 18 #include "base/sys_info.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20 #include "components/metrics/environment_recorder.h"
20 #include "components/metrics/metrics_pref_names.h" 21 #include "components/metrics/metrics_pref_names.h"
21 #include "components/metrics/metrics_state_manager.h" 22 #include "components/metrics/metrics_state_manager.h"
22 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" 23 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
23 #include "components/metrics/test_metrics_provider.h" 24 #include "components/metrics/test_metrics_provider.h"
24 #include "components/metrics/test_metrics_service_client.h" 25 #include "components/metrics/test_metrics_service_client.h"
25 #include "components/prefs/pref_service.h" 26 #include "components/prefs/pref_service.h"
26 #include "components/prefs/testing_pref_service.h" 27 #include "components/prefs/testing_pref_service.h"
27 #include "components/variations/active_field_trials.h" 28 #include "components/variations/active_field_trials.h"
28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
29 30
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 TestingPrefServiceSimple* prefs_; 97 TestingPrefServiceSimple* prefs_;
97 98
98 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); 99 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
99 }; 100 };
100 101
101 } // namespace 102 } // namespace
102 103
103 class MetricsLogTest : public testing::Test { 104 class MetricsLogTest : public testing::Test {
104 public: 105 public:
105 MetricsLogTest() { 106 MetricsLogTest() {
106 MetricsLog::RegisterPrefs(prefs_.registry()); 107 EnvironmentRecorder::RegisterPrefs(prefs_.registry());
107 MetricsStateManager::RegisterPrefs(prefs_.registry()); 108 MetricsStateManager::RegisterPrefs(prefs_.registry());
108 } 109 }
109 110
110 ~MetricsLogTest() override {} 111 ~MetricsLogTest() override {}
111 112
112 protected: 113 protected:
113 // Check that the values in |system_values| correspond to the test data 114 // Check that the values in |system_values| correspond to the test data
114 // defined at the top of this file. 115 // defined at the top of this file.
115 void CheckSystemProfile(const SystemProfileProto& system_profile) { 116 void CheckSystemProfile(const SystemProfileProto& system_profile) {
116 EXPECT_EQ(kInstallDateExpected, system_profile.install_date()); 117 EXPECT_EQ(kInstallDateExpected, system_profile.install_date());
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 // Add two synthetic trials. 289 // Add two synthetic trials.
289 synthetic_trials.push_back(kSyntheticTrials[0]); 290 synthetic_trials.push_back(kSyntheticTrials[0]);
290 synthetic_trials.push_back(kSyntheticTrials[1]); 291 synthetic_trials.push_back(kSyntheticTrials[1]);
291 292
292 log.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), 293 log.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(),
293 synthetic_trials, kInstallDate, kEnabledDate); 294 synthetic_trials, kInstallDate, kEnabledDate);
294 // Check that the system profile on the log has the correct values set. 295 // Check that the system profile on the log has the correct values set.
295 CheckSystemProfile(log.system_profile()); 296 CheckSystemProfile(log.system_profile());
296 297
297 // Check that the system profile has also been written to prefs. 298 // Check that the system profile has also been written to prefs.
298 const std::string base64_system_profile =
299 prefs_.GetString(prefs::kStabilitySavedSystemProfile);
300 EXPECT_FALSE(base64_system_profile.empty());
301 std::string serialied_system_profile;
302 EXPECT_TRUE(base::Base64Decode(base64_system_profile,
303 &serialied_system_profile));
304 SystemProfileProto decoded_system_profile; 299 SystemProfileProto decoded_system_profile;
305 EXPECT_TRUE(decoded_system_profile.ParseFromString(serialied_system_profile)); 300 EnvironmentRecorder recorder(&prefs_);
301 EXPECT_TRUE(recorder.LoadEnvironmentFromPrefs(&decoded_system_profile));
306 CheckSystemProfile(decoded_system_profile); 302 CheckSystemProfile(decoded_system_profile);
307 } 303 }
308 304
309 TEST_F(MetricsLogTest, LoadSavedEnvironmentFromPrefs) {
310 const char* kSystemProfilePref = prefs::kStabilitySavedSystemProfile;
311 const char* kSystemProfileHashPref =
312 prefs::kStabilitySavedSystemProfileHash;
313
314 TestMetricsServiceClient client;
315 client.set_version_string("bogus version");
316
317 // The pref value is empty, so loading it from prefs should fail.
318 {
319 TestMetricsLog log(
320 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
321 std::string app_version;
322 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs(&app_version));
323 EXPECT_TRUE(app_version.empty());
324 }
325
326 // Do a RecordEnvironment() call and check whether the pref is recorded.
327 {
328 TestMetricsLog log(
329 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
330 log.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(),
331 std::vector<variations::ActiveGroupId>(),
332 kInstallDate, kEnabledDate);
333 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
334 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
335 }
336
337 {
338 TestMetricsLog log(
339 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
340 std::string app_version;
341 EXPECT_TRUE(log.LoadSavedEnvironmentFromPrefs(&app_version));
342 EXPECT_EQ("bogus version", app_version);
343 // Check some values in the system profile.
344 EXPECT_EQ(kInstallDateExpected, log.system_profile().install_date());
345 EXPECT_EQ(kEnabledDateExpected, log.system_profile().uma_enabled_date());
346 // Ensure that the call did not clear the prefs.
347 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
348 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
349 }
350
351 // Ensure that a non-matching hash results in the pref being invalid.
352 {
353 TestMetricsLog log(
354 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
355 // Call RecordEnvironment() to record the pref again.
356 log.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(),
357 std::vector<variations::ActiveGroupId>(),
358 kInstallDate, kEnabledDate);
359 }
360
361 {
362 // Set the hash to a bad value.
363 prefs_.SetString(kSystemProfileHashPref, "deadbeef");
364 TestMetricsLog log(
365 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
366 std::string app_version;
367 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs(&app_version));
368 EXPECT_TRUE(app_version.empty());
369 // Ensure that the prefs are not cleared, even if the call failed.
370 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
371 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
372 }
373 }
374
375 TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) { 305 TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) {
376 TestMetricsServiceClient client; 306 TestMetricsServiceClient client;
377 TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG, 307 TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
378 &client, &prefs_); 308 &client, &prefs_);
379 309
380 std::vector<variations::ActiveGroupId> synthetic_trials; 310 std::vector<variations::ActiveGroupId> synthetic_trials;
381 311
382 log_unknown.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(), 312 log_unknown.RecordEnvironment(std::vector<std::unique_ptr<MetricsProvider>>(),
383 synthetic_trials, kInstallDate, kEnabledDate); 313 synthetic_trials, kInstallDate, kEnabledDate);
384 EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state()); 314 EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state());
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 &client, 349 &client,
420 &prefs_); 350 &prefs_);
421 TestMetricsProvider* test_provider = new TestMetricsProvider(); 351 TestMetricsProvider* test_provider = new TestMetricsProvider();
422 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers; 352 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers;
423 metrics_providers.push_back(base::WrapUnique<MetricsProvider>(test_provider)); 353 metrics_providers.push_back(base::WrapUnique<MetricsProvider>(test_provider));
424 log.RecordEnvironment(metrics_providers, 354 log.RecordEnvironment(metrics_providers,
425 std::vector<variations::ActiveGroupId>(), kInstallDate, 355 std::vector<variations::ActiveGroupId>(), kInstallDate,
426 kEnabledDate); 356 kEnabledDate);
427 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(), 357 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
428 base::TimeDelta()); 358 base::TimeDelta());
429 const SystemProfileProto_Stability& stability =
430 log.system_profile().stability();
431 // Required metrics:
432 EXPECT_TRUE(stability.has_launch_count());
433 EXPECT_TRUE(stability.has_crash_count());
434 // Initial log metrics: only expected if non-zero.
435 EXPECT_FALSE(stability.has_incomplete_shutdown_count());
436 EXPECT_FALSE(stability.has_breakpad_registration_success_count());
437 EXPECT_FALSE(stability.has_breakpad_registration_failure_count());
438 EXPECT_FALSE(stability.has_debugger_present_count());
439 EXPECT_FALSE(stability.has_debugger_not_present_count());
440 359
441 // The test provider should have been called upon to provide initial 360 // The test provider should have been called upon to provide initial
442 // stability and regular stability metrics. 361 // stability and regular stability metrics.
443 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); 362 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called());
444 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); 363 EXPECT_TRUE(test_provider->provide_stability_metrics_called());
445 } 364 }
446 365
447 TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) { 366 TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
448 TestMetricsServiceClient client; 367 TestMetricsServiceClient client;
449 TestMetricsLog log( 368 TestMetricsLog log(
450 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); 369 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
451 TestMetricsProvider* test_provider = new TestMetricsProvider(); 370 TestMetricsProvider* test_provider = new TestMetricsProvider();
452 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers; 371 std::vector<std::unique_ptr<MetricsProvider>> metrics_providers;
453 metrics_providers.push_back(base::WrapUnique<MetricsProvider>(test_provider)); 372 metrics_providers.push_back(base::WrapUnique<MetricsProvider>(test_provider));
454 log.RecordEnvironment(metrics_providers, 373 log.RecordEnvironment(metrics_providers,
455 std::vector<variations::ActiveGroupId>(), kInstallDate, 374 std::vector<variations::ActiveGroupId>(), kInstallDate,
456 kEnabledDate); 375 kEnabledDate);
457 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(), 376 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
458 base::TimeDelta()); 377 base::TimeDelta());
459 const SystemProfileProto_Stability& stability =
460 log.system_profile().stability();
461 // Required metrics:
462 EXPECT_TRUE(stability.has_launch_count());
463 EXPECT_TRUE(stability.has_crash_count());
464 // Initial log metrics: only expected if non-zero.
465 EXPECT_FALSE(stability.has_incomplete_shutdown_count());
466 EXPECT_FALSE(stability.has_breakpad_registration_success_count());
467 EXPECT_FALSE(stability.has_breakpad_registration_failure_count());
468 EXPECT_FALSE(stability.has_debugger_present_count());
469 EXPECT_FALSE(stability.has_debugger_not_present_count());
470 378
471 // The test provider should have been called upon to provide regular but not 379 // The test provider should have been called upon to provide regular but not
472 // initial stability metrics. 380 // initial stability metrics.
473 EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called()); 381 EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called());
474 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); 382 EXPECT_TRUE(test_provider->provide_stability_metrics_called());
475 } 383 }
476 384
477 TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) { 385 TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) {
478 TestMetricsServiceClient client; 386 TestMetricsServiceClient client;
479 TestMetricsLog log( 387 TestMetricsLog log(
(...skipping 19 matching lines...) Expand all
499 TestMetricsServiceClient client; 407 TestMetricsServiceClient client;
500 client.set_product(kTestProduct); 408 client.set_product(kTestProduct);
501 TestMetricsLog log( 409 TestMetricsLog log(
502 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_); 410 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
503 // Check that the product is set to |kTestProduct|. 411 // Check that the product is set to |kTestProduct|.
504 EXPECT_TRUE(log.uma_proto().has_product()); 412 EXPECT_TRUE(log.uma_proto().has_product());
505 EXPECT_EQ(kTestProduct, log.uma_proto().product()); 413 EXPECT_EQ(kTestProduct, log.uma_proto().product());
506 } 414 }
507 415
508 } // namespace metrics 416 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/metrics_log.cc ('k') | components/metrics/metrics_pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698