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

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

Issue 2304953002: NoState Prefetch: nostate prefetch browser tests. (Closed)
Patch Set: All browser tests added and cleaned up for prefetch-specific issues. Created 4 years, 3 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/browser/prerender/prerender_test_utils.h" 5 #include "chrome/browser/prerender/prerender_test_utils.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/prerender/prerender_manager.h" 9 #include "chrome/browser/prerender/prerender_manager.h"
9 #include "chrome/browser/prerender/prerender_manager_factory.h" 10 #include "chrome/browser/prerender/prerender_manager_factory.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate. h" 12 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate. h"
12 #include "chrome/browser/safe_browsing/local_database_manager.h" 13 #include "chrome/browser/safe_browsing/local_database_manager.h"
13 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/prefs/pref_service.h" 19 #include "components/prefs/pref_service.h"
17 #include "content/public/browser/notification_details.h" 20 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_source.h" 21 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/notification_types.h" 22 #include "content/public/browser/notification_types.h"
20 #include "content/public/browser/render_view_host.h" 23 #include "content/public/browser/render_view_host.h"
21 #include "content/public/common/content_switches.h" 24 #include "content/public/common/content_switches.h"
22 #include "content/public/common/url_constants.h" 25 #include "content/public/common/url_constants.h"
23 #include "content/public/test/ppapi_test_utils.h" 26 #include "content/public/test/ppapi_test_utils.h"
24 #include "net/url_request/url_request_filter.h" 27 #include "net/url_request/url_request_filter.h"
25 #include "ppapi/shared_impl/ppapi_switches.h" 28 #include "ppapi/shared_impl/ppapi_switches.h"
26 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/base/l10n/l10n_util.h"
27 31
28 using content::BrowserThread; 32 using content::BrowserThread;
29 using content::RenderViewHost; 33 using content::RenderViewHost;
30 34
31 namespace prerender { 35 namespace prerender {
32 36
33 namespace test_utils { 37 namespace test_utils {
34 38
35 namespace { 39 namespace {
36 40
(...skipping 19 matching lines...) Expand all
56 start_callback_.Run(); 60 start_callback_.Run();
57 net::URLRequestMockHTTPJob::Start(); 61 net::URLRequestMockHTTPJob::Start();
58 } 62 }
59 63
60 private: 64 private:
61 ~MockHTTPJob() override {} 65 ~MockHTTPJob() override {}
62 66
63 base::Closure start_callback_; 67 base::Closure start_callback_;
64 }; 68 };
65 69
66 // Protocol handler which counts the number of requests that start. 70 // URLRequestInterceptor which counts the number of requests that start.
67 class CountingInterceptor : public net::URLRequestInterceptor { 71 class CountingInterceptor : public net::URLRequestInterceptor {
68 public: 72 public:
69 CountingInterceptor(const base::FilePath& file, 73 CountingInterceptor(const base::FilePath& file,
70 const base::WeakPtr<RequestCounter>& counter) 74 const base::WeakPtr<RequestCounter>& counter)
71 : file_(file), counter_(counter), weak_factory_(this) {} 75 : file_(file), counter_(counter), weak_factory_(this) {}
72 ~CountingInterceptor() override {} 76 ~CountingInterceptor() override {}
73 77
74 net::URLRequestJob* MaybeInterceptRequest( 78 net::URLRequestJob* MaybeInterceptRequest(
75 net::URLRequest* request, 79 net::URLRequest* request,
76 net::NetworkDelegate* network_delegate) const override { 80 net::NetworkDelegate* network_delegate) const override {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 expected_threats.push_back(safe_browsing::SB_THREAT_TYPE_URL_PHISHING); 203 expected_threats.push_back(safe_browsing::SB_THREAT_TYPE_URL_PHISHING);
200 // TODO(nparker): Replace SafeBrowsingCheck w/ a call to 204 // TODO(nparker): Replace SafeBrowsingCheck w/ a call to
201 // client->OnCheckBrowseUrlResult() 205 // client->OnCheckBrowseUrlResult()
202 safe_browsing::LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check( 206 safe_browsing::LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check(
203 std::vector<GURL>(1, gurl), std::vector<safe_browsing::SBFullHash>(), 207 std::vector<GURL>(1, gurl), std::vector<safe_browsing::SBFullHash>(),
204 client, safe_browsing::MALWARE, expected_threats); 208 client, safe_browsing::MALWARE, expected_threats);
205 sb_check.url_results[0] = bad_urls_[gurl.spec()]; 209 sb_check.url_results[0] = bad_urls_[gurl.spec()];
206 sb_check.OnSafeBrowsingResult(); 210 sb_check.OnSafeBrowsingResult();
207 } 211 }
208 212
213 TestPrerenderContents::DestroyObserver::~DestroyObserver() {}
214
209 TestPrerenderContents::TestPrerenderContents( 215 TestPrerenderContents::TestPrerenderContents(
210 PrerenderManager* prerender_manager, 216 PrerenderManager* prerender_manager,
211 Profile* profile, 217 Profile* profile,
212 const GURL& url, 218 const GURL& url,
213 const content::Referrer& referrer, 219 const content::Referrer& referrer,
214 Origin origin, 220 Origin origin,
215 FinalStatus expected_final_status) 221 FinalStatus expected_final_status)
216 : PrerenderContents(prerender_manager, profile, url, referrer, origin), 222 : PrerenderContents(prerender_manager, profile, url, referrer, origin),
217 expected_final_status_(expected_final_status), 223 expected_final_status_(expected_final_status),
218 new_render_view_host_(nullptr), 224 new_render_view_host_(nullptr),
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 } 270 }
265 271
266 bool TestPrerenderContents::CheckURL(const GURL& url) { 272 bool TestPrerenderContents::CheckURL(const GURL& url) {
267 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in 273 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
268 // the PrerenderRendererCrash test. 274 // the PrerenderRendererCrash test.
269 if (url.spec() != content::kChromeUICrashURL) 275 if (url.spec() != content::kChromeUICrashURL)
270 return PrerenderContents::CheckURL(url); 276 return PrerenderContents::CheckURL(url);
271 return true; 277 return true;
272 } 278 }
273 279
280 void TestPrerenderContents::AddDestroyObserver(
281 std::unique_ptr<DestroyObserver> observer) {
282 if (final_status() != FINAL_STATUS_MAX) {
283 // This contents has already been destroyed, so update the observer
284 // immediately.
285 observer->OnDestroy(final_status());
286 }
287 destroy_observers_.push_back(std::move(observer));
288 }
289
290 void TestPrerenderContents::Destroy(FinalStatus reason) {
291 for (const auto& observer : destroy_observers_) {
292 observer->OnDestroy(reason);
293 }
294 PrerenderContents::Destroy(reason);
295 }
296
274 void TestPrerenderContents::OnRenderViewHostCreated( 297 void TestPrerenderContents::OnRenderViewHostCreated(
275 RenderViewHost* new_render_view_host) { 298 RenderViewHost* new_render_view_host) {
276 // Used to make sure the RenderViewHost is hidden and, if used, 299 // Used to make sure the RenderViewHost is hidden and, if used,
277 // subsequently shown. 300 // subsequently shown.
278 notification_registrar().Add( 301 notification_registrar().Add(
279 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, 302 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
280 content::Source<content::RenderWidgetHost>( 303 content::Source<content::RenderWidgetHost>(
281 new_render_view_host->GetWidget())); 304 new_render_view_host->GetWidget()));
282 305
283 new_render_view_host_ = new_render_view_host; 306 new_render_view_host_ = new_render_view_host;
(...skipping 16 matching lines...) Expand all
300 // Once hidden, a prerendered RenderViewHost should only be shown after 323 // Once hidden, a prerendered RenderViewHost should only be shown after
301 // being removed from the PrerenderContents for display. 324 // being removed from the PrerenderContents for display.
302 EXPECT_FALSE(GetRenderViewHost()); 325 EXPECT_FALSE(GetRenderViewHost());
303 was_shown_ = true; 326 was_shown_ = true;
304 } 327 }
305 return; 328 return;
306 } 329 }
307 PrerenderContents::Observe(type, source, details); 330 PrerenderContents::Observe(type, source, details);
308 } 331 }
309 332
333 DestructionWaiter::DestructionWaiter(TestPrerenderContents* prerender_contents,
334 FinalStatus expected_final_status)
335 : expected_final_status_(expected_final_status),
336 saw_correct_status_(false) {
337 prerender_contents->AddDestroyObserver(
338 base::MakeUnique<DestructionMarker>(this));
339 }
340
341 bool DestructionWaiter::WaitForDestroy() {
342 if (!saw_correct_status_) {
343 wait_loop_.Run();
344 }
345 return saw_correct_status_;
346 }
347
348 void DestructionWaiter::MarkDestruction(FinalStatus reason) {
349 saw_correct_status_ = (reason == expected_final_status_);
350 wait_loop_.Quit();
351 }
352
353 DestructionWaiter::DestructionMarker::DestructionMarker(
354 DestructionWaiter* waiter)
355 : waiter_(waiter) {}
356
357 void DestructionWaiter::DestructionMarker::OnDestroy(FinalStatus reason) {
358 waiter_->MarkDestruction(reason);
359 }
360
310 TestPrerender::TestPrerender() 361 TestPrerender::TestPrerender()
311 : contents_(nullptr), number_of_loads_(0), expected_number_of_loads_(0) {} 362 : contents_(nullptr), number_of_loads_(0), expected_number_of_loads_(0) {}
312 363
313 TestPrerender::~TestPrerender() { 364 TestPrerender::~TestPrerender() {
314 if (contents_) 365 if (contents_)
315 contents_->RemoveObserver(this); 366 contents_->RemoveObserver(this);
316 } 367 }
317 368
318 void TestPrerender::WaitForLoads(int expected_number_of_loads) { 369 void TestPrerender::WaitForLoads(int expected_number_of_loads) {
319 DCHECK(!load_waiter_); 370 DCHECK(!load_waiter_);
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 } 476 }
426 477
427 content::SessionStorageNamespace* 478 content::SessionStorageNamespace*
428 PrerenderInProcessBrowserTest::GetSessionStorageNamespace() const { 479 PrerenderInProcessBrowserTest::GetSessionStorageNamespace() const {
429 content::WebContents* web_contents = GetActiveWebContents(); 480 content::WebContents* web_contents = GetActiveWebContents();
430 if (!web_contents) 481 if (!web_contents)
431 return nullptr; 482 return nullptr;
432 return web_contents->GetController().GetDefaultSessionStorageNamespace(); 483 return web_contents->GetController().GetDefaultSessionStorageNamespace();
433 } 484 }
434 485
486 std::string PrerenderInProcessBrowserTest::MakeAbsolute(
487 const std::string& path) {
488 CHECK(!path.empty());
489 if (path.front() == '/') {
490 return path;
491 }
492 return "/" + path;
493 }
494
435 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager( 495 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager(
436 const std::string& html_file) const { 496 const std::string& html_file) const {
437 return UrlIsInPrerenderManager(embedded_test_server()->GetURL(html_file)); 497 return UrlIsInPrerenderManager(embedded_test_server()->GetURL(html_file));
438 } 498 }
439 499
440 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager( 500 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager(
441 const GURL& url) const { 501 const GURL& url) const {
442 return GetPrerenderManager()->FindPrerenderData( 502 return GetPrerenderManager()->FindPrerenderData(
443 url, GetSessionStorageNamespace()) != nullptr; 503 url, GetSessionStorageNamespace()) != nullptr;
444 } 504 }
(...skipping 12 matching lines...) Expand all
457 PrerenderManager::PrerenderData* prerender_data = 517 PrerenderManager::PrerenderData* prerender_data =
458 GetPrerenderManager()->FindPrerenderData(url, nullptr); 518 GetPrerenderManager()->FindPrerenderData(url, nullptr);
459 return static_cast<TestPrerenderContents*>( 519 return static_cast<TestPrerenderContents*>(
460 prerender_data ? prerender_data->contents() : nullptr); 520 prerender_data ? prerender_data->contents() : nullptr);
461 } 521 }
462 522
463 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL( 523 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
464 const std::string& html_file, 524 const std::string& html_file,
465 FinalStatus expected_final_status, 525 FinalStatus expected_final_status,
466 int expected_number_of_loads) { 526 int expected_number_of_loads) {
467 GURL url = embedded_test_server()->GetURL(html_file); 527 GURL url = src_server()->GetURL(MakeAbsolute(html_file));
468 return PrerenderTestURL(url, expected_final_status, expected_number_of_loads); 528 return PrerenderTestURL(url, expected_final_status, expected_number_of_loads);
469 } 529 }
470 530
471 ScopedVector<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
472 const std::string& html_file,
473 const std::vector<FinalStatus>& expected_final_status_queue,
474 int expected_number_of_loads) {
475 GURL url = embedded_test_server()->GetURL(html_file);
476 return PrerenderTestURLImpl(url, expected_final_status_queue,
477 expected_number_of_loads);
478 }
479
480 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL( 531 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
481 const GURL& url, 532 const GURL& url,
482 FinalStatus expected_final_status, 533 FinalStatus expected_final_status,
483 int expected_number_of_loads) { 534 int expected_number_of_loads) {
484 std::vector<FinalStatus> expected_final_status_queue(1, 535 std::vector<FinalStatus> expected_final_status_queue(1,
485 expected_final_status); 536 expected_final_status);
486 std::vector<TestPrerender*> prerenders; 537 std::vector<TestPrerender*> prerenders;
487 PrerenderTestURLImpl(url, expected_final_status_queue, 538 PrerenderTestURLImpl(url, expected_final_status_queue,
488 expected_number_of_loads) 539 expected_number_of_loads)
489 .release(&prerenders); 540 .release(&prerenders);
490 CHECK_EQ(1u, prerenders.size()); 541 CHECK_EQ(1u, prerenders.size());
491 return std::unique_ptr<TestPrerender>(prerenders[0]); 542 return std::unique_ptr<TestPrerender>(prerenders[0]);
492 } 543 }
493 544
545 ScopedVector<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
546 const std::string& html_file,
547 const std::vector<FinalStatus>& expected_final_status_queue,
548 int expected_number_of_loads) {
549 GURL url = src_server()->GetURL(MakeAbsolute(html_file));
550 return PrerenderTestURLImpl(url, expected_final_status_queue,
551 expected_number_of_loads);
552 }
553
494 void PrerenderInProcessBrowserTest::SetUpInProcessBrowserTestFixture() { 554 void PrerenderInProcessBrowserTest::SetUpInProcessBrowserTestFixture() {
495 safe_browsing_factory_->SetTestDatabaseManager( 555 safe_browsing_factory_->SetTestDatabaseManager(
496 new test_utils::FakeSafeBrowsingDatabaseManager()); 556 new test_utils::FakeSafeBrowsingDatabaseManager());
497 safe_browsing::SafeBrowsingService::RegisterFactory( 557 safe_browsing::SafeBrowsingService::RegisterFactory(
498 safe_browsing_factory_.get()); 558 safe_browsing_factory_.get());
499 } 559 }
500 560
501 void PrerenderInProcessBrowserTest::SetUpOnMainThread() { 561 void PrerenderInProcessBrowserTest::SetUpOnMainThread() {
502 // Increase the memory allowed in a prerendered page above normal settings. 562 // Increase the memory allowed in a prerendered page above normal settings.
503 // Debug build bots occasionally run against the default limit, and tests 563 // Debug build bots occasionally run against the default limit, and tests
(...skipping 12 matching lines...) Expand all
516 PrerenderManager* prerender_manager = GetPrerenderManager(); 576 PrerenderManager* prerender_manager = GetPrerenderManager();
517 ASSERT_TRUE(prerender_manager); 577 ASSERT_TRUE(prerender_manager);
518 prerender_manager->mutable_config().rate_limit_enabled = false; 578 prerender_manager->mutable_config().rate_limit_enabled = false;
519 ASSERT_FALSE(prerender_contents_factory_); 579 ASSERT_FALSE(prerender_contents_factory_);
520 prerender_contents_factory_ = new TestPrerenderContentsFactory; 580 prerender_contents_factory_ = new TestPrerenderContentsFactory;
521 prerender_manager->SetPrerenderContentsFactoryForTest( 581 prerender_manager->SetPrerenderContentsFactoryForTest(
522 prerender_contents_factory_); 582 prerender_contents_factory_);
523 ASSERT_TRUE(safe_browsing_factory_->test_safe_browsing_service()); 583 ASSERT_TRUE(safe_browsing_factory_->test_safe_browsing_service());
524 } 584 }
525 585
586 void PrerenderInProcessBrowserTest::UseHttpsSrcServer(
587 std::function<void(net::EmbeddedTestServer*)> startup) {
588 if (https_src_server_)
589 return;
590 https_src_server_.reset(
591 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
592 https_src_server_->ServeFilesFromSourceDirectory("chrome/test/data");
593 startup(https_src_server_.get());
594 CHECK(https_src_server_->Start());
595 }
596
597 base::string16 PrerenderInProcessBrowserTest::MatchTaskManagerTab(
598 const char* page_title) {
599 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX,
600 base::ASCIIToUTF16(page_title));
601 }
602
603 base::string16 PrerenderInProcessBrowserTest::MatchTaskManagerPrerender(
604 const char* page_title) {
605 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
606 base::ASCIIToUTF16(page_title));
607 }
608
609 ScopedVector<TestPrerender>
610 PrerenderInProcessBrowserTest::NavigateWithPrerenders(
611 GURL loader_url,
612 const std::vector<FinalStatus>& expected_final_status_queue,
613 int expected_number_of_loads) {
614 CHECK(!expected_final_status_queue.empty());
615 ScopedVector<TestPrerender> prerenders;
616 for (size_t i = 0; i < expected_final_status_queue.size(); i++) {
617 prerenders.push_back(
618 prerender_contents_factory()
619 ->ExpectPrerenderContents(expected_final_status_queue[i])
620 .release());
621 }
622
623 // Navigate to the loader URL and then wait for the first prerender to be
624 // created.
625 ui_test_utils::NavigateToURL(current_browser(), loader_url);
626 prerenders.get().at(0)->WaitForCreate();
627 prerenders.get().at(0)->WaitForLoads(expected_number_of_loads);
628
629 return prerenders;
630 }
631
526 void CreateCountingInterceptorOnIO( 632 void CreateCountingInterceptorOnIO(
527 const GURL& url, 633 const GURL& url,
528 const base::FilePath& file, 634 const base::FilePath& file,
529 const base::WeakPtr<RequestCounter>& counter) { 635 const base::WeakPtr<RequestCounter>& counter) {
530 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 636 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
531 std::unique_ptr<net::URLRequestInterceptor> request_interceptor(
532 new CountingInterceptor(file, counter));
533 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 637 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
534 url, std::move(request_interceptor)); 638 url, base::MakeUnique<CountingInterceptor>(file, counter));
535 } 639 }
536 640
537 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) { 641 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
538 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 642 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
539 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 643 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
540 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile( 644 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
541 file, content::BrowserThread::GetBlockingPool())); 645 file, content::BrowserThread::GetBlockingPool()));
542 } 646 }
543 647
544 } // namespace test_utils 648 } // namespace test_utils
545 649
546 } // namespace prerender 650 } // namespace prerender
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698