Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/plugin_metrics_provider.h" | 5 #include "chrome/browser/metrics/plugin_metrics_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/prefs/scoped_user_pref_update.h" | 11 #include "base/prefs/scoped_user_pref_update.h" |
| 12 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
| 13 #include "base/run_loop.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "components/metrics/proto/system_profile.pb.h" | 16 #include "components/metrics/proto/system_profile.pb.h" |
| 17 #include "content/public/browser/child_process_data.h" | |
| 16 #include "content/public/common/process_type.h" | 18 #include "content/public/common/process_type.h" |
| 17 #include "content/public/common/webplugininfo.h" | 19 #include "content/public/common/webplugininfo.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 content::WebPluginInfo CreateFakePluginInfo( | 25 content::WebPluginInfo CreateFakePluginInfo( |
| 24 const std::string& name, | 26 const std::string& name, |
| 25 const base::FilePath::CharType* path, | 27 const base::FilePath::CharType* path, |
| 26 const std::string& version, | 28 const std::string& version, |
| 27 bool is_pepper) { | 29 bool is_pepper) { |
| 28 content::WebPluginInfo plugin(base::UTF8ToUTF16(name), | 30 content::WebPluginInfo plugin(base::UTF8ToUTF16(name), |
| 29 base::FilePath(path), | 31 base::FilePath(path), |
| 30 base::UTF8ToUTF16(version), | 32 base::UTF8ToUTF16(version), |
| 31 base::string16()); | 33 base::string16()); |
| 32 if (is_pepper) | 34 if (is_pepper) |
| 33 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS; | 35 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS; |
| 34 else | 36 else |
| 35 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_NPAPI; | 37 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_NPAPI; |
| 36 return plugin; | 38 return plugin; |
| 37 } | 39 } |
| 38 | 40 |
| 39 } // namespace | 41 } // namespace |
| 40 | 42 |
| 41 TEST(PluginMetricsProviderTest, IsPluginProcess) { | 43 class PluginMetricsProviderTest : public ::testing::Test { |
|
Alexei Svitkine (slow)
2014/08/06 19:43:40
Move this to the anon namespace above.
gayane -on leave until 09-2017
2014/08/06 20:47:07
Done.
| |
| 44 protected: | |
| 45 PluginMetricsProviderTest() | |
| 46 : prefs_(new TestingPrefServiceSimple) {} | |
| 47 | |
| 48 virtual void SetUp() OVERRIDE { | |
| 49 PluginMetricsProvider::RegisterPrefs(prefs()->registry()); | |
|
Alexei Svitkine (slow)
2014/08/06 19:43:40
Nit: You can do this in the ctor and remove this m
gayane -on leave until 09-2017
2014/08/06 20:47:07
Done.
| |
| 50 } | |
| 51 | |
| 52 TestingPrefServiceSimple* prefs() { | |
| 53 return prefs_.get(); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 scoped_ptr<TestingPrefServiceSimple> prefs_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(PluginMetricsProviderTest); | |
| 60 | |
| 61 }; | |
| 62 | |
| 63 TEST_F(PluginMetricsProviderTest, IsPluginProcess) { | |
| 42 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( | 64 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( |
| 43 content::PROCESS_TYPE_PLUGIN)); | 65 content::PROCESS_TYPE_PLUGIN)); |
| 44 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( | 66 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( |
| 45 content::PROCESS_TYPE_PPAPI_PLUGIN)); | 67 content::PROCESS_TYPE_PPAPI_PLUGIN)); |
| 46 EXPECT_FALSE(PluginMetricsProvider::IsPluginProcess( | 68 EXPECT_FALSE(PluginMetricsProvider::IsPluginProcess( |
| 47 content::PROCESS_TYPE_GPU)); | 69 content::PROCESS_TYPE_GPU)); |
| 48 } | 70 } |
| 49 | 71 |
| 50 TEST(PluginMetricsProviderTest, Plugins) { | 72 TEST_F(PluginMetricsProviderTest, Plugins) { |
| 51 content::TestBrowserThreadBundle thread_bundle; | 73 content::TestBrowserThreadBundle thread_bundle; |
| 52 | 74 |
| 53 TestingPrefServiceSimple prefs; | 75 PluginMetricsProvider provider(prefs()); |
| 54 PluginMetricsProvider::RegisterPrefs(prefs.registry()); | |
| 55 PluginMetricsProvider provider(&prefs); | |
| 56 | 76 |
| 57 std::vector<content::WebPluginInfo> plugins; | 77 std::vector<content::WebPluginInfo> plugins; |
| 58 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), | 78 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), |
| 59 "1.5", true)); | 79 "1.5", true)); |
| 60 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), | 80 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), |
| 61 "2.0", false)); | 81 "2.0", false)); |
| 62 provider.SetPluginsForTesting(plugins); | 82 provider.SetPluginsForTesting(plugins); |
| 63 | 83 |
| 64 metrics::SystemProfileProto system_profile; | 84 metrics::SystemProfileProto system_profile; |
| 65 provider.ProvideSystemProfileMetrics(&system_profile); | 85 provider.ProvideSystemProfileMetrics(&system_profile); |
| 66 | 86 |
| 67 ASSERT_EQ(2, system_profile.plugin_size()); | 87 ASSERT_EQ(2, system_profile.plugin_size()); |
| 68 EXPECT_EQ("p1", system_profile.plugin(0).name()); | 88 EXPECT_EQ("p1", system_profile.plugin(0).name()); |
| 69 EXPECT_EQ("p1.plugin", system_profile.plugin(0).filename()); | 89 EXPECT_EQ("p1.plugin", system_profile.plugin(0).filename()); |
| 70 EXPECT_EQ("1.5", system_profile.plugin(0).version()); | 90 EXPECT_EQ("1.5", system_profile.plugin(0).version()); |
| 71 EXPECT_TRUE(system_profile.plugin(0).is_pepper()); | 91 EXPECT_TRUE(system_profile.plugin(0).is_pepper()); |
| 72 EXPECT_EQ("p2", system_profile.plugin(1).name()); | 92 EXPECT_EQ("p2", system_profile.plugin(1).name()); |
| 73 EXPECT_EQ("p2.plugin", system_profile.plugin(1).filename()); | 93 EXPECT_EQ("p2.plugin", system_profile.plugin(1).filename()); |
| 74 EXPECT_EQ("2.0", system_profile.plugin(1).version()); | 94 EXPECT_EQ("2.0", system_profile.plugin(1).version()); |
| 75 EXPECT_FALSE(system_profile.plugin(1).is_pepper()); | 95 EXPECT_FALSE(system_profile.plugin(1).is_pepper()); |
| 76 | 96 |
| 77 // Now set some plugin stability stats for p2 and verify they're recorded. | 97 // Now set some plugin stability stats for p2 and verify they're recorded. |
| 78 scoped_ptr<base::DictionaryValue> plugin_dict(new base::DictionaryValue); | 98 scoped_ptr<base::DictionaryValue> plugin_dict(new base::DictionaryValue); |
| 79 plugin_dict->SetString(prefs::kStabilityPluginName, "p2"); | 99 plugin_dict->SetString(prefs::kStabilityPluginName, "p2"); |
| 80 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 1); | 100 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 1); |
| 81 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 2); | 101 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 2); |
| 82 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 3); | 102 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 3); |
| 83 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 4); | 103 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 4); |
| 84 { | 104 { |
| 85 ListPrefUpdate update(&prefs, prefs::kStabilityPluginStats); | 105 ListPrefUpdate update(prefs(), prefs::kStabilityPluginStats); |
| 86 update.Get()->Append(plugin_dict.release()); | 106 update.Get()->Append(plugin_dict.release()); |
| 87 } | 107 } |
| 88 | 108 |
| 89 provider.ProvideStabilityMetrics(&system_profile); | 109 provider.ProvideStabilityMetrics(&system_profile); |
| 90 | 110 |
| 91 const metrics::SystemProfileProto_Stability& stability = | 111 const metrics::SystemProfileProto_Stability& stability = |
| 92 system_profile.stability(); | 112 system_profile.stability(); |
| 93 ASSERT_EQ(1, stability.plugin_stability_size()); | 113 ASSERT_EQ(1, stability.plugin_stability_size()); |
| 94 EXPECT_EQ("p2", stability.plugin_stability(0).plugin().name()); | 114 EXPECT_EQ("p2", stability.plugin_stability(0).plugin().name()); |
| 95 EXPECT_EQ("p2.plugin", stability.plugin_stability(0).plugin().filename()); | 115 EXPECT_EQ("p2.plugin", stability.plugin_stability(0).plugin().filename()); |
| 96 EXPECT_EQ("2.0", stability.plugin_stability(0).plugin().version()); | 116 EXPECT_EQ("2.0", stability.plugin_stability(0).plugin().version()); |
| 97 EXPECT_FALSE(stability.plugin_stability(0).plugin().is_pepper()); | 117 EXPECT_FALSE(stability.plugin_stability(0).plugin().is_pepper()); |
| 98 EXPECT_EQ(1, stability.plugin_stability(0).launch_count()); | 118 EXPECT_EQ(1, stability.plugin_stability(0).launch_count()); |
| 99 EXPECT_EQ(2, stability.plugin_stability(0).crash_count()); | 119 EXPECT_EQ(2, stability.plugin_stability(0).crash_count()); |
| 100 EXPECT_EQ(3, stability.plugin_stability(0).instance_count()); | 120 EXPECT_EQ(3, stability.plugin_stability(0).instance_count()); |
| 101 EXPECT_EQ(4, stability.plugin_stability(0).loading_error_count()); | 121 EXPECT_EQ(4, stability.plugin_stability(0).loading_error_count()); |
| 102 } | 122 } |
| 123 | |
| 124 TEST_F(PluginMetricsProviderTest, RecordCurrentStateWithDelay) { | |
| 125 content::TestBrowserThreadBundle thread_bundle; | |
| 126 | |
| 127 PluginMetricsProvider provider(prefs()); | |
| 128 | |
| 129 int delay_ms = 10; | |
| 130 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); | |
| 131 EXPECT_FALSE(provider.RecordCurrentStateWithDelay(delay_ms)); | |
| 132 | |
| 133 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(delay_ms)); | |
| 134 base::RunLoop().RunUntilIdle(); | |
| 135 | |
| 136 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); | |
| 137 } | |
| 138 | |
| 139 TEST_F(PluginMetricsProviderTest, RecordCurrentStateIfPending) { | |
| 140 content::TestBrowserThreadBundle thread_bundle; | |
| 141 | |
| 142 PluginMetricsProvider provider(prefs()); | |
| 143 | |
| 144 // First there should be no need to force RecordCurrentState. | |
| 145 EXPECT_FALSE(provider.RecordCurrentStateIfPending()); | |
| 146 | |
| 147 // After delayed task is posted RecordCurrentStateIfPending should return | |
| 148 // true. | |
| 149 int delay_ms = 100000; | |
| 150 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); | |
| 151 EXPECT_TRUE(provider.RecordCurrentStateIfPending()); | |
| 152 | |
| 153 // If RecordCurrentStateIfPending was successful then we should be able to | |
| 154 // post a new delayed task. | |
| 155 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); | |
| 156 } | |
| 157 | |
| 158 TEST_F(PluginMetricsProviderTest, ProvideStabilityMetricsWhenPendingTask) { | |
| 159 content::TestBrowserThreadBundle thread_bundle; | |
| 160 | |
| 161 PluginMetricsProvider provider(prefs()); | |
| 162 | |
| 163 // Create plugin information for testing. | |
| 164 std::vector<content::WebPluginInfo> plugins; | |
| 165 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), | |
| 166 "1.5", true)); | |
| 167 provider.SetPluginsForTesting(plugins); | |
| 168 metrics::SystemProfileProto system_profile; | |
| 169 provider.ProvideSystemProfileMetrics(&system_profile); | |
| 170 | |
| 171 // Increase number of created instances which should also start a delayed | |
| 172 // task. | |
| 173 content::ChildProcessData child_process_data(content::PROCESS_TYPE_PLUGIN); | |
| 174 child_process_data.name = base::UTF8ToUTF16("p1"); | |
| 175 provider.BrowserChildProcessInstanceCreated(child_process_data); | |
| 176 | |
| 177 // Call ProvideStabilityMetrics to check that it will force pending tasks to | |
| 178 // be executed immediately. | |
| 179 provider.ProvideStabilityMetrics(&system_profile); | |
| 180 | |
| 181 // Check current number of instances created. | |
| 182 const metrics::SystemProfileProto_Stability& stability = | |
| 183 system_profile.stability(); | |
| 184 EXPECT_EQ(1, stability.plugin_stability(0).instance_count()); | |
| 185 } | |
| OLD | NEW |