holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/metrics/stability_metrics_provider.h" |
| 6 | |
Devlin Cronin | 69228f4 | 2018-06-01 17:25:10 | [diff] [blame] | 7 | #include "base/test/metrics/histogram_tester.h" |
Sigurdur Asgeirsson | 8c67a59 | 2018-02-02 21:10:33 | [diff] [blame] | 8 | #include "build/build_config.h" |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 9 | #include "components/prefs/testing_pref_service.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 11 | #include "third_party/metrics_proto/system_profile.pb.h" |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 12 | |
| 13 | namespace metrics { |
| 14 | |
| 15 | class StabilityMetricsProviderTest : public testing::Test { |
| 16 | public: |
| 17 | StabilityMetricsProviderTest() { |
| 18 | StabilityMetricsProvider::RegisterPrefs(prefs_.registry()); |
| 19 | } |
| 20 | |
| 21 | ~StabilityMetricsProviderTest() override {} |
| 22 | |
| 23 | protected: |
| 24 | TestingPrefServiceSimple prefs_; |
| 25 | |
| 26 | private: |
| 27 | DISALLOW_COPY_AND_ASSIGN(StabilityMetricsProviderTest); |
| 28 | }; |
| 29 | |
| 30 | TEST_F(StabilityMetricsProviderTest, ProvideStabilityMetrics) { |
| 31 | StabilityMetricsProvider stability_provider(&prefs_); |
| 32 | MetricsProvider* provider = &stability_provider; |
| 33 | SystemProfileProto system_profile; |
| 34 | provider->ProvideStabilityMetrics(&system_profile); |
| 35 | |
| 36 | const SystemProfileProto_Stability& stability = system_profile.stability(); |
| 37 | // Initial log metrics: only expected if non-zero. |
| 38 | EXPECT_FALSE(stability.has_launch_count()); |
| 39 | EXPECT_FALSE(stability.has_crash_count()); |
| 40 | EXPECT_FALSE(stability.has_incomplete_shutdown_count()); |
| 41 | EXPECT_FALSE(stability.has_breakpad_registration_success_count()); |
| 42 | EXPECT_FALSE(stability.has_breakpad_registration_failure_count()); |
| 43 | EXPECT_FALSE(stability.has_debugger_present_count()); |
| 44 | EXPECT_FALSE(stability.has_debugger_not_present_count()); |
| 45 | } |
| 46 | |
| 47 | TEST_F(StabilityMetricsProviderTest, RecordStabilityMetrics) { |
| 48 | { |
| 49 | StabilityMetricsProvider recorder(&prefs_); |
| 50 | recorder.LogLaunch(); |
Sigurdur Asgeirsson | 8c67a59 | 2018-02-02 21:10:33 | [diff] [blame] | 51 | recorder.LogCrash(base::Time()); |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 52 | recorder.MarkSessionEndCompleted(false); |
| 53 | recorder.CheckLastSessionEndCompleted(); |
| 54 | recorder.RecordBreakpadRegistration(true); |
| 55 | recorder.RecordBreakpadRegistration(false); |
| 56 | recorder.RecordBreakpadHasDebugger(true); |
| 57 | recorder.RecordBreakpadHasDebugger(false); |
| 58 | } |
| 59 | |
| 60 | { |
| 61 | StabilityMetricsProvider stability_provider(&prefs_); |
| 62 | MetricsProvider* provider = &stability_provider; |
| 63 | SystemProfileProto system_profile; |
| 64 | provider->ProvideStabilityMetrics(&system_profile); |
| 65 | |
| 66 | const SystemProfileProto_Stability& stability = system_profile.stability(); |
| 67 | // Initial log metrics: only expected if non-zero. |
| 68 | EXPECT_EQ(1, stability.launch_count()); |
| 69 | EXPECT_EQ(1, stability.crash_count()); |
| 70 | EXPECT_EQ(1, stability.incomplete_shutdown_count()); |
| 71 | EXPECT_EQ(1, stability.breakpad_registration_success_count()); |
| 72 | EXPECT_EQ(1, stability.breakpad_registration_failure_count()); |
| 73 | EXPECT_EQ(1, stability.debugger_present_count()); |
| 74 | EXPECT_EQ(1, stability.debugger_not_present_count()); |
| 75 | } |
| 76 | } |
| 77 | |
Sigurdur Asgeirsson | 8c67a59 | 2018-02-02 21:10:33 | [diff] [blame] | 78 | #if defined(OS_WIN) |
| 79 | namespace { |
| 80 | |
| 81 | class TestingStabilityMetricsProvider : public StabilityMetricsProvider { |
| 82 | public: |
| 83 | TestingStabilityMetricsProvider(PrefService* local_state, |
| 84 | base::Time unclean_session_time) |
| 85 | : StabilityMetricsProvider(local_state), |
| 86 | unclean_session_time_(unclean_session_time) {} |
| 87 | |
| 88 | bool IsUncleanSystemSession(base::Time last_live_timestamp) override { |
| 89 | return last_live_timestamp == unclean_session_time_; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | const base::Time unclean_session_time_; |
| 94 | }; |
| 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | TEST_F(StabilityMetricsProviderTest, RecordSystemCrashMetrics) { |
| 99 | { |
| 100 | base::Time unclean_time = base::Time::Now(); |
| 101 | TestingStabilityMetricsProvider recorder(&prefs_, unclean_time); |
| 102 | |
| 103 | // Any crash with a last_live_timestamp equal to unclean_time will |
| 104 | // be logged as a system crash as per the implementation of |
| 105 | // TestingStabilityMetricsProvider, so this will log a system crash. |
| 106 | recorder.LogCrash(unclean_time); |
| 107 | |
| 108 | // Record a crash with no system crash. |
| 109 | recorder.LogCrash(unclean_time - base::TimeDelta::FromMinutes(1)); |
| 110 | } |
| 111 | |
| 112 | { |
| 113 | StabilityMetricsProvider stability_provider(&prefs_); |
| 114 | MetricsProvider* provider = &stability_provider; |
| 115 | SystemProfileProto system_profile; |
| 116 | |
| 117 | base::HistogramTester histogram_tester; |
| 118 | |
| 119 | provider->ProvideStabilityMetrics(&system_profile); |
| 120 | |
| 121 | const SystemProfileProto_Stability& stability = system_profile.stability(); |
| 122 | // Two crashes, one system crash. |
| 123 | EXPECT_EQ(2, stability.crash_count()); |
| 124 | |
| 125 | histogram_tester.ExpectTotalCount("Stability.Internals.SystemCrashCount", |
| 126 | 1); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | #endif |
| 131 | |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 132 | } // namespace metrics |