Chromium Code Reviews| 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 <functional> | |
| 9 | |
| 8 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 9 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/test/histogram_tester.h" | |
| 10 #include "chrome/browser/external_protocol/external_protocol_handler.h" | 13 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| 11 #include "chrome/browser/prerender/prerender_contents.h" | 14 #include "chrome/browser/prerender/prerender_contents.h" |
| 15 #include "chrome/browser/prerender/prerender_manager.h" | |
| 12 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | 16 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "components/safe_browsing_db/test_database_manager.h" | 18 #include "components/safe_browsing_db/test_database_manager.h" |
| 15 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 16 #include "net/test/url_request/url_request_mock_http_job.h" | 20 #include "net/test/url_request/url_request_mock_http_job.h" |
| 17 #include "net/url_request/url_request_interceptor.h" | 21 #include "net/url_request/url_request_interceptor.h" |
| 18 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 19 | 23 |
| 20 namespace base { | 24 namespace base { |
| 21 class FilePath; | 25 class FilePath; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 int expected_number_of_loads_; | 179 int expected_number_of_loads_; |
| 176 std::unique_ptr<base::RunLoop> load_waiter_; | 180 std::unique_ptr<base::RunLoop> load_waiter_; |
| 177 | 181 |
| 178 base::RunLoop create_loop_; | 182 base::RunLoop create_loop_; |
| 179 base::RunLoop start_loop_; | 183 base::RunLoop start_loop_; |
| 180 base::RunLoop stop_loop_; | 184 base::RunLoop stop_loop_; |
| 181 | 185 |
| 182 DISALLOW_COPY_AND_ASSIGN(TestPrerender); | 186 DISALLOW_COPY_AND_ASSIGN(TestPrerender); |
| 183 }; | 187 }; |
| 184 | 188 |
| 185 // PrerenderManager that uses TestPrerenderContents. | 189 // Blocks until a TestPrerenderContents has been destroyed with the given final |
| 190 // status. Should be created with a TestPrerenderContents, and then | |
| 191 // WaitForDestroy should be called and its return value checked. | |
| 192 class DestructionWaiter { | |
| 193 public: | |
| 194 // Does not own the prerender_contents, which must outlive any call to | |
| 195 // WaitForDestroy(). | |
| 196 DestructionWaiter(TestPrerenderContents* prerender_contents, | |
| 197 FinalStatus expected_final_status); | |
| 198 | |
| 199 ~DestructionWaiter(); | |
| 200 | |
| 201 // Returns true if the TestPrerenderContents was destroyed with the correct | |
| 202 // final status, or false otherwise. Note this also may hang if the contents | |
| 203 // is never destroyed (which will presumably cause the test to time out). | |
| 204 bool WaitForDestroy(); | |
| 205 | |
| 206 private: | |
| 207 class DestructionMarker : public PrerenderContents::Observer { | |
| 208 public: | |
| 209 // Does not own the waiter which must outlive the TestPrerenderContents. | |
| 210 explicit DestructionMarker(DestructionWaiter* waiter); | |
| 211 | |
| 212 ~DestructionMarker() override; | |
| 213 | |
| 214 void OnPrerenderStop(PrerenderContents* contents) override; | |
| 215 | |
| 216 private: | |
| 217 DestructionWaiter* waiter_; | |
| 218 }; | |
| 219 | |
| 220 // To be called by a DestructionMarker. | |
| 221 void MarkDestruction(FinalStatus reason); | |
| 222 | |
| 223 base::RunLoop wait_loop_; | |
| 224 FinalStatus expected_final_status_; | |
| 225 bool saw_correct_status_; | |
| 226 std::unique_ptr<DestructionMarker> marker_; | |
| 227 }; | |
| 228 | |
| 229 // PrerenderContentsFactory that uses TestPrerenderContents. | |
| 186 class TestPrerenderContentsFactory : public PrerenderContents::Factory { | 230 class TestPrerenderContentsFactory : public PrerenderContents::Factory { |
| 187 public: | 231 public: |
| 188 TestPrerenderContentsFactory(); | 232 TestPrerenderContentsFactory(); |
| 189 | 233 |
| 190 ~TestPrerenderContentsFactory() override; | 234 ~TestPrerenderContentsFactory() override; |
| 191 | 235 |
| 192 std::unique_ptr<TestPrerender> ExpectPrerenderContents( | 236 std::unique_ptr<TestPrerender> ExpectPrerenderContents( |
| 193 FinalStatus final_status); | 237 FinalStatus final_status); |
| 194 | 238 |
| 195 PrerenderContents* CreatePrerenderContents( | 239 PrerenderContents* CreatePrerenderContents( |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 219 PrerenderInProcessBrowserTest(); | 263 PrerenderInProcessBrowserTest(); |
| 220 | 264 |
| 221 ~PrerenderInProcessBrowserTest() override; | 265 ~PrerenderInProcessBrowserTest() override; |
| 222 | 266 |
| 223 void SetUpCommandLine(base::CommandLine* command_line) override; | 267 void SetUpCommandLine(base::CommandLine* command_line) override; |
| 224 void SetUpInProcessBrowserTestFixture() override; | 268 void SetUpInProcessBrowserTestFixture() override; |
| 225 void TearDownInProcessBrowserTestFixture() override; | 269 void TearDownInProcessBrowserTestFixture() override; |
| 226 void SetUpOnMainThread() override; | 270 void SetUpOnMainThread() override; |
| 227 content::SessionStorageNamespace* GetSessionStorageNamespace() const; | 271 content::SessionStorageNamespace* GetSessionStorageNamespace() const; |
| 228 | 272 |
| 273 // Many of the file and server manipulation commands are fussy about paths | |
| 274 // being relative or absolute. This makes path absolute if it is not | |
| 275 // already. The path must not be empty. | |
| 276 std::string MakeAbsolute(const std::string& path); | |
| 277 | |
| 229 bool UrlIsInPrerenderManager(const std::string& html_file) const; | 278 bool UrlIsInPrerenderManager(const std::string& html_file) const; |
| 230 bool UrlIsInPrerenderManager(const GURL& url) const; | 279 bool UrlIsInPrerenderManager(const GURL& url) const; |
| 231 | 280 |
| 232 // Convenience function to get the currently active WebContents in | 281 // Convenience function to get the currently active WebContents in |
| 233 // current_browser(). | 282 // current_browser(). |
| 234 content::WebContents* GetActiveWebContents() const; | 283 content::WebContents* GetActiveWebContents() const; |
| 235 | 284 |
| 236 PrerenderManager* GetPrerenderManager() const; | 285 PrerenderManager* GetPrerenderManager() const; |
| 237 | 286 |
| 238 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; | 287 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; |
| 239 | 288 |
| 240 std::unique_ptr<TestPrerender> PrerenderTestURL( | 289 std::unique_ptr<TestPrerender> PrerenderTestURL( |
| 241 const std::string& html_file, | 290 const std::string& html_file, |
| 242 FinalStatus expected_final_status, | 291 FinalStatus expected_final_status, |
| 243 int expected_number_of_loads); | 292 int expected_number_of_loads); |
| 244 | 293 |
| 294 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
| 295 const GURL& url, | |
| 296 FinalStatus expected_final_status, | |
| 297 int expected_number_of_loads); | |
| 298 | |
| 245 ScopedVector<TestPrerender> PrerenderTestURL( | 299 ScopedVector<TestPrerender> PrerenderTestURL( |
| 246 const std::string& html_file, | 300 const std::string& html_file, |
| 247 const std::vector<FinalStatus>& expected_final_status_queue, | 301 const std::vector<FinalStatus>& expected_final_status_queue, |
| 248 int expected_number_of_loads); | 302 int expected_number_of_loads); |
| 249 | 303 |
| 250 std::unique_ptr<TestPrerender> PrerenderTestURL( | 304 void UseHttpsSrcServer(); |
| 251 const GURL& url, | 305 |
| 252 FinalStatus expected_final_status, | 306 // Returns the currently active server. See UseHttpsSrcServer. |
|
pasko
2016/10/11 13:56:23
nit: |UseHttpsSrcServer()|
mattcary
2016/10/11 15:08:58
Done.
| |
| 253 int expected_number_of_loads); | 307 net::EmbeddedTestServer* src_server(); |
| 254 | 308 |
| 255 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { | 309 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { |
| 256 return safe_browsing_factory_.get(); | 310 return safe_browsing_factory_.get(); |
| 257 } | 311 } |
| 258 | 312 |
| 313 test_utils::FakeSafeBrowsingDatabaseManager* | |
| 314 GetFakeSafeBrowsingDatabaseManager(); | |
| 315 | |
| 259 TestPrerenderContentsFactory* prerender_contents_factory() const { | 316 TestPrerenderContentsFactory* prerender_contents_factory() const { |
| 260 return prerender_contents_factory_; | 317 return prerender_contents_factory_; |
| 261 } | 318 } |
| 262 | 319 |
| 263 void set_autostart_test_server(bool value) { autostart_test_server_ = value; } | 320 void set_autostart_test_server(bool value) { autostart_test_server_ = value; } |
| 264 | 321 |
| 265 void set_browser(Browser* browser) { explicitly_set_browser_ = browser; } | 322 void set_browser(Browser* browser) { explicitly_set_browser_ = browser; } |
| 266 | 323 |
| 267 Browser* current_browser() const { | 324 Browser* current_browser() const { |
| 268 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); | 325 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); |
| 269 } | 326 } |
| 270 | 327 |
| 328 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | |
| 329 | |
| 330 // Returns a string for pattern-matching TaskManager tab entries. | |
| 331 base::string16 MatchTaskManagerTab(const char* page_title); | |
| 332 | |
| 333 // Returns a string for pattern-matching TaskManager prerender entries. | |
| 334 base::string16 MatchTaskManagerPrerender(const char* page_title); | |
| 335 | |
| 336 protected: | |
| 337 // To be called from PrerenderTestUrlImpl. Sets up the appropraite prerenders, | |
| 338 // checking for the expected final status, navigates to the loader url, and | |
| 339 // waits for the load. | |
| 340 ScopedVector<TestPrerender> NavigateWithPrerenders( | |
| 341 const GURL& loader_url, | |
| 342 const std::vector<FinalStatus>& expected_final_status_queue, | |
| 343 int expected_number_of_loads); | |
| 344 | |
| 271 private: | 345 private: |
| 272 // Implement load of a url for a prerender test. prerender_url should be | 346 // Implement load of a url for a prerender test. prerender_url should be |
| 273 // loaded, and we should expect to see one prerenderer created, and exit, for | 347 // loaded, and we should expect to see one prerenderer created, and exit, for |
| 274 // each entry in expected_final_status_queue, and seeing | 348 // each entry in expected_final_status_queue, and seeing |
| 275 // expected_number_of_loads. Specific tests can provide additional | 349 // expected_number_of_loads. Specific tests can provide additional |
| 276 // verification. Note this should be called by one of the convenience wrappers | 350 // verification. Note this should be called by one of the convenience wrappers |
| 277 // defined above. | 351 // defined above. |
| 278 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl( | 352 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl( |
| 279 const GURL& prerender_url, | 353 const GURL& prerender_url, |
| 280 const std::vector<FinalStatus>& expected_final_status_queue, | 354 const std::vector<FinalStatus>& expected_final_status_queue, |
| 281 int expected_number_of_loads) = 0; | 355 int expected_number_of_loads) = 0; |
| 282 | 356 |
| 283 std::unique_ptr<ExternalProtocolHandler::Delegate> | 357 std::unique_ptr<ExternalProtocolHandler::Delegate> |
| 284 external_protocol_handler_delegate_; | 358 external_protocol_handler_delegate_; |
| 285 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> | 359 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> |
| 286 safe_browsing_factory_; | 360 safe_browsing_factory_; |
| 287 TestPrerenderContentsFactory* prerender_contents_factory_; | 361 TestPrerenderContentsFactory* prerender_contents_factory_; |
| 288 Browser* explicitly_set_browser_; | 362 Browser* explicitly_set_browser_; |
| 289 bool autostart_test_server_; | 363 bool autostart_test_server_; |
| 364 base::HistogramTester histogram_tester_; | |
| 365 std::unique_ptr<net::EmbeddedTestServer> https_src_server_; | |
| 290 }; | 366 }; |
| 291 | 367 |
| 292 // Makes |url| respond to requests with the contents of |file|, counting the | 368 // Makes |url| respond to requests with the contents of |file|, counting the |
| 293 // number that start in |counter|. | 369 // number that start in |counter|. |
| 294 void CreateCountingInterceptorOnIO( | 370 void CreateCountingInterceptorOnIO( |
| 295 const GURL& url, | 371 const GURL& url, |
| 296 const base::FilePath& file, | 372 const base::FilePath& file, |
| 297 const base::WeakPtr<RequestCounter>& counter); | 373 const base::WeakPtr<RequestCounter>& counter); |
| 298 | 374 |
| 299 // Makes |url| respond to requests with the contents of |file|. | 375 // Makes |url| respond to requests with the contents of |file|. |
| 300 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); | 376 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); |
| 301 | 377 |
| 302 } // namespace test_utils | 378 } // namespace test_utils |
| 303 | 379 |
| 304 } // namespace prerender | 380 } // namespace prerender |
| 305 | 381 |
| 306 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | 382 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
| OLD | NEW |