OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <functional> | 9 #include <functional> |
10 #include <memory> | 10 #include <memory> |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 // Set to true when the prerendering RenderWidget is hidden. | 137 // Set to true when the prerendering RenderWidget is hidden. |
138 bool was_hidden_; | 138 bool was_hidden_; |
139 // Set to true when the prerendering RenderWidget is shown, after having been | 139 // Set to true when the prerendering RenderWidget is shown, after having been |
140 // hidden. | 140 // hidden. |
141 bool was_shown_; | 141 bool was_shown_; |
142 // Expected final value of was_shown_. Defaults to true for | 142 // Expected final value of was_shown_. Defaults to true for |
143 // FINAL_STATUS_USED, and false otherwise. | 143 // FINAL_STATUS_USED, and false otherwise. |
144 bool should_be_shown_; | 144 bool should_be_shown_; |
145 // If true, |expected_final_status_| and other shutdown checks are skipped. | 145 // If true, |expected_final_status_| and other shutdown checks are skipped. |
146 bool skip_final_checks_; | 146 bool skip_final_checks_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(TestPrerenderContents); |
147 }; | 149 }; |
148 | 150 |
149 // A handle to a TestPrerenderContents whose lifetime is under the caller's | 151 // A handle to a TestPrerenderContents whose lifetime is under the caller's |
150 // control. A PrerenderContents may be destroyed at any point. This allows | 152 // control. A PrerenderContents may be destroyed at any point. This allows |
151 // tracking the final status, etc. | 153 // tracking the FinalStatus. |
152 class TestPrerender : public PrerenderContents::Observer, | 154 class TestPrerender : public PrerenderContents::Observer, |
153 public base::SupportsWeakPtr<TestPrerender> { | 155 public base::SupportsWeakPtr<TestPrerender> { |
154 public: | 156 public: |
155 TestPrerender(); | 157 TestPrerender(); |
156 ~TestPrerender() override; | 158 ~TestPrerender() override; |
157 | 159 |
158 TestPrerenderContents* contents() const { return contents_; } | 160 TestPrerenderContents* contents() const { return contents_; } |
159 int number_of_loads() const { return number_of_loads_; } | 161 int number_of_loads() const { return number_of_loads_; } |
| 162 FinalStatus GetFinalStatus() const; |
160 | 163 |
161 void WaitForCreate(); | 164 void WaitForCreate(); |
162 void WaitForStart(); | 165 void WaitForStart(); |
163 void WaitForStop(); | 166 void WaitForStop(); |
164 | 167 |
165 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR | 168 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR |
166 // for the prerender to stop running (just to avoid a timeout if the prerender | 169 // for the prerender to stop running (just to avoid a timeout if the prerender |
167 // dies). Note: this does not assert equality on the number of loads; the | 170 // dies). Note: this does not assert equality on the number of loads; the |
168 // caller must do it instead. | 171 // caller must do it instead. |
169 void WaitForLoads(int expected_number_of_loads); | 172 void WaitForLoads(int expected_number_of_loads); |
170 | 173 |
171 void OnPrerenderCreated(TestPrerenderContents* contents); | 174 void OnPrerenderCreated(TestPrerenderContents* contents); |
172 | 175 |
173 // PrerenderContents::Observer implementation: | 176 // PrerenderContents::Observer implementation: |
174 void OnPrerenderStart(PrerenderContents* contents) override; | 177 void OnPrerenderStart(PrerenderContents* contents) override; |
175 | 178 |
176 void OnPrerenderStopLoading(PrerenderContents* contents) override; | 179 void OnPrerenderStopLoading(PrerenderContents* contents) override; |
177 | 180 |
178 void OnPrerenderStop(PrerenderContents* contents) override; | 181 void OnPrerenderStop(PrerenderContents* contents) override; |
179 | 182 |
180 private: | 183 private: |
181 TestPrerenderContents* contents_; | 184 TestPrerenderContents* contents_; |
| 185 FinalStatus final_status_; |
182 int number_of_loads_; | 186 int number_of_loads_; |
183 | 187 |
184 int expected_number_of_loads_; | 188 int expected_number_of_loads_; |
185 std::unique_ptr<base::RunLoop> load_waiter_; | 189 std::unique_ptr<base::RunLoop> load_waiter_; |
186 | 190 |
187 bool started_; | 191 bool started_; |
188 bool stopped_; | 192 bool stopped_; |
189 | 193 |
190 base::RunLoop create_loop_; | 194 base::RunLoop create_loop_; |
191 base::RunLoop start_loop_; | 195 base::RunLoop start_loop_; |
(...skipping 24 matching lines...) Expand all Loading... |
216 public: | 220 public: |
217 // Does not own the waiter which must outlive the TestPrerenderContents. | 221 // Does not own the waiter which must outlive the TestPrerenderContents. |
218 explicit DestructionMarker(DestructionWaiter* waiter); | 222 explicit DestructionMarker(DestructionWaiter* waiter); |
219 | 223 |
220 ~DestructionMarker() override; | 224 ~DestructionMarker() override; |
221 | 225 |
222 void OnPrerenderStop(PrerenderContents* contents) override; | 226 void OnPrerenderStop(PrerenderContents* contents) override; |
223 | 227 |
224 private: | 228 private: |
225 DestructionWaiter* waiter_; | 229 DestructionWaiter* waiter_; |
| 230 |
| 231 DISALLOW_COPY_AND_ASSIGN(DestructionMarker); |
226 }; | 232 }; |
227 | 233 |
228 // To be called by a DestructionMarker. | 234 // To be called by a DestructionMarker. |
229 void MarkDestruction(FinalStatus reason); | 235 void MarkDestruction(FinalStatus reason); |
230 | 236 |
231 base::RunLoop wait_loop_; | 237 base::RunLoop wait_loop_; |
232 FinalStatus expected_final_status_; | 238 FinalStatus expected_final_status_; |
233 bool saw_correct_status_; | 239 bool saw_correct_status_; |
234 std::unique_ptr<DestructionMarker> marker_; | 240 std::unique_ptr<DestructionMarker> marker_; |
| 241 |
| 242 DISALLOW_COPY_AND_ASSIGN(DestructionWaiter); |
235 }; | 243 }; |
236 | 244 |
237 // PrerenderContentsFactory that uses TestPrerenderContents. | 245 // PrerenderContentsFactory that uses TestPrerenderContents. |
238 class TestPrerenderContentsFactory : public PrerenderContents::Factory { | 246 class TestPrerenderContentsFactory : public PrerenderContents::Factory { |
239 public: | 247 public: |
240 TestPrerenderContentsFactory(); | 248 TestPrerenderContentsFactory(); |
241 | 249 |
242 ~TestPrerenderContentsFactory() override; | 250 ~TestPrerenderContentsFactory() override; |
243 | 251 |
244 std::unique_ptr<TestPrerender> ExpectPrerenderContents( | 252 std::unique_ptr<TestPrerender> ExpectPrerenderContents( |
(...skipping 12 matching lines...) Expand all Loading... |
257 ExpectedContents(const ExpectedContents& other); | 265 ExpectedContents(const ExpectedContents& other); |
258 ExpectedContents(FinalStatus final_status, | 266 ExpectedContents(FinalStatus final_status, |
259 const base::WeakPtr<TestPrerender>& handle); | 267 const base::WeakPtr<TestPrerender>& handle); |
260 ~ExpectedContents(); | 268 ~ExpectedContents(); |
261 | 269 |
262 FinalStatus final_status; | 270 FinalStatus final_status; |
263 base::WeakPtr<TestPrerender> handle; | 271 base::WeakPtr<TestPrerender> handle; |
264 }; | 272 }; |
265 | 273 |
266 std::deque<ExpectedContents> expected_contents_queue_; | 274 std::deque<ExpectedContents> expected_contents_queue_; |
| 275 |
| 276 DISALLOW_COPY_AND_ASSIGN(TestPrerenderContentsFactory); |
267 }; | 277 }; |
268 | 278 |
269 class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { | 279 class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { |
270 public: | 280 public: |
271 PrerenderInProcessBrowserTest(); | 281 PrerenderInProcessBrowserTest(); |
272 | 282 |
273 ~PrerenderInProcessBrowserTest() override; | 283 ~PrerenderInProcessBrowserTest() override; |
274 | 284 |
275 void SetUpCommandLine(base::CommandLine* command_line) override; | 285 void SetUpCommandLine(base::CommandLine* command_line) override; |
276 void SetUpInProcessBrowserTestFixture() override; | 286 void SetUpInProcessBrowserTestFixture() override; |
(...skipping 10 matching lines...) Expand all Loading... |
287 bool UrlIsInPrerenderManager(const GURL& url) const; | 297 bool UrlIsInPrerenderManager(const GURL& url) const; |
288 | 298 |
289 // Convenience function to get the currently active WebContents in | 299 // Convenience function to get the currently active WebContents in |
290 // current_browser(). | 300 // current_browser(). |
291 content::WebContents* GetActiveWebContents() const; | 301 content::WebContents* GetActiveWebContents() const; |
292 | 302 |
293 PrerenderManager* GetPrerenderManager() const; | 303 PrerenderManager* GetPrerenderManager() const; |
294 | 304 |
295 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; | 305 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; |
296 | 306 |
297 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
298 const std::string& html_file, | |
299 FinalStatus expected_final_status, | |
300 int expected_number_of_loads); | |
301 | |
302 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
303 const GURL& url, | |
304 FinalStatus expected_final_status, | |
305 int expected_number_of_loads); | |
306 | |
307 std::vector<std::unique_ptr<TestPrerender>> PrerenderTestURL( | |
308 const std::string& html_file, | |
309 const std::vector<FinalStatus>& expected_final_status_queue, | |
310 int expected_number_of_loads); | |
311 | |
312 // Set up an HTTPS server. | 307 // Set up an HTTPS server. |
313 void UseHttpsSrcServer(); | 308 void UseHttpsSrcServer(); |
314 | 309 |
315 // Returns the currently active server. See |UseHttpsSrcServer|. | 310 // Returns the currently active server. See |UseHttpsSrcServer|. |
316 net::EmbeddedTestServer* src_server(); | 311 net::EmbeddedTestServer* src_server(); |
317 | 312 |
318 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { | 313 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { |
319 return safe_browsing_factory_.get(); | 314 return safe_browsing_factory_.get(); |
320 } | 315 } |
321 | 316 |
(...skipping 14 matching lines...) Expand all Loading... |
336 | 331 |
337 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | 332 const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
338 | 333 |
339 // Returns a string for pattern-matching TaskManager tab entries. | 334 // Returns a string for pattern-matching TaskManager tab entries. |
340 base::string16 MatchTaskManagerTab(const char* page_title); | 335 base::string16 MatchTaskManagerTab(const char* page_title); |
341 | 336 |
342 // Returns a string for pattern-matching TaskManager prerender entries. | 337 // Returns a string for pattern-matching TaskManager prerender entries. |
343 base::string16 MatchTaskManagerPrerender(const char* page_title); | 338 base::string16 MatchTaskManagerPrerender(const char* page_title); |
344 | 339 |
345 protected: | 340 protected: |
346 // To be called from PrerenderTestUrlImpl. Sets up the appropraite prerenders, | 341 // For each FinalStatus in |expected_final_status_queue| creates a prerender |
347 // checking for the expected final status, navigates to the loader url, and | 342 // that is going to verify the correctness of its FinalStatus upon |
348 // waits for the load. | 343 // destruction. Waits for creation of the first PrerenderContents. |
349 std::vector<std::unique_ptr<TestPrerender>> NavigateWithPrerenders( | 344 std::vector<std::unique_ptr<TestPrerender>> NavigateWithPrerenders( |
350 const GURL& loader_url, | 345 const GURL& loader_url, |
351 const std::vector<FinalStatus>& expected_final_status_queue, | 346 const std::vector<FinalStatus>& expected_final_status_queue); |
352 int expected_number_of_loads); | 347 |
| 348 // Creates the URL that instructs the test server to substitute the text |
| 349 // |replacement_variable| in the contents of the file pointed to by |
| 350 // |loader_path| with |url_to_prerender|. Also appends the |loader_query| to |
| 351 // the URL. |
| 352 GURL ServeLoaderURL(const std::string& loader_path, |
| 353 const std::string& replacement_variable, |
| 354 const GURL& url_to_prerender, |
| 355 const std::string& loader_query); |
353 | 356 |
354 private: | 357 private: |
355 // Implement load of a url for a prerender test. prerender_url should be | |
356 // loaded, and we should expect to see one prerenderer created, and exit, for | |
357 // each entry in expected_final_status_queue, and seeing | |
358 // expected_number_of_loads. Specific tests can provide additional | |
359 // verification. Note this should be called by one of the convenience wrappers | |
360 // defined above. | |
361 virtual std::vector<std::unique_ptr<TestPrerender>> PrerenderTestURLImpl( | |
362 const GURL& prerender_url, | |
363 const std::vector<FinalStatus>& expected_final_status_queue, | |
364 int expected_number_of_loads) = 0; | |
365 | |
366 std::unique_ptr<ExternalProtocolHandler::Delegate> | 358 std::unique_ptr<ExternalProtocolHandler::Delegate> |
367 external_protocol_handler_delegate_; | 359 external_protocol_handler_delegate_; |
368 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> | 360 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> |
369 safe_browsing_factory_; | 361 safe_browsing_factory_; |
370 TestPrerenderContentsFactory* prerender_contents_factory_; | 362 TestPrerenderContentsFactory* prerender_contents_factory_; |
371 Browser* explicitly_set_browser_; | 363 Browser* explicitly_set_browser_; |
372 bool autostart_test_server_; | 364 bool autostart_test_server_; |
373 base::HistogramTester histogram_tester_; | 365 base::HistogramTester histogram_tester_; |
374 std::unique_ptr<net::EmbeddedTestServer> https_src_server_; | 366 std::unique_ptr<net::EmbeddedTestServer> https_src_server_; |
| 367 |
| 368 DISALLOW_COPY_AND_ASSIGN(PrerenderInProcessBrowserTest); |
375 }; | 369 }; |
376 | 370 |
377 // Makes |url| respond to requests with the contents of |file|, counting the | 371 // Makes |url| respond to requests with the contents of |file|, counting the |
378 // number that start in |counter|. | 372 // number that start in |counter|. |
379 void CreateCountingInterceptorOnIO( | 373 void CreateCountingInterceptorOnIO( |
380 const GURL& url, | 374 const GURL& url, |
381 const base::FilePath& file, | 375 const base::FilePath& file, |
382 const base::WeakPtr<RequestCounter>& counter); | 376 const base::WeakPtr<RequestCounter>& counter); |
383 | 377 |
384 // Makes |url| respond to requests with the contents of |file|. | 378 // Makes |url| respond to requests with the contents of |file|. |
385 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); | 379 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); |
386 | 380 |
387 } // namespace test_utils | 381 } // namespace test_utils |
388 | 382 |
389 } // namespace prerender | 383 } // namespace prerender |
390 | 384 |
391 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | 385 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
OLD | NEW |