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 1627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1638 FrameFocusedObserver::~FrameFocusedObserver() {} | 1638 FrameFocusedObserver::~FrameFocusedObserver() {} |
1639 | 1639 |
1640 void FrameFocusedObserver::Wait() { | 1640 void FrameFocusedObserver::Wait() { |
1641 impl_->Run(); | 1641 impl_->Run(); |
1642 } | 1642 } |
1643 | 1643 |
1644 TestNavigationManager::TestNavigationManager(WebContents* web_contents, | 1644 TestNavigationManager::TestNavigationManager(WebContents* web_contents, |
1645 const GURL& url) | 1645 const GURL& url) |
1646 : WebContentsObserver(web_contents), | 1646 : WebContentsObserver(web_contents), |
1647 url_(url), | 1647 url_(url), |
1648 navigation_paused_in_will_start_(false), | |
1649 navigation_paused_in_will_process_response_(false), | |
1650 handle_(nullptr), | 1648 handle_(nullptr), |
1651 handled_navigation_(false), | 1649 navigation_paused_(false), |
| 1650 current_state_(NavigationState::INITIAL), |
| 1651 desired_state_(NavigationState::STARTED), |
1652 weak_factory_(this) {} | 1652 weak_factory_(this) {} |
1653 | 1653 |
1654 TestNavigationManager::~TestNavigationManager() { | 1654 TestNavigationManager::~TestNavigationManager() { |
1655 ResumeNavigation(); | 1655 if (navigation_paused_) |
| 1656 handle_->Resume(); |
1656 } | 1657 } |
1657 | 1658 |
1658 bool TestNavigationManager::WaitForWillStartRequest() { | 1659 bool TestNavigationManager::WaitForWillStartRequest() { |
1659 DCHECK(!did_finish_loop_runner_); | 1660 // This is the default desired state. In PlzNavigate, a browser-initiated |
1660 if (!handle_ && handled_navigation_) | 1661 // navigation can reach this state synchronously, so the TestNavigationManager |
1661 return true; | 1662 // is set to always pause navigations at WillStartRequest. This ensures the |
1662 if (navigation_paused_in_will_start_) | 1663 // user can always call WaitForWillStartRequest. |
1663 return true; | 1664 DCHECK(desired_state_ == NavigationState::STARTED); |
1664 DCHECK(!navigation_paused_in_will_process_response_); | 1665 return WaitForDesiredState(); |
1665 will_start_loop_runner_ = new MessageLoopRunner(); | |
1666 will_start_loop_runner_->Run(); | |
1667 will_start_loop_runner_ = nullptr; | |
1668 | |
1669 // This will only be false if DidFinishNavigation is called before | |
1670 // OnWillStartRequest, which could occur if a throttle cancels the navigation | |
1671 // before the TestNavigationManagerThrottle's method is called. | |
1672 return !handled_navigation_; | |
1673 } | 1666 } |
1674 | 1667 |
1675 bool TestNavigationManager::WaitForWillProcessResponse() { | 1668 bool TestNavigationManager::WaitForResponse() { |
1676 DCHECK(!did_finish_loop_runner_); | 1669 desired_state_ = NavigationState::RESPONSE; |
1677 if (!handle_ && handled_navigation_) | 1670 return WaitForDesiredState(); |
1678 return true; | |
1679 if (navigation_paused_in_will_process_response_) | |
1680 return true; | |
1681 // Ensure the navigation is resumed if the manager paused it previously. | |
1682 if (navigation_paused_in_will_start_) | |
1683 ResumeNavigation(); | |
1684 will_process_response_loop_runner_ = new MessageLoopRunner(); | |
1685 will_process_response_loop_runner_->Run(); | |
1686 will_process_response_loop_runner_ = nullptr; | |
1687 | |
1688 // This will only be false if DidFinishNavigation is called before | |
1689 // OnWillProcessResponse. | |
1690 return !handled_navigation_; | |
1691 } | 1671 } |
1692 | 1672 |
1693 void TestNavigationManager::WaitForNavigationFinished() { | 1673 void TestNavigationManager::WaitForNavigationFinished() { |
1694 DCHECK(!will_start_loop_runner_); | 1674 desired_state_ = NavigationState::FINISHED; |
1695 if (!handle_ && handled_navigation_) | 1675 WaitForDesiredState(); |
1696 return; | |
1697 // Ensure the navigation is resumed if the manager paused it previously. | |
1698 if (navigation_paused_in_will_start_ || | |
1699 navigation_paused_in_will_process_response_) { | |
1700 ResumeNavigation(); | |
1701 } | |
1702 did_finish_loop_runner_ = new MessageLoopRunner(); | |
1703 did_finish_loop_runner_->Run(); | |
1704 did_finish_loop_runner_ = nullptr; | |
1705 } | 1676 } |
1706 | 1677 |
1707 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { | 1678 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { |
1708 if (!ShouldMonitorNavigation(handle)) | 1679 if (!ShouldMonitorNavigation(handle)) |
1709 return; | 1680 return; |
1710 | 1681 |
1711 handle_ = handle; | 1682 handle_ = handle; |
1712 std::unique_ptr<NavigationThrottle> throttle( | 1683 std::unique_ptr<NavigationThrottle> throttle( |
1713 new TestNavigationManagerThrottle( | 1684 new TestNavigationManagerThrottle( |
1714 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, | 1685 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, |
1715 weak_factory_.GetWeakPtr()), | 1686 weak_factory_.GetWeakPtr()), |
1716 base::Bind(&TestNavigationManager::OnWillProcessResponse, | 1687 base::Bind(&TestNavigationManager::OnWillProcessResponse, |
1717 weak_factory_.GetWeakPtr()))); | 1688 weak_factory_.GetWeakPtr()))); |
1718 handle_->RegisterThrottleForTesting(std::move(throttle)); | 1689 handle_->RegisterThrottleForTesting(std::move(throttle)); |
1719 } | 1690 } |
1720 | 1691 |
1721 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { | 1692 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { |
1722 if (handle != handle_) | 1693 if (handle != handle_) |
1723 return; | 1694 return; |
| 1695 current_state_ = NavigationState::FINISHED; |
| 1696 navigation_paused_ = false; |
1724 handle_ = nullptr; | 1697 handle_ = nullptr; |
1725 handled_navigation_ = true; | 1698 OnNavigationStateChanged(); |
1726 navigation_paused_in_will_start_ = false; | |
1727 navigation_paused_in_will_process_response_ = false; | |
1728 | |
1729 // Resume any clients that are waiting for the end of the navigation. Note | |
1730 // that |will_start_loop_runner_| can be running if the navigation was | |
1731 // cancelled while it was deferred. | |
1732 if (did_finish_loop_runner_) | |
1733 did_finish_loop_runner_->Quit(); | |
1734 if (will_start_loop_runner_) | |
1735 will_start_loop_runner_->Quit(); | |
1736 if (will_process_response_loop_runner_) | |
1737 will_process_response_loop_runner_->Quit(); | |
1738 } | 1699 } |
1739 | 1700 |
1740 void TestNavigationManager::OnWillStartRequest() { | 1701 void TestNavigationManager::OnWillStartRequest() { |
1741 navigation_paused_in_will_start_ = true; | 1702 current_state_ = NavigationState::STARTED; |
1742 if (will_start_loop_runner_) | 1703 navigation_paused_ = true; |
1743 will_start_loop_runner_->Quit(); | 1704 OnNavigationStateChanged(); |
1744 | |
1745 // If waiting for further events in the navigation, resume the navigation. | |
1746 if (did_finish_loop_runner_ || will_process_response_loop_runner_) | |
1747 ResumeNavigation(); | |
1748 } | 1705 } |
1749 | 1706 |
1750 void TestNavigationManager::OnWillProcessResponse() { | 1707 void TestNavigationManager::OnWillProcessResponse() { |
1751 navigation_paused_in_will_process_response_ = true; | 1708 current_state_ = NavigationState::RESPONSE; |
1752 DCHECK(!will_start_loop_runner_); | 1709 navigation_paused_ = true; |
1753 if (will_process_response_loop_runner_) | 1710 OnNavigationStateChanged(); |
1754 will_process_response_loop_runner_->Quit(); | |
1755 | |
1756 // If waiting for further events in the navigation, resume the navigation. | |
1757 if (did_finish_loop_runner_) | |
1758 ResumeNavigation(); | |
1759 } | 1711 } |
1760 | 1712 |
1761 void TestNavigationManager::ResumeNavigation() { | 1713 bool TestNavigationManager::WaitForDesiredState() { |
1762 if (!(navigation_paused_in_will_start_ || | 1714 // If the desired state has laready been reached, just return. |
1763 navigation_paused_in_will_process_response_) || | 1715 if (current_state_ == desired_state_) |
1764 !handle_) { | 1716 return true; |
| 1717 |
| 1718 // Resume the navigation if it was paused. |
| 1719 if (navigation_paused_) |
| 1720 handle_->Resume(); |
| 1721 |
| 1722 // Wait for the desired state if needed. |
| 1723 if (current_state_ < desired_state_) { |
| 1724 DCHECK(!loop_runner_); |
| 1725 loop_runner_ = new MessageLoopRunner(); |
| 1726 loop_runner_->Run(); |
| 1727 loop_runner_ = nullptr; |
| 1728 } |
| 1729 |
| 1730 // Return false if the navigation did not reach the state specified by the |
| 1731 // user. |
| 1732 return current_state_ == desired_state_; |
| 1733 } |
| 1734 |
| 1735 void TestNavigationManager::OnNavigationStateChanged() { |
| 1736 // If the state the user was waiting for has been reached, exit the message |
| 1737 // loop. |
| 1738 if (current_state_ >= desired_state_) { |
| 1739 if (loop_runner_) |
| 1740 loop_runner_->Quit(); |
1765 return; | 1741 return; |
1766 } | 1742 } |
1767 navigation_paused_in_will_start_ = false; | 1743 |
1768 navigation_paused_in_will_process_response_ = false; | 1744 // Otherwise, the navigation should be resumed if it was previously paused. |
1769 handle_->Resume(); | 1745 if (navigation_paused_) |
| 1746 handle_->Resume(); |
1770 } | 1747 } |
1771 | 1748 |
1772 bool TestNavigationManager::ShouldMonitorNavigation(NavigationHandle* handle) { | 1749 bool TestNavigationManager::ShouldMonitorNavigation(NavigationHandle* handle) { |
1773 if (handle_ || handle->GetURL() != url_) | 1750 if (handle_ || handle->GetURL() != url_) |
1774 return false; | 1751 return false; |
1775 if (handled_navigation_) | 1752 if (current_state_ != NavigationState::INITIAL) |
1776 return false; | 1753 return false; |
1777 return true; | 1754 return true; |
1778 } | 1755 } |
1779 | 1756 |
1780 } // namespace content | 1757 } // namespace content |
OLD | NEW |