Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: content/public/test/navigation_simulator.h

Issue 2698393002: Allow asynchronous deferral in NavigationSimulator (Closed)
Patch Set: Just return the throttle check on Start/Redirect/COmmit Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 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 #ifndef CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_ 5 #ifndef CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_
6 #define CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_ 6 #define CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/callback.h"
11 #include "base/optional.h"
12 #include "content/public/browser/navigation_throttle.h"
10 #include "content/public/browser/web_contents_observer.h" 13 #include "content/public/browser/web_contents_observer.h"
11 #include "content/public/common/referrer.h" 14 #include "content/public/common/referrer.h"
12 #include "content/public/test/navigation_simulator.h" 15 #include "content/public/test/navigation_simulator.h"
13 #include "ui/base/page_transition_types.h" 16 #include "ui/base/page_transition_types.h"
14 17
15 class GURL; 18 class GURL;
16 19
17 namespace content { 20 namespace content {
18 21
19 class NavigationHandleImpl; 22 class NavigationHandleImpl;
20 class RenderFrameHost; 23 class RenderFrameHost;
21 class TestRenderFrameHost; 24 class TestRenderFrameHost;
22 struct Referrer; 25 struct Referrer;
23 26
27 // To avoid DEPS violation by bringing in navigation_handle_impl.h.
28 using ThrottleChecksFinishedCallback =
29 base::Callback<void(NavigationThrottle::ThrottleCheckResult)>;
30
24 // An interface for simulating a navigation in unit tests. Currently this only 31 // An interface for simulating a navigation in unit tests. Currently this only
25 // supports renderer-initiated navigations. 32 // supports renderer-initiated navigations.
26 // Note: this should not be used in browser tests. 33 // Note: this should not be used in browser tests.
27 // TODO(clamy): support browser-initiated navigations. 34 // TODO(clamy): support browser-initiated navigations.
28 class NavigationSimulator : public WebContentsObserver { 35 class NavigationSimulator : public WebContentsObserver {
29 public: 36 public:
30 // Simulates a renderer-initiated navigation to |url| started in 37 // Simulates a renderer-initiated navigation to |url| started in
31 // |render_frame_host| from start to commit. 38 // |render_frame_host| from start to commit.
32 static void NavigateAndCommitFromDocument(const GURL& original_url, 39 static void NavigateAndCommitFromDocument(const GURL& original_url,
33 RenderFrameHost* render_frame_host); 40 RenderFrameHost* render_frame_host);
(...skipping 23 matching lines...) Expand all
57 // -------------------------------------------------------------------------- 64 // --------------------------------------------------------------------------
58 65
59 // The following functions should be used to simulate events happening during 66 // The following functions should be used to simulate events happening during
60 // a navigation. 67 // a navigation.
61 // 68 //
62 // Example of usage for a successful renderer-initiated navigation: 69 // Example of usage for a successful renderer-initiated navigation:
63 // unique_ptr<NavigationSimulator> simulator = 70 // unique_ptr<NavigationSimulator> simulator =
64 // NavigationSimulator::CreateRendererInitiated( 71 // NavigationSimulator::CreateRendererInitiated(
65 // original_url, render_frame_host); 72 // original_url, render_frame_host);
66 // simulator->SetTransition(ui::PAGE_TRANSITION_LINK); 73 // simulator->SetTransition(ui::PAGE_TRANSITION_LINK);
67 // simulator->Start(); 74 // if (simulator->Start() == NavigationThrottle::DEFER)
clamy 2017/03/06 15:22:15 In fact, that's not the interface I had in mind. W
Charlie Harrison 2017/03/06 20:18:43 As discussed offline, this isn't possible with how
engedy 2017/03/07 15:24:56 I found it practical to look at this from the foll
Charlie Harrison 2017/03/07 16:42:43 I see what you are saying. I am in favor of this a
engedy 2017/03/09 11:41:34 Yep, you understood correctly, but I should have p
Charlie Harrison 2017/03/09 14:36:01 Thanks for the clarification.
68 // for (GURL redirect_url : redirects) 75 // simulator->WaitForThrottleChecksComplete();
69 // simulator->Redirect(redirect_url); 76 // for (GURL redirect_url : redirects) {
70 // simulator->Commit(); 77 // EXPECT_EQ(NavigationThrottle::PROCEED,
78 // simulator->Redirect(redirect_url));
79 // }
80 // EXPECT_EQ(NavigationThrottle::PROCEED, simulator->Commit());
71 // 81 //
72 // Example of usage for a failed renderer-initiated navigation: 82 // Example of usage for a failed renderer-initiated navigation:
73 // unique_ptr<NavigationSimulator> simulator = 83 // unique_ptr<NavigationSimulator> simulator =
74 // NavigationSimulator::CreateRendererInitiated( 84 // NavigationSimulator::CreateRendererInitiated(
75 // original_url, render_frame_host); 85 // original_url, render_frame_host);
76 // simulator->SetTransition(ui::PAGE_TRANSITION_LINK); 86 // simulator->SetTransition(ui::PAGE_TRANSITION_LINK);
77 // simulator->Start(); 87 // EXPECT_EQ(NavigationThrottle::PROCEED, simulator->Start());
78 // for (GURL redirect_url : redirects) 88 // for (GURL redirect_url : redirects) {
79 // simulator->Redirect(redirect_url); 89 // EXPECT_EQ(NavigationThrottle::DEFER,
90 // simulator->Redirect(redirect_url));
91 // simulator->WaitForThrottleChecksComplete();
92 // }
80 // simulator->Fail(net::ERR_TIMED_OUT); 93 // simulator->Fail(net::ERR_TIMED_OUT);
81 // simulator->CommitErrorPage(); 94 // simulator->CommitErrorPage();
82 // 95 //
83 // Example of usage for a same-page renderer-initiated navigation: 96 // Example of usage for a same-page renderer-initiated navigation:
84 // unique_ptr<NavigationSimulator> simulator = 97 // unique_ptr<NavigationSimulator> simulator =
85 // NavigationSimulator::CreateRendererInitiated( 98 // NavigationSimulator::CreateRendererInitiated(
86 // original_url, render_frame_host); 99 // original_url, render_frame_host);
87 // simulator->CommitSamePage(); 100 // simulator->CommitSamePage();
88 101
89 // Simulates the start of the navigation. 102 // Simulates the start of the navigation.
90 virtual void Start(); 103 virtual NavigationThrottle::ThrottleCheckResult Start();
91 104
92 // Simulates a redirect to |new_url| for the navigation. 105 // Simulates a redirect to |new_url| for the navigation.
93 virtual void Redirect(const GURL& new_url); 106 virtual NavigationThrottle::ThrottleCheckResult Redirect(const GURL& new_url);
94 107
95 // Simulates the commit of the navigation in the RenderFrameHost. 108 // Simulates the commit of the navigation in the RenderFrameHost.
96 virtual void Commit(); 109 virtual NavigationThrottle::ThrottleCheckResult Commit();
97 110
98 // Simulates the navigation failing with the error code |error_code|. 111 // Simulates the navigation failing with the error code |error_code|.
99 virtual void Fail(int error_code); 112 virtual void Fail(int error_code);
100 113
101 // Simulates the commit of an error page following a navigation failure. 114 // Simulates the commit of an error page following a navigation failure.
102 virtual void CommitErrorPage(); 115 virtual void CommitErrorPage();
103 116
104 // Simulates the commit of a same-page navigation, ie fragment navigations or 117 // Simulates the commit of a same-page navigation, ie fragment navigations or
105 // pushState/popState navigations. 118 // pushState/popState navigations.
106 virtual void CommitSamePage(); 119 virtual void CommitSamePage();
107 120
108 // -------------------------------------------------------------------------- 121 // --------------------------------------------------------------------------
109 122
110 // The following functions are used to specify the parameters of the 123 // The following functions are used to specify the parameters of the
111 // navigation. Changes should be made before calling |Start|, unless they are 124 // 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 125 // meant to apply to a redirect. In that case, they should be made before
113 // calling |Redirect|. 126 // calling |Redirect|.
114 127
115 // The following parameters are constant during the navigation and may only be 128 // The following parameters are constant during the navigation and may only be
116 // specified before calling |Start|. 129 // specified before calling |Start|.
117 virtual void SetTransition(ui::PageTransition transition); 130 virtual void SetTransition(ui::PageTransition transition);
118 131
119 // The following parameters can change during redirects. They should be 132 // The following parameters can change during redirects. They should be
120 // specified before calling |Start| if they need to apply to the navigation to 133 // specified before calling |Start| if they need to apply to the navigation to
121 // the original url. Otherwise, they should be specified before calling 134 // the original url. Otherwise, they should be specified before calling
122 // |Redirect|. 135 // |Redirect|.
123 virtual void SetReferrer(const Referrer& referrer); 136 virtual void SetReferrer(const Referrer& referrer);
124 137
138 // This method will wait for throttle checks to complete if the simulator is
139 // running in AllowThrottleChecks mode.
clamy 2017/03/06 15:22:15 We no longer have a non AllowThrottleChecks mode :
Charlie Harrison 2017/03/06 20:18:43 Done.
140 virtual NavigationThrottle::ThrottleCheckResult
141 WaitForThrottleChecksComplete();
142
125 private: 143 private:
126 // WebContentsObserver: 144 // WebContentsObserver:
127 void DidStartNavigation(NavigationHandle* navigation_handle) override; 145 void DidStartNavigation(NavigationHandle* navigation_handle) override;
128 void DidRedirectNavigation(NavigationHandle* navigation_handle) override; 146 void DidRedirectNavigation(NavigationHandle* navigation_handle) override;
129 void ReadyToCommitNavigation(NavigationHandle* navigation_handle) override; 147 void ReadyToCommitNavigation(NavigationHandle* navigation_handle) override;
130 void DidFinishNavigation(NavigationHandle* navigation_handle) override; 148 void DidFinishNavigation(NavigationHandle* navigation_handle) override;
131 149
132 void OnWillStartRequest(); 150 void OnWillStartRequest();
133 void OnWillRedirectRequest(); 151 void OnWillRedirectRequest();
134 void OnWillProcessResponse(); 152 void OnWillProcessResponse();
135 153
154 // Called after all the throttle checks are finished for these steps.
155 void FinishStart(NavigationThrottle::ThrottleCheckResult result);
156 void FinishRedirect(int previous_num_will_redirect_request_called,
157 int previous_did_redirect_navigation_called,
158 NavigationThrottle::ThrottleCheckResult result);
159 void FinishCommit(NavigationThrottle::ThrottleCheckResult result);
160
161 // Returns the sync result of the throttle checks if it is available, or DEFER
162 // if it is not.
163 NavigationThrottle::ThrottleCheckResult RunOrStoreThrottleCompleteCallback(
164 ThrottleChecksFinishedCallback finish_callback);
165
166 void OnThrottleChecksComplete(NavigationThrottle::ThrottleCheckResult result);
167
168 // Helper method to get the OnThrottleChecksComplete callback.
169 ThrottleChecksFinishedCallback GenerateCompleteCallback();
170
171 // Helper method for places in the code which expect the navigation to start
172 // successfuly (e.g. when calling Commit() before Start()).
173 void StartAndExpectToProceed();
174
136 enum State { 175 enum State {
137 INITIALIZATION, 176 INITIALIZATION,
138 STARTED, 177 STARTED,
139 FAILED, 178 FAILED,
140 FINISHED, 179 FINISHED,
141 }; 180 };
142 181
143 State state_ = INITIALIZATION; 182 State state_ = INITIALIZATION;
144 183
145 // The renderer associated with this navigation. 184 // The renderer associated with this navigation.
146 TestRenderFrameHost* render_frame_host_; 185 TestRenderFrameHost* render_frame_host_;
147 186
148 // The NavigationHandle associated with this navigation. 187 // The NavigationHandle associated with this navigation.
149 NavigationHandleImpl* handle_; 188 NavigationHandleImpl* handle_;
150 189
151 GURL navigation_url_; 190 GURL navigation_url_;
152 Referrer referrer_; 191 Referrer referrer_;
153 ui::PageTransition transition_ = ui::PAGE_TRANSITION_LINK; 192 ui::PageTransition transition_ = ui::PAGE_TRANSITION_LINK;
154 193
155 // These are used to sanity check the content/public/ API calls emitted as 194 // These are used to sanity check the content/public/ API calls emitted as
156 // part of the navigation. 195 // part of the navigation.
157 int num_did_start_navigation_called_ = 0; 196 int num_did_start_navigation_called_ = 0;
158 int num_will_start_request_called_ = 0; 197 int num_will_start_request_called_ = 0;
159 int num_will_redirect_request_called_ = 0; 198 int num_will_redirect_request_called_ = 0;
160 int num_did_redirect_navigation_called_ = 0; 199 int num_did_redirect_navigation_called_ = 0;
161 int num_will_process_response_called_ = 0; 200 int num_will_process_response_called_ = 0;
162 int num_ready_to_commit_called_ = 0; 201 int num_ready_to_commit_called_ = 0;
163 int num_did_finish_navigation_called_ = 0; 202 int num_did_finish_navigation_called_ = 0;
164 203
204 // Holds the last ThrottleCheckResult calculated by the navigation's
205 // throttles. Will be unset before WillStartRequest is finished. Will be unset
206 // while throttles are being run, but before they finish.
207 base::Optional<NavigationThrottle::ThrottleCheckResult>
208 last_throttle_check_result_;
209
210 // Used to execute the Finish* methods, after the throttles have all finished.
211 ThrottleChecksFinishedCallback complete_callback_;
212
213 // Closure that is set when WaitForThrottleChecksComplete is called.
214 base::Closure throttle_checks_wait_closure_;
215
165 base::WeakPtrFactory<NavigationSimulator> weak_factory_; 216 base::WeakPtrFactory<NavigationSimulator> weak_factory_;
166 }; 217 };
167 218
168 } // namespace content 219 } // namespace content
169 220
170 #endif // CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_ 221 #endif // CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698