1 // Copyright 2015 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.
5 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h"
7 #include "base/basictypes.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "chrome/test/base/testing_profile_manager.h"
14 #include "components/metrics/proto/system_profile.pb.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/child_process_data.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/notification_types.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/site_instance.h"
22 #include "content/public/common/process_type.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 #if defined(ENABLE_EXTENSIONS)
28 #include "extensions/browser/process_map.h"
33 class ChromeStabilityMetricsProviderTest
: public testing::Test
{
35 ChromeStabilityMetricsProviderTest() : prefs_(new TestingPrefServiceSimple
) {
36 ChromeStabilityMetricsProvider::RegisterPrefs(prefs()->registry());
39 TestingPrefServiceSimple
* prefs() { return prefs_
.get(); }
42 scoped_ptr
<TestingPrefServiceSimple
> prefs_
;
43 content::TestBrowserThreadBundle thread_bundle_
;
45 DISALLOW_COPY_AND_ASSIGN(ChromeStabilityMetricsProviderTest
);
50 TEST_F(ChromeStabilityMetricsProviderTest
, BrowserChildProcessObserver
) {
51 ChromeStabilityMetricsProvider
provider(prefs());
53 content::ChildProcessData
child_process_data(content::PROCESS_TYPE_RENDERER
);
54 provider
.BrowserChildProcessCrashed(child_process_data
, 1);
55 provider
.BrowserChildProcessCrashed(child_process_data
, 1);
57 // Call ProvideStabilityMetrics to check that it will force pending tasks to
58 // be executed immediately.
59 metrics::SystemProfileProto system_profile
;
61 provider
.ProvideStabilityMetrics(&system_profile
);
63 // Check current number of instances created.
64 const metrics::SystemProfileProto_Stability
& stability
=
65 system_profile
.stability();
67 EXPECT_EQ(2, stability
.child_process_crash_count());
70 TEST_F(ChromeStabilityMetricsProviderTest
, NotificationObserver
) {
71 ChromeStabilityMetricsProvider
provider(prefs());
72 scoped_ptr
<TestingProfileManager
> profile_manager(
73 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
74 EXPECT_TRUE(profile_manager
->SetUp());
76 // Owned by profile_manager.
77 TestingProfile
* profile(
78 profile_manager
->CreateTestingProfile("StabilityTestProfile"));
80 scoped_ptr
<content::MockRenderProcessHostFactory
> rph_factory(
81 new content::MockRenderProcessHostFactory());
82 scoped_refptr
<content::SiteInstance
> site_instance(
83 content::SiteInstance::Create(profile
));
85 // Owned by rph_factory.
86 content::RenderProcessHost
* host(
87 rph_factory
->CreateRenderProcessHost(profile
, site_instance
.get()));
89 // Crash and abnormal termination should increment renderer crash count.
90 content::RenderProcessHost::RendererClosedDetails
crash_details(
91 base::TERMINATION_STATUS_PROCESS_CRASHED
, 1);
93 content::NOTIFICATION_RENDERER_PROCESS_CLOSED
,
94 content::Source
<content::RenderProcessHost
>(host
),
95 content::Details
<content::RenderProcessHost::RendererClosedDetails
>(
98 content::RenderProcessHost::RendererClosedDetails
term_details(
99 base::TERMINATION_STATUS_ABNORMAL_TERMINATION
, 1);
101 content::NOTIFICATION_RENDERER_PROCESS_CLOSED
,
102 content::Source
<content::RenderProcessHost
>(host
),
103 content::Details
<content::RenderProcessHost::RendererClosedDetails
>(
106 // Kill does not increment renderer crash count.
107 content::RenderProcessHost::RendererClosedDetails
kill_details(
108 base::TERMINATION_STATUS_PROCESS_WAS_KILLED
, 1);
110 content::NOTIFICATION_RENDERER_PROCESS_CLOSED
,
111 content::Source
<content::RenderProcessHost
>(host
),
112 content::Details
<content::RenderProcessHost::RendererClosedDetails
>(
115 // Failed launch increments crash count.
116 content::RenderProcessHost::RendererClosedDetails
failed_launch_details(
117 base::TERMINATION_STATUS_LAUNCH_FAILED
, 1);
119 content::NOTIFICATION_RENDERER_PROCESS_CLOSED
,
120 content::Source
<content::RenderProcessHost
>(host
),
121 content::Details
<content::RenderProcessHost::RendererClosedDetails
>(
122 &failed_launch_details
));
124 metrics::SystemProfileProto system_profile
;
126 // Call ProvideStabilityMetrics to check that it will force pending tasks to
127 // be executed immediately.
128 provider
.ProvideStabilityMetrics(&system_profile
);
130 EXPECT_EQ(3, system_profile
.stability().renderer_crash_count());
131 EXPECT_EQ(0, system_profile
.stability().extension_renderer_crash_count());
133 #if defined(ENABLE_EXTENSIONS)
134 provider
.ClearSavedStabilityMetrics();
136 // Owned by rph_factory.
137 content::RenderProcessHost
* extension_host(
138 rph_factory
->CreateRenderProcessHost(profile
, site_instance
.get()));
140 // Make the rph an extension rph.
141 extensions::ProcessMap::Get(profile
)
142 ->Insert("1", extension_host
->GetID(), site_instance
->GetId());
144 // Crash and abnormal termination should increment extension crash count.
146 content::NOTIFICATION_RENDERER_PROCESS_CLOSED
,
147 content::Source
<content::RenderProcessHost
>(extension_host
),
148 content::Details
<content::RenderProcessHost::RendererClosedDetails
>(
151 system_profile
.Clear();
152 provider
.ProvideStabilityMetrics(&system_profile
);
154 EXPECT_EQ(0, system_profile
.stability().renderer_crash_count());
155 EXPECT_EQ(1, system_profile
.stability().extension_renderer_crash_count());
158 profile_manager
->DeleteAllTestingProfiles();