| 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/power/process_power_collector.h" | 5 #include "chrome/browser/power/process_power_collector.h" |
| 6 | 6 |
| 7 #include "chrome/browser/apps/scoped_keep_alive.h" | 7 #include "chrome/browser/apps/scoped_keep_alive.h" |
| 8 #include "chrome/browser/profiles/profile_manager.h" | 8 #include "chrome/browser/profiles/profile_manager.h" |
| 9 #include "chrome/browser/ui/apps/chrome_app_delegate.h" | 9 #include "chrome/browser/ui/apps/chrome_app_delegate.h" |
| 10 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/test/base/browser_with_test_window_test.h" | 12 #include "chrome/test/base/browser_with_test_window_test.h" |
| 13 #include "chrome/test/base/testing_browser_process.h" | 13 #include "chrome/test/base/testing_browser_process.h" |
| 14 #include "chrome/test/base/testing_profile_manager.h" | 14 #include "chrome/test/base/testing_profile_manager.h" |
| 15 #include "components/power/origin_power_map.h" | 15 #include "components/power/origin_power_map.h" |
| 16 #include "components/power/origin_power_map_factory.h" | 16 #include "components/power/origin_power_map_factory.h" |
| 17 #include "content/public/browser/site_instance.h" | 17 #include "content/public/browser/site_instance.h" |
| 18 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
| 19 #include "content/public/test/mock_render_process_host.h" | 19 #include "content/public/test/mock_render_process_host.h" |
| 20 #include "extensions/browser/app_window/app_window_client.h" | 20 #include "extensions/browser/app_window/app_window_client.h" |
| 21 #include "extensions/browser/app_window/app_window_contents.h" | |
| 22 #include "extensions/browser/app_window/app_window_registry.h" | 21 #include "extensions/browser/app_window/app_window_registry.h" |
| 23 #include "extensions/browser/app_window/native_app_window.h" | 22 #include "extensions/browser/app_window/native_app_window.h" |
| 23 #include "extensions/browser/app_window/test_app_window_contents.h" |
| 24 #include "extensions/common/extension.h" | 24 #include "extensions/common/extension.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
| 29 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | 29 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
| 30 #endif | 30 #endif |
| 31 | 31 |
| 32 using power::OriginPowerMap; | 32 using power::OriginPowerMap; |
| 33 using power::OriginPowerMapFactory; | 33 using power::OriginPowerMapFactory; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 process_id) | 82 process_id) |
| 83 #endif | 83 #endif |
| 84 ); | 84 ); |
| 85 return proc_handle.Pass(); | 85 return proc_handle.Pass(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 scoped_ptr<ProcessPowerCollector> collector; | 88 scoped_ptr<ProcessPowerCollector> collector; |
| 89 scoped_ptr<TestingProfileManager> profile_manager_; | 89 scoped_ptr<TestingProfileManager> profile_manager_; |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 class TestAppWindowContents : public extensions::AppWindowContents { | |
| 93 public: | |
| 94 explicit TestAppWindowContents(content::WebContents* web_contents) | |
| 95 : web_contents_(web_contents) {} | |
| 96 | |
| 97 // apps:AppWindowContents | |
| 98 void Initialize(content::BrowserContext* context, const GURL& url) override {} | |
| 99 void LoadContents(int32 creator_process_id) override {} | |
| 100 void NativeWindowChanged( | |
| 101 extensions::NativeAppWindow* native_app_window) override {} | |
| 102 void NativeWindowClosed() override {} | |
| 103 void DispatchWindowShownForTests() const override {} | |
| 104 content::WebContents* GetWebContents() const override { | |
| 105 return web_contents_.get(); | |
| 106 } | |
| 107 | |
| 108 private: | |
| 109 scoped_ptr<content::WebContents> web_contents_; | |
| 110 }; | |
| 111 | |
| 112 TEST_F(BrowserProcessPowerTest, NoSite) { | 92 TEST_F(BrowserProcessPowerTest, NoSite) { |
| 113 collector->UpdatePowerConsumptionForTesting(); | 93 collector->UpdatePowerConsumptionForTesting(); |
| 114 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size()); | 94 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size()); |
| 115 } | 95 } |
| 116 | 96 |
| 117 TEST_F(BrowserProcessPowerTest, OneSite) { | 97 TEST_F(BrowserProcessPowerTest, OneSite) { |
| 118 GURL url("http://www.google.com"); | 98 GURL url("http://www.google.com"); |
| 119 AddTab(browser(), url); | 99 AddTab(browser(), url); |
| 120 collector->UpdatePowerConsumptionForTesting(); | 100 collector->UpdatePowerConsumptionForTesting(); |
| 121 ProcessPowerCollector::ProcessMetricsMap* metrics_map = | 101 ProcessPowerCollector::ProcessMetricsMap* metrics_map = |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 GURL url("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | 272 GURL url("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
| 293 extensions::AppWindow* window = new extensions::AppWindow( | 273 extensions::AppWindow* window = new extensions::AppWindow( |
| 294 current_profile, new ChromeAppDelegate(scoped_ptr<ScopedKeepAlive>()), | 274 current_profile, new ChromeAppDelegate(scoped_ptr<ScopedKeepAlive>()), |
| 295 extension.get()); | 275 extension.get()); |
| 296 content::WebContents* web_contents( | 276 content::WebContents* web_contents( |
| 297 content::WebContents::Create(content::WebContents::CreateParams( | 277 content::WebContents::Create(content::WebContents::CreateParams( |
| 298 current_profile, | 278 current_profile, |
| 299 content::SiteInstance::CreateForURL(current_profile, url)))); | 279 content::SiteInstance::CreateForURL(current_profile, url)))); |
| 300 window->SetAppWindowContentsForTesting( | 280 window->SetAppWindowContentsForTesting( |
| 301 scoped_ptr<extensions::AppWindowContents>( | 281 scoped_ptr<extensions::AppWindowContents>( |
| 302 new TestAppWindowContents(web_contents))); | 282 new extensions::TestAppWindowContents(web_contents))); |
| 303 extensions::AppWindowRegistry* app_registry = | 283 extensions::AppWindowRegistry* app_registry = |
| 304 extensions::AppWindowRegistry::Get(current_profile); | 284 extensions::AppWindowRegistry::Get(current_profile); |
| 305 app_registry->AddAppWindow(window); | 285 app_registry->AddAppWindow(window); |
| 306 | 286 |
| 307 collector->set_cpu_usage_callback_for_testing( | 287 collector->set_cpu_usage_callback_for_testing( |
| 308 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant, | 288 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant, |
| 309 base::Unretained(this), | 289 base::Unretained(this), |
| 310 5)); | 290 5)); |
| 311 collector->UpdatePowerConsumptionForTesting(); | 291 collector->UpdatePowerConsumptionForTesting(); |
| 312 EXPECT_EQ(1u, collector->metrics_map_for_testing()->size()); | 292 EXPECT_EQ(1u, collector->metrics_map_for_testing()->size()); |
| 313 | 293 |
| 314 // Clear the AppWindowContents before trying to close. | |
| 315 window->SetAppWindowContentsForTesting( | |
| 316 scoped_ptr<extensions::AppWindowContents>()); | |
| 317 window->OnNativeClose(); | 294 window->OnNativeClose(); |
| 318 collector->UpdatePowerConsumptionForTesting(); | 295 collector->UpdatePowerConsumptionForTesting(); |
| 319 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size()); | 296 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size()); |
| 320 } | 297 } |
| OLD | NEW |