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.
5 #include "chrome/browser/power/process_power_collector.h"
7 #include "chrome/browser/profiles/profile_manager.h"
8 #include "chrome/browser/ui/apps/chrome_app_delegate.h"
9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/browser_with_test_window_test.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile_manager.h"
14 #include "components/power/origin_power_map.h"
15 #include "components/power/origin_power_map_factory.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "content/public/test/mock_render_process_host.h"
19 #include "extensions/browser/app_window/app_window_contents.h"
20 #include "extensions/browser/app_window/app_window_registry.h"
21 #include "extensions/browser/app_window/apps_client.h"
22 #include "extensions/browser/app_window/native_app_window.h"
23 #include "extensions/common/extension.h"
24 #include "testing/gtest/include/gtest/gtest.h"
27 #if defined(OS_CHROMEOS)
28 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
31 using power::OriginPowerMap
;
32 using power::OriginPowerMapFactory
;
34 class BrowserProcessPowerTest
: public BrowserWithTestWindowTest
{
36 BrowserProcessPowerTest() {}
37 virtual ~BrowserProcessPowerTest() {}
39 virtual void SetUp() OVERRIDE
{
40 BrowserWithTestWindowTest::SetUp();
41 collector
.reset(new ProcessPowerCollector
);
43 #if defined(OS_CHROMEOS)
44 power_manager::PowerSupplyProperties prop
;
45 prop
.set_external_power(power_manager::PowerSupplyProperties::AC
);
46 prop
.set_battery_state(power_manager::PowerSupplyProperties::DISCHARGING
);
47 prop
.set_battery_percent(20.00);
48 prop
.set_battery_discharge_rate(1);
49 collector
->PowerChanged(prop
);
52 profile_manager_
.reset(
53 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
54 ASSERT_TRUE(profile_manager_
->SetUp());
57 virtual void TearDown() OVERRIDE
{
59 BrowserWithTestWindowTest::TearDown();
62 // Mocks out CPU usage for all processes as |value| percent.
63 double ReturnCpuAsConstant(double value
, base::ProcessHandle handle
) {
68 content::MockRenderProcessHost
* GetProcess(Browser
* browser
) {
69 return static_cast<content::MockRenderProcessHost
*>(
70 browser
->tab_strip_model()
71 ->GetActiveWebContents()
76 scoped_ptr
<base::ProcessHandle
> MakeProcessHandle(int process_id
) {
77 scoped_ptr
<base::ProcessHandle
> proc_handle(new base::ProcessHandle(
79 reinterpret_cast<HANDLE
>(process_id
))
84 return proc_handle
.Pass();
87 scoped_ptr
<ProcessPowerCollector
> collector
;
88 scoped_ptr
<TestingProfileManager
> profile_manager_
;
91 class TestAppWindowContents
: public extensions::AppWindowContents
{
93 explicit TestAppWindowContents(content::WebContents
* web_contents
)
94 : web_contents_(web_contents
) {}
96 // apps:AppWindowContents
97 virtual void Initialize(content::BrowserContext
* context
,
98 const GURL
& url
) OVERRIDE
{}
99 virtual void LoadContents(int32 creator_process_id
) OVERRIDE
{}
100 virtual void NativeWindowChanged(
101 extensions::NativeAppWindow
* native_app_window
) OVERRIDE
{}
102 virtual void NativeWindowClosed() OVERRIDE
{}
103 virtual void DispatchWindowShownForTests() const OVERRIDE
{}
104 virtual content::WebContents
* GetWebContents() const OVERRIDE
{
105 return web_contents_
.get();
109 scoped_ptr
<content::WebContents
> web_contents_
;
112 TEST_F(BrowserProcessPowerTest
, NoSite
) {
113 collector
->UpdatePowerConsumptionForTesting();
114 EXPECT_EQ(0u, collector
->metrics_map_for_testing()->size());
117 TEST_F(BrowserProcessPowerTest
, OneSite
) {
118 GURL
url("http://www.google.com");
119 AddTab(browser(), url
);
120 collector
->UpdatePowerConsumptionForTesting();
121 ProcessPowerCollector::ProcessMetricsMap
* metrics_map
=
122 collector
->metrics_map_for_testing();
123 EXPECT_EQ(1u, metrics_map
->size());
125 // Create fake process numbers.
126 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
128 OriginPowerMap
* origin_power_map
=
129 OriginPowerMapFactory::GetForBrowserContext(profile());
130 EXPECT_EQ(0, origin_power_map
->GetPowerForOrigin(url
));
132 collector
->set_cpu_usage_callback_for_testing(
133 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant
,
134 base::Unretained(this),
136 EXPECT_DOUBLE_EQ(5, collector
->UpdatePowerConsumptionForTesting());
137 EXPECT_EQ(100, origin_power_map
->GetPowerForOrigin(url
));
140 TEST_F(BrowserProcessPowerTest
, MultipleSites
) {
141 Browser::CreateParams
native_params(profile(),
142 chrome::HOST_DESKTOP_TYPE_NATIVE
);
143 GURL
url1("http://www.google.com");
144 GURL
url2("http://www.example.com");
145 GURL
url3("https://www.google.com");
146 scoped_ptr
<Browser
> browser2(
147 chrome::CreateBrowserWithTestWindowForParams(&native_params
));
148 scoped_ptr
<Browser
> browser3(
149 chrome::CreateBrowserWithTestWindowForParams(&native_params
));
150 AddTab(browser(), url1
);
151 AddTab(browser2
.get(), url2
);
152 AddTab(browser3
.get(), url3
);
154 // Create fake process numbers.
155 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
156 GetProcess(browser2
.get())->SetProcessHandle(MakeProcessHandle(2).Pass());
157 GetProcess(browser3
.get())->SetProcessHandle(MakeProcessHandle(3).Pass());
159 collector
->UpdatePowerConsumptionForTesting();
160 ProcessPowerCollector::ProcessMetricsMap
* metrics_map
=
161 collector
->metrics_map_for_testing();
162 EXPECT_EQ(3u, metrics_map
->size());
164 // Since all handlers are uninitialized, this should be 0.
165 EXPECT_DOUBLE_EQ(0, collector
->UpdatePowerConsumptionForTesting());
166 OriginPowerMap
* origin_power_map
=
167 OriginPowerMapFactory::GetForBrowserContext(profile());
168 EXPECT_EQ(0, origin_power_map
->GetPowerForOrigin(url1
));
169 EXPECT_EQ(0, origin_power_map
->GetPowerForOrigin(url2
));
170 EXPECT_EQ(0, origin_power_map
->GetPowerForOrigin(url3
));
172 collector
->set_cpu_usage_callback_for_testing(
173 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant
,
174 base::Unretained(this),
176 EXPECT_DOUBLE_EQ(15, collector
->UpdatePowerConsumptionForTesting());
177 EXPECT_EQ(33, origin_power_map
->GetPowerForOrigin(url1
));
178 EXPECT_EQ(33, origin_power_map
->GetPowerForOrigin(url2
));
179 EXPECT_EQ(33, origin_power_map
->GetPowerForOrigin(url3
));
181 // Close some tabs and verify that they are removed from the metrics map.
182 chrome::CloseTab(browser2
.get());
183 chrome::CloseTab(browser3
.get());
185 collector
->UpdatePowerConsumptionForTesting();
186 EXPECT_EQ(1u, metrics_map
->size());
189 TEST_F(BrowserProcessPowerTest
, IncognitoDoesntRecordPowerUsage
) {
190 Browser::CreateParams
native_params(profile()->GetOffTheRecordProfile(),
191 chrome::HOST_DESKTOP_TYPE_NATIVE
);
192 scoped_ptr
<Browser
> incognito_browser(
193 chrome::CreateBrowserWithTestWindowForParams(&native_params
));
194 GURL
url("http://www.google.com");
195 AddTab(browser(), url
);
197 GURL
hidden_url("http://foo.com");
198 AddTab(incognito_browser
.get(), hidden_url
);
200 // Create fake process numbers.
201 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
202 GetProcess(incognito_browser
.get())
203 ->SetProcessHandle(MakeProcessHandle(2).Pass());
205 EXPECT_DOUBLE_EQ(0, collector
->UpdatePowerConsumptionForTesting());
206 ProcessPowerCollector::ProcessMetricsMap
* metrics_map
=
207 collector
->metrics_map_for_testing();
208 EXPECT_EQ(1u, metrics_map
->size());
210 OriginPowerMap
* origin_power_map
=
211 OriginPowerMapFactory::GetForBrowserContext(profile());
212 EXPECT_EQ(0, origin_power_map
->GetPowerForOrigin(url
));
214 collector
->set_cpu_usage_callback_for_testing(
215 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant
,
216 base::Unretained(this),
218 EXPECT_DOUBLE_EQ(5, collector
->UpdatePowerConsumptionForTesting());
220 // Verify that the incognito data was not stored.
221 EXPECT_EQ(100, origin_power_map
->GetPowerForOrigin(url
));
222 EXPECT_EQ(0, origin_power_map
->GetPowerForOrigin(hidden_url
));
224 chrome::CloseTab(incognito_browser
.get());
227 TEST_F(BrowserProcessPowerTest
, MultipleProfilesRecordSeparately
) {
228 scoped_ptr
<Profile
> other_profile(CreateProfile());
229 Browser::CreateParams
native_params(other_profile
.get(),
230 chrome::HOST_DESKTOP_TYPE_NATIVE
);
231 scoped_ptr
<Browser
> other_user(
232 chrome::CreateBrowserWithTestWindowForParams(&native_params
));
234 GURL
url("http://www.google.com");
235 AddTab(browser(), url
);
237 GURL
hidden_url("http://foo.com");
238 AddTab(other_user
.get(), hidden_url
);
240 // Create fake process numbers.
241 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
242 GetProcess(other_user
.get())->SetProcessHandle(MakeProcessHandle(2).Pass());
244 EXPECT_DOUBLE_EQ(0, collector
->UpdatePowerConsumptionForTesting());
245 EXPECT_EQ(2u, collector
->metrics_map_for_testing()->size());
247 collector
->set_cpu_usage_callback_for_testing(
248 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant
,
249 base::Unretained(this),
251 EXPECT_DOUBLE_EQ(10, collector
->UpdatePowerConsumptionForTesting());
253 // profile() should have an entry for |url| but not |hidden_url|.
254 OriginPowerMap
* origin_power_map_first
=
255 OriginPowerMapFactory::GetForBrowserContext(profile());
256 EXPECT_EQ(100, origin_power_map_first
->GetPowerForOrigin(url
));
257 EXPECT_EQ(0, origin_power_map_first
->GetPowerForOrigin(hidden_url
));
259 // |other_profile| should have an entry for |hidden_url| but not |url|.
260 OriginPowerMap
* origin_power_map_second
=
261 OriginPowerMapFactory::GetForBrowserContext(other_profile
.get());
262 EXPECT_EQ(0, origin_power_map_second
->GetPowerForOrigin(url
));
263 EXPECT_EQ(100, origin_power_map_second
->GetPowerForOrigin(hidden_url
));
266 chrome::CloseTab(other_user
.get());
269 TEST_F(BrowserProcessPowerTest
, AppsRecordPowerUsage
) {
270 // Install an app (an extension*).
272 base::FilePath
extension_path(FILE_PATH_LITERAL("c:\\foo"));
273 #elif defined(OS_POSIX)
274 base::FilePath
extension_path(FILE_PATH_LITERAL("/foo"));
276 base::DictionaryValue manifest
;
277 manifest
.SetString("name", "Fake Name");
278 manifest
.SetString("version", "1");
280 char kTestAppId
[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
281 scoped_refptr
<extensions::Extension
> extension(
282 extensions::Extension::Create(extension_path
,
283 extensions::Manifest::INTERNAL
,
285 extensions::Extension::NO_FLAGS
,
288 EXPECT_TRUE(extension
.get()) << error
;
289 // Increment the apps count to avoid a DCHECK later.
290 extensions::AppsClient::Get()->IncrementKeepAliveCount();
292 Profile
* current_profile
=
293 profile_manager_
->CreateTestingProfile("Test user");
294 GURL
url("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
295 extensions::AppWindow
* window
= new extensions::AppWindow(
296 current_profile
, new ChromeAppDelegate(), extension
.get());
297 content::WebContents
* web_contents(
298 content::WebContents::Create(content::WebContents::CreateParams(
300 content::SiteInstance::CreateForURL(current_profile
, url
))));
301 window
->SetAppWindowContentsForTesting(
302 scoped_ptr
<extensions::AppWindowContents
>(
303 new TestAppWindowContents(web_contents
)));
304 extensions::AppWindowRegistry
* app_registry
=
305 extensions::AppWindowRegistry::Get(current_profile
);
306 app_registry
->AddAppWindow(window
);
308 collector
->set_cpu_usage_callback_for_testing(
309 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant
,
310 base::Unretained(this),
312 collector
->UpdatePowerConsumptionForTesting();
313 EXPECT_EQ(1u, collector
->metrics_map_for_testing()->size());
315 // Clear the AppWindowContents before trying to close.
316 window
->SetAppWindowContentsForTesting(
317 scoped_ptr
<extensions::AppWindowContents
>());
318 window
->OnNativeClose();
319 collector
->UpdatePowerConsumptionForTesting();
320 EXPECT_EQ(0u, collector
->metrics_map_for_testing()->size());