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

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: Comments and fixing some memory issues. 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
83 class TestAppWindowContents : public apps::AppWindowContents {
84 public:
85 explicit TestAppWindowContents(content::WebContents* web_contents) {
86 web_contents_.reset(web_contents);
87 }
88 // apps:AppWindowContents
89 virtual void Initialize(content::BrowserContext* context,
90 const GURL& url) OVERRIDE{};
91 virtual void LoadContents(int32 creator_process_id) OVERRIDE{};
92 virtual void NativeWindowChanged(
93 apps::NativeAppWindow* native_app_window) OVERRIDE{};
94 virtual void NativeWindowClosed() OVERRIDE{};
95 virtual void DispatchWindowShownForTests() const OVERRIDE{};
96 virtual content::WebContents* GetWebContents() const OVERRIDE {
97 return web_contents_.get();
98 }
99
100 private:
101 scoped_ptr<content::WebContents> web_contents_;
102 };
103
104 TEST_F(BrowserProcessPowerTest, NoSite) {
105 ProcessPowerCollector collector;
106 collector.UpdateMetricsMap();
107 collector.PopulateCpuUsageByOrigin();
108 EXPECT_EQ(size_t(0), collector.GetMetricsMapForTesting()->size());
109 }
110
111 TEST_F(BrowserProcessPowerTest, OneSite) {
112 GURL url("http://www.google.com");
113 AddTab(browser(), url);
114 ProcessPowerCollector collector;
115 collector.UpdateMetricsMap();
116 collector.PopulateCpuUsageByOrigin();
117 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
118 collector.GetMetricsMapForTesting();
119 EXPECT_EQ(size_t(1), metrics_map->size());
120 collector.RecordCpuUsageByOrigin(0);
121
122 OriginPowerMap* origin_power_map =
123 OriginPowerMapFactory::GetForBrowserContext(profile());
124 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url));
125
126 collector.UpdateMetricsMap();
127 collector.PopulateCpuUsageByOrigin();
128 // Manually update the map to make the CPU usage work.
129 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
130 metrics_map->begin();
131 it != metrics_map->end();
132 ++it) {
133 it->second->last_cpu = 5;
134 }
135 collector.RecordCpuUsageByOrigin(5);
136 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url));
137 }
138
139 TEST_F(BrowserProcessPowerTest, MultipleSites) {
140 Browser::CreateParams native_params(profile(),
141 chrome::HOST_DESKTOP_TYPE_NATIVE);
142 GURL url1("http://www.google.com");
143 GURL url2("http://www.example.com");
144 GURL url3("https://www.google.com");
145 scoped_ptr<Browser> browser2(
146 chrome::CreateBrowserWithTestWindowForParams(&native_params));
147 scoped_ptr<Browser> browser3(
148 chrome::CreateBrowserWithTestWindowForParams(&native_params));
149 AddTab(browser(), url1);
150 AddTab(browser2.get(), url2);
151 AddTab(browser3.get(), url3);
152
153 // Create fake process numbers.
154 content::MockRenderProcessHost* rph = process(browser());
155 rph->SetProcessHandle(1);
156 rph = process(browser2.get());
157 rph->SetProcessHandle(2);
158 rph = process(browser3.get());
159 rph->SetProcessHandle(3);
160
161 ProcessPowerCollector collector;
162 collector.UpdateMetricsMap();
163 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
164 collector.GetMetricsMapForTesting();
165 EXPECT_EQ(size_t(3), metrics_map->size());
166
167 // Since all handlers are uninitialized, this should be 0.
168 EXPECT_EQ(0, collector.PopulateCpuUsageByOrigin());
169 collector.RecordCpuUsageByOrigin(0);
170 OriginPowerMap* origin_power_map =
171 OriginPowerMapFactory::GetForBrowserContext(profile());
172 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url1));
173 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url2));
174 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url3));
175
176 collector.UpdateMetricsMap();
177 collector.PopulateCpuUsageByOrigin();
178 // Manually update the map to make the CPU usage work.
179 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
180 metrics_map->begin();
181 it != metrics_map->end();
182 ++it) {
183 it->second->last_cpu = 5;
184 }
185 collector.RecordCpuUsageByOrigin(15);
186 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url1));
187 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url2));
188 EXPECT_EQ(33, origin_power_map->GetPowerForOrigin(url3));
189
190 // Close some tabs and verify that they are removed from the metrics map.
191 chrome::CloseTab(browser2.get());
192 chrome::CloseTab(browser3.get());
193
194 collector.UpdateMetricsMap();
195 collector.PopulateCpuUsageByOrigin();
196 collector.RecordCpuUsageByOrigin(0);
197 EXPECT_EQ(size_t(1), metrics_map->size());
198 }
199
200 TEST_F(BrowserProcessPowerTest, IncognitoDoesntRecordPowerUsage) {
201 Browser::CreateParams native_params(profile()->GetOffTheRecordProfile(),
202 chrome::HOST_DESKTOP_TYPE_NATIVE);
203 scoped_ptr<Browser> incognito_browser(
204 chrome::CreateBrowserWithTestWindowForParams(&native_params));
205 GURL url("http://www.google.com");
206 AddTab(browser(), url);
207
208 GURL hidden_url("http://foo.com");
209 AddTab(incognito_browser.get(), hidden_url);
210
211 ProcessPowerCollector collector;
212 collector.UpdateMetricsMap();
213 collector.PopulateCpuUsageByOrigin();
214 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
215 collector.GetMetricsMapForTesting();
216 EXPECT_EQ(size_t(1), metrics_map->size());
217 collector.RecordCpuUsageByOrigin(0);
218
219 OriginPowerMap* origin_power_map =
220 OriginPowerMapFactory::GetForBrowserContext(profile());
221 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(url));
222
223 collector.UpdateMetricsMap();
224 collector.PopulateCpuUsageByOrigin();
225 // Manually update the map to make the CPU usage work.
226 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
227 metrics_map->begin();
228 it != metrics_map->end();
229 ++it) {
230 it->second->last_cpu = 5;
231 }
232 collector.RecordCpuUsageByOrigin(5);
233
234 // Verify that the incognito data was not stored.
235 EXPECT_EQ(100, origin_power_map->GetPowerForOrigin(url));
236 EXPECT_EQ(0, origin_power_map->GetPowerForOrigin(hidden_url));
237
238 chrome::CloseTab(incognito_browser.get());
239 }
240
241 TEST_F(BrowserProcessPowerTest, MultipleProfilesRecordSeparately) {
242 scoped_ptr<Profile> other_profile(CreateProfile());
243 Browser::CreateParams native_params(other_profile.get(),
244 chrome::HOST_DESKTOP_TYPE_NATIVE);
245 scoped_ptr<Browser> other_user(
246 chrome::CreateBrowserWithTestWindowForParams(&native_params));
247
248 GURL url("http://www.google.com");
249 AddTab(browser(), url);
250
251 GURL hidden_url("http://foo.com");
252 AddTab(other_user.get(), hidden_url);
253
254 // Create fake process numbers.
255 content::MockRenderProcessHost* rph = process(browser());
256 rph->SetProcessHandle(1);
257 rph = process(other_user.get());
258 rph->SetProcessHandle(2);
259
260 ProcessPowerCollector collector;
261 collector.UpdateMetricsMap();
262 collector.PopulateCpuUsageByOrigin();
263
264 EXPECT_EQ(size_t(2), collector.GetMetricsMapForTesting()->size());
265 collector.RecordCpuUsageByOrigin(0);
266
267 // Manually update the map to make the CPU usage work.
268 ProcessPowerCollector::ProcessMetricsMap* metrics_map =
269 collector.GetMetricsMapForTesting();
270 for (ProcessPowerCollector::ProcessMetricsMap::iterator it =
271 metrics_map->begin();
272 it != metrics_map->end();
273 ++it) {
274 it->second->last_cpu = 5;
275 }
276 collector.RecordCpuUsageByOrigin(5);
277
278 // profile() should have an entry for |url| but not |hidden_url|.
279 OriginPowerMap* origin_power_map_first =
280 OriginPowerMapFactory::GetForBrowserContext(profile());
281 EXPECT_EQ(100, origin_power_map_first->GetPowerForOrigin(url));
282 EXPECT_EQ(0, origin_power_map_first->GetPowerForOrigin(hidden_url));
283
284 // |other_profile| should have an entry for |hidden_url| but not |url|.
285 OriginPowerMap* origin_power_map_second =
286 OriginPowerMapFactory::GetForBrowserContext(other_profile.get());
287 EXPECT_EQ(0, origin_power_map_second->GetPowerForOrigin(url));
288 EXPECT_EQ(100, origin_power_map_second->GetPowerForOrigin(hidden_url));
289
290 // Clean up
291 chrome::CloseTab(other_user.get());
292 }
293
294 const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
295
296 TEST_F(BrowserProcessPowerTest, AppsRecordPowerUsage) {
297 Profile* current_profile =
298 profile_manager_->CreateTestingProfile("Test user");
299
300 // Install an app (an extension*).
301 #if defined(OS_WIN)
302 base::FilePath extension_path(FILE_PATH_LITERAL("c:\\foo"));
303 #elif defined(OS_POSIX)
304 base::FilePath extension_path(FILE_PATH_LITERAL("/foo"));
305 #endif
306 base::DictionaryValue manifest;
307 manifest.SetString("name", "Fake Name");
308 manifest.SetString("version", "1");
309 std::string error;
310 scoped_refptr<extensions::Extension> extension(
311 extensions::Extension::Create(extension_path,
312 extensions::Manifest::INTERNAL,
313 manifest,
314 extensions::Extension::NO_FLAGS,
315 kTestAppId,
316 &error));
317 EXPECT_TRUE(extension.get()) << error;
318
319 GURL url("chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
320 // Use that to create a new AppWindow -- hopefully that can avoid
321 // dependencies.
322 apps::AppWindow* window =
323 new apps::AppWindow(current_profile, new ChromeAppDelegate(), extension);
324 scoped_ptr<content::WebContents> web_contents(
325 content::WebContents::Create(content::WebContents::CreateParams(
326 current_profile,
327 content::SiteInstance::CreateForURL(current_profile, url))));
328 window->SetAppWindowContentsForTesting(scoped_ptr<apps::AppWindowContents>(
329 new TestAppWindowContents(web_contents.get())));
330 apps::AppWindowRegistry* app_registry =
331 apps::AppWindowRegistry::Get(current_profile);
332 app_registry->AddAppWindow(window);
333
334 ProcessPowerCollector collector;
335 collector.UpdateMetricsMap();
336 collector.PopulateCpuUsageByOrigin();
337 EXPECT_EQ(size_t(1), collector.GetMetricsMapForTesting()->size());
338 collector.RecordCpuUsageByOrigin(0);
339
340 app_registry->RemoveAppWindow(window);
341 collector.UpdateMetricsMap();
342 collector.PopulateCpuUsageByOrigin();
343 EXPECT_EQ(size_t(0), collector.GetMetricsMapForTesting()->size());
344 collector.RecordCpuUsageByOrigin(0);
345 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698