Chromium Code Reviews| 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" | |
| 35 #include "content/public/test/test_utils.h" | 42 #include "content/public/test/test_utils.h" |
| 36 | 43 |
| 44 #if defined(OS_MACOSX) | |
| 45 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 46 #endif | |
| 47 | |
| 37 using extensions::Extension; | 48 using extensions::Extension; |
| 38 using performance_monitor::Event; | 49 using performance_monitor::Event; |
| 39 | 50 |
| 40 namespace { | 51 namespace { |
|
Yoyo Zhou
2012/08/02 11:33:34
nit: newline after
Devlin
2012/08/07 22:51:22
Done.
| |
| 52 const base::TimeDelta kMaxStartupTime = base::TimeDelta::FromMinutes(3); | |
| 53 | |
| 41 // Helper struct to store the information of an extension; this is needed if the | 54 // Helper struct to store the information of an extension; this is needed if the |
| 42 // pointer to the extension ever becomes invalid (e.g., if we uninstall the | 55 // pointer to the extension ever becomes invalid (e.g., if we uninstall the |
| 43 // extension). | 56 // extension). |
| 44 struct ExtensionBasicInfo { | 57 struct ExtensionBasicInfo { |
| 45 // Empty constructor for stl-container-happiness. | 58 // Empty constructor for stl-container-happiness. |
| 46 ExtensionBasicInfo() { | 59 ExtensionBasicInfo() { |
| 47 } | 60 } |
| 48 explicit ExtensionBasicInfo(const Extension* extension) | 61 explicit ExtensionBasicInfo(const Extension* extension) |
| 49 : description(extension->description()), | 62 : description(extension->description()), |
| 50 id(extension->id()), | 63 id(extension->id()), |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 Database::kDatabaseSequenceToken, | 185 Database::kDatabaseSequenceToken, |
| 173 FROM_HERE, | 186 FROM_HERE, |
| 174 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, | 187 base::Bind(&PerformanceMonitorBrowserTest::GetEventsOnBackgroundThread, |
| 175 base::Unretained(this), | 188 base::Unretained(this), |
| 176 &events)); | 189 &events)); |
| 177 | 190 |
| 178 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 191 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 179 return events; | 192 return events; |
| 180 } | 193 } |
| 181 | 194 |
| 195 void GetStatsOnBackgroundThread(Database::MetricInfoVector* metrics, | |
| 196 MetricType type) { | |
| 197 *metrics = performance_monitor_->database()->GetStatsForActivityAndMetric( | |
| 198 type, base::Time(), base::Time::FromInternalValue(kint64max)); | |
| 199 } | |
| 200 | |
| 201 // A handle for getting statistics from the database (see previous comments on | |
| 202 // GetEvents() and GetEventsOnBackgroundThread). | |
| 203 Database::MetricInfoVector GetStats(MetricType type) { | |
| 204 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 205 content::RunAllPendingInMessageLoop(); | |
| 206 | |
| 207 Database::MetricInfoVector metrics; | |
| 208 content::BrowserThread::PostBlockingPoolSequencedTask( | |
| 209 Database::kDatabaseSequenceToken, | |
| 210 FROM_HERE, | |
| 211 base::Bind(&PerformanceMonitorBrowserTest::GetStatsOnBackgroundThread, | |
| 212 base::Unretained(this), | |
| 213 &metrics, | |
| 214 type)); | |
| 215 | |
| 216 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
| 217 return metrics; | |
| 218 } | |
| 219 | |
| 182 // A handle for inserting a state value into the database, which must be done | 220 // A handle for inserting a state value into the database, which must be done |
| 183 // on the background thread. This is useful for mocking up a scenario in which | 221 // on the background thread. This is useful for mocking up a scenario in which |
| 184 // the database has prior data stored. We mock synchronicity with | 222 // the database has prior data stored. We mock synchronicity with |
| 185 // FlushForTesting(). | 223 // FlushForTesting(). |
| 186 void AddStateValue(const std::string& key, const std::string& value) { | 224 void AddStateValue(const std::string& key, const std::string& value) { |
| 187 content::BrowserThread::PostBlockingPoolSequencedTask( | 225 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 188 Database::kDatabaseSequenceToken, | 226 Database::kDatabaseSequenceToken, |
| 189 FROM_HERE, | 227 FROM_HERE, |
| 190 base::Bind(base::IgnoreResult(&Database::AddStateValue), | 228 base::Bind(base::IgnoreResult(&Database::AddStateValue), |
| 191 base::Unretained(performance_monitor()->database()), | 229 base::Unretained(performance_monitor()->database()), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 CHECK(file_util::PathExists(second_profile_prefs_file)); | 294 CHECK(file_util::PathExists(second_profile_prefs_file)); |
| 257 | 295 |
| 258 return true; | 296 return true; |
| 259 } | 297 } |
| 260 | 298 |
| 261 protected: | 299 protected: |
| 262 std::string first_profile_name_; | 300 std::string first_profile_name_; |
| 263 std::string second_profile_name_; | 301 std::string second_profile_name_; |
| 264 }; | 302 }; |
| 265 | 303 |
| 304 class PerformanceMonitorSessionRestoreBrowserTest | |
| 305 : public PerformanceMonitorBrowserTest { | |
| 306 public: | |
| 307 virtual void SetUpOnMainThread() OVERRIDE { | |
| 308 SessionStartupPref pref(SessionStartupPref::LAST); | |
| 309 SessionStartupPref::SetStartupPref(browser()->profile(), pref); | |
| 310 #if defined(OS_CHROMEOS) || defined (OS_MACOSX) | |
| 311 // Undo the effect of kBrowserAliveWithNoWindows in defaults.cc so that we | |
| 312 // can get these test to work without quitting. | |
| 313 SessionServiceFactory::GetForProfile(browser()->profile())-> | |
| 314 force_browser_not_alive_with_no_windows_ = true; | |
| 315 #endif | |
| 316 | |
| 317 PerformanceMonitorBrowserTest::SetUpOnMainThread(); | |
| 318 } | |
| 319 | |
| 320 Browser* QuitBrowserAndRestore(Browser* browser, int expected_tab_count) { | |
| 321 Profile* profile = browser->profile(); | |
| 322 | |
| 323 // Close the browser. | |
| 324 g_browser_process->AddRefModule(); | |
| 325 content::WindowedNotificationObserver observer( | |
| 326 chrome::NOTIFICATION_BROWSER_CLOSED, | |
| 327 content::NotificationService::AllSources()); | |
| 328 browser->window()->Close(); | |
| 329 #if defined(OS_MACOSX) | |
| 330 // BrowserWindowController depends on the auto release pool being recycled | |
| 331 // in the message loop to delete itself, which frees the Browser object | |
| 332 // which fires this event. | |
| 333 AutoreleasePool()->Recycle(); | |
| 334 #endif | |
| 335 observer.Wait(); | |
| 336 | |
| 337 // Create a new window, which should trigger session restore. | |
| 338 ui_test_utils::BrowserAddedObserver window_observer; | |
| 339 content::TestNavigationObserver navigation_observer( | |
| 340 content::NotificationService::AllSources(), NULL, expected_tab_count); | |
| 341 chrome::NewEmptyWindow(profile); | |
| 342 Browser* new_browser = window_observer.WaitForSingleNewBrowser(); | |
| 343 navigation_observer.Wait(); | |
| 344 g_browser_process->ReleaseModule(); | |
| 345 | |
| 346 return new_browser; | |
| 347 } | |
| 348 }; | |
| 349 | |
| 266 // Test that PerformanceMonitor will correctly record an extension installation | 350 // Test that PerformanceMonitor will correctly record an extension installation |
| 267 // event. | 351 // event. |
| 268 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, InstallExtensionEvent) { | 352 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, InstallExtensionEvent) { |
| 269 FilePath extension_path; | 353 FilePath extension_path; |
| 270 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); | 354 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); |
| 271 extension_path = extension_path.AppendASCII("performance_monitor") | 355 extension_path = extension_path.AppendASCII("performance_monitor") |
| 272 .AppendASCII("extensions") | 356 .AppendASCII("extensions") |
| 273 .AppendASCII("simple_extension_v1"); | 357 .AppendASCII("simple_extension_v1"); |
| 274 const Extension* extension = LoadExtension(extension_path); | 358 const Extension* extension = LoadExtension(extension_path); |
| 275 | 359 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 543 CheckEventType(EVENT_UNCLEAN_EXIT, events[1]); | 627 CheckEventType(EVENT_UNCLEAN_EXIT, events[1]); |
| 544 | 628 |
| 545 std::string event_profile; | 629 std::string event_profile; |
| 546 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); | 630 ASSERT_TRUE(events[0]->data()->GetString("profileName", &event_profile)); |
| 547 ASSERT_EQ(first_profile_name_, event_profile); | 631 ASSERT_EQ(first_profile_name_, event_profile); |
| 548 | 632 |
| 549 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); | 633 ASSERT_TRUE(events[1]->data()->GetString("profileName", &event_profile)); |
| 550 ASSERT_EQ(second_profile_name_, event_profile); | 634 ASSERT_EQ(second_profile_name_, event_profile); |
| 551 } | 635 } |
| 552 | 636 |
| 637 IN_PROC_BROWSER_TEST_F(PerformanceMonitorBrowserTest, StartupTime) { | |
| 638 Database::MetricInfoVector metrics = GetStats(METRIC_TEST_STARTUP_TIME); | |
| 639 | |
| 640 ASSERT_EQ(1u, metrics.size()); | |
| 641 ASSERT_LT(metrics[0].value, kMaxStartupTime.ToInternalValue()); | |
| 642 } | |
| 643 | |
| 644 IN_PROC_BROWSER_TEST_F(PerformanceMonitorSessionRestoreBrowserTest, | |
| 645 StartupWithSessionRestore) { | |
| 646 ui_test_utils::NavigateToURL( | |
| 647 browser(), | |
| 648 ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), | |
| 649 FilePath(FILE_PATH_LITERAL("title1.html")))); | |
| 650 | |
| 651 QuitBrowserAndRestore(browser(), 1); | |
| 652 | |
| 653 Database::MetricInfoVector metrics = GetStats(METRIC_TEST_STARTUP_TIME); | |
| 654 ASSERT_EQ(1u, metrics.size()); | |
| 655 ASSERT_LT(metrics[0].value, kMaxStartupTime.ToInternalValue()); | |
| 656 | |
| 657 metrics = GetStats(METRIC_SESSION_RESTORE_TIME); | |
| 658 ASSERT_EQ(1u, metrics.size()); | |
| 659 ASSERT_LT(metrics[0].value, kMaxStartupTime.ToInternalValue()); | |
| 660 } | |
| 661 | |
| 553 } // namespace performance_monitor | 662 } // namespace performance_monitor |
| OLD | NEW |