OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
| 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
| 7 |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| 11 #include "chrome/browser/prerender/prerender_contents.h" |
| 12 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "components/safe_browsing_db/test_database_manager.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "net/test/url_request/url_request_mock_http_job.h" |
| 17 #include "net/url_request/url_request_interceptor.h" |
| 18 #include "url/gurl.h" |
| 19 |
| 20 namespace base { |
| 21 class FilePath; |
| 22 } // namespace base |
| 23 |
| 24 namespace net { |
| 25 class URLRequest; |
| 26 class NetworkDelegate; |
| 27 } // namespace net |
| 28 |
| 29 namespace prerender { |
| 30 |
| 31 namespace test_utils { |
| 32 |
| 33 // Dummy counter class to live on the UI thread for counting requests. |
| 34 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> { |
| 35 public: |
| 36 RequestCounter(); |
| 37 |
| 38 ~RequestCounter(); |
| 39 |
| 40 int count() const { return count_; } |
| 41 |
| 42 void RequestStarted(); |
| 43 void WaitForCount(int expected_count); |
| 44 |
| 45 private: |
| 46 int count_; |
| 47 int expected_count_; |
| 48 std::unique_ptr<base::RunLoop> loop_; |
| 49 }; |
| 50 |
| 51 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for |
| 52 // a given URL. |
| 53 class FakeSafeBrowsingDatabaseManager |
| 54 : public safe_browsing::TestSafeBrowsingDatabaseManager { |
| 55 public: |
| 56 FakeSafeBrowsingDatabaseManager(); |
| 57 |
| 58 // Called on the IO thread to check if the given url is safe or not. If we |
| 59 // can synchronously determine that the url is safe, CheckUrl returns true. |
| 60 // Otherwise it returns false, and "client" is called asynchronously with the |
| 61 // result when it is ready. |
| 62 // Returns true, indicating a SAFE result, unless the URL is the fixed URL |
| 63 // specified by the user, and the user-specified result is not SAFE |
| 64 // (in which that result will be communicated back via a call into the |
| 65 // client, and false will be returned). |
| 66 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl. |
| 67 bool CheckBrowseUrl(const GURL& gurl, Client* client) override; |
| 68 |
| 69 void SetThreatTypeForUrl(const GURL& url, |
| 70 safe_browsing::SBThreatType threat_type) { |
| 71 bad_urls_[url.spec()] = threat_type; |
| 72 } |
| 73 |
| 74 // These are called when checking URLs, so we implement them. |
| 75 bool IsSupported() const override; |
| 76 bool ChecksAreAlwaysAsync() const override; |
| 77 bool CanCheckResourceType( |
| 78 content::ResourceType /* resource_type */) const override; |
| 79 |
| 80 bool CheckExtensionIDs(const std::set<std::string>& extension_ids, |
| 81 Client* client) override; |
| 82 |
| 83 private: |
| 84 ~FakeSafeBrowsingDatabaseManager() override; |
| 85 |
| 86 void OnCheckBrowseURLDone(const GURL& gurl, Client* client); |
| 87 |
| 88 std::unordered_map<std::string, safe_browsing::SBThreatType> bad_urls_; |
| 89 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); |
| 90 }; |
| 91 |
| 92 // PrerenderContents that stops the UI message loop on DidStopLoading(). |
| 93 class TestPrerenderContents : public PrerenderContents { |
| 94 public: |
| 95 TestPrerenderContents(PrerenderManager* prerender_manager, |
| 96 Profile* profile, |
| 97 const GURL& url, |
| 98 const content::Referrer& referrer, |
| 99 Origin origin, |
| 100 FinalStatus expected_final_status); |
| 101 |
| 102 ~TestPrerenderContents() override; |
| 103 |
| 104 void RenderProcessGone(base::TerminationStatus status) override; |
| 105 bool CheckURL(const GURL& url) override; |
| 106 |
| 107 // 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 |
| 109 // even though it is used. |
| 110 void set_should_be_shown(bool value) { should_be_shown_ = value; } |
| 111 |
| 112 // For tests which do not know whether the prerender will be used. |
| 113 void set_skip_final_checks(bool value) { skip_final_checks_ = value; } |
| 114 |
| 115 FinalStatus expected_final_status() const { return expected_final_status_; } |
| 116 |
| 117 private: |
| 118 void OnRenderViewHostCreated( |
| 119 content::RenderViewHost* new_render_view_host) override; |
| 120 void Observe(int type, |
| 121 const content::NotificationSource& source, |
| 122 const content::NotificationDetails& details) override; |
| 123 |
| 124 FinalStatus expected_final_status_; |
| 125 |
| 126 // The RenderViewHost created for the prerender, if any. |
| 127 content::RenderViewHost* new_render_view_host_; |
| 128 // Set to true when the prerendering RenderWidget is hidden. |
| 129 bool was_hidden_; |
| 130 // Set to true when the prerendering RenderWidget is shown, after having been |
| 131 // hidden. |
| 132 bool was_shown_; |
| 133 // Expected final value of was_shown_. Defaults to true for |
| 134 // FINAL_STATUS_USED, and false otherwise. |
| 135 bool should_be_shown_; |
| 136 // If true, |expected_final_status_| and other shutdown checks are skipped. |
| 137 bool skip_final_checks_; |
| 138 }; |
| 139 |
| 140 // A handle to a TestPrerenderContents whose lifetime is under the caller's |
| 141 // control. A PrerenderContents may be destroyed at any point. This allows |
| 142 // tracking the final status, etc. |
| 143 class TestPrerender : public PrerenderContents::Observer, |
| 144 public base::SupportsWeakPtr<TestPrerender> { |
| 145 public: |
| 146 TestPrerender(); |
| 147 ~TestPrerender() override; |
| 148 |
| 149 TestPrerenderContents* contents() const { return contents_; } |
| 150 int number_of_loads() const { return number_of_loads_; } |
| 151 |
| 152 void WaitForCreate() { create_loop_.Run(); } |
| 153 void WaitForStart() { start_loop_.Run(); } |
| 154 void WaitForStop() { stop_loop_.Run(); } |
| 155 |
| 156 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR |
| 157 // for the prerender to stop running (just to avoid a timeout if the prerender |
| 158 // dies). Note: this does not assert equality on the number of loads; the |
| 159 // caller must do it instead. |
| 160 void WaitForLoads(int expected_number_of_loads); |
| 161 |
| 162 void OnPrerenderCreated(TestPrerenderContents* contents); |
| 163 |
| 164 // PrerenderContents::Observer implementation: |
| 165 void OnPrerenderStart(PrerenderContents* contents) override; |
| 166 |
| 167 void OnPrerenderStopLoading(PrerenderContents* contents) override; |
| 168 |
| 169 void OnPrerenderStop(PrerenderContents* contents) override; |
| 170 |
| 171 private: |
| 172 TestPrerenderContents* contents_; |
| 173 int number_of_loads_; |
| 174 |
| 175 int expected_number_of_loads_; |
| 176 std::unique_ptr<base::RunLoop> load_waiter_; |
| 177 |
| 178 base::RunLoop create_loop_; |
| 179 base::RunLoop start_loop_; |
| 180 base::RunLoop stop_loop_; |
| 181 |
| 182 DISALLOW_COPY_AND_ASSIGN(TestPrerender); |
| 183 }; |
| 184 |
| 185 // PrerenderManager that uses TestPrerenderContents. |
| 186 class TestPrerenderContentsFactory : public PrerenderContents::Factory { |
| 187 public: |
| 188 TestPrerenderContentsFactory(); |
| 189 |
| 190 ~TestPrerenderContentsFactory() override; |
| 191 |
| 192 std::unique_ptr<TestPrerender> ExpectPrerenderContents( |
| 193 FinalStatus final_status); |
| 194 |
| 195 PrerenderContents* CreatePrerenderContents( |
| 196 PrerenderManager* prerender_manager, |
| 197 Profile* profile, |
| 198 const GURL& url, |
| 199 const content::Referrer& referrer, |
| 200 Origin origin) override; |
| 201 |
| 202 private: |
| 203 struct ExpectedContents { |
| 204 ExpectedContents(); |
| 205 ExpectedContents(const ExpectedContents& other); |
| 206 ExpectedContents(FinalStatus final_status, |
| 207 const base::WeakPtr<TestPrerender>& handle); |
| 208 ~ExpectedContents(); |
| 209 |
| 210 FinalStatus final_status; |
| 211 base::WeakPtr<TestPrerender> handle; |
| 212 }; |
| 213 |
| 214 std::deque<ExpectedContents> expected_contents_queue_; |
| 215 }; |
| 216 |
| 217 class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { |
| 218 public: |
| 219 PrerenderInProcessBrowserTest(); |
| 220 |
| 221 ~PrerenderInProcessBrowserTest() override; |
| 222 |
| 223 void SetUpCommandLine(base::CommandLine* command_line) override; |
| 224 void SetUpInProcessBrowserTestFixture() override; |
| 225 void TearDownInProcessBrowserTestFixture() override; |
| 226 void SetUpOnMainThread() override; |
| 227 content::SessionStorageNamespace* GetSessionStorageNamespace() const; |
| 228 |
| 229 bool UrlIsInPrerenderManager(const std::string& html_file) const; |
| 230 bool UrlIsInPrerenderManager(const GURL& url) const; |
| 231 |
| 232 // Convenience function to get the currently active WebContents in |
| 233 // current_browser(). |
| 234 content::WebContents* GetActiveWebContents() const; |
| 235 |
| 236 PrerenderManager* GetPrerenderManager() const; |
| 237 |
| 238 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; |
| 239 |
| 240 std::unique_ptr<TestPrerender> PrerenderTestURL( |
| 241 const std::string& html_file, |
| 242 FinalStatus expected_final_status, |
| 243 int expected_number_of_loads); |
| 244 |
| 245 ScopedVector<TestPrerender> PrerenderTestURL( |
| 246 const std::string& html_file, |
| 247 const std::vector<FinalStatus>& expected_final_status_queue, |
| 248 int expected_number_of_loads); |
| 249 |
| 250 std::unique_ptr<TestPrerender> PrerenderTestURL( |
| 251 const GURL& url, |
| 252 FinalStatus expected_final_status, |
| 253 int expected_number_of_loads); |
| 254 |
| 255 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { |
| 256 return safe_browsing_factory_.get(); |
| 257 } |
| 258 |
| 259 TestPrerenderContentsFactory* prerender_contents_factory() const { |
| 260 return prerender_contents_factory_; |
| 261 } |
| 262 |
| 263 void set_autostart_test_server(bool value) { autostart_test_server_ = value; } |
| 264 |
| 265 void set_browser(Browser* browser) { explicitly_set_browser_ = browser; } |
| 266 |
| 267 Browser* current_browser() const { |
| 268 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); |
| 269 } |
| 270 |
| 271 private: |
| 272 // 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 |
| 274 // each entry in expected_final_status_queue, and seeing |
| 275 // expected_number_of_loads. Specific tests can provide additional |
| 276 // verification. Note this should be called by one of the convenience wrappers |
| 277 // defined above. |
| 278 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl( |
| 279 const GURL& prerender_url, |
| 280 const std::vector<FinalStatus>& expected_final_status_queue, |
| 281 int expected_number_of_loads) = 0; |
| 282 |
| 283 std::unique_ptr<ExternalProtocolHandler::Delegate> |
| 284 external_protocol_handler_delegate_; |
| 285 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> |
| 286 safe_browsing_factory_; |
| 287 TestPrerenderContentsFactory* prerender_contents_factory_; |
| 288 Browser* explicitly_set_browser_; |
| 289 bool autostart_test_server_; |
| 290 }; |
| 291 |
| 292 // Makes |url| respond to requests with the contents of |file|, counting the |
| 293 // number that start in |counter|. |
| 294 void CreateCountingInterceptorOnIO( |
| 295 const GURL& url, |
| 296 const base::FilePath& file, |
| 297 const base::WeakPtr<RequestCounter>& counter); |
| 298 |
| 299 // Makes |url| respond to requests with the contents of |file|. |
| 300 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); |
| 301 |
| 302 } // namespace test_utils |
| 303 |
| 304 } // namespace prerender |
| 305 |
| 306 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ |
OLD | NEW |