Chromium Code Reviews| Index: chrome/browser/prerender/prerender_test_utils.h |
| diff --git a/chrome/browser/prerender/prerender_test_utils.h b/chrome/browser/prerender/prerender_test_utils.h |
| index 280cde00336ed448eb263eb1673394b9a3cd5ab7..a84044fc0445c32687c8c8ea7a1382631df06854 100644 |
| --- a/chrome/browser/prerender/prerender_test_utils.h |
| +++ b/chrome/browser/prerender/prerender_test_utils.h |
| @@ -5,8 +5,11 @@ |
| #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
| #define CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
| +#include <functional> |
| + |
| #include "base/memory/weak_ptr.h" |
| #include "base/run_loop.h" |
| +#include "base/test/histogram_tester.h" |
| #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| #include "chrome/browser/prerender/prerender_contents.h" |
| #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| @@ -92,6 +95,12 @@ class FakeSafeBrowsingDatabaseManager |
| // PrerenderContents that stops the UI message loop on DidStopLoading(). |
| class TestPrerenderContents : public PrerenderContents { |
| public: |
| + class DestroyObserver { |
| + public: |
| + virtual ~DestroyObserver(); |
| + virtual void OnDestroy(FinalStatus reason) = 0; |
| + }; |
| + |
| TestPrerenderContents(PrerenderManager* prerender_manager, |
| Profile* profile, |
| const GURL& url, |
| @@ -114,6 +123,11 @@ class TestPrerenderContents : public PrerenderContents { |
| FinalStatus expected_final_status() const { return expected_final_status_; } |
| + // Will own the observer. |
| + void AddDestroyObserver(std::unique_ptr<DestroyObserver> observer); |
| + |
| + void Destroy(FinalStatus reason) override; |
| + |
| private: |
| void OnRenderViewHostCreated( |
| content::RenderViewHost* new_render_view_host) override; |
| @@ -135,6 +149,7 @@ class TestPrerenderContents : public PrerenderContents { |
| bool should_be_shown_; |
| // If true, |expected_final_status_| and other shutdown checks are skipped. |
| bool skip_final_checks_; |
| + 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.
|
| }; |
| // A handle to a TestPrerenderContents whose lifetime is under the caller's |
| @@ -182,6 +197,40 @@ class TestPrerender : public PrerenderContents::Observer, |
| DISALLOW_COPY_AND_ASSIGN(TestPrerender); |
| }; |
| +// Blocks until a TestPrerenderContents has been destroyed with the given final |
| +// status. Should be created with a TestPrerenderContents, and then |
| +// WaitForDestroy should be called and its return value checked. |
| +class DestructionWaiter { |
| + public: |
| + // Does not own the prerender_contents, which must outlive any call to |
| + // WaitForDestroy(). |
| + DestructionWaiter(TestPrerenderContents* prerender_contents, |
| + FinalStatus expected_final_status); |
| + |
| + // Returns true if the TestPrerenderContents was destroyed with the correct |
| + // final status, or false otherwise. Note this also may hang if the contents |
| + // is never destroyed (which will presumably cause the test to time out). |
| + bool WaitForDestroy(); |
| + |
| + private: |
| + class DestructionMarker : public TestPrerenderContents::DestroyObserver { |
| + public: |
| + // 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
|
| + explicit DestructionMarker(DestructionWaiter* waiter); |
| + void OnDestroy(FinalStatus reason) override; |
| + |
| + private: |
| + DestructionWaiter* waiter_; |
| + }; |
| + |
| + // To be called by a DestructionMarker. |
| + void MarkDestruction(FinalStatus reason); |
| + |
| + base::RunLoop wait_loop_; |
| + FinalStatus expected_final_status_; |
| + bool saw_correct_status_; |
| +}; |
| + |
| // PrerenderManager that uses TestPrerenderContents. |
| class TestPrerenderContentsFactory : public PrerenderContents::Factory { |
| public: |
| @@ -226,6 +275,11 @@ class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { |
| void SetUpOnMainThread() override; |
| content::SessionStorageNamespace* GetSessionStorageNamespace() const; |
| + // Many of the file and server manipulation commands are fussy about paths |
| + // being relative or absolute. This makes path absolute if it is not |
| + // already. The path must not be empty. |
| + std::string MakeAbsolute(const std::string& path); |
| + |
| bool UrlIsInPrerenderManager(const std::string& html_file) const; |
| bool UrlIsInPrerenderManager(const GURL& url) const; |
| @@ -242,20 +296,43 @@ class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { |
| FinalStatus expected_final_status, |
| int expected_number_of_loads); |
| + std::unique_ptr<TestPrerender> PrerenderTestURL( |
| + const GURL& url, |
| + FinalStatus expected_final_status, |
| + int expected_number_of_loads); |
| + |
| ScopedVector<TestPrerender> PrerenderTestURL( |
| const std::string& html_file, |
| const std::vector<FinalStatus>& expected_final_status_queue, |
| int expected_number_of_loads); |
| - std::unique_ptr<TestPrerender> PrerenderTestURL( |
| - const GURL& url, |
| - FinalStatus expected_final_status, |
| - int expected_number_of_loads); |
| + // Set up an HTTPS server. The lambda passed in is run after the server is |
| + // created but before Start() is called. |
| + void UseHttpsSrcServer(std::function<void(net::EmbeddedTestServer*)> startup); |
| + void UseHttpsSrcServer() { |
| + UseHttpsSrcServer([](net::EmbeddedTestServer*) {}); |
| + } |
| + |
| + // Returns the currently active server (see UseHttpsSrcServer). |
| + net::EmbeddedTestServer* src_server() { |
| + if (https_src_server_) |
| + return https_src_server_.get(); |
| + return embedded_test_server(); |
| + } |
| safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { |
| return safe_browsing_factory_.get(); |
| } |
| + test_utils::FakeSafeBrowsingDatabaseManager* |
| + GetFakeSafeBrowsingDatabaseManager() { |
| + return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>( |
| + safe_browsing_factory() |
| + ->test_safe_browsing_service() |
| + ->database_manager() |
| + .get()); |
| + } |
| + |
| TestPrerenderContentsFactory* prerender_contents_factory() const { |
| return prerender_contents_factory_; |
| } |
| @@ -268,6 +345,23 @@ class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { |
| return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); |
| } |
| + const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
| + |
| + // Returns a string for pattern-matching TaskManager tab entries. |
| + base::string16 MatchTaskManagerTab(const char* page_title); |
| + |
| + // Returns a string for pattern-matching TaskManager prerender entries. |
| + base::string16 MatchTaskManagerPrerender(const char* page_title); |
| + |
| + protected: |
| + // To be called from PrerenderTestUrlImpl. Sets up the appropraite prerenders, |
| + // checking for the expected final status, navigates to the loader url, and |
| + // waits for the load. |
| + 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
|
| + GURL loader_url, |
|
droger
2016/09/20 10:57:20
const GURL&
mattcary
2016/09/21 08:45:10
Done.
|
| + const std::vector<FinalStatus>& expected_final_status_queue, |
| + int expected_number_of_loads); |
| + |
| private: |
| // Implement load of a url for a prerender test. prerender_url should be |
| // loaded, and we should expect to see one prerenderer created, and exit, for |
| @@ -287,6 +381,8 @@ class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { |
| TestPrerenderContentsFactory* prerender_contents_factory_; |
| Browser* explicitly_set_browser_; |
| bool autostart_test_server_; |
| + base::HistogramTester histogram_tester_; |
| + std::unique_ptr<net::EmbeddedTestServer> https_src_server_; |
| }; |
| // Makes |url| respond to requests with the contents of |file|, counting the |