OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/test/base/in_process_browser_test.h" | 5 #include "chrome/test/base/in_process_browser_test.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/performance_monitor/constants.h" | 14 #include "chrome/browser/performance_monitor/constants.h" |
15 #include "chrome/browser/performance_monitor/database.h" | 15 #include "chrome/browser/performance_monitor/database.h" |
16 #include "chrome/browser/performance_monitor/performance_monitor.h" | 16 #include "chrome/browser/performance_monitor/performance_monitor.h" |
17 #include "chrome/browser/extensions/extension_browsertest.h" | 17 #include "chrome/browser/extensions/extension_browsertest.h" |
18 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
19 #include "chrome/browser/extensions/unpacked_installer.h" | 19 #include "chrome/browser/extensions/unpacked_installer.h" |
20 #include "chrome/browser/prefs/session_startup_pref.h" | |
20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/profiles/profile_manager.h" | 22 #include "chrome/browser/profiles/profile_manager.h" |
23 #include "chrome/browser/sessions/session_restore.h" | |
24 #include "chrome/browser/sessions/session_service.h" | |
25 #include "chrome/browser/sessions/session_service_factory.h" | |
22 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
27 #include "chrome/browser/ui/browser_commands.h" | |
23 #include "chrome/browser/ui/browser_tabstrip.h" | 28 #include "chrome/browser/ui/browser_tabstrip.h" |
29 #include "chrome/browser/ui/browser_window.h" | |
24 #include "chrome/common/chrome_constants.h" | 30 #include "chrome/common/chrome_constants.h" |
25 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
26 #include "chrome/common/chrome_paths.h" | 32 #include "chrome/common/chrome_paths.h" |
27 #include "chrome/common/chrome_switches.h" | 33 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/chrome_version_info.h" | 34 #include "chrome/common/chrome_version_info.h" |
29 #include "chrome/common/extensions/extension.h" | 35 #include "chrome/common/extensions/extension.h" |
30 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
31 #include "chrome/test/base/ui_test_utils.h" | 37 #include "chrome/test/base/ui_test_utils.h" |
32 #include "content/public/browser/notification_registrar.h" | 38 #include "content/public/browser/notification_registrar.h" |
33 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
34 #include "content/public/test/browser_test_utils.h" | 40 #include "content/public/test/browser_test_utils.h" |
41 #include "content/public/test/test_navigation_observer.h" | |
42 | |
43 #if defined(OS_MACOSX) | |
44 #include "base/mac/scoped_nsautorelease_pool.h" | |
45 #endif | |
35 | 46 |
36 using extensions::Extension; | 47 using extensions::Extension; |
37 using performance_monitor::Event; | 48 using performance_monitor::Event; |
38 | 49 |
39 namespace { | 50 namespace { |
40 // Helper struct to store the information of an extension; this is needed if the | 51 // Helper struct to store the information of an extension; this is needed if the |
41 // pointer to the extension ever becomes invalid (e.g., if we uninstall the | 52 // pointer to the extension ever becomes invalid (e.g., if we uninstall the |
42 // extension). | 53 // extension). |
43 struct ExtensionBasicInfo { | 54 struct ExtensionBasicInfo { |
44 // Empty constructor for stl-container-happiness. | 55 // Empty constructor for stl-container-happiness. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 Database::kDatabaseSequenceToken, | 182 Database::kDatabaseSequenceToken, |
172 FROM_HERE, | 183 FROM_HERE, |
173 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, | 184 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, |
174 base::Unretained(this), | 185 base::Unretained(this), |
175 &events)); | 186 &events)); |
176 | 187 |
177 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 188 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
178 return events; | 189 return events; |
179 } | 190 } |
180 | 191 |
192 void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics, | |
193 MetricType type) { | |
194 *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric( | |
195 type, base::Time(), base::Time::FromInternalValue(kint64max)); | |
196 } | |
197 | |
198 // A handle for getting statistics from the database (see previous comments on | |
199 // GetEvents() and GetEventsOnBackgroundThread). | |
200 Database::MetricInfoVector GetStats(MetricType type) { | |
201 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
202 ui_test_utils::RunAllPendingInMessageLoop(); | |
203 | |
204 Database::MetricInfoVector metrics; | |
205 content::BrowserThread::PostBlockingPoolSequencedTask( | |
206 Database::kDatabaseSequenceToken, | |
207 FROM_HERE, | |
208 base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread, | |
209 base::Unretained(this), | |
210 &metrics, | |
211 type)); | |
212 | |
213 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
214 return metrics; | |
215 } | |
216 | |
181 // A handle for inserting a state value into the database, which must be done | 217 // A handle for inserting a state value into the database, which must be done |
182 // on the background thread. This is useful for mocking up a scenario in which | 218 // on the background thread. This is useful for mocking up a scenario in which |
183 // the database has prior data stored. We mock synchronicity with | 219 // the database has prior data stored. We mock synchronicity with |
184 // FlushForTesting(). | 220 // FlushForTesting(). |
185 void AddStateValue(const std::string& key, const std::string& value) { | 221 void AddStateValue(const std::string& key, const std::string& value) { |
186 content::BrowserThread::PostBlockingPoolSequencedTask( | 222 content::BrowserThread::PostBlockingPoolSequencedTask( |
187 Database::kDatabaseSequenceToken, | 223 Database::kDatabaseSequenceToken, |
188 FROM_HERE, | 224 FROM_HERE, |
189 base::Bind(base::IgnoreResult(&Database::AddStateValue), | 225 base::Bind(base::IgnoreResult(&Database::AddStateValue), |
190 base::Unretained(performance_monitor()->database()), | 226 base::Unretained(performance_monitor()->database()), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 CHECK(file_util::PathExists(second_profile_prefs_file)); | 291 CHECK(file_util::PathExists(second_profile_prefs_file)); |
256 | 292 |
257 return true; | 293 return true; |
258 } | 294 } |
259 | 295 |
260 protected: | 296 protected: |
261 std::string first_profile_name_; | 297 std::string first_profile_name_; |
262 std::string second_profile_name_; | 298 std::string second_profile_name_; |
263 }; | 299 }; |
264 | 300 |
301 class PerformanceMonitorSessionRestoreBrowserTest | |
302 : public PerformanceMonitorBrowserTest { | |
303 public: | |
304 virtual void SetUpOnMainThread() OVERRIDE { | |
305 SessionStartupPref pref(SessionStartupPref::LAST); | |
306 SessionStartupPref::SetStartupPref(browser()->profile(), pref); | |
307 #if defined(OS_CHROMEOS) || defined (OS_MACOSX) | |
308 // Undo the effect of kBrowserAliveWithNoWindows in defaults.cc so that we | |
309 // can get these test to work without quitting. | |
310 SessionServiceFactory::GetForProfile(browser()->profile())-> | |
311 force_browser_not_alive_with_no_windows_ = true; | |
312 #endif | |
313 | |
314 PerformanceMonitorBrowserTest::SetUpOnMainThread(); | |
315 } | |
316 | |
317 Browser* QuitBrowserAndRestore(Browser* browser, int expected_tab_count) { | |
318 Profile* profile = browser->profile(); | |
319 | |
320 // Close the browser. | |
321 g_browser_process->AddRefModule(); | |
322 content::WindowedNotificationObserver observer( | |
323 chrome::NOTIFICATION_BROWSER_CLOSED, | |
324 content::NotificationService::AllSources()); | |
325 browser->window()->Close(); | |
326 #if defined(OS_MACOSX) | |
327 // BrowserWindowController depends on the auto release pool being recycled | |
328 // in the message loop to delete itself, which frees the Browser object | |
329 // which fires this event. | |
330 AutoreleasePool()->Recycle(); | |
331 #endif | |
332 observer.Wait(); | |
333 | |
334 // Create a new window, which should trigger session restore. | |
335 ui_test_utils::BrowserAddedObserver window_observer; | |
336 content::TestNavigationObserver navigation_observer( | |
337 content::NotificationService::AllSources(), NULL, expected_tab_count); | |
338 chrome::NewEmptyWindow(profile); | |
339 Browser* new_browser = window_observer.WaitForSingleNewBrowser(); | |
340 navigation_observer.Wait(); | |
341 g_browser_process->ReleaseModule(); | |
342 | |
343 return new_browser; | |
344 } | |
345 }; | |
346 | |
265 // Test that PerformanceMonitor will correctly record an extension installation | 347 // Test that PerformanceMonitor will correctly record an extension installation |
266 // event. | 348 // event. |
267 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, InstallExtensionEvent) { | 349 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, InstallExtensionEvent) { |
268 FilePath extension_path; | 350 FilePath extension_path; |
269 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); | 351 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); |
270 extension_path = extension_path.AppendASCII("performance_monitor") | 352 extension_path = extension_path.AppendASCII("performance_monitor") |
271 .AppendASCII("extensions") | 353 .AppendASCII("extensions") |
272 .AppendASCII("simple_extension_v1"); | 354 .AppendASCII("simple_extension_v1"); |
273 const Extension* extension = LoadExtension(extension_path); | 355 const Extension* extension = LoadExtension(extension_path); |
274 | 356 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
542 CheckEventType(EVENT_UNCLEAN_EXIT, events[1]); | 624 CheckEventType(EVENT_UNCLEAN_EXIT, events[1]); |
543 | 625 |
544 std::string event_profile; | 626 std::string event_profile; |
545 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); | 627 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); |
546 ASSERT_EQ(first_profile_name_, event_profile); | 628 ASSERT_EQ(first_profile_name_, event_profile); |
547 | 629 |
548 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); | 630 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); |
549 ASSERT_EQ(second_profile_name_, event_profile); | 631 ASSERT_EQ(second_profile_name_, event_profile); |
550 } | 632 } |
551 | 633 |
634 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, StartupTime) { | |
635 // Since the test itself is limited to a timeout of 45 seconds, we should | |
636 // never report a startup time of a minute or more. | |
637 const base::TimeDelta kMaxStartupTime = base::TimeDelta::FromMinutes(1); | |
Yoyo Zhou
2012/07/31 09:57:32
Seems like you could put this in an anonymous name
Devlin
2012/07/31 16:43:40
Moved to anonymous namespace, but I'm not sure abo
| |
638 | |
639 Database::MetricInfoVector metrics = GetStats(METRIC_TEST_STARTUP_TIME); | |
640 | |
641 ASSERT_EQ(1u, metrics.size()); | |
642 ASSERT_LT(metrics[0].value, kMaxStartupTime.ToInternalValue()); | |
643 } | |
644 | |
645 IN_PROC_BROWSER_TEST_F(PerformanceMonitorSessionRestoreBrowserTest, | |
646 StartupWithSessionRestore) { | |
647 const base::TimeDelta kMaxStartupTime = base::TimeDelta::FromMinutes(1); | |
648 | |
649 ui_test_utils::NavigateToURL( | |
650 browser(), | |
651 ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), | |
652 FilePath(FILE_PATH_LITERAL("title1.html")))); | |
653 | |
654 QuitBrowserAndRestore(browser(), 1); | |
655 | |
656 Database::MetricInfoVector metrics = GetStats(METRIC_TEST_STARTUP_TIME); | |
657 ASSERT_EQ(1u, metrics.size()); | |
658 ASSERT_LT(metrics[0].value, kMaxStartupTime.ToInternalValue()); | |
659 | |
660 metrics = GetStats(METRIC_SESSION_RESTORE_TIME); | |
661 ASSERT_EQ(1u, metrics.size()); | |
662 ASSERT_LT(metrics[0].value, kMaxStartupTime.ToInternalValue()); | |
663 } | |
664 | |
552 } // namespace performance_monitor | 665 } // namespace performance_monitor |
OLD | NEW |