Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: chrome/browser/power/process_power_collector_unittest.cc

Issue 472383002: Add ProcessPowerCollector to audit power information. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "apps/ui/native_app_window.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "chrome/browser/ui/apps/chrome_app_delegate.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/browser_with_test_window_test.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile_manager.h"
17 #include "components/power/origin_power_map.h"
18 #include "components/power/origin_power_map_factory.h"
19 #include "content/public/browser/site_instance.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "content/public/test/mock_render_process_host.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 "chrome/browser/chromeos/power/power_data_collector.h"
28 #include "chromeos/dbus/dbus_thread_manager.h"
29 #include "chromeos/dbus/fake_dbus_thread_manager.h"
30 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
31 #endif
32
33 using power::OriginPowerMap;
34 using power::OriginPowerMapFactory;
35
36 class BrowserProcessPowerTest : public BrowserWithTestWindowTest {
37 public:
38 BrowserProcessPowerTest() {}
39 virtual ~BrowserProcessPowerTest() {}
40
41 virtual void SetUp() OVERRIDE {
42 BrowserWithTestWindowTest::SetUp();
43 #if defined(OS_CHROMEOS)
44 chromeos::DBusThreadManager::Shutdown();
45 chromeos::FakeDBusThreadManager* fake_dbus_thread_manager =
46 new chromeos::FakeDBusThreadManager;
47 fake_dbus_thread_manager->SetFakeClients();
48 chromeos::DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
49 chromeos::PowerDataCollector::InitializeForTesting();
50
51 power_manager::PowerSupplyProperties prop1;
52 prop1.set_external_power(
53 power_manager::PowerSupplyProperties::DISCONNECTED);
54 prop1.set_battery_percent(20.00);
55 prop1.set_battery_discharge_rate(1);
56 chromeos::PowerDataCollector::Get()->PowerChanged(prop1);
57 #endif
58
59 profile_manager_.reset(
60 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
61 ASSERT_TRUE(profile_manager_->SetUp());
62 }
63
64 virtual void TearDown() OVERRIDE {
65 #if defined(OS_CHROMEOS)
66 chromeos::PowerDataCollector::Shutdown();
67 #endif
68 BrowserWithTestWindowTest::TearDown();
69 }
70
71 protected:
72 scoped_ptr<TestingProfileManager> profile_manager_;
73
74 content::MockRenderProcessHost* process(Browser* browser) {
75 return static_cast<content::MockRenderProcessHost*>(
76 browser->tab_strip_model()
77 ->GetActiveWebContents()
78 ->GetRenderViewHost()
79 ->GetProcess());
80 }
81
82 scoped_ptr<base::ProcessHandle> MakeProcessHandle(int process_id) {
83 scoped_ptr<base::ProcessHandle> proc_handle(new base::ProcessHandle(
84 #if defined(OS_WIN)
85 reinterpret_cast<HANDLE>(process_id))
86 #else
87 process_id)
88 #endif
89 );
90 return proc_handle.Pass();
91 }
92 };
93
94 class TestAppWindowContents : public apps::AppWindowContents {
95 public:
96 explicit TestAppWindowContents(content::WebContents* web_contents) {
97 web_contents_.reset(web_contents);
98 }
99 // apps:AppWindowContents
100 virtual void Initialize(content::BrowserContext* context,
101 const GURL& url) OVERRIDE{};
102 virtual void LoadContents(int32 creator_process_id) OVERRIDE{};
103 virtual void NativeWindowChanged(
104 apps::NativeAppWindow* native_app_window) OVERRIDE{};
105 virtual void NativeWindowClosed() OVERRIDE{};
106 virtual void DispatchWindowShownForTests() const OVERRIDE{};
107 virtual content::WebContents* GetWebContents() const OVERRIDE {
108 return web_contents_.get();
109 }
110
111 private:
112 scoped_ptr<content::WebContents> web_contents_;
113 };
114
115 TEST_F(BrowserProcessPowerTest, NoSite) {
116 ProcessPowerCollector collector;
117 collector.UpdateMetricsMap();
118 collector.PopulateCpuUsageByOrigin();
119 EXPECT_EQ(size_t(0), collector.metrics_map_for_testing()->size());
120 }
121
122 TEST_F(BrowserProcessPowerTest, OneSite) {
123 GURL url("http://www.google.com");
124 AddTab(browser(), url);
125 ProcessPowerCollector collector;
126 collector.UpdateMetricsMap();
127 collector.PopulateCpuUsageByOrigin();
128 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
129 collector.metrics_map_for_testing();
130 EXPECT_EQ(size_t(1), metrics_map->size());
131 collector.RecordCpuUsageByOrigin(0);
132
133 OriginPowerMap* origin_power_map =
134 OriginPowerMapFactory::GetForBrowserContext(profile());
135 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url));
136
137 collector.UpdateMetricsMap();
138 collector.PopulateCpuUsageByOrigin();
139 // Manually update the map to make the CPU usage work.
140 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
141 metrics_map->begin();
142 it != metrics_map->end();
143 ++it) {
144 it->second->SetLastCpuPercent(5);
145 }
146 collector.RecordCpuUsageByOrigin(5);
147 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url));
148 }
149
150 TEST_F(BrowserProcessPowerTest, MultipleSites) {
151 Browser::CreateParams native_params(profile(),
152 chrome::HOST_DESKTOP_TYPE_NATIVE);
153 GURL url1("http://www.google.com");
154 GURL url2("http://www.example.com");
155 GURL url3("https://www.google.com");
156 scoped_ptr<Browser> browser2(
157 chrome::CreateBrowserWithTestWindowForParams(&native_params));
158 scoped_ptr<Browser> browser3(
159 chrome::CreateBrowserWithTestWindowForParams(&native_params));
160 AddTab(browser(), url1);
161 AddTab(browser2.get(), url2);
162 AddTab(browser3.get(), url3);
163
164 // Create fake process numbers.
165 content::MockRenderProcessHost* rph = process(browser());
166 rph->SetProcessHandle(MakeProcessHandle(1).Pass());
167 rph = process(browser2.get());
168 rph->SetProcessHandle(MakeProcessHandle(2).Pass());
169 rph = process(browser3.get());
170 rph->SetProcessHandle(MakeProcessHandle(3).Pass());
171
172 ProcessPowerCollector collector;
173 collector.UpdateMetricsMap();
174 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
175 collector.metrics_map_for_testing();
176 EXPECT_EQ(size_t(3), metrics_map->size());
177
178 // Since all handlers are uninitialized, this should be 0.
179 EXPECT_EQ(0, collector.PopulateCpuUsageByOrigin());
180 collector.RecordCpuUsageByOrigin(0);
181 OriginPowerMap* origin_power_map =
182 OriginPowerMapFactory::GetForBrowserContext(profile());
183 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url1));
184 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url2));
185 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url3));
186
187 collector.UpdateMetricsMap();
188 collector.PopulateCpuUsageByOrigin();
189 // Manually update the map to make the CPU usage work.
190 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
191 metrics_map->begin();
192 it != metrics_map->end();
193 ++it) {
194 it->second->SetLastCpuPercent(5);
195 }
196 collector.RecordCpuUsageByOrigin(15);
197 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url1));
198 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url2));
199 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url3));
200
201 // Close some tabs and verify that they are removed from the metrics map.
202 chrome::CloseTab(browser2.get());
203 chrome::CloseTab(browser3.get());
204
205 collector.UpdateMetricsMap();
206 collector.PopulateCpuUsageByOrigin();
207 collector.RecordCpuUsageByOrigin(0);
208 EXPECT_EQ(size_t(1), metrics_map->size());
209 }
210
211 TEST_F(BrowserProcessPowerTest, IncognitoDoesntRecordPowerUsage) {
212 Browser::CreateParams native_params(profile()->GetOffTheRecordProfile(),
213 chrome::HOST_DESKTOP_TYPE_NATIVE);
214 scoped_ptr<Browser> incognito_browser(
215 chrome::CreateBrowserWithTestWindowForParams(&native_params));
216 GURL url("http://www.google.com");
217 AddTab(browser(), url);
218
219 GURL hidden_url("http://foo.com");
220 AddTab(incognito_browser.get(), hidden_url);
221
222 ProcessPowerCollector collector;
223 collector.UpdateMetricsMap();
224 collector.PopulateCpuUsageByOrigin();
225 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
226 collector.metrics_map_for_testing();
227 EXPECT_EQ(size_t(1), metrics_map->size());
228 collector.RecordCpuUsageByOrigin(0);
229
230 OriginPowerMap* origin_power_map =
231 OriginPowerMapFactory::GetForBrowserContext(profile());
232 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url));
233
234 collector.UpdateMetricsMap();
235 collector.PopulateCpuUsageByOrigin();
236 // Manually update the map to make the CPU usage work.
237 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
238 metrics_map->begin();
239 it != metrics_map->end();
240 ++it) {
241 it->second->SetLastCpuPercent(5);
242 }
243 collector.RecordCpuUsageByOrigin(5);
244
245 // Verify that the incognito data was not stored.
246 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url));
247 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(hidden_url));
248
249 chrome::CloseTab(incognito_browser.get());
250 }
251
252 TEST_F(BrowserProcessPowerTest, MultipleProfilesRecordSeparately) {
253 scoped_ptr<Profile> other_profile(CreateProfile());
254 Browser::CreateParams native_params(other_profile.get(),
255 chrome::HOST_DESKTOP_TYPE_NATIVE);
256 scoped_ptr<Browser> other_user(
257 chrome::CreateBrowserWithTestWindowForParams(&native_params));
258
259 GURL url("http://www.google.com");
260 AddTab(browser(), url);
261
262 GURL hidden_url("http://foo.com");
263 AddTab(other_user.get(), hidden_url);
264
265 // Create fake process numbers.
266 content::MockRenderProcessHost* rph = process(browser());
267 rph->SetProcessHandle(MakeProcessHandle(1).Pass());
268 rph = process(other_user.get());
269 rph->SetProcessHandle(MakeProcessHandle(2).Pass());
270
271 ProcessPowerCollector collector;
272 collector.UpdateMetricsMap();
273 collector.PopulateCpuUsageByOrigin();
274
275 EXPECT_EQ(size_t(2), collector.metrics_map_for_testing()->size());
276 collector.RecordCpuUsageByOrigin(0);
277
278 // Manually update the map to make the CPU usage work.
279 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
280 collector.metrics_map_for_testing();
281 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
282 metrics_map->begin();
283 it != metrics_map->end();
284 ++it) {
285 it->second->SetLastCpuPercent(5);
286 }
287 collector.RecordCpuUsageByOrigin(5);
288
289 // profile() should have an entry for |url| but not |hidden_url|.
290 OriginPowerMap* origin_power_map_first =
291 OriginPowerMapFactory::GetForBrowserContext(profile());
292 EXPECT_EQ(100, origin_power_map_first->GetPowerForOrigin(url));
293 EXPECT_EQ(0, origin_power_map_first->GetPowerForOrigin(hidden_url));
294
295 // |other_profile| should have an entry for |hidden_url| but not |url|.
296 OriginPowerMap* origin_power_map_second =
297 OriginPowerMapFactory::GetForBrowserContext(other_profile.get());
298 EXPECT_EQ(0, origin_power_map_second->GetPowerForOrigin(url));
299 EXPECT_EQ(100, origin_power_map_second->GetPowerForOrigin(hidden_url));
300
301 // Clean up
302 chrome::CloseTab(other_user.get());
303 }
304
305 const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
306
307 TEST_F(BrowserProcessPowerTest, AppsRecordPowerUsage) {
308 Profile* current_profile =
309 profile_manager_->CreateTestingProfile("Test user");
310
311 // Install an app (an extension*).
312 #if defined(OS_WIN)
313 base::FilePath extension_path(FILE_PATH_LITERAL("c:\\foo"));
314 #elif defined(OS_POSIX)
315 base::FilePath extension_path(FILE_PATH_LITERAL("/foo"));
316 #endif
317 base::DictionaryValue manifest;
318 manifest.SetString("name", "Fake Name");
319 manifest.SetString("version", "1");
320 std::string error;
321 scoped_refptr<extensions::Extension> extension(
322 extensions::Extension::Create(extension_path,
323 extensions::Manifest::INTERNAL,
324 manifest,
325 extensions::Extension::NO_FLAGS,
326 kTestAppId,
327 &error));
328 EXPECT_TRUE(extension.get()) << error;
329
330 GURL url("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
331 // Use that to create a new AppWindow -- hopefully that can avoid
332 // dependencies.
333 apps::AppWindow* window =
334 new apps::AppWindow(current_profile, new ChromeAppDelegate(), extension);
335 scoped_ptr<content::WebContents> web_contents(
336 content::WebContents::Create(content::WebContents::CreateParams(
337 current_profile,
338 content::SiteInstance::CreateForURL(current_profile, url))));
339 window->SetAppWindowContentsForTesting(scoped_ptr<apps::AppWindowContents>(
340 new TestAppWindowContents(web_contents.get())));
341 apps::AppWindowRegistry* app_registry =
342 apps::AppWindowRegistry::Get(current_profile);
343 app_registry->AddAppWindow(window);
344
345 ProcessPowerCollector collector;
346 collector.UpdateMetricsMap();
347 collector.PopulateCpuUsageByOrigin();
348 EXPECT_EQ(size_t(1), collector.metrics_map_for_testing()->size());
349 collector.RecordCpuUsageByOrigin(0);
350
351 app_registry->RemoveAppWindow(window);
352 collector.UpdateMetricsMap();
353 collector.PopulateCpuUsageByOrigin();
354 EXPECT_EQ(size_t(0), collector.metrics_map_for_testing()->size());
355 collector.RecordCpuUsageByOrigin(0);
356 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698