Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / power / process_power_collector_unittest.cc
blob063088ab243dc7edf4e4509dc90d842b7a9bb984
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/apps/scoped_keep_alive.h"
8 #include "chrome/browser/profiles/profile_manager.h"
9 #include "chrome/browser/ui/apps/chrome_app_delegate.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/browser_with_test_window_test.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile_manager.h"
15 #include "components/power/origin_power_map.h"
16 #include "components/power/origin_power_map_factory.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "content/public/test/mock_render_process_host.h"
20 #include "extensions/browser/app_window/app_window_client.h"
21 #include "extensions/browser/app_window/app_window_registry.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"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
28 #if defined(OS_CHROMEOS)
29 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
30 #endif
32 using power::OriginPowerMap;
33 using power::OriginPowerMapFactory;
35 class BrowserProcessPowerTest : public BrowserWithTestWindowTest {
36 public:
37 BrowserProcessPowerTest() {}
38 ~BrowserProcessPowerTest() override {}
40 void SetUp() override {
41 BrowserWithTestWindowTest::SetUp();
42 collector.reset(new ProcessPowerCollector);
44 #if defined(OS_CHROMEOS)
45 power_manager::PowerSupplyProperties prop;
46 prop.set_external_power(power_manager::PowerSupplyProperties::AC);
47 prop.set_battery_state(power_manager::PowerSupplyProperties::DISCHARGING);
48 prop.set_battery_percent(20.00);
49 prop.set_battery_discharge_rate(1);
50 collector->PowerChanged(prop);
51 #endif
53 profile_manager_.reset(
54 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
55 ASSERT_TRUE(profile_manager_->SetUp());
58 void TearDown() override {
59 collector.reset();
60 BrowserWithTestWindowTest::TearDown();
63 // Mocks out CPU usage for all processes as |value| percent.
64 double ReturnCpuAsConstant(double value, base::ProcessHandle handle) {
65 return value;
68 protected:
69 content::MockRenderProcessHost* GetProcess(Browser* browser) {
70 return static_cast<content::MockRenderProcessHost*>(
71 browser->tab_strip_model()
72 ->GetActiveWebContents()
73 ->GetRenderViewHost()
74 ->GetProcess());
77 scoped_ptr<base::ProcessHandle> MakeProcessHandle(int process_id) {
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
85 return proc_handle.Pass();
88 scoped_ptr<ProcessPowerCollector> collector;
89 scoped_ptr<TestingProfileManager> profile_manager_;
92 TEST_F(BrowserProcessPowerTest, NoSite) {
93 collector->UpdatePowerConsumptionForTesting();
94 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size());
97 TEST_F(BrowserProcessPowerTest, OneSite) {
98 GURL url("http://www.google.com");
99 AddTab(browser(), url);
100 collector->UpdatePowerConsumptionForTesting();
101 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
102 collector->metrics_map_for_testing();
103 EXPECT_EQ(1u, metrics_map->size());
105 // Create fake process numbers.
106 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
108 OriginPowerMap* origin_power_map =
109 OriginPowerMapFactory::GetForBrowserContext(profile());
110 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url));
112 collector->set_cpu_usage_callback_for_testing(
113 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant,
114 base::Unretained(this),
115 5));
116 EXPECT_DOUBLE_EQ(5, collector->UpdatePowerConsumptionForTesting());
117 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url));
120 TEST_F(BrowserProcessPowerTest, MultipleSites) {
121 Browser::CreateParams native_params(profile(),
122 chrome::HOST_DESKTOP_TYPE_NATIVE);
123 GURL url1("http://www.google.com");
124 GURL url2("http://www.example.com");
125 GURL url3("https://www.google.com");
126 scoped_ptr<Browser> browser2(
127 chrome::CreateBrowserWithTestWindowForParams(&native_params));
128 scoped_ptr<Browser> browser3(
129 chrome::CreateBrowserWithTestWindowForParams(&native_params));
130 AddTab(browser(), url1);
131 AddTab(browser2.get(), url2);
132 AddTab(browser3.get(), url3);
134 // Create fake process numbers.
135 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
136 GetProcess(browser2.get())->SetProcessHandle(MakeProcessHandle(2).Pass());
137 GetProcess(browser3.get())->SetProcessHandle(MakeProcessHandle(3).Pass());
139 collector->UpdatePowerConsumptionForTesting();
140 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
141 collector->metrics_map_for_testing();
142 EXPECT_EQ(3u, metrics_map->size());
144 // Since all handlers are uninitialized, this should be 0.
145 EXPECT_DOUBLE_EQ(0, collector->UpdatePowerConsumptionForTesting());
146 OriginPowerMap* origin_power_map =
147 OriginPowerMapFactory::GetForBrowserContext(profile());
148 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url1));
149 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url2));
150 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url3));
152 collector->set_cpu_usage_callback_for_testing(
153 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant,
154 base::Unretained(this),
155 5));
156 EXPECT_DOUBLE_EQ(15, collector->UpdatePowerConsumptionForTesting());
157 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url1));
158 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url2));
159 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url3));
161 // Close some tabs and verify that they are removed from the metrics map.
162 chrome::CloseTab(browser2.get());
163 chrome::CloseTab(browser3.get());
165 collector->UpdatePowerConsumptionForTesting();
166 EXPECT_EQ(1u, metrics_map->size());
169 TEST_F(BrowserProcessPowerTest, IncognitoDoesntRecordPowerUsage) {
170 Browser::CreateParams native_params(profile()->GetOffTheRecordProfile(),
171 chrome::HOST_DESKTOP_TYPE_NATIVE);
172 scoped_ptr<Browser> incognito_browser(
173 chrome::CreateBrowserWithTestWindowForParams(&native_params));
174 GURL url("http://www.google.com");
175 AddTab(browser(), url);
177 GURL hidden_url("http://foo.com");
178 AddTab(incognito_browser.get(), hidden_url);
180 // Create fake process numbers.
181 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
182 GetProcess(incognito_browser.get())
183 ->SetProcessHandle(MakeProcessHandle(2).Pass());
185 EXPECT_DOUBLE_EQ(0, collector->UpdatePowerConsumptionForTesting());
186 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
187 collector->metrics_map_for_testing();
188 EXPECT_EQ(1u, metrics_map->size());
190 OriginPowerMap* origin_power_map =
191 OriginPowerMapFactory::GetForBrowserContext(profile());
192 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url));
194 collector->set_cpu_usage_callback_for_testing(
195 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant,
196 base::Unretained(this),
197 5));
198 EXPECT_DOUBLE_EQ(5, collector->UpdatePowerConsumptionForTesting());
200 // Verify that the incognito data was not stored.
201 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url));
202 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(hidden_url));
204 chrome::CloseTab(incognito_browser.get());
207 TEST_F(BrowserProcessPowerTest, MultipleProfilesRecordSeparately) {
208 scoped_ptr<Profile> other_profile(CreateProfile());
209 Browser::CreateParams native_params(other_profile.get(),
210 chrome::HOST_DESKTOP_TYPE_NATIVE);
211 scoped_ptr<Browser> other_user(
212 chrome::CreateBrowserWithTestWindowForParams(&native_params));
214 GURL url("http://www.google.com");
215 AddTab(browser(), url);
217 GURL hidden_url("http://foo.com");
218 AddTab(other_user.get(), hidden_url);
220 // Create fake process numbers.
221 GetProcess(browser())->SetProcessHandle(MakeProcessHandle(1).Pass());
222 GetProcess(other_user.get())->SetProcessHandle(MakeProcessHandle(2).Pass());
224 EXPECT_DOUBLE_EQ(0, collector->UpdatePowerConsumptionForTesting());
225 EXPECT_EQ(2u, collector->metrics_map_for_testing()->size());
227 collector->set_cpu_usage_callback_for_testing(
228 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant,
229 base::Unretained(this),
230 5));
231 EXPECT_DOUBLE_EQ(10, collector->UpdatePowerConsumptionForTesting());
233 // profile() should have an entry for |url| but not |hidden_url|.
234 OriginPowerMap* origin_power_map_first =
235 OriginPowerMapFactory::GetForBrowserContext(profile());
236 EXPECT_EQ(100, origin_power_map_first->GetPowerForOrigin(url));
237 EXPECT_EQ(0, origin_power_map_first->GetPowerForOrigin(hidden_url));
239 // |other_profile| should have an entry for |hidden_url| but not |url|.
240 OriginPowerMap* origin_power_map_second =
241 OriginPowerMapFactory::GetForBrowserContext(other_profile.get());
242 EXPECT_EQ(0, origin_power_map_second->GetPowerForOrigin(url));
243 EXPECT_EQ(100, origin_power_map_second->GetPowerForOrigin(hidden_url));
245 // Clean up
246 chrome::CloseTab(other_user.get());
249 TEST_F(BrowserProcessPowerTest, AppsRecordPowerUsage) {
250 // Install an app (an extension*).
251 #if defined(OS_WIN)
252 base::FilePath extension_path(FILE_PATH_LITERAL("c:\\foo"));
253 #elif defined(OS_POSIX)
254 base::FilePath extension_path(FILE_PATH_LITERAL("/foo"));
255 #endif
256 base::DictionaryValue manifest;
257 manifest.SetString("name", "Fake Name");
258 manifest.SetString("version", "1");
259 std::string error;
260 char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
261 scoped_refptr<extensions::Extension> extension(
262 extensions::Extension::Create(extension_path,
263 extensions::Manifest::INTERNAL,
264 manifest,
265 extensions::Extension::NO_FLAGS,
266 kTestAppId,
267 &error));
268 EXPECT_TRUE(extension.get()) << error;
270 Profile* current_profile =
271 profile_manager_->CreateTestingProfile("Test user");
272 GURL url("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
273 extensions::AppWindow* window = new extensions::AppWindow(
274 current_profile, new ChromeAppDelegate(scoped_ptr<ScopedKeepAlive>()),
275 extension.get());
276 content::WebContents* web_contents(
277 content::WebContents::Create(content::WebContents::CreateParams(
278 current_profile,
279 content::SiteInstance::CreateForURL(current_profile, url))));
280 window->SetAppWindowContentsForTesting(
281 scoped_ptr<extensions::AppWindowContents>(
282 new extensions::TestAppWindowContents(web_contents)));
283 extensions::AppWindowRegistry* app_registry =
284 extensions::AppWindowRegistry::Get(current_profile);
285 app_registry->AddAppWindow(window);
287 collector->set_cpu_usage_callback_for_testing(
288 base::Bind(&BrowserProcessPowerTest::ReturnCpuAsConstant,
289 base::Unretained(this),
290 5));
291 collector->UpdatePowerConsumptionForTesting();
292 EXPECT_EQ(1u, collector->metrics_map_for_testing()->size());
294 window->OnNativeClose();
295 collector->UpdatePowerConsumptionForTesting();
296 EXPECT_EQ(0u, collector->metrics_map_for_testing()->size());