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" |
| 12 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | 15 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "components/safe_browsing_db/test_database_manager.h" | 17 #include "components/safe_browsing_db/test_database_manager.h" |
| 15 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 16 #include "net/test/url_request/url_request_mock_http_job.h" | 19 #include "net/test/url_request/url_request_mock_http_job.h" |
| 17 #include "net/url_request/url_request_interceptor.h" | 20 #include "net/url_request/url_request_interceptor.h" |
| 18 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 19 | 22 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 | 88 |
| 86 void OnCheckBrowseURLDone(const GURL& gurl, Client* client); | 89 void OnCheckBrowseURLDone(const GURL& gurl, Client* client); |
| 87 | 90 |
| 88 std::unordered_map<std::string, safe_browsing::SBThreatType> bad_urls_; | 91 std::unordered_map<std::string, safe_browsing::SBThreatType> bad_urls_; |
| 89 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); | 92 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); |
| 90 }; | 93 }; |
| 91 | 94 |
| 92 // PrerenderContents that stops the UI message loop on DidStopLoading(). | 95 // PrerenderContents that stops the UI message loop on DidStopLoading(). |
| 93 class TestPrerenderContents : public PrerenderContents { | 96 class TestPrerenderContents : public PrerenderContents { |
| 94 public: | 97 public: |
| 98 class DestroyObserver { | |
| 99 public: | |
| 100 virtual ~DestroyObserver(); | |
| 101 virtual void OnDestroy(FinalStatus reason) = 0; | |
| 102 }; | |
| 103 | |
| 95 TestPrerenderContents(PrerenderManager* prerender_manager, | 104 TestPrerenderContents(PrerenderManager* prerender_manager, |
| 96 Profile* profile, | 105 Profile* profile, |
| 97 const GURL& url, | 106 const GURL& url, |
| 98 const content::Referrer& referrer, | 107 const content::Referrer& referrer, |
| 99 Origin origin, | 108 Origin origin, |
| 100 FinalStatus expected_final_status); | 109 FinalStatus expected_final_status); |
| 101 | 110 |
| 102 ~TestPrerenderContents() override; | 111 ~TestPrerenderContents() override; |
| 103 | 112 |
| 104 void RenderProcessGone(base::TerminationStatus status) override; | 113 void RenderProcessGone(base::TerminationStatus status) override; |
| 105 bool CheckURL(const GURL& url) override; | 114 bool CheckURL(const GURL& url) override; |
| 106 | 115 |
| 107 // For tests that open the prerender in a new background tab, the RenderView | 116 // For tests that open the prerender in a new background tab, the RenderView |
| 108 // will not have been made visible when the PrerenderContents is destroyed | 117 // will not have been made visible when the PrerenderContents is destroyed |
| 109 // even though it is used. | 118 // even though it is used. |
| 110 void set_should_be_shown(bool value) { should_be_shown_ = value; } | 119 void set_should_be_shown(bool value) { should_be_shown_ = value; } |
| 111 | 120 |
| 112 // For tests which do not know whether the prerender will be used. | 121 // For tests which do not know whether the prerender will be used. |
| 113 void set_skip_final_checks(bool value) { skip_final_checks_ = value; } | 122 void set_skip_final_checks(bool value) { skip_final_checks_ = value; } |
| 114 | 123 |
| 115 FinalStatus expected_final_status() const { return expected_final_status_; } | 124 FinalStatus expected_final_status() const { return expected_final_status_; } |
| 116 | 125 |
| 126 // Will own the observer. | |
| 127 void AddDestroyObserver(std::unique_ptr<DestroyObserver> observer); | |
| 128 | |
| 129 void Destroy(FinalStatus reason) override; | |
| 130 | |
| 117 private: | 131 private: |
| 118 void OnRenderViewHostCreated( | 132 void OnRenderViewHostCreated( |
| 119 content::RenderViewHost* new_render_view_host) override; | 133 content::RenderViewHost* new_render_view_host) override; |
| 120 void Observe(int type, | 134 void Observe(int type, |
| 121 const content::NotificationSource& source, | 135 const content::NotificationSource& source, |
| 122 const content::NotificationDetails& details) override; | 136 const content::NotificationDetails& details) override; |
| 123 | 137 |
| 124 FinalStatus expected_final_status_; | 138 FinalStatus expected_final_status_; |
| 125 | 139 |
| 126 // The RenderViewHost created for the prerender, if any. | 140 // The RenderViewHost created for the prerender, if any. |
| 127 content::RenderViewHost* new_render_view_host_; | 141 content::RenderViewHost* new_render_view_host_; |
| 128 // Set to true when the prerendering RenderWidget is hidden. | 142 // Set to true when the prerendering RenderWidget is hidden. |
| 129 bool was_hidden_; | 143 bool was_hidden_; |
| 130 // Set to true when the prerendering RenderWidget is shown, after having been | 144 // Set to true when the prerendering RenderWidget is shown, after having been |
| 131 // hidden. | 145 // hidden. |
| 132 bool was_shown_; | 146 bool was_shown_; |
| 133 // Expected final value of was_shown_. Defaults to true for | 147 // Expected final value of was_shown_. Defaults to true for |
| 134 // FINAL_STATUS_USED, and false otherwise. | 148 // FINAL_STATUS_USED, and false otherwise. |
| 135 bool should_be_shown_; | 149 bool should_be_shown_; |
| 136 // If true, |expected_final_status_| and other shutdown checks are skipped. | 150 // If true, |expected_final_status_| and other shutdown checks are skipped. |
| 137 bool skip_final_checks_; | 151 bool skip_final_checks_; |
| 152 std::vector<std::unique_ptr<DestroyObserver>> destroy_observers_; | |
|
droger
2016/09/20 10:57:20
Use ObserverList instead (base/observer_list.h)
mattcary
2016/09/21 08:45:10
thanks, but moot now.
| |
| 138 }; | 153 }; |
| 139 | 154 |
| 140 // A handle to a TestPrerenderContents whose lifetime is under the caller's | 155 // A handle to a TestPrerenderContents whose lifetime is under the caller's |
| 141 // control. A PrerenderContents may be destroyed at any point. This allows | 156 // control. A PrerenderContents may be destroyed at any point. This allows |
| 142 // tracking the final status, etc. | 157 // tracking the final status, etc. |
| 143 class TestPrerender : public PrerenderContents::Observer, | 158 class TestPrerender : public PrerenderContents::Observer, |
| 144 public base::SupportsWeakPtr<TestPrerender> { | 159 public base::SupportsWeakPtr<TestPrerender> { |
| 145 public: | 160 public: |
| 146 TestPrerender(); | 161 TestPrerender(); |
| 147 ~TestPrerender() override; | 162 ~TestPrerender() override; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 175 int expected_number_of_loads_; | 190 int expected_number_of_loads_; |
| 176 std::unique_ptr<base::RunLoop> load_waiter_; | 191 std::unique_ptr<base::RunLoop> load_waiter_; |
| 177 | 192 |
| 178 base::RunLoop create_loop_; | 193 base::RunLoop create_loop_; |
| 179 base::RunLoop start_loop_; | 194 base::RunLoop start_loop_; |
| 180 base::RunLoop stop_loop_; | 195 base::RunLoop stop_loop_; |
| 181 | 196 |
| 182 DISALLOW_COPY_AND_ASSIGN(TestPrerender); | 197 DISALLOW_COPY_AND_ASSIGN(TestPrerender); |
| 183 }; | 198 }; |
| 184 | 199 |
| 200 // Blocks until a TestPrerenderContents has been destroyed with the given final | |
| 201 // status. Should be created with a TestPrerenderContents, and then | |
| 202 // WaitForDestroy should be called and its return value checked. | |
| 203 class DestructionWaiter { | |
| 204 public: | |
| 205 // Does not own the prerender_contents, which must outlive any call to | |
| 206 // WaitForDestroy(). | |
| 207 DestructionWaiter(TestPrerenderContents* prerender_contents, | |
| 208 FinalStatus expected_final_status); | |
| 209 | |
| 210 // Returns true if the TestPrerenderContents was destroyed with the correct | |
| 211 // final status, or false otherwise. Note this also may hang if the contents | |
| 212 // is never destroyed (which will presumably cause the test to time out). | |
| 213 bool WaitForDestroy(); | |
| 214 | |
| 215 private: | |
| 216 class DestructionMarker : public TestPrerenderContents::DestroyObserver { | |
| 217 public: | |
| 218 // Does not on the waiter which must outlive the TestPrerenderContents. | |
|
droger
2016/09/20 10:57:20
I don't understand the comment, maybe there is a w
mattcary
2016/09/21 08:45:10
Done. Only a letter missing, but an important one
| |
| 219 explicit DestructionMarker(DestructionWaiter* waiter); | |
| 220 void OnDestroy(FinalStatus reason) override; | |
| 221 | |
| 222 private: | |
| 223 DestructionWaiter* waiter_; | |
| 224 }; | |
| 225 | |
| 226 // To be called by a DestructionMarker. | |
| 227 void MarkDestruction(FinalStatus reason); | |
| 228 | |
| 229 base::RunLoop wait_loop_; | |
| 230 FinalStatus expected_final_status_; | |
| 231 bool saw_correct_status_; | |
| 232 }; | |
| 233 | |
| 185 // PrerenderManager that uses TestPrerenderContents. | 234 // PrerenderManager that uses TestPrerenderContents. |
| 186 class TestPrerenderContentsFactory : public PrerenderContents::Factory { | 235 class TestPrerenderContentsFactory : public PrerenderContents::Factory { |
| 187 public: | 236 public: |
| 188 TestPrerenderContentsFactory(); | 237 TestPrerenderContentsFactory(); |
| 189 | 238 |
| 190 ~TestPrerenderContentsFactory() override; | 239 ~TestPrerenderContentsFactory() override; |
| 191 | 240 |
| 192 std::unique_ptr<TestPrerender> ExpectPrerenderContents( | 241 std::unique_ptr<TestPrerender> ExpectPrerenderContents( |
| 193 FinalStatus final_status); | 242 FinalStatus final_status); |
| 194 | 243 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 219 PrerenderInProcessBrowserTest(); | 268 PrerenderInProcessBrowserTest(); |
| 220 | 269 |
| 221 ~PrerenderInProcessBrowserTest() override; | 270 ~PrerenderInProcessBrowserTest() override; |
| 222 | 271 |
| 223 void SetUpCommandLine(base::CommandLine* command_line) override; | 272 void SetUpCommandLine(base::CommandLine* command_line) override; |
| 224 void SetUpInProcessBrowserTestFixture() override; | 273 void SetUpInProcessBrowserTestFixture() override; |
| 225 void TearDownInProcessBrowserTestFixture() override; | 274 void TearDownInProcessBrowserTestFixture() override; |
| 226 void SetUpOnMainThread() override; | 275 void SetUpOnMainThread() override; |
| 227 content::SessionStorageNamespace* GetSessionStorageNamespace() const; | 276 content::SessionStorageNamespace* GetSessionStorageNamespace() const; |
| 228 | 277 |
| 278 // Many of the file and server manipulation commands are fussy about paths | |
| 279 // being relative or absolute. This makes path absolute if it is not | |
| 280 // already. The path must not be empty. | |
| 281 std::string MakeAbsolute(const std::string& path); | |
| 282 | |
| 229 bool UrlIsInPrerenderManager(const std::string& html_file) const; | 283 bool UrlIsInPrerenderManager(const std::string& html_file) const; |
| 230 bool UrlIsInPrerenderManager(const GURL& url) const; | 284 bool UrlIsInPrerenderManager(const GURL& url) const; |
| 231 | 285 |
| 232 // Convenience function to get the currently active WebContents in | 286 // Convenience function to get the currently active WebContents in |
| 233 // current_browser(). | 287 // current_browser(). |
| 234 content::WebContents* GetActiveWebContents() const; | 288 content::WebContents* GetActiveWebContents() const; |
| 235 | 289 |
| 236 PrerenderManager* GetPrerenderManager() const; | 290 PrerenderManager* GetPrerenderManager() const; |
| 237 | 291 |
| 238 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; | 292 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; |
| 239 | 293 |
| 240 std::unique_ptr<TestPrerender> PrerenderTestURL( | 294 std::unique_ptr<TestPrerender> PrerenderTestURL( |
| 241 const std::string& html_file, | 295 const std::string& html_file, |
| 242 FinalStatus expected_final_status, | 296 FinalStatus expected_final_status, |
| 243 int expected_number_of_loads); | 297 int expected_number_of_loads); |
| 244 | 298 |
| 299 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
| 300 const GURL& url, | |
| 301 FinalStatus expected_final_status, | |
| 302 int expected_number_of_loads); | |
| 303 | |
| 245 ScopedVector<TestPrerender> PrerenderTestURL( | 304 ScopedVector<TestPrerender> PrerenderTestURL( |
| 246 const std::string& html_file, | 305 const std::string& html_file, |
| 247 const std::vector<FinalStatus>& expected_final_status_queue, | 306 const std::vector<FinalStatus>& expected_final_status_queue, |
| 248 int expected_number_of_loads); | 307 int expected_number_of_loads); |
| 249 | 308 |
| 250 std::unique_ptr<TestPrerender> PrerenderTestURL( | 309 // Set up an HTTPS server. The lambda passed in is run after the server is |
| 251 const GURL& url, | 310 // created but before Start() is called. |
| 252 FinalStatus expected_final_status, | 311 void UseHttpsSrcServer(std::function<void(net::EmbeddedTestServer*)> startup); |
| 253 int expected_number_of_loads); | 312 void UseHttpsSrcServer() { |
| 313 UseHttpsSrcServer([](net::EmbeddedTestServer*) {}); | |
| 314 } | |
| 315 | |
| 316 // Returns the currently active server (see UseHttpsSrcServer). | |
| 317 net::EmbeddedTestServer* src_server() { | |
| 318 if (https_src_server_) | |
| 319 return https_src_server_.get(); | |
| 320 return embedded_test_server(); | |
| 321 } | |
| 254 | 322 |
| 255 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { | 323 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { |
| 256 return safe_browsing_factory_.get(); | 324 return safe_browsing_factory_.get(); |
| 257 } | 325 } |
| 258 | 326 |
| 327 test_utils::FakeSafeBrowsingDatabaseManager* | |
| 328 GetFakeSafeBrowsingDatabaseManager() { | |
| 329 return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>( | |
| 330 safe_browsing_factory() | |
| 331 ->test_safe_browsing_service() | |
| 332 ->database_manager() | |
| 333 .get()); | |
| 334 } | |
| 335 | |
| 259 TestPrerenderContentsFactory* prerender_contents_factory() const { | 336 TestPrerenderContentsFactory* prerender_contents_factory() const { |
| 260 return prerender_contents_factory_; | 337 return prerender_contents_factory_; |
| 261 } | 338 } |
| 262 | 339 |
| 263 void set_autostart_test_server(bool value) { autostart_test_server_ = value; } | 340 void set_autostart_test_server(bool value) { autostart_test_server_ = value; } |
| 264 | 341 |
| 265 void set_browser(Browser* browser) { explicitly_set_browser_ = browser; } | 342 void set_browser(Browser* browser) { explicitly_set_browser_ = browser; } |
| 266 | 343 |
| 267 Browser* current_browser() const { | 344 Browser* current_browser() const { |
| 268 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); | 345 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); |
| 269 } | 346 } |
| 270 | 347 |
| 348 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | |
| 349 | |
| 350 // Returns a string for pattern-matching TaskManager tab entries. | |
| 351 base::string16 MatchTaskManagerTab(const char* page_title); | |
| 352 | |
| 353 // Returns a string for pattern-matching TaskManager prerender entries. | |
| 354 base::string16 MatchTaskManagerPrerender(const char* page_title); | |
| 355 | |
| 356 protected: | |
| 357 // To be called from PrerenderTestUrlImpl. Sets up the appropraite prerenders, | |
| 358 // checking for the expected final status, navigates to the loader url, and | |
| 359 // waits for the load. | |
| 360 ScopedVector<TestPrerender> NavigateWithPrerenders( | |
|
droger
2016/09/20 10:57:20
same: std::vector<std::unique_ptr<>>
mattcary
2016/09/21 08:45:10
This will be fixed when I tear out the scoped vect
| |
| 361 GURL loader_url, | |
|
droger
2016/09/20 10:57:20
const GURL&
mattcary
2016/09/21 08:45:10
Done.
| |
| 362 const std::vector<FinalStatus>& expected_final_status_queue, | |
| 363 int expected_number_of_loads); | |
| 364 | |
| 271 private: | 365 private: |
| 272 // Implement load of a url for a prerender test. prerender_url should be | 366 // 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 | 367 // loaded, and we should expect to see one prerenderer created, and exit, for |
| 274 // each entry in expected_final_status_queue, and seeing | 368 // each entry in expected_final_status_queue, and seeing |
| 275 // expected_number_of_loads. Specific tests can provide additional | 369 // expected_number_of_loads. Specific tests can provide additional |
| 276 // verification. Note this should be called by one of the convenience wrappers | 370 // verification. Note this should be called by one of the convenience wrappers |
| 277 // defined above. | 371 // defined above. |
| 278 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl( | 372 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl( |
| 279 const GURL& prerender_url, | 373 const GURL& prerender_url, |
| 280 const std::vector<FinalStatus>& expected_final_status_queue, | 374 const std::vector<FinalStatus>& expected_final_status_queue, |
| 281 int expected_number_of_loads) = 0; | 375 int expected_number_of_loads) = 0; |
| 282 | 376 |
| 283 std::unique_ptr<ExternalProtocolHandler::Delegate> | 377 std::unique_ptr<ExternalProtocolHandler::Delegate> |
| 284 external_protocol_handler_delegate_; | 378 external_protocol_handler_delegate_; |
| 285 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> | 379 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> |
| 286 safe_browsing_factory_; | 380 safe_browsing_factory_; |
| 287 TestPrerenderContentsFactory* prerender_contents_factory_; | 381 TestPrerenderContentsFactory* prerender_contents_factory_; |
| 288 Browser* explicitly_set_browser_; | 382 Browser* explicitly_set_browser_; |
| 289 bool autostart_test_server_; | 383 bool autostart_test_server_; |
| 384 base::HistogramTester histogram_tester_; | |
| 385 std::unique_ptr<net::EmbeddedTestServer> https_src_server_; | |
| 290 }; | 386 }; |
| 291 | 387 |
| 292 // Makes |url| respond to requests with the contents of |file|, counting the | 388 // Makes |url| respond to requests with the contents of |file|, counting the |
| 293 // number that start in |counter|. | 389 // number that start in |counter|. |
| 294 void CreateCountingInterceptorOnIO( | 390 void CreateCountingInterceptorOnIO( |
| 295 const GURL& url, | 391 const GURL& url, |
| 296 const base::FilePath& file, | 392 const base::FilePath& file, |
| 297 const base::WeakPtr<RequestCounter>& counter); | 393 const base::WeakPtr<RequestCounter>& counter); |
| 298 | 394 |
| 299 // Makes |url| respond to requests with the contents of |file|. | 395 // Makes |url| respond to requests with the contents of |file|. |
| 300 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); | 396 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); |
| 301 | 397 |
| 302 } // namespace test_utils | 398 } // namespace test_utils |
| 303 | 399 |
| 304 } // namespace prerender | 400 } // namespace prerender |
| 305 | 401 |
| 306 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ | 402 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
| OLD | NEW |