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