OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <deque> | 5 #include <deque> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/content_settings/host_content_settings_map.h" | 11 #include "chrome/browser/content_settings/host_content_settings_map.h" |
11 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
12 #include "chrome/browser/prerender/prerender_contents.h" | 13 #include "chrome/browser/prerender/prerender_contents.h" |
13 #include "chrome/browser/prerender/prerender_manager.h" | 14 #include "chrome/browser/prerender/prerender_manager.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
16 #include "chrome/browser/task_manager/task_manager.h" | 17 #include "chrome/browser/task_manager/task_manager.h" |
17 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
20 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
21 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
23 #include "chrome/test/in_process_browser_test.h" | 24 #include "chrome/test/in_process_browser_test.h" |
24 #include "chrome/test/ui_test_utils.h" | 25 #include "chrome/test/ui_test_utils.h" |
25 #include "content/browser/renderer_host/render_view_host.h" | 26 #include "content/browser/renderer_host/render_view_host.h" |
26 #include "content/browser/tab_contents/tab_contents.h" | 27 #include "content/browser/tab_contents/tab_contents.h" |
28 #include "content/browser/tab_contents/title_updated_details.h" | |
27 #include "content/common/notification_service.h" | 29 #include "content/common/notification_service.h" |
28 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
29 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
30 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
31 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
32 | 34 |
33 // Prerender tests work as follows: | 35 // Prerender tests work as follows: |
34 // | 36 // |
35 // A page with a prefetch link to the test page is loaded. Once prerendered, | 37 // A page with a prefetch link to the test page is loaded. Once prerendered, |
36 // its Javascript function DidPrerenderPass() is called, which returns true if | 38 // its Javascript function DidPrerenderPass() is called, which returns true if |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 private: | 310 private: |
309 FakeSafeBrowsingService* most_recent_service_; | 311 FakeSafeBrowsingService* most_recent_service_; |
310 }; | 312 }; |
311 | 313 |
312 class PrerenderBrowserTest : public InProcessBrowserTest { | 314 class PrerenderBrowserTest : public InProcessBrowserTest { |
313 public: | 315 public: |
314 PrerenderBrowserTest() | 316 PrerenderBrowserTest() |
315 : safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), | 317 : safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), |
316 prerender_contents_factory_(NULL), | 318 prerender_contents_factory_(NULL), |
317 use_https_src_server_(false), | 319 use_https_src_server_(false), |
318 call_javascript_(true) { | 320 call_javascript_(true), |
321 loader_path_("files/prerender/prerender_loader.html") { | |
319 EnableDOMAutomation(); | 322 EnableDOMAutomation(); |
320 } | 323 } |
321 | 324 |
322 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 325 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
323 SafeBrowsingService::RegisterFactory(safe_browsing_factory_.get()); | 326 SafeBrowsingService::RegisterFactory(safe_browsing_factory_.get()); |
324 } | 327 } |
325 | 328 |
326 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 329 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
327 command_line->AppendSwitchASCII(switches::kPrerender, | 330 command_line->AppendSwitchASCII(switches::kPrerender, |
328 switches::kPrerenderSwitchValueEnabled); | 331 switches::kPrerenderSwitchValueEnabled); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 PrerenderManager* prerender_manager() const { | 461 PrerenderManager* prerender_manager() const { |
459 Profile* profile = browser()->GetSelectedTabContents()->profile(); | 462 Profile* profile = browser()->GetSelectedTabContents()->profile(); |
460 PrerenderManager* prerender_manager = profile->GetPrerenderManager(); | 463 PrerenderManager* prerender_manager = profile->GetPrerenderManager(); |
461 return prerender_manager; | 464 return prerender_manager; |
462 } | 465 } |
463 | 466 |
464 FakeSafeBrowsingService* GetSafeBrowsingService() { | 467 FakeSafeBrowsingService* GetSafeBrowsingService() { |
465 return safe_browsing_factory_->most_recent_service(); | 468 return safe_browsing_factory_->most_recent_service(); |
466 } | 469 } |
467 | 470 |
471 void set_loader_path(const std::string& path) { | |
472 loader_path_ = path; | |
473 } | |
474 | |
468 private: | 475 private: |
469 void PrerenderTestURLImpl( | 476 void PrerenderTestURLImpl( |
470 const GURL& url, | 477 const GURL& url, |
471 const std::deque<FinalStatus>& expected_final_status_queue, | 478 const std::deque<FinalStatus>& expected_final_status_queue, |
472 int total_navigations) { | 479 int total_navigations) { |
473 dest_url_ = url; | 480 dest_url_ = url; |
474 | 481 |
475 std::vector<net::TestServer::StringPair> replacement_text; | 482 std::vector<net::TestServer::StringPair> replacement_text; |
476 replacement_text.push_back( | 483 replacement_text.push_back( |
477 make_pair("REPLACE_WITH_PRERENDER_URL", dest_url_.spec())); | 484 make_pair("REPLACE_WITH_PRERENDER_URL", dest_url_.spec())); |
478 std::string replacement_path; | 485 std::string replacement_path; |
479 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( | 486 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( |
480 "files/prerender/prerender_loader.html", | 487 loader_path_, |
481 replacement_text, | 488 replacement_text, |
482 &replacement_path)); | 489 &replacement_path)); |
483 | 490 |
484 net::TestServer* src_server = test_server(); | 491 net::TestServer* src_server = test_server(); |
485 scoped_ptr<net::TestServer> https_src_server; | 492 scoped_ptr<net::TestServer> https_src_server; |
486 if (use_https_src_server_) { | 493 if (use_https_src_server_) { |
487 https_src_server.reset( | 494 https_src_server.reset( |
488 new net::TestServer(net::TestServer::TYPE_HTTPS, | 495 new net::TestServer(net::TestServer::TYPE_HTTPS, |
489 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); | 496 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); |
490 ASSERT_TRUE(https_src_server->Start()); | 497 ASSERT_TRUE(https_src_server->Start()); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
564 &display_test_result)); | 571 &display_test_result)); |
565 EXPECT_TRUE(display_test_result); | 572 EXPECT_TRUE(display_test_result); |
566 } | 573 } |
567 } | 574 } |
568 | 575 |
569 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_; | 576 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_; |
570 WaitForLoadPrerenderContentsFactory* prerender_contents_factory_; | 577 WaitForLoadPrerenderContentsFactory* prerender_contents_factory_; |
571 GURL dest_url_; | 578 GURL dest_url_; |
572 bool use_https_src_server_; | 579 bool use_https_src_server_; |
573 bool call_javascript_; | 580 bool call_javascript_; |
581 std::string loader_path_; | |
574 }; | 582 }; |
575 | 583 |
576 // Checks that a page is correctly prerendered in the case of a | 584 // Checks that a page is correctly prerendered in the case of a |
577 // <link rel=prerender> tag and then loaded into a tab in response to a | 585 // <link rel=prerender> tag and then loaded into a tab in response to a |
578 // navigation. | 586 // navigation. |
579 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { | 587 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { |
580 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1); | 588 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1); |
581 NavigateToDestURL(); | 589 NavigateToDestURL(); |
582 } | 590 } |
583 | 591 |
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1333 } | 1341 } |
1334 | 1342 |
1335 // Checks that a local storage write will not cause prerender to fail. | 1343 // Checks that a local storage write will not cause prerender to fail. |
1336 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) { | 1344 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) { |
1337 PrerenderTestURL("files/prerender/prerender_localstorage_write.html", | 1345 PrerenderTestURL("files/prerender/prerender_localstorage_write.html", |
1338 FINAL_STATUS_USED, | 1346 FINAL_STATUS_USED, |
1339 1); | 1347 1); |
1340 NavigateToDestURL(); | 1348 NavigateToDestURL(); |
1341 } | 1349 } |
1342 | 1350 |
1351 class TitleWatcher : public NotificationObserver { | |
dominich
2011/06/08 22:19:19
Can you put this up with the other classes at the
mmenke
2011/06/08 22:29:05
nit: Should stick this in an anonymous namespace,
cbentzel
2011/06/09 00:49:49
Done.
cbentzel
2011/06/09 00:49:49
Done.
| |
1352 public: | |
1353 TitleWatcher(TabContents* tab_contents, const string16& expected_title) | |
1354 : expected_title_(expected_title), | |
1355 title_observed_(false), | |
1356 quit_loop_on_observation_(false) { | |
1357 notification_registrar_.Add(this, | |
1358 NotificationType::TAB_CONTENTS_TITLE_UPDATED, | |
1359 Source<TabContents>(tab_contents)); | |
1360 } | |
1361 | |
1362 ~TitleWatcher() { | |
1363 } | |
1364 | |
1365 virtual void Observe(NotificationType type, | |
1366 const NotificationSource& source, | |
1367 const NotificationDetails& details) OVERRIDE { | |
1368 if (type != NotificationType::TAB_CONTENTS_TITLE_UPDATED) | |
1369 return; | |
1370 | |
1371 TitleUpdatedDetails* title_details = | |
1372 Details<TitleUpdatedDetails>(details).ptr(); | |
1373 if (!title_details || title_details->entry()->title() != expected_title_) | |
1374 return; | |
1375 | |
1376 title_observed_ = true; | |
1377 if (quit_loop_on_observation_) | |
1378 MessageLoopForUI::current()->Quit(); | |
dominich
2011/06/08 22:19:19
Please make sure you run this on the release trybo
| |
1379 } | |
1380 | |
1381 bool WaitForTitleChange() { | |
1382 if (title_observed_) | |
1383 return true; | |
1384 quit_loop_on_observation_ = true; | |
1385 ui_test_utils::RunMessageLoop(); | |
1386 return title_observed_; | |
1387 } | |
1388 | |
1389 private: | |
1390 string16 expected_title_; | |
1391 NotificationRegistrar notification_registrar_; | |
1392 bool title_observed_; | |
1393 bool quit_loop_on_observation_; | |
1394 }; | |
1395 | |
1396 // Checks that when a prerendered page is swapped in to a referring page, the | |
1397 // unload handlers on the referring page are executed. | |
1398 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderUnload) { | |
1399 set_loader_path("files/prerender/prerender_loader_with_unload.html"); | |
1400 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1); | |
1401 TabContents* tab_contents = browser()->GetSelectedTabContents(); | |
1402 string16 expected_title = ASCIIToUTF16("Unloaded"); | |
dominich
2011/06/08 22:19:19
nit: No need for this to be a local variable.
cbentzel
2011/06/09 00:49:49
Done.
| |
1403 TitleWatcher title_watcher(tab_contents, expected_title); | |
1404 NavigateToDestURL(); | |
1405 EXPECT_TRUE(title_watcher.WaitForTitleChange()); | |
1406 } | |
1407 | |
1343 } // namespace prerender | 1408 } // namespace prerender |
OLD | NEW |