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

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

Issue 2304953002: NoState Prefetch: nostate prefetch browser tests. (Closed)
Patch Set: reliable waiting for paints 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) 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/loader/chrome_resource_dispatcher_host_delegate.h" 9 #include "chrome/browser/loader/chrome_resource_dispatcher_host_delegate.h"
9 #include "chrome/browser/prerender/prerender_manager.h" 10 #include "chrome/browser/prerender/prerender_manager.h"
10 #include "chrome/browser/prerender/prerender_manager_factory.h" 11 #include "chrome/browser/prerender/prerender_manager_factory.h"
11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 new_render_view_host_(nullptr), 222 new_render_view_host_(nullptr),
219 was_hidden_(false), 223 was_hidden_(false),
220 was_shown_(false), 224 was_shown_(false),
221 should_be_shown_(expected_final_status == FINAL_STATUS_USED), 225 should_be_shown_(expected_final_status == FINAL_STATUS_USED),
222 skip_final_checks_(false) {} 226 skip_final_checks_(false) {}
223 227
224 TestPrerenderContents::~TestPrerenderContents() { 228 TestPrerenderContents::~TestPrerenderContents() {
225 if (skip_final_checks_) 229 if (skip_final_checks_)
226 return; 230 return;
227 231
228 EXPECT_EQ(expected_final_status_, final_status()) 232 if (expected_final_status_ != FINAL_STATUS_TESTING_IGNORE) {
229 << " when testing URL " << prerender_url().path() 233 EXPECT_EQ(expected_final_status_, final_status())
230 << " (Expected: " << NameFromFinalStatus(expected_final_status_) 234 << " when testing URL " << prerender_url().path()
231 << ", Actual: " << NameFromFinalStatus(final_status()) << ")"; 235 << " (Expected: " << NameFromFinalStatus(expected_final_status_)
236 << ", Actual: " << NameFromFinalStatus(final_status()) << ")";
237 }
232 238
233 // Prerendering RenderViewHosts should be hidden before the first 239 // Prerendering RenderViewHosts should be hidden before the first
234 // navigation, so this should be happen for every PrerenderContents for 240 // navigation, so this should be happen for every PrerenderContents for
235 // which a RenderViewHost is created, regardless of whether or not it's 241 // which a RenderViewHost is created, regardless of whether or not it's
236 // used. 242 // used.
237 if (new_render_view_host_) 243 if (new_render_view_host_)
238 EXPECT_TRUE(was_hidden_); 244 EXPECT_TRUE(was_hidden_);
239 245
240 // A used PrerenderContents will only be destroyed when we swap out 246 // A used PrerenderContents will only be destroyed when we swap out
241 // WebContents, at the end of a navigation caused by a call to 247 // WebContents, at the end of a navigation caused by a call to
242 // NavigateToURLImpl(). 248 // NavigateToURLImpl().
243 if (final_status() == FINAL_STATUS_USED) 249 if (final_status() == FINAL_STATUS_USED)
244 EXPECT_TRUE(new_render_view_host_); 250 EXPECT_TRUE(new_render_view_host_);
245 251
246 EXPECT_EQ(should_be_shown_, was_shown_); 252 EXPECT_EQ(should_be_shown_, was_shown_);
247 } 253 }
248 254
249 void TestPrerenderContents::RenderProcessGone(base::TerminationStatus status) { 255 void TestPrerenderContents::RenderProcessGone(base::TerminationStatus status) {
250 // On quit, it's possible to end up here when render processes are closed 256 // On quit, it's possible to end up here when render processes are closed
251 // before the PrerenderManager is destroyed. As a result, it's possible to 257 // before the PrerenderManager is destroyed. As a result, it's possible to
252 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED 258 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
253 // on quit. 259 // on quit.
254 // 260 //
255 // It's also possible for this to be called after we've been notified of 261 // It's also possible for this to be called after we've been notified of
256 // app termination, but before we've been deleted, which is why the second 262 // app termination, but before we've been deleted, which is why the second
257 // check is needed. 263 // check is needed.
258 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING && 264 if (expected_final_status_ != FINAL_STATUS_TESTING_IGNORE &&
265 expected_final_status_ == FINAL_STATUS_APP_TERMINATING &&
259 final_status() != expected_final_status_) { 266 final_status() != expected_final_status_) {
260 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; 267 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED;
261 } 268 }
262 269
263 PrerenderContents::RenderProcessGone(status); 270 PrerenderContents::RenderProcessGone(status);
264 } 271 }
265 272
266 bool TestPrerenderContents::CheckURL(const GURL& url) { 273 bool TestPrerenderContents::CheckURL(const GURL& url) {
267 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in 274 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
268 // the PrerenderRendererCrash test. 275 // the PrerenderRendererCrash test.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 // Once hidden, a prerendered RenderViewHost should only be shown after 307 // Once hidden, a prerendered RenderViewHost should only be shown after
301 // being removed from the PrerenderContents for display. 308 // being removed from the PrerenderContents for display.
302 EXPECT_FALSE(GetRenderViewHost()); 309 EXPECT_FALSE(GetRenderViewHost());
303 was_shown_ = true; 310 was_shown_ = true;
304 } 311 }
305 return; 312 return;
306 } 313 }
307 PrerenderContents::Observe(type, source, details); 314 PrerenderContents::Observe(type, source, details);
308 } 315 }
309 316
317 DestructionWaiter::DestructionWaiter(TestPrerenderContents* prerender_contents,
318 FinalStatus expected_final_status)
319 : expected_final_status_(expected_final_status),
320 saw_correct_status_(false) {
321 if (prerender_contents->final_status() != FINAL_STATUS_MAX) {
322 // The contents was already destroyed by the time this was called.
323 MarkDestruction(prerender_contents->final_status());
324 } else {
325 marker_ = base::MakeUnique<DestructionMarker>(this);
326 prerender_contents->AddObserver(marker_.get());
327 }
328 }
329
330 DestructionWaiter::~DestructionWaiter() {}
331
332 bool DestructionWaiter::WaitForDestroy() {
333 if (!saw_correct_status_) {
334 wait_loop_.Run();
335 }
336 return saw_correct_status_;
337 }
338
339 void DestructionWaiter::MarkDestruction(FinalStatus reason) {
340 saw_correct_status_ = (reason == expected_final_status_);
341 wait_loop_.Quit();
342 }
343
344 DestructionWaiter::DestructionMarker::DestructionMarker(
345 DestructionWaiter* waiter)
346 : waiter_(waiter) {}
347
348 DestructionWaiter::DestructionMarker::~DestructionMarker() {}
349
350 void DestructionWaiter::DestructionMarker::OnPrerenderStop(
351 PrerenderContents* contents) {
352 waiter_->MarkDestruction(contents->final_status());
353 }
354
310 TestPrerender::TestPrerender() 355 TestPrerender::TestPrerender()
311 : contents_(nullptr), number_of_loads_(0), expected_number_of_loads_(0) {} 356 : contents_(nullptr), number_of_loads_(0), expected_number_of_loads_(0) {}
312 357
313 TestPrerender::~TestPrerender() { 358 TestPrerender::~TestPrerender() {
314 if (contents_) 359 if (contents_)
315 contents_->RemoveObserver(this); 360 contents_->RemoveObserver(this);
316 } 361 }
317 362
318 void TestPrerender::WaitForLoads(int expected_number_of_loads) { 363 void TestPrerender::WaitForLoads(int expected_number_of_loads) {
319 DCHECK(!load_waiter_); 364 DCHECK(!load_waiter_);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 } 467 }
423 468
424 content::SessionStorageNamespace* 469 content::SessionStorageNamespace*
425 PrerenderInProcessBrowserTest::GetSessionStorageNamespace() const { 470 PrerenderInProcessBrowserTest::GetSessionStorageNamespace() const {
426 content::WebContents* web_contents = GetActiveWebContents(); 471 content::WebContents* web_contents = GetActiveWebContents();
427 if (!web_contents) 472 if (!web_contents)
428 return nullptr; 473 return nullptr;
429 return web_contents->GetController().GetDefaultSessionStorageNamespace(); 474 return web_contents->GetController().GetDefaultSessionStorageNamespace();
430 } 475 }
431 476
477 std::string PrerenderInProcessBrowserTest::MakeAbsolute(
478 const std::string& path) {
479 CHECK(!path.empty());
480 if (path.front() == '/') {
481 return path;
482 }
483 return "/" + path;
484 }
485
432 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager( 486 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager(
433 const std::string& html_file) const { 487 const std::string& html_file) const {
434 return UrlIsInPrerenderManager(embedded_test_server()->GetURL(html_file)); 488 return UrlIsInPrerenderManager(embedded_test_server()->GetURL(html_file));
435 } 489 }
436 490
437 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager( 491 bool PrerenderInProcessBrowserTest::UrlIsInPrerenderManager(
438 const GURL& url) const { 492 const GURL& url) const {
439 return GetPrerenderManager()->FindPrerenderData( 493 return GetPrerenderManager()->FindPrerenderData(
440 url, GetSessionStorageNamespace()) != nullptr; 494 url, GetSessionStorageNamespace()) != nullptr;
441 } 495 }
(...skipping 13 matching lines...) Expand all
455 PrerenderManager::PrerenderData* prerender_data = 509 PrerenderManager::PrerenderData* prerender_data =
456 GetPrerenderManager()->FindPrerenderData(url, nullptr); 510 GetPrerenderManager()->FindPrerenderData(url, nullptr);
457 return static_cast<TestPrerenderContents*>( 511 return static_cast<TestPrerenderContents*>(
458 prerender_data ? prerender_data->contents() : nullptr); 512 prerender_data ? prerender_data->contents() : nullptr);
459 } 513 }
460 514
461 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL( 515 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
462 const std::string& html_file, 516 const std::string& html_file,
463 FinalStatus expected_final_status, 517 FinalStatus expected_final_status,
464 int expected_number_of_loads) { 518 int expected_number_of_loads) {
465 GURL url = embedded_test_server()->GetURL(html_file); 519 GURL url = src_server()->GetURL(MakeAbsolute(html_file));
466 return PrerenderTestURL(url, expected_final_status, expected_number_of_loads); 520 return PrerenderTestURL(url, expected_final_status, expected_number_of_loads);
467 } 521 }
468 522
469 ScopedVector<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
470 const std::string& html_file,
471 const std::vector<FinalStatus>& expected_final_status_queue,
472 int expected_number_of_loads) {
473 GURL url = embedded_test_server()->GetURL(html_file);
474 return PrerenderTestURLImpl(url, expected_final_status_queue,
475 expected_number_of_loads);
476 }
477
478 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL( 523 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
479 const GURL& url, 524 const GURL& url,
480 FinalStatus expected_final_status, 525 FinalStatus expected_final_status,
481 int expected_number_of_loads) { 526 int expected_number_of_loads) {
482 std::vector<FinalStatus> expected_final_status_queue(1, 527 std::vector<FinalStatus> expected_final_status_queue(1,
483 expected_final_status); 528 expected_final_status);
484 std::vector<TestPrerender*> prerenders; 529 std::vector<TestPrerender*> prerenders;
485 PrerenderTestURLImpl(url, expected_final_status_queue, 530 PrerenderTestURLImpl(url, expected_final_status_queue,
486 expected_number_of_loads) 531 expected_number_of_loads)
487 .release(&prerenders); 532 .release(&prerenders);
488 CHECK_EQ(1u, prerenders.size()); 533 CHECK_EQ(1u, prerenders.size());
489 return std::unique_ptr<TestPrerender>(prerenders[0]); 534 return std::unique_ptr<TestPrerender>(prerenders[0]);
490 } 535 }
491 536
537 ScopedVector<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
538 const std::string& html_file,
539 const std::vector<FinalStatus>& expected_final_status_queue,
540 int expected_number_of_loads) {
541 GURL url = src_server()->GetURL(MakeAbsolute(html_file));
542 return PrerenderTestURLImpl(url, expected_final_status_queue,
543 expected_number_of_loads);
544 }
545
546 net::EmbeddedTestServer* PrerenderInProcessBrowserTest::src_server() {
547 if (https_src_server_)
548 return https_src_server_.get();
549 return embedded_test_server();
550 }
551
552 test_utils::FakeSafeBrowsingDatabaseManager*
553 PrerenderInProcessBrowserTest::GetFakeSafeBrowsingDatabaseManager() {
554 return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>(
555 safe_browsing_factory()
556 ->test_safe_browsing_service()
557 ->database_manager()
558 .get());
559 }
560
492 void PrerenderInProcessBrowserTest::SetUpInProcessBrowserTestFixture() { 561 void PrerenderInProcessBrowserTest::SetUpInProcessBrowserTestFixture() {
493 safe_browsing_factory_->SetTestDatabaseManager( 562 safe_browsing_factory_->SetTestDatabaseManager(
494 new test_utils::FakeSafeBrowsingDatabaseManager()); 563 new test_utils::FakeSafeBrowsingDatabaseManager());
495 safe_browsing::SafeBrowsingService::RegisterFactory( 564 safe_browsing::SafeBrowsingService::RegisterFactory(
496 safe_browsing_factory_.get()); 565 safe_browsing_factory_.get());
497 } 566 }
498 567
499 void PrerenderInProcessBrowserTest::SetUpOnMainThread() { 568 void PrerenderInProcessBrowserTest::SetUpOnMainThread() {
500 // Increase the memory allowed in a prerendered page above normal settings. 569 // Increase the memory allowed in a prerendered page above normal settings.
501 // Debug build bots occasionally run against the default limit, and tests 570 // Debug build bots occasionally run against the default limit, and tests
(...skipping 12 matching lines...) Expand all
514 PrerenderManager* prerender_manager = GetPrerenderManager(); 583 PrerenderManager* prerender_manager = GetPrerenderManager();
515 ASSERT_TRUE(prerender_manager); 584 ASSERT_TRUE(prerender_manager);
516 prerender_manager->mutable_config().rate_limit_enabled = false; 585 prerender_manager->mutable_config().rate_limit_enabled = false;
517 ASSERT_FALSE(prerender_contents_factory_); 586 ASSERT_FALSE(prerender_contents_factory_);
518 prerender_contents_factory_ = new TestPrerenderContentsFactory; 587 prerender_contents_factory_ = new TestPrerenderContentsFactory;
519 prerender_manager->SetPrerenderContentsFactoryForTest( 588 prerender_manager->SetPrerenderContentsFactoryForTest(
520 prerender_contents_factory_); 589 prerender_contents_factory_);
521 ASSERT_TRUE(safe_browsing_factory_->test_safe_browsing_service()); 590 ASSERT_TRUE(safe_browsing_factory_->test_safe_browsing_service());
522 } 591 }
523 592
593 void PrerenderInProcessBrowserTest::UseHttpsSrcServer() {
594 if (https_src_server_)
595 return;
596 https_src_server_.reset(
597 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
598 https_src_server_->ServeFilesFromSourceDirectory("chrome/test/data");
599 CHECK(https_src_server_->Start());
600 }
601
602 base::string16 PrerenderInProcessBrowserTest::MatchTaskManagerTab(
603 const char* page_title) {
604 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX,
605 base::ASCIIToUTF16(page_title));
606 }
607
608 base::string16 PrerenderInProcessBrowserTest::MatchTaskManagerPrerender(
609 const char* page_title) {
610 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
611 base::ASCIIToUTF16(page_title));
612 }
613
614 ScopedVector<TestPrerender>
615 PrerenderInProcessBrowserTest::NavigateWithPrerenders(
616 const GURL& loader_url,
617 const std::vector<FinalStatus>& expected_final_status_queue,
618 int expected_number_of_loads) {
619 CHECK(!expected_final_status_queue.empty());
620 ScopedVector<TestPrerender> prerenders;
621 for (size_t i = 0; i < expected_final_status_queue.size(); i++) {
622 prerenders.push_back(
623 prerender_contents_factory()
624 ->ExpectPrerenderContents(expected_final_status_queue[i])
625 .release());
626 }
627
628 // Navigate to the loader URL and then wait for the first prerender to be
629 // created.
630 ui_test_utils::NavigateToURL(current_browser(), loader_url);
631 prerenders.get().at(0)->WaitForCreate();
632 prerenders.get().at(0)->WaitForLoads(expected_number_of_loads);
633
634 return prerenders;
635 }
636
524 void CreateCountingInterceptorOnIO( 637 void CreateCountingInterceptorOnIO(
525 const GURL& url, 638 const GURL& url,
526 const base::FilePath& file, 639 const base::FilePath& file,
527 const base::WeakPtr<RequestCounter>& counter) { 640 const base::WeakPtr<RequestCounter>& counter) {
528 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 641 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
529 std::unique_ptr<net::URLRequestInterceptor> request_interceptor(
530 new CountingInterceptor(file, counter));
531 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 642 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
532 url, std::move(request_interceptor)); 643 url, base::MakeUnique<CountingInterceptor>(file, counter));
533 } 644 }
534 645
535 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) { 646 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
536 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 647 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
537 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 648 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
538 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile( 649 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
539 file, content::BrowserThread::GetBlockingPool())); 650 file, content::BrowserThread::GetBlockingPool()));
540 } 651 }
541 652
542 } // namespace test_utils 653 } // namespace test_utils
543 654
544 } // namespace prerender 655 } // namespace prerender
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698