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 "content/public/test/browser_test_utils.h" | 5 #include "content/public/test/browser_test_utils.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <tuple> | 8 #include <tuple> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "content/browser/web_contents/web_contents_impl.h" | 37 #include "content/browser/web_contents/web_contents_impl.h" |
38 #include "content/browser/web_contents/web_contents_view.h" | 38 #include "content/browser/web_contents/web_contents_view.h" |
39 #include "content/common/input/synthetic_web_input_event_builders.h" | 39 #include "content/common/input/synthetic_web_input_event_builders.h" |
40 #include "content/common/input_messages.h" | 40 #include "content/common/input_messages.h" |
41 #include "content/common/view_messages.h" | 41 #include "content/common/view_messages.h" |
42 #include "content/public/browser/browser_context.h" | 42 #include "content/public/browser/browser_context.h" |
43 #include "content/public/browser/browser_plugin_guest_manager.h" | 43 #include "content/public/browser/browser_plugin_guest_manager.h" |
44 #include "content/public/browser/browser_thread.h" | 44 #include "content/public/browser/browser_thread.h" |
45 #include "content/public/browser/histogram_fetcher.h" | 45 #include "content/public/browser/histogram_fetcher.h" |
46 #include "content/public/browser/navigation_entry.h" | 46 #include "content/public/browser/navigation_entry.h" |
| 47 #include "content/public/browser/navigation_handle.h" |
| 48 #include "content/public/browser/navigation_throttle.h" |
47 #include "content/public/browser/notification_service.h" | 49 #include "content/public/browser/notification_service.h" |
48 #include "content/public/browser/notification_types.h" | 50 #include "content/public/browser/notification_types.h" |
49 #include "content/public/browser/render_frame_host.h" | 51 #include "content/public/browser/render_frame_host.h" |
50 #include "content/public/browser/render_process_host.h" | 52 #include "content/public/browser/render_process_host.h" |
51 #include "content/public/browser/render_view_host.h" | 53 #include "content/public/browser/render_view_host.h" |
52 #include "content/public/browser/storage_partition.h" | 54 #include "content/public/browser/storage_partition.h" |
53 #include "content/public/browser/web_contents.h" | 55 #include "content/public/browser/web_contents.h" |
54 #include "content/public/test/test_navigation_observer.h" | 56 #include "content/public/test/test_navigation_observer.h" |
55 #include "content/public/test/test_utils.h" | 57 #include "content/public/test/test_utils.h" |
56 #include "content/test/accessibility_browser_test_utils.h" | 58 #include "content/test/accessibility_browser_test_utils.h" |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 GURL redirect_target(redirect_server.Resolve(path)); | 345 GURL redirect_target(redirect_server.Resolve(path)); |
344 DCHECK(redirect_target.is_valid()); | 346 DCHECK(redirect_target.is_valid()); |
345 | 347 |
346 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( | 348 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
347 new net::test_server::BasicHttpResponse); | 349 new net::test_server::BasicHttpResponse); |
348 http_response->set_code(http_status_code); | 350 http_response->set_code(http_status_code); |
349 http_response->AddCustomHeader("Location", redirect_target.spec()); | 351 http_response->AddCustomHeader("Location", redirect_target.spec()); |
350 return std::move(http_response); | 352 return std::move(http_response); |
351 } | 353 } |
352 | 354 |
| 355 // Helper class used by the TestNavigationManager to pause navigations. |
| 356 // Note: the throttle should be added to the *end* of the list of throttles, |
| 357 // so all NavigationThrottles that should be attached observe the |
| 358 // WillStartRequest callback. RegisterThrottleForTesting has this behavior. |
| 359 class TestNavigationManagerThrottle : public NavigationThrottle { |
| 360 public: |
| 361 TestNavigationManagerThrottle(NavigationHandle* handle, |
| 362 base::Closure on_will_start_request_closure) |
| 363 : NavigationThrottle(handle), |
| 364 on_will_start_request_closure_(on_will_start_request_closure) {} |
| 365 ~TestNavigationManagerThrottle() override {} |
| 366 |
| 367 private: |
| 368 // NavigationThrottle: |
| 369 NavigationThrottle::ThrottleCheckResult WillStartRequest() override { |
| 370 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 371 on_will_start_request_closure_); |
| 372 return NavigationThrottle::DEFER; |
| 373 } |
| 374 |
| 375 base::Closure on_will_start_request_closure_; |
| 376 }; |
| 377 |
353 } // namespace | 378 } // namespace |
354 | 379 |
355 bool NavigateIframeToURL(WebContents* web_contents, | 380 bool NavigateIframeToURL(WebContents* web_contents, |
356 std::string iframe_id, | 381 std::string iframe_id, |
357 const GURL& url) { | 382 const GURL& url) { |
358 std::string script = base::StringPrintf( | 383 std::string script = base::StringPrintf( |
359 "setTimeout(\"" | 384 "setTimeout(\"" |
360 "var iframes = document.getElementById('%s');iframes.src='%s';" | 385 "var iframes = document.getElementById('%s');iframes.src='%s';" |
361 "\",0)", | 386 "\",0)", |
362 iframe_id.c_str(), url.spec().c_str()); | 387 iframe_id.c_str(), url.spec().c_str()); |
(...skipping 1168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1531 FrameFocusedObserver::FrameFocusedObserver(RenderFrameHost* owner_host) | 1556 FrameFocusedObserver::FrameFocusedObserver(RenderFrameHost* owner_host) |
1532 : impl_(new FrameTreeNodeObserverImpl( | 1557 : impl_(new FrameTreeNodeObserverImpl( |
1533 static_cast<RenderFrameHostImpl*>(owner_host)->frame_tree_node())) {} | 1558 static_cast<RenderFrameHostImpl*>(owner_host)->frame_tree_node())) {} |
1534 | 1559 |
1535 FrameFocusedObserver::~FrameFocusedObserver() {} | 1560 FrameFocusedObserver::~FrameFocusedObserver() {} |
1536 | 1561 |
1537 void FrameFocusedObserver::Wait() { | 1562 void FrameFocusedObserver::Wait() { |
1538 impl_->Run(); | 1563 impl_->Run(); |
1539 } | 1564 } |
1540 | 1565 |
| 1566 TestNavigationManager::TestNavigationManager(WebContents* web_contents, |
| 1567 const GURL& url) |
| 1568 : WebContentsObserver(web_contents), |
| 1569 url_(url), |
| 1570 navigation_paused_(false), |
| 1571 handle_(nullptr), |
| 1572 handled_navigation_(false), |
| 1573 weak_factory_(this) {} |
| 1574 |
| 1575 TestNavigationManager::~TestNavigationManager() { |
| 1576 ResumeNavigation(); |
| 1577 } |
| 1578 |
| 1579 bool TestNavigationManager::WaitForWillStartRequest() { |
| 1580 DCHECK(!did_finish_loop_runner_); |
| 1581 if (!handle_ && handled_navigation_) |
| 1582 return true; |
| 1583 if (navigation_paused_) |
| 1584 return true; |
| 1585 will_start_loop_runner_ = new MessageLoopRunner(); |
| 1586 will_start_loop_runner_->Run(); |
| 1587 will_start_loop_runner_ = nullptr; |
| 1588 |
| 1589 // This will only be false if DidFinishNavigation is called before |
| 1590 // OnWillStartRequest, which could occur if a throttle cancels the navigation |
| 1591 // before the TestNavigationManagerThrottle's method is called. |
| 1592 return !handled_navigation_; |
| 1593 } |
| 1594 |
| 1595 void TestNavigationManager::WaitForNavigationFinished() { |
| 1596 DCHECK(!will_start_loop_runner_); |
| 1597 if (!handle_ && handled_navigation_) |
| 1598 return; |
| 1599 // Ensure the navigation is resumed if the manager paused it previously. |
| 1600 if (navigation_paused_) |
| 1601 ResumeNavigation(); |
| 1602 did_finish_loop_runner_ = new MessageLoopRunner(); |
| 1603 did_finish_loop_runner_->Run(); |
| 1604 did_finish_loop_runner_ = nullptr; |
| 1605 } |
| 1606 |
| 1607 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { |
| 1608 if (!ShouldMonitorNavigation(handle)) |
| 1609 return; |
| 1610 |
| 1611 handle_ = handle; |
| 1612 std::unique_ptr<NavigationThrottle> throttle( |
| 1613 new TestNavigationManagerThrottle( |
| 1614 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, |
| 1615 weak_factory_.GetWeakPtr()))); |
| 1616 handle_->RegisterThrottleForTesting(std::move(throttle)); |
| 1617 } |
| 1618 |
| 1619 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { |
| 1620 if (handle != handle_) |
| 1621 return; |
| 1622 handle_ = nullptr; |
| 1623 handled_navigation_ = true; |
| 1624 navigation_paused_ = false; |
| 1625 |
| 1626 // Resume any clients that are waiting for the end of the navigation. Note |
| 1627 // that |will_start_loop_runner_| can be running if the navigation was |
| 1628 // cancelled while it was deferred. |
| 1629 if (did_finish_loop_runner_) |
| 1630 did_finish_loop_runner_->Quit(); |
| 1631 if (will_start_loop_runner_) |
| 1632 will_start_loop_runner_->Quit(); |
| 1633 } |
| 1634 |
| 1635 void TestNavigationManager::OnWillStartRequest() { |
| 1636 navigation_paused_ = true; |
| 1637 if (will_start_loop_runner_) |
| 1638 will_start_loop_runner_->Quit(); |
| 1639 |
| 1640 // If waiting for the navigation to finish, resume the navigation. |
| 1641 if (did_finish_loop_runner_) |
| 1642 ResumeNavigation(); |
| 1643 } |
| 1644 |
| 1645 void TestNavigationManager::ResumeNavigation() { |
| 1646 if (!navigation_paused_ || !handle_) |
| 1647 return; |
| 1648 navigation_paused_ = false; |
| 1649 handle_->Resume(); |
| 1650 } |
| 1651 |
| 1652 bool TestNavigationManager::ShouldMonitorNavigation(NavigationHandle* handle) { |
| 1653 if (handle_ || handle->GetURL() != url_) |
| 1654 return false; |
| 1655 if (handled_navigation_) |
| 1656 return false; |
| 1657 return true; |
| 1658 } |
| 1659 |
1541 } // namespace content | 1660 } // namespace content |
OLD | NEW |