1 // Copyright 2015 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_helper.h"
6
7 #include <memory>
8
9 #include "base/test/metrics/histogram_tester.h"
10 #include "build/build_config.h"
11 #include "components/prefs/pref_service.h"
12 #include "components/prefs/scoped_user_pref_update.h"
13 #include "components/prefs/testing_pref_service.h"
14 #include "extensions/buildflags/buildflags.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/metrics_proto/system_profile.pb.h"
17
18 namespace metrics {
19
20 namespace {
21
22 enum RendererType {
23 RENDERER_TYPE_RENDERER = 1,
24 RENDERER_TYPE_EXTENSION,
25 // NOTE: Add new action types only immediately above this line. Also,
26 // make sure the enum list in tools/metrics/histograms/histograms.xml is
27 // updated with any change in here.
28 RENDERER_TYPE_COUNT
29 };
30
31 class StabilityMetricsHelperTest : public testing::Test {
32 public:
33 StabilityMetricsHelperTest(const StabilityMetricsHelperTest&) = delete;
34 StabilityMetricsHelperTest& operator=(const StabilityMetricsHelperTest&) =
35 delete;
36
37 protected:
StabilityMetricsHelperTest()38 StabilityMetricsHelperTest() : prefs_(new TestingPrefServiceSimple) {
39 StabilityMetricsHelper::RegisterPrefs(prefs()->registry());
40 }
41
prefs()42 TestingPrefServiceSimple* prefs() { return prefs_.get(); }
43
44 private:
45 std::unique_ptr<TestingPrefServiceSimple> prefs_;
46 };
47
48 } // namespace
49
50 #if !BUILDFLAG(IS_ANDROID)
TEST_F(StabilityMetricsHelperTest,LogRendererCrash)51 TEST_F(StabilityMetricsHelperTest, LogRendererCrash) {
52 StabilityMetricsHelper helper(prefs());
53 base::HistogramTester histogram_tester;
54
55 // Crash and abnormal termination should increment renderer crash count.
56 helper.LogRendererCrash(false, base::TERMINATION_STATUS_PROCESS_CRASHED, 1);
57
58 helper.LogRendererCrash(false, base::TERMINATION_STATUS_ABNORMAL_TERMINATION,
59 1);
60
61 // OOM should increment renderer crash count.
62 helper.LogRendererCrash(false, base::TERMINATION_STATUS_OOM, 1);
63
64 // Kill does not increment renderer crash count.
65 helper.LogRendererCrash(false, base::TERMINATION_STATUS_PROCESS_WAS_KILLED,
66 1);
67
68 // Failed launch increments failed launch count.
69 helper.LogRendererCrash(false, base::TERMINATION_STATUS_LAUNCH_FAILED, 1);
70
71 histogram_tester.ExpectUniqueSample("CrashExitCodes.Renderer", 1, 3);
72 histogram_tester.ExpectBucketCount("BrowserRenderProcessHost.ChildCrashes",
73 RENDERER_TYPE_RENDERER, 3);
74 histogram_tester.ExpectBucketCount("Stability.Counts2",
75 StabilityEventType::kRendererCrash, 3);
76 histogram_tester.ExpectBucketCount(
77 "Stability.Counts2", StabilityEventType::kRendererFailedLaunch, 1);
78 histogram_tester.ExpectBucketCount("Stability.Counts2",
79 StabilityEventType::kExtensionCrash, 0);
80
81 // One launch failure each.
82 histogram_tester.ExpectBucketCount(
83 "BrowserRenderProcessHost.ChildLaunchFailures", RENDERER_TYPE_RENDERER,
84 1);
85
86 // TERMINATION_STATUS_PROCESS_WAS_KILLED for a renderer.
87 histogram_tester.ExpectBucketCount("BrowserRenderProcessHost.ChildKills",
88 RENDERER_TYPE_RENDERER, 1);
89 histogram_tester.ExpectBucketCount("BrowserRenderProcessHost.ChildKills",
90 RENDERER_TYPE_EXTENSION, 0);
91 histogram_tester.ExpectBucketCount(
92 "BrowserRenderProcessHost.ChildLaunchFailureCodes", 1, 1);
93 }
94 #endif // !BUILDFLAG(IS_ANDROID)
95
96 // Note: ENABLE_EXTENSIONS is set to false in Android
97 #if BUILDFLAG(ENABLE_EXTENSIONS)
TEST_F(StabilityMetricsHelperTest,LogRendererCrashEnableExtensions)98 TEST_F(StabilityMetricsHelperTest, LogRendererCrashEnableExtensions) {
99 StabilityMetricsHelper helper(prefs());
100 base::HistogramTester histogram_tester;
101
102 // Crash and abnormal termination should increment extension crash count.
103 helper.LogRendererCrash(true, base::TERMINATION_STATUS_PROCESS_CRASHED, 1);
104
105 // OOM should increment extension renderer crash count.
106 helper.LogRendererCrash(true, base::TERMINATION_STATUS_OOM, 1);
107
108 // Failed launch increments extension failed launch count.
109 helper.LogRendererCrash(true, base::TERMINATION_STATUS_LAUNCH_FAILED, 1);
110
111 histogram_tester.ExpectBucketCount("Stability.Counts2",
112 StabilityEventType::kRendererCrash, 0);
113 histogram_tester.ExpectBucketCount(
114 "Stability.Counts2", StabilityEventType::kExtensionRendererFailedLaunch,
115 1);
116 histogram_tester.ExpectBucketCount("Stability.Counts2",
117 StabilityEventType::kExtensionCrash, 2);
118
119 histogram_tester.ExpectBucketCount(
120 "BrowserRenderProcessHost.ChildLaunchFailureCodes", 1, 1);
121 histogram_tester.ExpectUniqueSample("CrashExitCodes.Extension", 1, 2);
122 histogram_tester.ExpectBucketCount("BrowserRenderProcessHost.ChildCrashes",
123 RENDERER_TYPE_EXTENSION, 2);
124 histogram_tester.ExpectBucketCount(
125 "BrowserRenderProcessHost.ChildLaunchFailures", RENDERER_TYPE_EXTENSION,
126 1);
127 }
128 #endif
129
130 } // namespace metrics
131