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

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

Issue 7046053: Add PrerenderUnload browser test (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix TitleWatcher Created 9 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/prerender/prerender_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 profile, url, 235 profile, url,
234 referrer, number_of_loads_, 236 referrer, number_of_loads_,
235 expected_final_status); 237 expected_final_status);
236 } 238 }
237 239
238 private: 240 private:
239 int number_of_loads_; 241 int number_of_loads_;
240 std::deque<FinalStatus> expected_final_status_queue_; 242 std::deque<FinalStatus> expected_final_status_queue_;
241 }; 243 };
242 244
245 // Watches title changes on a tab, blocking until an expected change occurs.
246 class TitleWatcher : public NotificationObserver {
Paweł Hajdan Jr. 2011/06/09 09:23:15 Could you move this to chrome/test/ui_test_utils?
cbentzel 2011/06/09 15:29:21 This seems like a pretty specific class, and doesn
Paweł Hajdan Jr. 2011/06/09 19:24:12 Yes please, for now it's not used by more tests bu
247 public:
248 TitleWatcher(TabContents* tab_contents, const string16& expected_title)
249 : expected_tab_(tab_contents),
250 expected_title_(expected_title),
251 title_observed_(false),
252 quit_loop_on_observation_(false) {
253 EXPECT_TRUE(tab_contents != NULL);
254 notification_registrar_.Add(this,
255 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
256 Source<TabContents>(tab_contents));
257 }
258
259 ~TitleWatcher() {
260 }
261
262 virtual void Observe(NotificationType type,
263 const NotificationSource& source,
264 const NotificationDetails& details) OVERRIDE {
265 if (type != NotificationType::TAB_CONTENTS_TITLE_UPDATED)
266 return;
267
268 TabContents* source_contents = Source<TabContents>(source).ptr();
269 ASSERT_EQ(expected_tab_, source_contents);
270 if (source_contents->GetTitle() != expected_title_)
271 return;
272
273 title_observed_ = true;
274 if (quit_loop_on_observation_)
275 MessageLoopForUI::current()->Quit();
276 }
277
278 bool WaitForTitleChange() {
Paweł Hajdan Jr. 2011/06/09 09:23:15 nit: Add WARN_UNUSED_RESULT from base/compiler_spe
cbentzel 2011/06/09 15:29:21 Done.
279 if (title_observed_)
280 return true;
281 quit_loop_on_observation_ = true;
282 ui_test_utils::RunMessageLoop();
283 return title_observed_;
284 }
285
286 private:
287 TabContents* expected_tab_;
288 string16 expected_title_;
289 NotificationRegistrar notification_registrar_;
290 bool title_observed_;
291 bool quit_loop_on_observation_;
Paweł Hajdan Jr. 2011/06/09 09:23:15 nit: DISALLOW_COPY_AND_ASSIGN.
cbentzel 2011/06/09 15:29:21 Done.
292 };
293
243 } // namespace 294 } // namespace
244 295
245 // A SafeBrowingService implementation that returns a fixed result for a given 296 // A SafeBrowingService implementation that returns a fixed result for a given
246 // URL. 297 // URL.
247 class FakeSafeBrowsingService : public SafeBrowsingService { 298 class FakeSafeBrowsingService : public SafeBrowsingService {
248 public: 299 public:
249 FakeSafeBrowsingService() : 300 FakeSafeBrowsingService() :
250 result_(SAFE) {} 301 result_(SAFE) {}
251 302
252 virtual ~FakeSafeBrowsingService() {} 303 virtual ~FakeSafeBrowsingService() {}
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 private: 359 private:
309 FakeSafeBrowsingService* most_recent_service_; 360 FakeSafeBrowsingService* most_recent_service_;
310 }; 361 };
311 362
312 class PrerenderBrowserTest : public InProcessBrowserTest { 363 class PrerenderBrowserTest : public InProcessBrowserTest {
313 public: 364 public:
314 PrerenderBrowserTest() 365 PrerenderBrowserTest()
315 : safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), 366 : safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
316 prerender_contents_factory_(NULL), 367 prerender_contents_factory_(NULL),
317 use_https_src_server_(false), 368 use_https_src_server_(false),
318 call_javascript_(true) { 369 call_javascript_(true),
370 loader_path_("files/prerender/prerender_loader.html") {
319 EnableDOMAutomation(); 371 EnableDOMAutomation();
320 } 372 }
321 373
322 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 374 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
323 SafeBrowsingService::RegisterFactory(safe_browsing_factory_.get()); 375 SafeBrowsingService::RegisterFactory(safe_browsing_factory_.get());
324 } 376 }
325 377
326 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 378 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
327 command_line->AppendSwitchASCII(switches::kPrerender, 379 command_line->AppendSwitchASCII(switches::kPrerender,
328 switches::kPrerenderSwitchValueEnabled); 380 switches::kPrerenderSwitchValueEnabled);
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 PrerenderManager* prerender_manager() const { 510 PrerenderManager* prerender_manager() const {
459 Profile* profile = browser()->GetSelectedTabContents()->profile(); 511 Profile* profile = browser()->GetSelectedTabContents()->profile();
460 PrerenderManager* prerender_manager = profile->GetPrerenderManager(); 512 PrerenderManager* prerender_manager = profile->GetPrerenderManager();
461 return prerender_manager; 513 return prerender_manager;
462 } 514 }
463 515
464 FakeSafeBrowsingService* GetSafeBrowsingService() { 516 FakeSafeBrowsingService* GetSafeBrowsingService() {
465 return safe_browsing_factory_->most_recent_service(); 517 return safe_browsing_factory_->most_recent_service();
466 } 518 }
467 519
520 void set_loader_path(const std::string& path) {
dominich 2011/06/09 14:26:56 Did you consider adding another PrerenderTestURL v
cbentzel 2011/06/09 15:29:21 I did consider it, but it seemed like the combinat
521 loader_path_ = path;
522 }
523
468 private: 524 private:
469 void PrerenderTestURLImpl( 525 void PrerenderTestURLImpl(
470 const GURL& url, 526 const GURL& url,
471 const std::deque<FinalStatus>& expected_final_status_queue, 527 const std::deque<FinalStatus>& expected_final_status_queue,
472 int total_navigations) { 528 int total_navigations) {
473 dest_url_ = url; 529 dest_url_ = url;
474 530
475 std::vector<net::TestServer::StringPair> replacement_text; 531 std::vector<net::TestServer::StringPair> replacement_text;
476 replacement_text.push_back( 532 replacement_text.push_back(
477 make_pair("REPLACE_WITH_PRERENDER_URL", dest_url_.spec())); 533 make_pair("REPLACE_WITH_PRERENDER_URL", dest_url_.spec()));
478 std::string replacement_path; 534 std::string replacement_path;
479 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( 535 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
480 "files/prerender/prerender_loader.html", 536 loader_path_,
481 replacement_text, 537 replacement_text,
482 &replacement_path)); 538 &replacement_path));
483 539
484 net::TestServer* src_server = test_server(); 540 net::TestServer* src_server = test_server();
485 scoped_ptr<net::TestServer> https_src_server; 541 scoped_ptr<net::TestServer> https_src_server;
486 if (use_https_src_server_) { 542 if (use_https_src_server_) {
487 https_src_server.reset( 543 https_src_server.reset(
488 new net::TestServer(net::TestServer::TYPE_HTTPS, 544 new net::TestServer(net::TestServer::TYPE_HTTPS,
489 FilePath(FILE_PATH_LITERAL("chrome/test/data")))); 545 FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
490 ASSERT_TRUE(https_src_server->Start()); 546 ASSERT_TRUE(https_src_server->Start());
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 &display_test_result)); 620 &display_test_result));
565 EXPECT_TRUE(display_test_result); 621 EXPECT_TRUE(display_test_result);
566 } 622 }
567 } 623 }
568 624
569 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_; 625 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_;
570 WaitForLoadPrerenderContentsFactory* prerender_contents_factory_; 626 WaitForLoadPrerenderContentsFactory* prerender_contents_factory_;
571 GURL dest_url_; 627 GURL dest_url_;
572 bool use_https_src_server_; 628 bool use_https_src_server_;
573 bool call_javascript_; 629 bool call_javascript_;
630 std::string loader_path_;
574 }; 631 };
575 632
576 // Checks that a page is correctly prerendered in the case of a 633 // 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 634 // <link rel=prerender> tag and then loaded into a tab in response to a
578 // navigation. 635 // navigation.
579 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { 636 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) {
580 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1); 637 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
581 NavigateToDestURL(); 638 NavigateToDestURL();
582 } 639 }
583 640
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after
1333 } 1390 }
1334 1391
1335 // Checks that a local storage write will not cause prerender to fail. 1392 // Checks that a local storage write will not cause prerender to fail.
1336 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) { 1393 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) {
1337 PrerenderTestURL("files/prerender/prerender_localstorage_write.html", 1394 PrerenderTestURL("files/prerender/prerender_localstorage_write.html",
1338 FINAL_STATUS_USED, 1395 FINAL_STATUS_USED,
1339 1); 1396 1);
1340 NavigateToDestURL(); 1397 NavigateToDestURL();
1341 } 1398 }
1342 1399
1400 // Checks that when a prerendered page is swapped in to a referring page, the
1401 // unload handlers on the referring page are executed.
1402 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderUnload) {
1403 set_loader_path("files/prerender/prerender_loader_with_unload.html");
1404 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
1405 TabContents* tab_contents = browser()->GetSelectedTabContents();
dominich 2011/06/09 14:26:56 nit: no need for this to be a local variable.
cbentzel 2011/06/09 15:29:21 Done.
1406 TitleWatcher title_watcher(tab_contents, ASCIIToUTF16("Unloaded"));
1407 NavigateToDestURL();
1408 EXPECT_TRUE(title_watcher.WaitForTitleChange());
1409 }
1410
1343 } // namespace prerender 1411 } // namespace prerender
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/prerender/prerender_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698