Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / metrics / chrome_stability_metrics_provider_unittest.cc
blob31a9ad448385f1fa1087b4185adc2a657e2f0216
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"
29 #endif
31 namespace {
33 class ChromeStabilityMetricsProviderTest : public testing::Test {
34 protected:
35 ChromeStabilityMetricsProviderTest() : prefs_(new TestingPrefServiceSimple) {
36 ChromeStabilityMetricsProvider::RegisterPrefs(prefs()->registry());
39 TestingPrefServiceSimple* prefs() { return prefs_.get(); }
41 private:
42 scoped_ptr<TestingPrefServiceSimple> prefs_;
43 content::TestBrowserThreadBundle thread_bundle_;
45 DISALLOW_COPY_AND_ASSIGN(ChromeStabilityMetricsProviderTest);
48 } // namespace
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);
92 provider.Observe(
93 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
94 content::Source<content::RenderProcessHost>(host),
95 content::Details<content::RenderProcessHost::RendererClosedDetails>(
96 &crash_details));
98 content::RenderProcessHost::RendererClosedDetails term_details(
99 base::TERMINATION_STATUS_ABNORMAL_TERMINATION, 1);
100 provider.Observe(
101 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
102 content::Source<content::RenderProcessHost>(host),
103 content::Details<content::RenderProcessHost::RendererClosedDetails>(
104 &term_details));
106 // Kill does not increment renderer crash count.
107 content::RenderProcessHost::RendererClosedDetails kill_details(
108 base::TERMINATION_STATUS_PROCESS_WAS_KILLED, 1);
109 provider.Observe(
110 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
111 content::Source<content::RenderProcessHost>(host),
112 content::Details<content::RenderProcessHost::RendererClosedDetails>(
113 &kill_details));
115 // Failed launch increments failed launch count.
116 content::RenderProcessHost::RendererClosedDetails failed_launch_details(
117 base::TERMINATION_STATUS_LAUNCH_FAILED, 1);
118 provider.Observe(
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(2, system_profile.stability().renderer_crash_count());
131 EXPECT_EQ(1, system_profile.stability().renderer_failed_launch_count());
132 EXPECT_EQ(0, system_profile.stability().extension_renderer_crash_count());
134 #if defined(ENABLE_EXTENSIONS)
135 provider.ClearSavedStabilityMetrics();
137 // Owned by rph_factory.
138 content::RenderProcessHost* extension_host(
139 rph_factory->CreateRenderProcessHost(profile, site_instance.get()));
141 // Make the rph an extension rph.
142 extensions::ProcessMap::Get(profile)
143 ->Insert("1", extension_host->GetID(), site_instance->GetId());
145 // Crash and abnormal termination should increment extension crash count.
146 provider.Observe(
147 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
148 content::Source<content::RenderProcessHost>(extension_host),
149 content::Details<content::RenderProcessHost::RendererClosedDetails>(
150 &crash_details));
152 // Failed launch increments failed launch count.
153 provider.Observe(
154 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
155 content::Source<content::RenderProcessHost>(extension_host),
156 content::Details<content::RenderProcessHost::RendererClosedDetails>(
157 &failed_launch_details));
159 system_profile.Clear();
160 provider.ProvideStabilityMetrics(&system_profile);
162 EXPECT_EQ(0, system_profile.stability().renderer_crash_count());
163 EXPECT_EQ(1, system_profile.stability().extension_renderer_crash_count());
164 EXPECT_EQ(
165 1, system_profile.stability().extension_renderer_failed_launch_count());
166 #endif
168 profile_manager->DeleteAllTestingProfiles();