Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/power/process_power_collector.h" | |
| 6 | |
| 7 #include "apps/app_window_contents.h" | |
| 8 #include "apps/app_window_registry.h" | |
| 9 #include "chrome/browser/profiles/profile_manager.h" | |
| 10 #include "chrome/browser/ui/apps/chrome_app_delegate.h" | |
| 11 #include "chrome/browser/ui/browser_commands.h" | |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 13 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 14 #include "chrome/test/base/testing_browser_process.h" | |
| 15 #include "chrome/test/base/testing_profile_manager.h" | |
| 16 #include "components/power/origin_power_map.h" | |
| 17 #include "components/power/origin_power_map_factory.h" | |
| 18 #include "content/public/browser/site_instance.h" | |
| 19 #include "content/public/test/browser_test_utils.h" | |
| 20 #include "content/public/test/mock_render_process_host.h" | |
| 21 #include "extensions/browser/app_window/native_app_window.h" | |
| 22 #include "extensions/common/extension.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 #include "url/gurl.h" | |
| 25 | |
| 26 #if defined(OS_CHROMEOS) | |
| 27 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
| 28 #endif | |
| 29 | |
| 30 using power::OriginPowerMap; | |
| 31 using power::OriginPowerMapFactory; | |
| 32 | |
| 33 class BrowserProcessPowerTest : public BrowserWithTestWindowTest { | |
| 34 public: | |
| 35 BrowserProcessPowerTest() : collector(new ProcessPowerCollector) {} | |
| 36 virtual ~BrowserProcessPowerTest() {} | |
| 37 | |
| 38 virtual void SetUp() OVERRIDE { | |
| 39 BrowserWithTestWindowTest::SetUp(); | |
| 40 | |
| 41 #if defined(OS_CHROMEOS) | |
| 42 power_manager::PowerSupplyProperties prop; | |
| 43 prop.set_external_power(power_manager::PowerSupplyProperties::AC); | |
| 44 prop.set_battery_state(power_manager::PowerSupplyProperties::DISCHARGING); | |
| 45 prop.set_battery_percent(20.00); | |
| 46 prop.set_battery_discharge_rate(1); | |
| 47 collector->PowerChanged(prop); | |
| 48 #endif | |
| 49 | |
| 50 profile_manager_.reset( | |
| 51 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | |
| 52 ASSERT_TRUE(profile_manager_->SetUp()); | |
| 53 } | |
| 54 | |
| 55 virtual void TearDown() OVERRIDE { | |
| 56 collector.reset(); | |
| 57 BrowserWithTestWindowTest::TearDown(); | |
| 58 } | |
| 59 | |
| 60 // Mocks out CPU usage for all processes as |value| percent. | |
| 61 double ReturnCpuAsConstant(double value, base::ProcessHandle handle) { | |
| 62 return value; | |
| 63 } | |
| 64 | |
| 65 protected: | |
| 66 scoped_ptr<ProcessPowerCollector> collector; | |
| 67 scoped_ptr<TestingProfileManager> profile_manager_; | |
| 68 | |
| 69 content::MockRenderProcessHost* process(Browser* browser) { | |
|
Daniel Erat
2014/08/22 22:41:39
names_like_this() are only used for simple setters
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
| 70 return static_cast<content::MockRenderProcessHost*>( | |
| 71 browser->tab_strip_model() | |
| 72 ->GetActiveWebContents() | |
| 73 ->GetRenderViewHost() | |
| 74 ->GetProcess()); | |
| 75 } | |
| 76 | |
| 77 scoped_ptr<base::ProcessHandle> MakeProcessHandle(int process_id) { | |
|
Daniel Erat
2014/08/22 22:41:38
move this above the data members too
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
| 78 scoped_ptr<base::ProcessHandle> proc_handle(new base::ProcessHandle( | |
| 79 #if defined(OS_WIN) | |
| 80 reinterpret_cast<HANDLE>(process_id)) | |
| 81 #else | |
| 82 process_id) | |
| 83 #endif | |
| 84 ); | |
| 85 return proc_handle.Pass(); | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 class TestAppWindowContents : public apps::AppWindowContents { | |
| 90 public: | |
| 91 explicit TestAppWindowContents(content::WebContents* web_contents) { | |
| 92 web_contents_.reset(web_contents); | |
|
Daniel Erat
2014/08/22 22:41:38
nit: move this to an initialization list
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
| 93 } | |
|
Daniel Erat
2014/08/22 22:41:39
nit: blank line after this one
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
| 94 // apps:AppWindowContents | |
| 95 virtual void Initialize(content::BrowserContext* context, | |
| 96 const GURL& url) OVERRIDE{}; | |
|
Daniel Erat
2014/08/22 22:41:39
for all of these: add a space between OVERRIDE and
Daniel Nishi
2014/08/22 23:21:32
Done.
| |
| 97 virtual void LoadContents(int32 creator_process_id) OVERRIDE{}; | |
| 98 virtual void NativeWindowChanged( | |
| 99 extensions::NativeAppWindow* native_app_window) OVERRIDE{}; | |
| 100 virtual void NativeWindowClosed() OVERRIDE{}; | |
| 101 virtual void DispatchWindowShownForTests() const OVERRIDE{}; | |
| 102 virtual content::WebContents* GetWebContents() const OVERRIDE { | |
| 103 return web_contents_.get(); | |
| 104 } | |
| 105 | |
| 106 private: | |
| 107 scoped_ptr<content::WebContents> web_contents_; | |
| 108 }; | |
| 109 | |
| 110 TEST_F(BrowserProcessPowerTest, NoSite) { | |
| 111 collector->UpdatePowerConsumptionForTesting(); | |
| 112 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size()); | |
| 113 } | |
| 114 | |
| 115 TEST_F(BrowserProcessPowerTest, OneSite) { | |
| 116 GURL url("http://www.google.com"); | |
| 117 AddTab(browser(), url); | |
| 118 collector->UpdatePowerConsumptionForTesting(); | |
| 119 ProcessPowerCollector::ProcessMetricsMap* metrics_map = | |
| 120 collector->metrics_map_for_testing(); | |
| 121 EXPECT_EQ(1u, metrics_map->size()); | |
| 122 | |
| 123 // Create fake process numbers. | |
| 124 process(browser())->SetProcessHandle(MakeProcessHandle(1).Pass()); | |
| 125 | |
| 126 OriginPowerMap* origin_power_map = | |
| 127 OriginPowerMapFactory::GetForBrowserContext(profile()); | |
| 128 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url)); | |
| 129 | |
| 130 collector->set_cpu_usage_callback_for_testing( | |
| 131 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant, | |
| 132 base::Unretained(this), | |
| 133 5)); | |
| 134 EXPECT_DOUBLE_EQ(5, collector->UpdatePowerConsumptionForTesting()); | |
| 135 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url)); | |
| 136 } | |
| 137 | |
| 138 TEST_F(BrowserProcessPowerTest, MultipleSites) { | |
| 139 Browser::CreateParams native_params(profile(), | |
| 140 chrome::HOST_DESKTOP_TYPE_NATIVE); | |
| 141 GURL url1("http://www.google.com"); | |
| 142 GURL url2("http://www.example.com"); | |
| 143 GURL url3("https://www.google.com"); | |
| 144 scoped_ptr<Browser> browser2( | |
| 145 chrome::CreateBrowserWithTestWindowForParams(&native_params)); | |
| 146 scoped_ptr<Browser> browser3( | |
| 147 chrome::CreateBrowserWithTestWindowForParams(&native_params)); | |
| 148 AddTab(browser(), url1); | |
| 149 AddTab(browser2.get(), url2); | |
| 150 AddTab(browser3.get(), url3); | |
| 151 | |
| 152 // Create fake process numbers. | |
| 153 process(browser())->SetProcessHandle(MakeProcessHandle(1).Pass()); | |
| 154 process(browser2.get())->SetProcessHandle(MakeProcessHandle(2).Pass()); | |
| 155 process(browser3.get())->SetProcessHandle(MakeProcessHandle(3).Pass()); | |
| 156 | |
| 157 collector->UpdatePowerConsumptionForTesting(); | |
| 158 ProcessPowerCollector::ProcessMetricsMap* metrics_map = | |
| 159 collector->metrics_map_for_testing(); | |
| 160 EXPECT_EQ(3u, metrics_map->size()); | |
| 161 | |
| 162 // Since all handlers are uninitialized, this should be 0. | |
| 163 EXPECT_DOUBLE_EQ(0, collector->UpdatePowerConsumptionForTesting()); | |
| 164 OriginPowerMap* origin_power_map = | |
| 165 OriginPowerMapFactory::GetForBrowserContext(profile()); | |
| 166 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url1)); | |
| 167 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url2)); | |
| 168 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url3)); | |
| 169 | |
| 170 collector->set_cpu_usage_callback_for_testing( | |
| 171 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant, | |
| 172 base::Unretained(this), | |
| 173 5)); | |
| 174 EXPECT_DOUBLE_EQ(15, collector->UpdatePowerConsumptionForTesting()); | |
| 175 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url1)); | |
| 176 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url2)); | |
| 177 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url3)); | |
| 178 | |
| 179 // Close some tabs and verify that they are removed from the metrics map. | |
| 180 chrome::CloseTab(browser2.get()); | |
| 181 chrome::CloseTab(browser3.get()); | |
| 182 | |
| 183 collector->UpdatePowerConsumptionForTesting(); | |
| 184 EXPECT_EQ(1u, metrics_map->size()); | |
| 185 } | |
| 186 | |
| 187 TEST_F(BrowserProcessPowerTest, IncognitoDoesntRecordPowerUsage) { | |
| 188 Browser::CreateParams native_params(profile()->GetOffTheRecordProfile(), | |
| 189 chrome::HOST_DESKTOP_TYPE_NATIVE); | |
| 190 scoped_ptr<Browser> incognito_browser( | |
| 191 chrome::CreateBrowserWithTestWindowForParams(&native_params)); | |
| 192 GURL url("http://www.google.com"); | |
| 193 AddTab(browser(), url); | |
| 194 | |
| 195 GURL hidden_url("http://foo.com"); | |
| 196 AddTab(incognito_browser.get(), hidden_url); | |
| 197 | |
| 198 // Create fake process numbers. | |
| 199 process(browser())->SetProcessHandle(MakeProcessHandle(1).Pass()); | |
| 200 process(incognito_browser.get()) | |
| 201 ->SetProcessHandle(MakeProcessHandle(2).Pass()); | |
| 202 | |
| 203 EXPECT_DOUBLE_EQ(0, collector->UpdatePowerConsumptionForTesting()); | |
| 204 ProcessPowerCollector::ProcessMetricsMap* metrics_map = | |
| 205 collector->metrics_map_for_testing(); | |
| 206 EXPECT_EQ(1u, metrics_map->size()); | |
| 207 | |
| 208 OriginPowerMap* origin_power_map = | |
| 209 OriginPowerMapFactory::GetForBrowserContext(profile()); | |
| 210 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url)); | |
| 211 | |
| 212 collector->set_cpu_usage_callback_for_testing( | |
| 213 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant, | |
| 214 base::Unretained(this), | |
| 215 5)); | |
| 216 EXPECT_DOUBLE_EQ(5, collector->UpdatePowerConsumptionForTesting()); | |
| 217 | |
| 218 // Verify that the incognito data was not stored. | |
| 219 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url)); | |
| 220 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(hidden_url)); | |
| 221 | |
| 222 chrome::CloseTab(incognito_browser.get()); | |
| 223 } | |
| 224 | |
| 225 TEST_F(BrowserProcessPowerTest, MultipleProfilesRecordSeparately) { | |
| 226 scoped_ptr<Profile> other_profile(CreateProfile()); | |
| 227 Browser::CreateParams native_params(other_profile.get(), | |
| 228 chrome::HOST_DESKTOP_TYPE_NATIVE); | |
| 229 scoped_ptr<Browser> other_user( | |
| 230 chrome::CreateBrowserWithTestWindowForParams(&native_params)); | |
| 231 | |
| 232 GURL url("http://www.google.com"); | |
| 233 AddTab(browser(), url); | |
| 234 | |
| 235 GURL hidden_url("http://foo.com"); | |
| 236 AddTab(other_user.get(), hidden_url); | |
| 237 | |
| 238 // Create fake process numbers. | |
| 239 process(browser())->SetProcessHandle(MakeProcessHandle(1).Pass()); | |
| 240 process(other_user.get())->SetProcessHandle(MakeProcessHandle(2).Pass()); | |
| 241 | |
| 242 EXPECT_DOUBLE_EQ(0, collector->UpdatePowerConsumptionForTesting()); | |
| 243 EXPECT_EQ(2u, collector->metrics_map_for_testing()->size()); | |
| 244 | |
| 245 collector->set_cpu_usage_callback_for_testing( | |
| 246 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant, | |
| 247 base::Unretained(this), | |
| 248 5)); | |
| 249 EXPECT_DOUBLE_EQ(10, collector->UpdatePowerConsumptionForTesting()); | |
| 250 | |
| 251 // profile() should have an entry for |url| but not |hidden_url|. | |
| 252 OriginPowerMap* origin_power_map_first = | |
| 253 OriginPowerMapFactory::GetForBrowserContext(profile()); | |
| 254 EXPECT_EQ(100, origin_power_map_first->GetPowerForOrigin(url)); | |
| 255 EXPECT_EQ(0, origin_power_map_first->GetPowerForOrigin(hidden_url)); | |
| 256 | |
| 257 // |other_profile| should have an entry for |hidden_url| but not |url|. | |
| 258 OriginPowerMap* origin_power_map_second = | |
| 259 OriginPowerMapFactory::GetForBrowserContext(other_profile.get()); | |
| 260 EXPECT_EQ(0, origin_power_map_second->GetPowerForOrigin(url)); | |
| 261 EXPECT_EQ(100, origin_power_map_second->GetPowerForOrigin(hidden_url)); | |
| 262 | |
| 263 // Clean up | |
| 264 chrome::CloseTab(other_user.get()); | |
| 265 } | |
| 266 | |
| 267 TEST_F(BrowserProcessPowerTest, AppsRecordPowerUsage) { | |
| 268 // Install an app (an extension*). | |
| 269 #if defined(OS_WIN) | |
| 270 base::FilePath extension_path(FILE_PATH_LITERAL("c:\\foo")); | |
| 271 #elif defined(OS_POSIX) | |
| 272 base::FilePath extension_path(FILE_PATH_LITERAL("/foo")); | |
| 273 #endif | |
| 274 base::DictionaryValue manifest; | |
| 275 manifest.SetString("name", "Fake Name"); | |
| 276 manifest.SetString("version", "1"); | |
| 277 std::string error; | |
| 278 char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
| 279 scoped_refptr<extensions::Extension> extension( | |
| 280 extensions::Extension::Create(extension_path, | |
| 281 extensions::Manifest::INTERNAL, | |
| 282 manifest, | |
| 283 extensions::Extension::NO_FLAGS, | |
| 284 kTestAppId, | |
| 285 &error)); | |
| 286 EXPECT_TRUE(extension.get()) << error; | |
| 287 | |
| 288 Profile* current_profile = | |
| 289 profile_manager_->CreateTestingProfile("Test user"); | |
| 290 GURL url("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | |
| 291 apps::AppWindow* window = | |
| 292 new apps::AppWindow(current_profile, new ChromeAppDelegate(), extension); | |
| 293 scoped_ptr<content::WebContents> web_contents( | |
| 294 content::WebContents::Create(content::WebContents::CreateParams( | |
| 295 current_profile, | |
| 296 content::SiteInstance::CreateForURL(current_profile, url)))); | |
| 297 window->SetAppWindowContentsForTesting(scoped_ptr<apps::AppWindowContents>( | |
| 298 new TestAppWindowContents(web_contents.get()))); | |
| 299 apps::AppWindowRegistry* app_registry = | |
| 300 apps::AppWindowRegistry::Get(current_profile); | |
| 301 app_registry->AddAppWindow(window); | |
| 302 | |
| 303 collector->set_cpu_usage_callback_for_testing( | |
| 304 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant, | |
| 305 base::Unretained(this), | |
| 306 5)); | |
| 307 collector->UpdatePowerConsumptionForTesting(); | |
| 308 EXPECT_EQ(1u, collector->metrics_map_for_testing()->size()); | |
| 309 | |
| 310 app_registry->RemoveAppWindow(window); | |
| 311 collector->UpdatePowerConsumptionForTesting(); | |
| 312 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size()); | |
| 313 } | |
| OLD | NEW |