Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: chrome/browser/prerender/prerender_test_utils.h

Issue 2304953002: NoState Prefetch: nostate prefetch browser tests. (Closed)
Patch Set: histogram name fixes Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 11 matching lines...) Expand all
207 const base::WeakPtr<TestPrerender>& handle); 251 const base::WeakPtr<TestPrerender>& handle);
208 ~ExpectedContents(); 252 ~ExpectedContents();
209 253
210 FinalStatus final_status; 254 FinalStatus final_status;
211 base::WeakPtr<TestPrerender> handle; 255 base::WeakPtr<TestPrerender> handle;
212 }; 256 };
213 257
214 std::deque<ExpectedContents> expected_contents_queue_; 258 std::deque<ExpectedContents> expected_contents_queue_;
215 }; 259 };
216 260
261 // A PrerenderManager with custom time handling. PrerenderInProcessBrowserTest
262 // will set the testing factor for the PrerenderManagerFactory.
263 class TestPrerenderManager : public PrerenderManager {
264 public:
265 explicit TestPrerenderManager(Profile* profile);
266
267 // Returns the real current time, or a time set by AdvanceTime.
268 base::Time GetCurrentTime() const override;
269 base::TimeTicks GetCurrentTimeTicks() const override;
270
271 // Advance time by |delta|. Once this is called, the real time is no longer
272 // used, and GetCurrentTime*() will only reflect changes by AdvanceTime().
273 void AdvanceTime(base::TimeDelta delta);
274
275 private:
276 base::Time time_;
277 base::TimeTicks time_ticks_;
278 base::TimeDelta delta_;
279 };
280
217 class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest { 281 class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest {
218 public: 282 public:
219 PrerenderInProcessBrowserTest(); 283 PrerenderInProcessBrowserTest();
220 284
221 ~PrerenderInProcessBrowserTest() override; 285 ~PrerenderInProcessBrowserTest() override;
222 286
223 void SetUpCommandLine(base::CommandLine* command_line) override; 287 void SetUpCommandLine(base::CommandLine* command_line) override;
224 void SetUpInProcessBrowserTestFixture() override; 288 void SetUpInProcessBrowserTestFixture() override;
225 void TearDownInProcessBrowserTestFixture() override; 289 void TearDownInProcessBrowserTestFixture() override;
226 void SetUpOnMainThread() override; 290 void SetUpOnMainThread() override;
227 content::SessionStorageNamespace* GetSessionStorageNamespace() const; 291 content::SessionStorageNamespace* GetSessionStorageNamespace() const;
228 292
293 // Many of the file and server manipulation commands are fussy about paths
294 // being relative or absolute. This makes path absolute if it is not
295 // already. The path must not be empty.
296 std::string MakeAbsolute(const std::string& path);
297
229 bool UrlIsInPrerenderManager(const std::string& html_file) const; 298 bool UrlIsInPrerenderManager(const std::string& html_file) const;
230 bool UrlIsInPrerenderManager(const GURL& url) const; 299 bool UrlIsInPrerenderManager(const GURL& url) const;
231 300
232 // Convenience function to get the currently active WebContents in 301 // Convenience function to get the currently active WebContents in
233 // current_browser(). 302 // current_browser().
234 content::WebContents* GetActiveWebContents() const; 303 content::WebContents* GetActiveWebContents() const;
235 304
236 PrerenderManager* GetPrerenderManager() const; 305 PrerenderManager* GetPrerenderManager() const;
306 TestPrerenderManager* GetTestPrerenderManager() const {
307 return static_cast<TestPrerenderManager*>(GetPrerenderManager());
pasko 2016/09/30 18:29:21 Problems with having a new TestPrerenderManager as
mattcary 2016/10/03 10:58:35 I really dislike mixing testing overrides into pro
pasko 2016/10/03 18:54:16 Yes please. I agree that mocking out methods for t
308 }
237 309
238 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const; 310 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const;
239 311
240 std::unique_ptr<TestPrerender> PrerenderTestURL( 312 std::unique_ptr<TestPrerender> PrerenderTestURL(
241 const std::string& html_file, 313 const std::string& html_file,
242 FinalStatus expected_final_status, 314 FinalStatus expected_final_status,
243 int expected_number_of_loads); 315 int expected_number_of_loads);
244 316
317 std::unique_ptr<TestPrerender> PrerenderTestURL(
318 const GURL& url,
319 FinalStatus expected_final_status,
320 int expected_number_of_loads);
321
245 ScopedVector<TestPrerender> PrerenderTestURL( 322 ScopedVector<TestPrerender> PrerenderTestURL(
246 const std::string& html_file, 323 const std::string& html_file,
247 const std::vector<FinalStatus>& expected_final_status_queue, 324 const std::vector<FinalStatus>& expected_final_status_queue,
248 int expected_number_of_loads); 325 int expected_number_of_loads);
249 326
250 std::unique_ptr<TestPrerender> PrerenderTestURL( 327 // Set up an HTTPS server. The lambda passed in is run after the server is
251 const GURL& url, 328 // created but before Start() is called.
252 FinalStatus expected_final_status, 329 void UseHttpsSrcServer(std::function<void(net::EmbeddedTestServer*)> startup);
pasko 2016/09/30 18:29:21 This form with std::function is not called anywher
mattcary 2016/10/03 10:58:35 Oops, I guess this was a git screwup to have this
pasko 2016/10/03 18:54:16 Yes, the type of lambda is hidden from the languag
mattcary 2016/10/04 08:25:12 Acknowledged.
253 int expected_number_of_loads); 330 void UseHttpsSrcServer() {
331 UseHttpsSrcServer([](net::EmbeddedTestServer*) {});
332 }
333
334 // Returns the currently active server (see UseHttpsSrcServer).
pasko 2016/09/30 18:29:21 nit: // Returns the currently active server. See
mattcary 2016/10/03 10:58:35 Done.
335 net::EmbeddedTestServer* src_server() {
pasko 2016/09/30 18:29:21 non-trivial implementation -> .cc
mattcary 2016/10/03 10:58:35 Done.
336 if (https_src_server_)
337 return https_src_server_.get();
338 return embedded_test_server();
339 }
254 340
255 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const { 341 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const {
256 return safe_browsing_factory_.get(); 342 return safe_browsing_factory_.get();
257 } 343 }
258 344
345 test_utils::FakeSafeBrowsingDatabaseManager*
346 GetFakeSafeBrowsingDatabaseManager() {
pasko 2016/09/30 18:29:21 non-trivial implementation -> .cc
mattcary 2016/10/03 10:58:35 Done.
347 return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>(
348 safe_browsing_factory()
349 ->test_safe_browsing_service()
350 ->database_manager()
351 .get());
352 }
353
259 TestPrerenderContentsFactory* prerender_contents_factory() const { 354 TestPrerenderContentsFactory* prerender_contents_factory() const {
260 return prerender_contents_factory_; 355 return prerender_contents_factory_;
261 } 356 }
262 357
263 void set_autostart_test_server(bool value) { autostart_test_server_ = value; } 358 void set_autostart_test_server(bool value) { autostart_test_server_ = value; }
264 359
265 void set_browser(Browser* browser) { explicitly_set_browser_ = browser; } 360 void set_browser(Browser* browser) { explicitly_set_browser_ = browser; }
266 361
267 Browser* current_browser() const { 362 Browser* current_browser() const {
268 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); 363 return explicitly_set_browser_ ? explicitly_set_browser_ : browser();
269 } 364 }
270 365
366 const base::HistogramTester& histogram_tester() { return histogram_tester_; }
367
368 // Returns a string for pattern-matching TaskManager tab entries.
369 base::string16 MatchTaskManagerTab(const char* page_title);
370
371 // Returns a string for pattern-matching TaskManager prerender entries.
372 base::string16 MatchTaskManagerPrerender(const char* page_title);
373
374 protected:
375 // To be called from PrerenderTestUrlImpl. Sets up the appropraite prerenders,
376 // checking for the expected final status, navigates to the loader url, and
377 // waits for the load.
378 ScopedVector<TestPrerender> NavigateWithPrerenders(
379 const GURL& loader_url,
380 const std::vector<FinalStatus>& expected_final_status_queue,
381 int expected_number_of_loads);
382
271 private: 383 private:
272 // Implement load of a url for a prerender test. prerender_url should be 384 // 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 385 // loaded, and we should expect to see one prerenderer created, and exit, for
274 // each entry in expected_final_status_queue, and seeing 386 // each entry in expected_final_status_queue, and seeing
275 // expected_number_of_loads. Specific tests can provide additional 387 // expected_number_of_loads. Specific tests can provide additional
276 // verification. Note this should be called by one of the convenience wrappers 388 // verification. Note this should be called by one of the convenience wrappers
277 // defined above. 389 // defined above.
278 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl( 390 virtual ScopedVector<TestPrerender> PrerenderTestURLImpl(
279 const GURL& prerender_url, 391 const GURL& prerender_url,
280 const std::vector<FinalStatus>& expected_final_status_queue, 392 const std::vector<FinalStatus>& expected_final_status_queue,
281 int expected_number_of_loads) = 0; 393 int expected_number_of_loads) = 0;
282 394
283 std::unique_ptr<ExternalProtocolHandler::Delegate> 395 std::unique_ptr<ExternalProtocolHandler::Delegate>
284 external_protocol_handler_delegate_; 396 external_protocol_handler_delegate_;
285 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> 397 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory>
286 safe_browsing_factory_; 398 safe_browsing_factory_;
287 TestPrerenderContentsFactory* prerender_contents_factory_; 399 TestPrerenderContentsFactory* prerender_contents_factory_;
288 Browser* explicitly_set_browser_; 400 Browser* explicitly_set_browser_;
289 bool autostart_test_server_; 401 bool autostart_test_server_;
402 base::HistogramTester histogram_tester_;
403 std::unique_ptr<net::EmbeddedTestServer> https_src_server_;
290 }; 404 };
291 405
292 // Makes |url| respond to requests with the contents of |file|, counting the 406 // Makes |url| respond to requests with the contents of |file|, counting the
293 // number that start in |counter|. 407 // number that start in |counter|.
294 void CreateCountingInterceptorOnIO( 408 void CreateCountingInterceptorOnIO(
295 const GURL& url, 409 const GURL& url,
296 const base::FilePath& file, 410 const base::FilePath& file,
297 const base::WeakPtr<RequestCounter>& counter); 411 const base::WeakPtr<RequestCounter>& counter);
298 412
299 // Makes |url| respond to requests with the contents of |file|. 413 // Makes |url| respond to requests with the contents of |file|.
300 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file); 414 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file);
301 415
302 } // namespace test_utils 416 } // namespace test_utils
303 417
304 } // namespace prerender 418 } // namespace prerender
305 419
306 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_ 420 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698