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