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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/cancelable_callback.h" | 6 #include "base/cancelable_callback.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
16 #include "chrome/browser/devtools/browser_list_tabcontents_provider.h" | 16 #include "chrome/browser/devtools/browser_list_tabcontents_provider.h" |
17 #include "chrome/browser/devtools/devtools_window.h" | 17 #include "chrome/browser/devtools/devtools_window.h" |
18 #include "chrome/browser/extensions/extension_apitest.h" | 18 #include "chrome/browser/extensions/extension_apitest.h" |
19 #include "chrome/browser/extensions/extension_browsertest.h" | 19 #include "chrome/browser/extensions/extension_browsertest.h" |
20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
21 #include "chrome/browser/extensions/extension_system.h" | 21 #include "chrome/browser/extensions/extension_system.h" |
22 #include "chrome/browser/extensions/unpacked_installer.h" | 22 #include "chrome/browser/extensions/unpacked_installer.h" |
| 23 #include "chrome/browser/lifetime/application_lifetime.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
| 26 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" |
24 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
25 #include "chrome/browser/ui/browser_commands.h" | 28 #include "chrome/browser/ui/browser_commands.h" |
26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
27 #include "chrome/common/chrome_paths.h" | 30 #include "chrome/common/chrome_paths.h" |
28 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
30 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" |
31 #include "chrome/test/base/in_process_browser_test.h" | 34 #include "chrome/test/base/in_process_browser_test.h" |
32 #include "chrome/test/base/test_switches.h" | 35 #include "chrome/test/base/test_switches.h" |
33 #include "chrome/test/base/ui_test_utils.h" | 36 #include "chrome/test/base/ui_test_utils.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 150 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
148 content::Source<content::WebContents>(window_->web_contents())); | 151 content::Source<content::WebContents>(window_->web_contents())); |
149 devtools_manager->CloseAllClientHosts(); | 152 devtools_manager->CloseAllClientHosts(); |
150 close_observer.Wait(); | 153 close_observer.Wait(); |
151 } | 154 } |
152 | 155 |
153 DevToolsWindow* window_; | 156 DevToolsWindow* window_; |
154 RenderViewHost* inspected_rvh_; | 157 RenderViewHost* inspected_rvh_; |
155 }; | 158 }; |
156 | 159 |
157 // Used to block until a dev tools window gets beforeunload event. | 160 class DevToolsBeforeUnloadTest: public DevToolsSanityTest { |
158 class DevToolsWindowBeforeUnloadObserver | 161 protected: |
159 : public content::WebContentsObserver { | 162 void CloseInspectedTab() { |
160 public: | 163 browser()->tab_strip_model()->CloseWebContentsAt(0, |
161 explicit DevToolsWindowBeforeUnloadObserver( | 164 TabStripModel::CLOSE_NONE); |
162 content::WebContents* web_contents); | 165 } |
163 bool Fired(); | |
164 private: | |
165 // Invoked when the beforeunload handler fires. | |
166 virtual void BeforeUnloadFired(const base::TimeTicks& proceed_time) OVERRIDE; | |
167 | 166 |
168 bool m_fired; | 167 void InjectBeforeUnloadListener(content::WebContents* web_contents) { |
| 168 ASSERT_TRUE(content::ExecuteScript(web_contents->GetRenderViewHost(), |
| 169 "window.addEventListener('beforeunload'," |
| 170 "function() { return 'Foo'; });")); |
| 171 } |
169 | 172 |
170 DISALLOW_COPY_AND_ASSIGN(DevToolsWindowBeforeUnloadObserver); | 173 DevToolsWindow* OpenDevToolWindowOnWebContents( |
| 174 content::WebContents* contents) { |
| 175 content::WindowedNotificationObserver observer( |
| 176 content::NOTIFICATION_LOAD_STOP, |
| 177 content::NotificationService::AllSources()); |
| 178 DevToolsWindow* window = DevToolsWindow::OpenDevToolsWindow( |
| 179 contents->GetRenderViewHost()); |
| 180 observer.Wait(); |
| 181 return window; |
| 182 } |
| 183 |
| 184 void AcceptModalDialog() { |
| 185 NativeAppModalDialog* native_dialog = GetDialog(); |
| 186 native_dialog->AcceptAppModalDialog(); |
| 187 } |
| 188 |
| 189 void CancelModalDialog() { |
| 190 NativeAppModalDialog* native_dialog = GetDialog(); |
| 191 native_dialog->CancelAppModalDialog(); |
| 192 } |
| 193 |
| 194 NativeAppModalDialog* GetDialog() { |
| 195 AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog(); |
| 196 EXPECT_TRUE(dialog->IsJavaScriptModalDialog()); |
| 197 JavaScriptAppModalDialog* js_dialog = |
| 198 static_cast<JavaScriptAppModalDialog*>(dialog); |
| 199 NativeAppModalDialog* native_dialog = js_dialog->native_dialog(); |
| 200 EXPECT_TRUE(native_dialog); |
| 201 return native_dialog; |
| 202 } |
171 }; | 203 }; |
172 | 204 |
173 DevToolsWindowBeforeUnloadObserver::DevToolsWindowBeforeUnloadObserver( | |
174 content::WebContents* web_contents) | |
175 : WebContentsObserver(web_contents), | |
176 m_fired(false) { | |
177 } | |
178 | |
179 bool DevToolsWindowBeforeUnloadObserver::Fired() { | |
180 return m_fired; | |
181 } | |
182 | |
183 void DevToolsWindowBeforeUnloadObserver::BeforeUnloadFired( | |
184 const base::TimeTicks& proceed_time) { | |
185 m_fired = true; | |
186 } | |
187 | |
188 void TimeoutCallback(const std::string& timeout_message) { | 205 void TimeoutCallback(const std::string& timeout_message) { |
189 FAIL() << timeout_message; | 206 FAIL() << timeout_message; |
190 base::MessageLoop::current()->Quit(); | 207 base::MessageLoop::current()->Quit(); |
191 } | 208 } |
192 | 209 |
193 // Base class for DevTools tests that test devtools functionality for | 210 // Base class for DevTools tests that test devtools functionality for |
194 // extensions and content scripts. | 211 // extensions and content scripts. |
195 class DevToolsExtensionTest : public DevToolsSanityTest, | 212 class DevToolsExtensionTest : public DevToolsSanityTest, |
196 public content::NotificationObserver { | 213 public content::NotificationObserver { |
197 public: | 214 public: |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 DevToolsWindow* window_; | 450 DevToolsWindow* window_; |
434 }; | 451 }; |
435 | 452 |
436 // Test beforeunload event delivery. | 453 // Test beforeunload event delivery. |
437 // Crashes on Win only. http://crbug.com/313658 | 454 // Crashes on Win only. http://crbug.com/313658 |
438 #if defined(OS_WIN) | 455 #if defined(OS_WIN) |
439 #define MAYBE_TestBeforeUnloadEvents DISABLED_TestBeforeUnloadEvents | 456 #define MAYBE_TestBeforeUnloadEvents DISABLED_TestBeforeUnloadEvents |
440 #else | 457 #else |
441 #define MAYBE_TestBeforeUnloadEvents TestBeforeUnloadEvents | 458 #define MAYBE_TestBeforeUnloadEvents TestBeforeUnloadEvents |
442 #endif | 459 #endif |
443 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, MAYBE_TestBeforeUnloadEvents) { | 460 IN_PROC_BROWSER_TEST_F(DevToolsBeforeUnloadTest, MAYBE_TestBeforeUnloadEvents) { |
444 OpenDevToolsWindow(kDebuggerTestPage); | 461 OpenDevToolsWindow(kDebuggerTestPage); |
445 scoped_ptr<DevToolsWindowBeforeUnloadObserver> contents_observer; | 462 InjectBeforeUnloadListener(window_->web_contents()); |
446 contents_observer.reset( | 463 content::WindowedNotificationObserver close_observer( |
447 new DevToolsWindowBeforeUnloadObserver(window_->web_contents())); | 464 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
448 ToggleDevToolsWindow(); | 465 content::Source<content::WebContents>(window_->web_contents())); |
449 ASSERT_TRUE(contents_observer->Fired()); | 466 DevToolsWindow::ToggleDevToolsWindow(inspected_rvh_, false, |
| 467 DevToolsToggleAction::Toggle()); |
| 468 AcceptModalDialog(); |
| 469 close_observer.Wait(); |
| 470 } |
| 471 |
| 472 // Test beforeunload event delivery. |
| 473 IN_PROC_BROWSER_TEST_F(DevToolsBeforeUnloadTest, |
| 474 TestDockedDevToolsInspectedTabClosing) { |
| 475 OpenDevToolsWindow(kDebuggerTestPage); |
| 476 content::WindowedNotificationObserver devtools_close_observer( |
| 477 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 478 content::Source<content::WebContents>(window_->web_contents())); |
| 479 InjectBeforeUnloadListener(window_->web_contents()); |
| 480 // try to close browser window |
| 481 { |
| 482 content::WindowedNotificationObserver cancel_browser( |
| 483 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 484 content::NotificationService::AllSources()); |
| 485 chrome::CloseWindow(browser()); |
| 486 CancelModalDialog(); |
| 487 cancel_browser.Wait(); |
| 488 } |
| 489 // try to close inspected tab |
| 490 { |
| 491 content::WindowedNotificationObserver close_observer( |
| 492 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 493 content::Source<Browser>(browser())); |
| 494 CloseInspectedTab(); |
| 495 AcceptModalDialog(); |
| 496 close_observer.Wait(); |
| 497 } |
| 498 devtools_close_observer.Wait(); |
| 499 } |
| 500 |
| 501 IN_PROC_BROWSER_TEST_F(DevToolsBeforeUnloadTest, |
| 502 TestUndockedDevToolsInspectedTabClosing) { |
| 503 OpenDevToolsWindow(kDebuggerTestPage); |
| 504 content::WindowedNotificationObserver devtools_close_observer( |
| 505 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 506 content::Source<content::WebContents>(window_->web_contents())); |
| 507 |
| 508 window_->SetDockSideForTest(DEVTOOLS_DOCK_SIDE_UNDOCKED); |
| 509 InjectBeforeUnloadListener(window_->web_contents()); |
| 510 // try to close browser window |
| 511 { |
| 512 content::WindowedNotificationObserver cancel_browser( |
| 513 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 514 content::NotificationService::AllSources()); |
| 515 chrome::CloseWindow(browser()); |
| 516 CancelModalDialog(); |
| 517 cancel_browser.Wait(); |
| 518 } |
| 519 // try to exit application |
| 520 { |
| 521 content::WindowedNotificationObserver cancel_browser( |
| 522 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 523 content::NotificationService::AllSources()); |
| 524 chrome::CloseAllBrowsers(); |
| 525 CancelModalDialog(); |
| 526 cancel_browser.Wait(); |
| 527 } |
| 528 // try to close inspected tab |
| 529 { |
| 530 content::WindowedNotificationObserver close_observer( |
| 531 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 532 content::Source<Browser>(browser())); |
| 533 CloseInspectedTab(); |
| 534 AcceptModalDialog(); |
| 535 close_observer.Wait(); |
| 536 } |
| 537 devtools_close_observer.Wait(); |
| 538 } |
| 539 |
| 540 IN_PROC_BROWSER_TEST_F(DevToolsBeforeUnloadTest, TestDevToolsOnDevTools) { |
| 541 ASSERT_TRUE(test_server()->Start()); |
| 542 GURL url = test_server()->GetURL(kDebuggerTestPage); |
| 543 ui_test_utils::NavigateToURL(browser(), url); |
| 544 |
| 545 std::vector<DevToolsWindow*> windows; |
| 546 std::vector<content::WindowedNotificationObserver*> close_observers; |
| 547 content::WebContents* inspected_web_contents = GetInspectedTab(); |
| 548 for (int i = 0; i < 3; ++i) { |
| 549 DevToolsWindow* devtools_window = OpenDevToolWindowOnWebContents( |
| 550 inspected_web_contents); |
| 551 windows.push_back(devtools_window); |
| 552 content::WindowedNotificationObserver* close_observer = |
| 553 new content::WindowedNotificationObserver( |
| 554 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 555 content::Source<content::WebContents>( |
| 556 devtools_window->web_contents())); |
| 557 close_observers.push_back(close_observer); |
| 558 inspected_web_contents = devtools_window->web_contents(); |
| 559 } |
| 560 |
| 561 InjectBeforeUnloadListener(windows[0]->web_contents()); |
| 562 InjectBeforeUnloadListener(windows[2]->web_contents()); |
| 563 // try to close second DevTools |
| 564 { |
| 565 content::WindowedNotificationObserver cancel_browser( |
| 566 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 567 content::NotificationService::AllSources()); |
| 568 chrome::CloseWindow(windows[1]->browser()); |
| 569 CancelModalDialog(); |
| 570 cancel_browser.Wait(); |
| 571 } |
| 572 // try to close browser window |
| 573 { |
| 574 content::WindowedNotificationObserver cancel_browser( |
| 575 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 576 content::NotificationService::AllSources()); |
| 577 chrome::CloseWindow(browser()); |
| 578 AcceptModalDialog(); |
| 579 CancelModalDialog(); |
| 580 cancel_browser.Wait(); |
| 581 } |
| 582 // try to exit application |
| 583 { |
| 584 content::WindowedNotificationObserver close_observer( |
| 585 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 586 content::Source<Browser>(browser())); |
| 587 chrome::CloseAllBrowsers(); |
| 588 AcceptModalDialog(); |
| 589 AcceptModalDialog(); |
| 590 close_observer.Wait(); |
| 591 } |
| 592 for (size_t i = 0; i < close_observers.size(); ++i) { |
| 593 close_observers[i]->Wait(); |
| 594 delete close_observers[i]; |
| 595 } |
450 } | 596 } |
451 | 597 |
452 // Tests scripts panel showing. | 598 // Tests scripts panel showing. |
453 // TODO(pfeldman): figure out flake. | 599 // TODO(pfeldman): figure out flake. |
454 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, DISABLED_TestShowScriptsTab) { | 600 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, DISABLED_TestShowScriptsTab) { |
455 RunTest("testShowScriptsTab", kDebuggerTestPage); | 601 RunTest("testShowScriptsTab", kDebuggerTestPage); |
456 } | 602 } |
457 | 603 |
458 // Tests that scripts tab is populated with inspected scripts even if it | 604 // Tests that scripts tab is populated with inspected scripts even if it |
459 // hadn't been shown by the moment inspected paged refreshed. | 605 // hadn't been shown by the moment inspected paged refreshed. |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 #if defined(OS_WIN) && defined(USE_ASH) | 845 #if defined(OS_WIN) && defined(USE_ASH) |
700 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 846 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
701 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) | 847 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) |
702 return; | 848 return; |
703 #endif | 849 #endif |
704 | 850 |
705 ASSERT_TRUE(RunExtensionTest("target_list")) << message_; | 851 ASSERT_TRUE(RunExtensionTest("target_list")) << message_; |
706 } | 852 } |
707 | 853 |
708 } // namespace | 854 } // namespace |
OLD | NEW |