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