Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
Avi (use Gerrit)
2015/09/02 01:11:10
It's 2015.
Will Harris
2015/09/02 15:57:53
Done.
| |
| 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 "chrome/browser/metrics/chrome_stability_metrics_provider.h" | |
| 6 | |
| 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" | |
| 26 | |
| 27 #if defined(ENABLE_EXTENSIONS) | |
| 28 #include "extensions/browser/process_map.h" | |
| 29 #endif | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 class ChromeStabilityMetricsProviderTest : public testing::Test { | |
| 34 protected: | |
| 35 ChromeStabilityMetricsProviderTest() : prefs_(new TestingPrefServiceSimple) { | |
| 36 ChromeStabilityMetricsProvider::RegisterPrefs(prefs()->registry()); | |
| 37 } | |
| 38 | |
| 39 TestingPrefServiceSimple* prefs() { return prefs_.get(); } | |
| 40 | |
| 41 private: | |
| 42 scoped_ptr<TestingPrefServiceSimple> prefs_; | |
| 43 content::TestBrowserThreadBundle thread_bundle_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(ChromeStabilityMetricsProviderTest); | |
| 46 }; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 TEST_F(ChromeStabilityMetricsProviderTest, BrowserChildProcessObserver) { | |
| 51 ChromeStabilityMetricsProvider provider(prefs()); | |
| 52 | |
| 53 content::ChildProcessData child_process_data(content::PROCESS_TYPE_RENDERER); | |
| 54 provider.BrowserChildProcessCrashed(child_process_data, 1); | |
| 55 provider.BrowserChildProcessCrashed(child_process_data, 1); | |
| 56 | |
| 57 // Call ProvideStabilityMetrics to check that it will force pending tasks to | |
| 58 // be executed immediately. | |
| 59 metrics::SystemProfileProto system_profile; | |
| 60 | |
| 61 provider.ProvideStabilityMetrics(&system_profile); | |
| 62 | |
| 63 // Check current number of instances created. | |
| 64 const metrics::SystemProfileProto_Stability& stability = | |
| 65 system_profile.stability(); | |
| 66 | |
| 67 EXPECT_EQ(2, stability.child_process_crash_count()); | |
| 68 } | |
| 69 | |
| 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()); | |
| 75 | |
| 76 // Owned by profile_manager. | |
| 77 TestingProfile* profile( | |
| 78 profile_manager->CreateTestingProfile("StabilityTestProfile")); | |
| 79 | |
| 80 scoped_ptr<content::MockRenderProcessHostFactory> rph_factory( | |
| 81 new content::MockRenderProcessHostFactory()); | |
| 82 scoped_refptr<content::SiteInstance> site_instance( | |
| 83 content::SiteInstance::Create(profile)); | |
| 84 | |
| 85 // Owned by rph_factory. | |
| 86 content::RenderProcessHost* host( | |
| 87 rph_factory->CreateRenderProcessHost(profile, site_instance.get())); | |
| 88 | |
| 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)); | |
| 97 | |
| 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)); | |
| 105 | |
| 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)); | |
| 114 | |
| 115 metrics::SystemProfileProto system_profile; | |
| 116 | |
| 117 // Call ProvideStabilityMetrics to check that it will force pending tasks to | |
| 118 // be executed immediately. | |
| 119 provider.ProvideStabilityMetrics(&system_profile); | |
| 120 | |
| 121 EXPECT_EQ(2, system_profile.stability().renderer_crash_count()); | |
| 122 EXPECT_EQ(0, system_profile.stability().extension_renderer_crash_count()); | |
| 123 | |
| 124 #if defined(ENABLE_EXTENSIONS) | |
| 125 provider.ClearSavedStabilityMetrics(); | |
| 126 | |
| 127 // Owned by rph_factory. | |
| 128 content::RenderProcessHost* extension_host( | |
| 129 rph_factory->CreateRenderProcessHost(profile, site_instance.get())); | |
| 130 | |
| 131 // Make the rph an extension rph. | |
| 132 extensions::ProcessMap::Get(profile) | |
| 133 ->Insert("1", extension_host->GetID(), site_instance->GetId()); | |
| 134 | |
| 135 // Crash and abnormal termination should increment extension crash count. | |
| 136 provider.Observe( | |
| 137 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 138 content::Source<content::RenderProcessHost>(extension_host), | |
| 139 content::Details<content::RenderProcessHost::RendererClosedDetails>( | |
| 140 &crash_details)); | |
| 141 | |
| 142 system_profile.Clear(); | |
| 143 provider.ProvideStabilityMetrics(&system_profile); | |
| 144 | |
| 145 EXPECT_EQ(0, system_profile.stability().renderer_crash_count()); | |
| 146 EXPECT_EQ(1, system_profile.stability().extension_renderer_crash_count()); | |
| 147 #endif | |
| 148 | |
| 149 profile_manager->DeleteAllTestingProfiles(); | |
| 150 } | |
| OLD | NEW |