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.
5 #include "components/metrics/stability_metrics_helper.h"
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"
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.
31 class StabilityMetricsHelperTest : public testing::Test {
33 StabilityMetricsHelperTest(const StabilityMetricsHelperTest&) = delete;
34 StabilityMetricsHelperTest& operator=(const StabilityMetricsHelperTest&) =
38 StabilityMetricsHelperTest() : prefs_(new TestingPrefServiceSimple) {
39 StabilityMetricsHelper::RegisterPrefs(prefs()->registry());
42 TestingPrefServiceSimple* prefs() { return prefs_.get(); }
45 std::unique_ptr<TestingPrefServiceSimple> prefs_;
50 #if !BUILDFLAG(IS_ANDROID)
51 TEST_F(StabilityMetricsHelperTest, LogRendererCrash) {
52 StabilityMetricsHelper helper(prefs());
53 base::HistogramTester histogram_tester;
55 // Crash and abnormal termination should increment renderer crash count.
56 helper.LogRendererCrash(false, base::TERMINATION_STATUS_PROCESS_CRASHED, 1);
58 helper.LogRendererCrash(false, base::TERMINATION_STATUS_ABNORMAL_TERMINATION,
61 // OOM should increment renderer crash count.
62 helper.LogRendererCrash(false, base::TERMINATION_STATUS_OOM, 1);
64 // Kill does not increment renderer crash count.
65 helper.LogRendererCrash(false, base::TERMINATION_STATUS_PROCESS_WAS_KILLED,
68 // Failed launch increments failed launch count.
69 helper.LogRendererCrash(false, base::TERMINATION_STATUS_LAUNCH_FAILED, 1);
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);
81 // One launch failure each.
82 histogram_tester.ExpectBucketCount(
83 "BrowserRenderProcessHost.ChildLaunchFailures", RENDERER_TYPE_RENDERER,
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);
94 #endif // !BUILDFLAG(IS_ANDROID)
96 // Note: ENABLE_EXTENSIONS is set to false in Android
97 #if BUILDFLAG(ENABLE_EXTENSIONS)
98 TEST_F(StabilityMetricsHelperTest, LogRendererCrashEnableExtensions) {
99 StabilityMetricsHelper helper(prefs());
100 base::HistogramTester histogram_tester;
102 // Crash and abnormal termination should increment extension crash count.
103 helper.LogRendererCrash(true, base::TERMINATION_STATUS_PROCESS_CRASHED, 1);
105 // OOM should increment extension renderer crash count.
106 helper.LogRendererCrash(true, base::TERMINATION_STATUS_OOM, 1);
108 // Failed launch increments extension failed launch count.
109 helper.LogRendererCrash(true, base::TERMINATION_STATUS_LAUNCH_FAILED, 1);
111 histogram_tester.ExpectBucketCount("Stability.Counts2",
112 StabilityEventType::kRendererCrash, 0);
113 histogram_tester.ExpectBucketCount(
114 "Stability.Counts2", StabilityEventType::kExtensionRendererFailedLaunch,
116 histogram_tester.ExpectBucketCount("Stability.Counts2",
117 StabilityEventType::kExtensionCrash, 2);
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,
130 } // namespace metrics