1 // Copyright 2017 The Chromium Authors
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
7 #include "base/test/metrics/histogram_tester.h"
8 #include "base/time/time.h"
9 #include "build/build_config.h"
10 #include "components/metrics/stability_metrics_helper.h"
11 #include "components/prefs/testing_pref_service.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/metrics_proto/system_profile.pb.h"
14
15 namespace metrics {
16
17 class StabilityMetricsProviderTest : public testing::Test {
18 public:
StabilityMetricsProviderTest()19 StabilityMetricsProviderTest() {
20 StabilityMetricsProvider::RegisterPrefs(prefs_.registry());
21 }
22
23 StabilityMetricsProviderTest(const StabilityMetricsProviderTest&) = delete;
24 StabilityMetricsProviderTest& operator=(const StabilityMetricsProviderTest&) =
25 delete;
26
~StabilityMetricsProviderTest()27 ~StabilityMetricsProviderTest() override {}
28
29 protected:
30 TestingPrefServiceSimple prefs_;
31 };
32
TEST_F(StabilityMetricsProviderTest,ProvideStabilityMetrics)33 TEST_F(StabilityMetricsProviderTest, ProvideStabilityMetrics) {
34 base::HistogramTester histogram_tester;
35 StabilityMetricsProvider stability_provider(&prefs_);
36 MetricsProvider* provider = &stability_provider;
37 SystemProfileProto system_profile;
38 provider->ProvideStabilityMetrics(&system_profile);
39
40 #if BUILDFLAG(IS_ANDROID)
41 // Initial log metrics: only expected if non-zero.
42 const SystemProfileProto_Stability& stability = system_profile.stability();
43 // The launch count field is used on Android only.
44 EXPECT_FALSE(stability.has_launch_count());
45 #endif
46
47 histogram_tester.ExpectBucketCount("Stability.Counts2",
48 StabilityEventType::kLaunch, 0);
49 histogram_tester.ExpectBucketCount("Stability.Counts2",
50 StabilityEventType::kBrowserCrash, 0);
51 }
52
TEST_F(StabilityMetricsProviderTest,RecordStabilityMetrics)53 TEST_F(StabilityMetricsProviderTest, RecordStabilityMetrics) {
54 base::HistogramTester histogram_tester;
55 {
56 StabilityMetricsProvider recorder(&prefs_);
57 recorder.LogLaunch();
58 recorder.LogCrash(base::Time());
59 }
60
61 {
62 StabilityMetricsProvider stability_provider(&prefs_);
63 MetricsProvider* provider = &stability_provider;
64 SystemProfileProto system_profile;
65 provider->ProvideStabilityMetrics(&system_profile);
66
67 #if BUILDFLAG(IS_ANDROID)
68 // Initial log metrics: only expected if non-zero.
69 const SystemProfileProto_Stability& stability = system_profile.stability();
70 // The launch count field is populated only on Android.
71 EXPECT_EQ(1, stability.launch_count());
72 #endif
73
74 histogram_tester.ExpectBucketCount("Stability.Counts2",
75 StabilityEventType::kLaunch, 1);
76 histogram_tester.ExpectBucketCount("Stability.Counts2",
77 StabilityEventType::kBrowserCrash, 1);
78 }
79 }
80
81 #if BUILDFLAG(IS_WIN)
82 namespace {
83
84 class TestingStabilityMetricsProvider : public StabilityMetricsProvider {
85 public:
TestingStabilityMetricsProvider(PrefService * local_state,base::Time unclean_session_time)86 TestingStabilityMetricsProvider(PrefService* local_state,
87 base::Time unclean_session_time)
88 : StabilityMetricsProvider(local_state),
89 unclean_session_time_(unclean_session_time) {}
90
IsUncleanSystemSession(base::Time last_live_timestamp)91 bool IsUncleanSystemSession(base::Time last_live_timestamp) override {
92 return last_live_timestamp == unclean_session_time_;
93 }
94
95 private:
96 const base::Time unclean_session_time_;
97 };
98
99 } // namespace
100
TEST_F(StabilityMetricsProviderTest,RecordSystemCrashMetrics)101 TEST_F(StabilityMetricsProviderTest, RecordSystemCrashMetrics) {
102 base::HistogramTester histogram_tester;
103 {
104 base::Time unclean_time = base::Time::Now();
105 TestingStabilityMetricsProvider recorder(&prefs_, unclean_time);
106
107 // Any crash with a last_live_timestamp equal to unclean_time will
108 // be logged as a system crash as per the implementation of
109 // TestingStabilityMetricsProvider, so this will log a system crash.
110 recorder.LogCrash(unclean_time);
111
112 // Record a crash with no system crash.
113 recorder.LogCrash(unclean_time - base::Minutes(1));
114 }
115
116 {
117 StabilityMetricsProvider stability_provider(&prefs_);
118 MetricsProvider* provider = &stability_provider;
119 SystemProfileProto system_profile;
120 provider->ProvideStabilityMetrics(&system_profile);
121
122 // Two crashes, one system crash.
123 histogram_tester.ExpectUniqueSample("Stability.Counts2",
124 StabilityEventType::kBrowserCrash, 2);
125 histogram_tester.ExpectTotalCount("Stability.Internals.SystemCrashCount",
126 1);
127 }
128 }
129
130 #endif
131
132 } // namespace metrics
133