OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_ |
| 6 #define CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "content/public/browser/web_contents_observer.h" |
| 11 #include "content/public/common/referrer.h" |
| 12 #include "content/public/test/navigation_simulator.h" |
| 13 #include "ui/base/page_transition_types.h" |
| 14 |
| 15 class GURL; |
| 16 |
| 17 namespace content { |
| 18 |
| 19 class NavigationHandleImpl; |
| 20 class RenderFrameHost; |
| 21 class TestRenderFrameHost; |
| 22 struct Referrer; |
| 23 |
| 24 // An interface for simulating a navigation in unit tests. Currently this only |
| 25 // supports renderer-initiated navigations. |
| 26 // Note: this should not be used in browser tests. |
| 27 // TODO(clamy): support browser-initiated navigations. |
| 28 class NavigationSimulator : public WebContentsObserver { |
| 29 public: |
| 30 // Simulates a renderer-initiated navigation to |url| started in |
| 31 // |render_frame_host| from start to commit. |
| 32 static void NavigateAndCommitFromDocument(const GURL& original_url, |
| 33 RenderFrameHost* render_frame_host); |
| 34 |
| 35 // Simulates a failed renderer-initiated navigation to |url| started in |
| 36 // |render_frame_host| from start to commit. |
| 37 static void NavigateAndFailFromDocument(const GURL& original_url, |
| 38 int net_error_code, |
| 39 RenderFrameHost* render_frame_host); |
| 40 |
| 41 // --------------------------------------------------------------------------- |
| 42 |
| 43 // All the following methods should be used when more precise control over the |
| 44 // navigation is needed. |
| 45 |
| 46 // Creates a NavigationSimulator that will be used to simulate a |
| 47 // renderer-initiated navigation to |original_url| started by |
| 48 // |render_frame_host|. |
| 49 static std::unique_ptr<NavigationSimulator> CreateRendererInitiated( |
| 50 const GURL& original_url, |
| 51 RenderFrameHost* render_frame_host); |
| 52 |
| 53 NavigationSimulator(const GURL& original_url, |
| 54 TestRenderFrameHost* render_frame_host); |
| 55 ~NavigationSimulator() override; |
| 56 |
| 57 // -------------------------------------------------------------------------- |
| 58 |
| 59 // The following functions should be used to simulate events happening during |
| 60 // a navigation. |
| 61 // |
| 62 // Example of usage for a successful renderer-initiated navigation: |
| 63 // unique_ptr<NavigationSimulator> simulator = |
| 64 // NavigationSimulator::CreateRendererInitiated( |
| 65 // original_url, render_frame_host); |
| 66 // simulator->SetTransition(ui::PAGE_TRANSITION_LINK); |
| 67 // simulator->Start(); |
| 68 // for (GURL redirect_url : redirects) |
| 69 // simulator->Redirect(redirect_url); |
| 70 // simulator->Commit(); |
| 71 // |
| 72 // Example of usage for a failed renderer-initiated navigation: |
| 73 // unique_ptr<NavigationSimulator> simulator = |
| 74 // NavigationSimulator::CreateRendererInitiated( |
| 75 // original_url, render_frame_host); |
| 76 // simulator->SetTransition(ui::PAGE_TRANSITION_LINK); |
| 77 // simulator->Start(); |
| 78 // for (GURL redirect_url : redirects) |
| 79 // simulator->Redirect(redirect_url); |
| 80 // simulator->Fail(net::ERR_TIMED_OUT); |
| 81 // simulator->CommitErrorPage(); |
| 82 // |
| 83 // Example of usage for a same-page renderer-initiated navigation: |
| 84 // unique_ptr<NavigationSimulator> simulator = |
| 85 // NavigationSimulator::CreateRendererInitiated( |
| 86 // original_url, render_frame_host); |
| 87 // simulator->CommitSamePage(); |
| 88 |
| 89 // Simulates the start of the navigation. |
| 90 virtual void Start(); |
| 91 |
| 92 // Simulates a redirect to |new_url| for the navigation. |
| 93 virtual void Redirect(const GURL& new_url); |
| 94 |
| 95 // Simulates the commit of the navigation in the RenderFrameHost. |
| 96 virtual void Commit(); |
| 97 |
| 98 // Simulates the navigation failing with the error code |error_code|. |
| 99 virtual void Fail(int error_code); |
| 100 |
| 101 // Simulates the commit of an error page following a navigation failure. |
| 102 virtual void CommitErrorPage(); |
| 103 |
| 104 // Simulates the commit of a same-page navigation, ie fragment navigations or |
| 105 // pushState/popState navigations. |
| 106 virtual void CommitSamePage(); |
| 107 |
| 108 // -------------------------------------------------------------------------- |
| 109 |
| 110 // The following functions are used to specify the parameters of the |
| 111 // navigation. Changes should be made before calling |Start|, unless they are |
| 112 // meant to apply to a redirect. In that case, they should be made before |
| 113 // calling |Redirect|. |
| 114 |
| 115 // The following parameters are constant during the navigation and may only be |
| 116 // specified before calling |Start|. |
| 117 virtual void SetTransition(ui::PageTransition transition); |
| 118 |
| 119 // The following parameters can change during redirects. They should be |
| 120 // specified before calling |Start| if they need to apply to the navigation to |
| 121 // the original url. Otherwise, they should be specified before calling |
| 122 // |Redirect|. |
| 123 virtual void SetReferrer(const Referrer& referrer); |
| 124 |
| 125 private: |
| 126 // WebContentsObserver: |
| 127 void DidStartNavigation(NavigationHandle* navigation_handle) override; |
| 128 void DidRedirectNavigation(NavigationHandle* navigation_handle) override; |
| 129 void ReadyToCommitNavigation(NavigationHandle* navigation_handle) override; |
| 130 void DidFinishNavigation(NavigationHandle* navigation_handle) override; |
| 131 |
| 132 void OnWillStartRequest(); |
| 133 void OnWillRedirectRequest(); |
| 134 void OnWillProcessResponse(); |
| 135 |
| 136 enum State { |
| 137 INITIALIZATION, |
| 138 STARTED, |
| 139 FAILED, |
| 140 FINISHED, |
| 141 }; |
| 142 |
| 143 State state_ = INITIALIZATION; |
| 144 |
| 145 // The renderer associated with this navigation. |
| 146 TestRenderFrameHost* render_frame_host_; |
| 147 |
| 148 // The NavigationHandle associated with this navigation. |
| 149 NavigationHandleImpl* handle_; |
| 150 |
| 151 GURL navigation_url_; |
| 152 Referrer referrer_; |
| 153 ui::PageTransition transition_ = ui::PAGE_TRANSITION_LINK; |
| 154 |
| 155 // These are used to sanity check the content/public/ API calls emitted as |
| 156 // part of the navigation. |
| 157 int num_did_start_navigation_called_ = 0; |
| 158 int num_will_start_request_called_ = 0; |
| 159 int num_will_redirect_request_called_ = 0; |
| 160 int num_did_redirect_navigation_called_ = 0; |
| 161 int num_will_process_response_called_ = 0; |
| 162 int num_ready_to_commit_called_ = 0; |
| 163 int num_did_finish_navigation_called_ = 0; |
| 164 |
| 165 base::WeakPtrFactory<NavigationSimulator> weak_factory_; |
| 166 }; |
| 167 |
| 168 } // namespace content |
| 169 |
| 170 #endif // CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_ |
OLD | NEW |