OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 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 CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | |
7 | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/run_loop.h" | |
10 #include "chrome/browser/external_protocol/external_protocol_handler.h" | |
11 #include "chrome/browser/prerender/prerender_contents.h" | |
12 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | |
13 #include "chrome/test/base/in_process_browser_test.h" | |
14 #include "components/safe_browsing_db/test_database_manager.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "net/test/url_request/url_request_mock_http_job.h" | |
17 #include "net/url_request/url_request_filter.h" | |
pasko
2016/09/05 13:46:28
is this needed to be included in the .h file?
mattcary
2016/09/06 08:06:20
Thanks, I had the counting interceptor methods in
| |
18 #include "net/url_request/url_request_interceptor.h" | |
19 #include "url/gurl.h" | |
20 | |
21 namespace base { | |
22 class FilePath; | |
23 } // namespace base | |
24 | |
25 namespace net { | |
26 class URLRequest; | |
27 class NetworkDelegate; | |
28 } // namespace net | |
29 | |
30 namespace prerender { | |
31 | |
32 namespace test_utils { | |
33 | |
34 // Dummy counter class to live on the UI thread for counting requests. | |
35 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> { | |
36 public: | |
37 RequestCounter(); | |
38 | |
39 ~RequestCounter(); | |
40 | |
41 int count() const { return count_; } | |
42 | |
43 void RequestStarted(); | |
44 void WaitForCount(int expected_count); | |
45 | |
46 private: | |
47 int count_; | |
48 int expected_count_; | |
49 std::unique_ptr<base::RunLoop> loop_; | |
50 }; | |
51 | |
52 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for | |
53 // a given URL. | |
54 class FakeSafeBrowsingDatabaseManager | |
55 : public safe_browsing::TestSafeBrowsingDatabaseManager { | |
56 public: | |
57 FakeSafeBrowsingDatabaseManager(); | |
58 | |
59 // Called on the IO thread to check if the given url is safe or not. If we | |
60 // can synchronously determine that the url is safe, CheckUrl returns true. | |
61 // Otherwise it returns false, and "client" is called asynchronously with the | |
62 // result when it is ready. | |
63 // Returns true, indicating a SAFE result, unless the URL is the fixed URL | |
64 // specified by the user, and the user-specified result is not SAFE | |
65 // (in which that result will be communicated back via a call into the | |
66 // client, and false will be returned). | |
67 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl. | |
68 bool CheckBrowseUrl(const GURL& gurl, Client* client) override; | |
69 | |
70 void SetThreatTypeForUrl(const GURL& url, | |
71 safe_browsing::SBThreatType threat_type) { | |
72 bad_urls_[url.spec()] = threat_type; | |
73 } | |
74 | |
75 // These are called when checking URLs, so we implement them. | |
76 bool IsSupported() const override; | |
77 bool ChecksAreAlwaysAsync() const override; | |
78 bool CanCheckResourceType( | |
79 content::ResourceType /* resource_type */) const override; | |
80 | |
81 bool CheckExtensionIDs(const std::set<std::string>& extension_ids, | |
82 Client* client) override; | |
83 | |
84 private: | |
85 ~FakeSafeBrowsingDatabaseManager() override; | |
86 | |
87 void OnCheckBrowseURLDone(const GURL& gurl, Client* client); | |
88 | |
89 std::unordered_map<std::string, safe_browsing::SBThreatType> bad_urls_; | |
90 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); | |
91 }; | |
92 | |
93 // PrerenderContents that stops the UI message loop on DidStopLoading(). | |
94 class TestPrerenderContents : public PrerenderContents { | |
95 public: | |
96 TestPrerenderContents(PrerenderManager* prerender_manager, | |
97 Profile* profile, | |
98 const GURL& url, | |
99 const content::Referrer& referrer, | |
100 Origin origin, | |
101 FinalStatus expected_final_status); | |
102 | |
103 ~TestPrerenderContents() override; | |
104 | |
105 void RenderProcessGone(base::TerminationStatus status) override; | |
106 bool CheckURL(const GURL& url) override; | |
107 | |
108 // For tests that open the prerender in a new background tab, the RenderView | |
109 // will not have been made visible when the PrerenderContents is destroyed | |
110 // even though it is used. | |
111 void set_should_be_shown(bool value) { should_be_shown_ = value; } | |
112 | |
113 // For tests which do not know whether the prerender will be used. | |
114 void set_skip_final_checks(bool value) { skip_final_checks_ = value; } | |
115 | |
116 FinalStatus expected_final_status() const { return expected_final_status_; } | |
117 | |
118 private: | |
119 void OnRenderViewHostCreated( | |
120 content::RenderViewHost* new_render_view_host) override; | |
121 void Observe(int type, | |
122 const content::NotificationSource& source, | |
123 const content::NotificationDetails& details) override; | |
124 | |
125 FinalStatus expected_final_status_; | |
126 | |
127 // The RenderViewHost created for the prerender, if any. | |
128 content::RenderViewHost* new_render_view_host_; | |
129 // Set to true when the prerendering RenderWidget is hidden. | |
130 bool was_hidden_; | |
131 // Set to true when the prerendering RenderWidget is shown, after having been | |
132 // hidden. | |
133 bool was_shown_; | |
134 // Expected final value of was_shown_. Defaults to true for | |
135 // FINAL_STATUS_USED, and false otherwise. | |
136 bool should_be_shown_; | |
137 // If true, |expected_final_status_| and other shutdown checks are skipped. | |
138 bool skip_final_checks_; | |
139 }; | |
140 | |
141 // A handle to a TestPrerenderContents whose lifetime is under the caller's | |
142 // control. A PrerenderContents may be destroyed at any point. This allows | |
143 // tracking the final status, etc. | |
144 class TestPrerender : public PrerenderContents::Observer, | |
145 public base::SupportsWeakPtr<TestPrerender> { | |
146 public: | |
147 TestPrerender(); | |
148 ~TestPrerender() override; | |
149 | |
150 TestPrerenderContents* contents() const { return contents_; } | |
151 int number_of_loads() const { return number_of_loads_; } | |
152 | |
153 void WaitForCreate() { create_loop_.Run(); } | |
154 void WaitForStart() { start_loop_.Run(); } | |
155 void WaitForStop() { stop_loop_.Run(); } | |
156 | |
157 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR | |
158 // for the prerender to stop running (just to avoid a timeout if the prerender | |
159 // dies). Note: this does not assert equality on the number of loads; the | |
160 // caller must do it instead. | |
161 void WaitForLoads(int expected_number_of_loads); | |
162 | |
163 void OnPrerenderCreated(TestPrerenderContents* contents); | |
164 | |
165 // PrerenderContents::Observer implementation: | |
166 void OnPrerenderStart(PrerenderContents* contents) override; | |
167 | |
168 void OnPrerenderStopLoading(PrerenderContents* contents) override; | |
169 | |
170 void OnPrerenderStop(PrerenderContents* contents) override; | |
171 | |
172 private: | |
173 TestPrerenderContents* contents_; | |
174 int number_of_loads_; | |
175 | |
176 int expected_number_of_loads_; | |
177 std::unique_ptr<base::RunLoop> load_waiter_; | |
178 | |
179 base::RunLoop create_loop_; | |
180 base::RunLoop start_loop_; | |
181 base::RunLoop stop_loop_; | |
182 | |
183 DISALLOW_COPY_AND_ASSIGN(TestPrerender); | |
184 }; | |
185 | |
186 // PrerenderManager that uses TestPrerenderContents. | |
187 class TestPrerenderContentsFactory : public PrerenderContents::Factory { | |
188 public: | |
189 TestPrerenderContentsFactory(); | |
190 | |
191 ~TestPrerenderContentsFactory() override; | |
192 | |
193 std::unique_ptr<TestPrerender> ExpectPrerenderContents( | |
194 FinalStatus final_status); | |
195 | |
196 PrerenderContents* CreatePrerenderContents( | |
197 PrerenderManager* prerender_manager, | |
198 Profile* profile, | |
199 const GURL& url, | |
200 const content::Referrer& referrer, | |
201 Origin origin) override; | |
202 | |
203 private: | |
204 struct ExpectedContents { | |
205 ExpectedContents(); | |
206 ExpectedContents(const ExpectedContents& other); | |
207 ExpectedContents(FinalStatus final_status, | |
208 const base::WeakPtr<TestPrerender>& handle); | |
209 ~ExpectedContents(); | |
210 | |
211 FinalStatus final_status; | |
212 base::WeakPtr<TestPrerender> handle; | |
213 }; | |
214 | |
215 std::deque<ExpectedContents> expected_contents_queue_; | |
216 }; | |
217 | |
218 class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { | |
pasko
2016/09/05 13:46:28
do you know why virtual inheritance is used here?
mattcary
2016/09/06 08:06:20
Maybe. Some other uses of InProcessBrowserTest I'v
pasko
2016/09/06 14:40:56
Acknowledged.
| |
219 public: | |
220 PrerenderInProcessBrowserTest(); | |
221 | |
222 ~PrerenderInProcessBrowserTest() override; | |
223 | |
224 void SetUpCommandLine(base::CommandLine* command_line) override; | |
225 void SetUpInProcessBrowserTestFixture() override; | |
226 void TearDownInProcessBrowserTestFixture() override; | |
227 void SetUpOnMainThread() override; | |
228 content::SessionStorageNamespace* GetSessionStorageNamespace() const; | |
229 | |
230 bool UrlIsInPrerenderManager(const std::string& html_file) const; | |
231 bool UrlIsInPrerenderManager(const GURL& url) const; | |
232 | |
233 // Convenience function to get the currently active WebContents in | |
234 // current_browser(). | |
235 content::WebContents* GetActiveWebContents() const; | |
236 | |
237 PrerenderManager* GetPrerenderManager() const; | |
238 | |
239 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; | |
240 | |
241 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
242 const std::string& html_file, | |
243 FinalStatus expected_final_status, | |
244 int expected_number_of_loads); | |
245 | |
246 ScopedVector<TestPrerender> PrerenderTestURL( | |
247 const std::string& html_file, | |
248 const std::vector<FinalStatus>& expected_final_status_queue, | |
249 int expected_number_of_loads); | |
250 | |
251 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
252 const GURL& url, | |
253 FinalStatus expected_final_status, | |
254 int expected_number_of_loads); | |
255 | |
256 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { | |
257 return safe_browsing_factory_.get(); | |
258 } | |
259 | |
260 TestPrerenderContentsFactory* prerender_contents_factory() const { | |
261 return prerender_contents_factory_; | |
262 } | |
263 | |
264 void set_autostart_test_server(bool value) { | |
265 autostart_test_server_ = value; | |
266 } | |
267 | |
268 void set_browser(Browser* browser) { | |
pasko
2016/09/05 13:46:28
nit: fits on one line
mattcary
2016/09/06 08:06:20
Done (git cl format run).
| |
269 explicitly_set_browser_ = browser; | |
270 } | |
271 | |
272 Browser* current_browser() const { | |
273 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); | |
274 } | |
275 | |
276 private: | |
277 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl( | |
pasko
2016/09/05 13:46:28
What does this method do? Why having an "Impl" and
mattcary
2016/09/06 08:06:20
This is copied from the existing browser test.
Th
pasko
2016/09/06 14:40:56
Ah, now I understand. It looked weird to have an a
| |
278 const GURL& prerender_url, | |
279 const std::vector<FinalStatus>& expected_final_status_queue, | |
280 int expected_number_of_loads) = 0; | |
281 | |
282 std::unique_ptr<ExternalProtocolHandler::Delegate> | |
283 external_protocol_handler_delegate_; | |
284 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> | |
285 safe_browsing_factory_; | |
286 TestPrerenderContentsFactory* prerender_contents_factory_; | |
287 Browser* explicitly_set_browser_; | |
288 bool autostart_test_server_; | |
289 }; | |
290 | |
291 // Makes |url| respond to requests with the contents of |file|, counting the | |
292 // number that start in |counter|. | |
293 void CreateCountingInterceptorOnIO( | |
294 const GURL& url, | |
295 const base::FilePath& file, | |
296 const base::WeakPtr<RequestCounter>& counter); | |
297 | |
298 // Makes |url| respond to requests with the contents of |file|. | |
299 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); | |
300 | |
301 } // namespace test_utils | |
302 | |
303 } // namespace prerender | |
304 | |
305 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | |
OLD | NEW |