Return backed up TemplateURL on default search change
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_browsertest.cc
blobd26ce9e5e67b59ed4a2d35547e4ced86833dbc06
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
3 // found in the LICENSE file.
5 #include <deque>
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/stringprintf.h"
10 #include "base/string_util.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browsing_data_remover.h"
15 #include "chrome/browser/content_settings/host_content_settings_map.h"
16 #include "chrome/browser/favicon/favicon_tab_helper.h"
17 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/prerender/prerender_contents.h"
19 #include "chrome/browser/prerender/prerender_manager.h"
20 #include "chrome/browser/prerender/prerender_manager_factory.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
23 #include "chrome/browser/task_manager/task_manager.h"
24 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_list.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
29 #include "chrome/common/chrome_notification_types.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/test/base/in_process_browser_test.h"
34 #include "chrome/test/base/ui_test_utils.h"
35 #include "content/browser/renderer_host/render_view_host.h"
36 #include "content/browser/tab_contents/tab_contents.h"
37 #include "content/public/browser/devtools_agent_host_registry.h"
38 #include "content/public/browser/devtools_client_host.h"
39 #include "content/public/browser/devtools_manager.h"
40 #include "content/public/browser/notification_service.h"
41 #include "content/public/common/url_constants.h"
42 #include "grit/generated_resources.h"
43 #include "net/base/mock_host_resolver.h"
44 #include "net/url_request/url_request_context.h"
45 #include "net/url_request/url_request_context_getter.h"
46 #include "ui/base/l10n/l10n_util.h"
48 using content::BrowserThread;
49 using content::DevToolsAgentHost;
50 using content::DevToolsAgentHostRegistry;
51 using content::DevToolsClientHost;
52 using content::DevToolsManager;
54 // Prerender tests work as follows:
56 // A page with a prefetch link to the test page is loaded. Once prerendered,
57 // its Javascript function DidPrerenderPass() is called, which returns true if
58 // the page behaves as expected when prerendered.
60 // The prerendered page is then displayed on a tab. The Javascript function
61 // DidDisplayPass() is called, and returns true if the page behaved as it
62 // should while being displayed.
64 namespace prerender {
66 namespace {
68 std::string CreateClientRedirect(const std::string& dest_url) {
69 const char* const kClientRedirectBase = "client-redirect?";
70 return kClientRedirectBase + dest_url;
73 std::string CreateServerRedirect(const std::string& dest_url) {
74 const char* const kServerRedirectBase = "server-redirect?";
75 return kServerRedirectBase + dest_url;
78 // Clears the specified data using BrowsingDataRemover.
79 void ClearBrowsingData(Browser* browser, int remove_mask) {
80 BrowsingDataRemover* remover =
81 new BrowsingDataRemover(browser->profile(),
82 BrowsingDataRemover::EVERYTHING,
83 base::Time());
84 remover->Remove(remove_mask);
85 // BrowsingDataRemover deletes itself.
88 void CancelAllPrerenders(PrerenderManager* prerender_manager) {
89 prerender_manager->CancelAllPrerenders();
92 // Returns true if and only if the final status is one in which the prerendered
93 // page should prerender correctly. The page still may not be used.
94 bool ShouldRenderPrerenderedPageCorrectly(FinalStatus status) {
95 switch (status) {
96 case FINAL_STATUS_USED:
97 case FINAL_STATUS_WINDOW_OPENER:
98 case FINAL_STATUS_APP_TERMINATING:
99 case FINAL_STATUS_FRAGMENT_MISMATCH:
100 case FINAL_STATUS_CACHE_OR_HISTORY_CLEARED:
101 // We'll crash the renderer after it's loaded.
102 case FINAL_STATUS_RENDERER_CRASHED:
103 case FINAL_STATUS_CANCELLED:
104 case FINAL_STATUS_DEVTOOLS_ATTACHED:
105 case FINAL_STATUS_SESSION_STORAGE_NAMESPACE_MISMATCH:
106 return true;
107 default:
108 return false;
112 // PrerenderContents that stops the UI message loop on DidStopLoading().
113 class TestPrerenderContents : public PrerenderContents {
114 public:
115 TestPrerenderContents(
116 PrerenderManager* prerender_manager,
117 PrerenderTracker* prerender_tracker,
118 Profile* profile,
119 const GURL& url,
120 const content::Referrer& referrer,
121 int expected_number_of_loads,
122 FinalStatus expected_final_status)
123 : PrerenderContents(prerender_manager, prerender_tracker,
124 profile, url, referrer, ORIGIN_LINK_REL_PRERENDER,
125 PrerenderManager::kNoExperiment),
126 number_of_loads_(0),
127 expected_number_of_loads_(expected_number_of_loads),
128 expected_final_status_(expected_final_status),
129 new_render_view_host_(NULL),
130 was_hidden_(false),
131 was_shown_(false),
132 should_be_shown_(expected_final_status == FINAL_STATUS_USED),
133 quit_message_loop_on_destruction_(
134 expected_final_status != FINAL_STATUS_EVICTED &&
135 expected_final_status != FINAL_STATUS_APP_TERMINATING &&
136 expected_final_status != FINAL_STATUS_MATCH_COMPLETE_DUMMY),
137 expected_pending_prerenders_(0) {
138 if (expected_number_of_loads == 0)
139 MessageLoopForUI::current()->Quit();
142 virtual ~TestPrerenderContents() {
143 EXPECT_EQ(expected_final_status_, final_status()) <<
144 " when testing URL " << prerender_url().path() <<
145 " (Expected: " << NameFromFinalStatus(expected_final_status_) <<
146 ", Actual: " << NameFromFinalStatus(final_status()) << ")";
147 // Prerendering RenderViewHosts should be hidden before the first
148 // navigation, so this should be happen for every PrerenderContents for
149 // which a RenderViewHost is created, regardless of whether or not it's
150 // used.
151 if (new_render_view_host_)
152 EXPECT_TRUE(was_hidden_);
154 // A used PrerenderContents will only be destroyed when we swap out
155 // TabContents, at the end of a navigation caused by a call to
156 // NavigateToURLImpl().
157 if (final_status() == FINAL_STATUS_USED)
158 EXPECT_TRUE(new_render_view_host_);
160 EXPECT_EQ(should_be_shown_, was_shown_);
162 // When the PrerenderContents is destroyed, quit the UI message loop.
163 // This happens on navigation to used prerendered pages, and soon
164 // after cancellation of unused prerendered pages.
165 if (quit_message_loop_on_destruction_)
166 MessageLoopForUI::current()->Quit();
169 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE {
170 // On quit, it's possible to end up here when render processes are closed
171 // before the PrerenderManager is destroyed. As a result, it's possible to
172 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED
173 // on quit.
175 // It's also possible for this to be called after we've been notified of
176 // app termination, but before we've been deleted, which is why the second
177 // check is needed.
178 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING &&
179 final_status() != expected_final_status_) {
180 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED;
183 PrerenderContents::RenderViewGone(status);
186 virtual bool AddAliasURL(const GURL& url) OVERRIDE {
187 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in
188 // the PrerenderRendererCrash test.
189 if (url.spec() != chrome::kAboutCrashURL)
190 return PrerenderContents::AddAliasURL(url);
191 return true;
194 virtual void DidStopLoading() OVERRIDE {
195 PrerenderContents::DidStopLoading();
196 ++number_of_loads_;
197 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status_) &&
198 number_of_loads_ == expected_number_of_loads_) {
199 MessageLoopForUI::current()->Quit();
203 virtual void AddPendingPrerender(Origin origin,
204 const GURL& url,
205 const content::Referrer& referrer) OVERRIDE {
206 PrerenderContents::AddPendingPrerender(origin, url, referrer);
207 if (expected_pending_prerenders_ > 0 &&
208 pending_prerender_list()->size() == expected_pending_prerenders_) {
209 MessageLoop::current()->Quit();
213 // Waits until the prerender has |expected_pending_prerenders| pending
214 // prerenders.
215 void WaitForPendingPrerenders(size_t expected_pending_prerenders) {
216 if (pending_prerender_list()->size() < expected_pending_prerenders) {
217 expected_pending_prerenders_ = expected_pending_prerenders;
218 ui_test_utils::RunMessageLoop();
219 expected_pending_prerenders_ = 0;
222 EXPECT_EQ(expected_pending_prerenders, pending_prerender_list()->size());
225 // For tests that open the prerender in a new background tab, the RenderView
226 // will not have been made visible when the PrerenderContents is destroyed
227 // even though it is used.
228 void set_should_be_shown(bool value) { should_be_shown_ = value; }
230 int number_of_loads() const { return number_of_loads_; }
232 private:
233 virtual void OnRenderViewHostCreated(
234 RenderViewHost* new_render_view_host) OVERRIDE {
235 // Used to make sure the RenderViewHost is hidden and, if used,
236 // subsequently shown.
237 notification_registrar().Add(
238 this,
239 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
240 content::Source<RenderWidgetHost>(new_render_view_host));
242 new_render_view_host_ = new_render_view_host;
244 PrerenderContents::OnRenderViewHostCreated(new_render_view_host);
247 virtual void Observe(int type,
248 const content::NotificationSource& source,
249 const content::NotificationDetails& details) OVERRIDE {
250 if (type ==
251 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED) {
252 EXPECT_EQ(new_render_view_host_,
253 content::Source<RenderWidgetHost>(source).ptr());
254 bool is_visible = *content::Details<bool>(details).ptr();
256 if (!is_visible) {
257 was_hidden_ = true;
258 } else if (is_visible && was_hidden_) {
259 // Once hidden, a prerendered RenderViewHost should only be shown after
260 // being removed from the PrerenderContents for display.
261 EXPECT_FALSE(render_view_host());
262 was_shown_ = true;
264 return;
266 PrerenderContents::Observe(type, source, details);
269 int number_of_loads_;
270 int expected_number_of_loads_;
271 FinalStatus expected_final_status_;
273 // The RenderViewHost created for the prerender, if any.
274 RenderViewHost* new_render_view_host_;
275 // Set to true when the prerendering RenderWidget is hidden.
276 bool was_hidden_;
277 // Set to true when the prerendering RenderWidget is shown, after having been
278 // hidden.
279 bool was_shown_;
280 // Expected final value of was_shown_. Defaults to true for
281 // FINAL_STATUS_USED, and false otherwise.
282 bool should_be_shown_;
284 // If true, quits message loop on destruction of |this|.
285 bool quit_message_loop_on_destruction_;
287 // Total number of pending prerenders we're currently waiting for. Zero
288 // indicates we currently aren't waiting for any.
289 size_t expected_pending_prerenders_;
292 // PrerenderManager that uses TestPrerenderContents.
293 class WaitForLoadPrerenderContentsFactory : public PrerenderContents::Factory {
294 public:
295 WaitForLoadPrerenderContentsFactory(
296 int expected_number_of_loads,
297 const std::deque<FinalStatus>& expected_final_status_queue)
298 : expected_number_of_loads_(expected_number_of_loads),
299 expected_final_status_queue_(expected_final_status_queue) {
300 VLOG(1) << "Factory created with queue length " <<
301 expected_final_status_queue_.size();
304 virtual PrerenderContents* CreatePrerenderContents(
305 PrerenderManager* prerender_manager,
306 PrerenderTracker* prerender_tracker,
307 Profile* profile,
308 const GURL& url,
309 const content::Referrer& referrer,
310 Origin origin,
311 uint8 experiment_id) OVERRIDE {
312 FinalStatus expected_final_status = FINAL_STATUS_MATCH_COMPLETE_DUMMY;
313 if (!expected_final_status_queue_.empty()) {
314 expected_final_status = expected_final_status_queue_.front();
315 expected_final_status_queue_.pop_front();
317 VLOG(1) << "Creating prerender contents for " << url.path() <<
318 " with expected final status " << expected_final_status;
319 VLOG(1) << expected_final_status_queue_.size() << " left in the queue.";
320 return new TestPrerenderContents(prerender_manager, prerender_tracker,
321 profile, url,
322 referrer, expected_number_of_loads_,
323 expected_final_status);
326 private:
327 int expected_number_of_loads_;
328 std::deque<FinalStatus> expected_final_status_queue_;
331 #if defined(ENABLE_SAFE_BROWSING)
332 // A SafeBrowingService implementation that returns a fixed result for a given
333 // URL.
334 class FakeSafeBrowsingService : public SafeBrowsingService {
335 public:
336 FakeSafeBrowsingService() :
337 result_(SAFE) {}
339 virtual ~FakeSafeBrowsingService() {}
341 // Called on the IO thread to check if the given url is safe or not. If we
342 // can synchronously determine that the url is safe, CheckUrl returns true.
343 // Otherwise it returns false, and "client" is called asynchronously with the
344 // result when it is ready.
345 // Returns true, indicating a SAFE result, unless the URL is the fixed URL
346 // specified by the user, and the user-specified result is not SAFE
347 // (in which that result will be communicated back via a call into the
348 // client, and false will be returned).
349 // Overrides SafeBrowsingService::CheckBrowseUrl.
350 virtual bool CheckBrowseUrl(const GURL& gurl, Client* client) OVERRIDE {
351 if (gurl != url_ || result_ == SAFE)
352 return true;
354 BrowserThread::PostTask(
355 BrowserThread::IO, FROM_HERE,
356 base::Bind(&FakeSafeBrowsingService::OnCheckBrowseURLDone, this, gurl,
357 client));
358 return false;
361 void SetResultForUrl(const GURL& url, UrlCheckResult result) {
362 url_ = url;
363 result_ = result;
366 private:
367 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) {
368 SafeBrowsingService::SafeBrowsingCheck check;
369 check.urls.push_back(gurl);
370 check.client = client;
371 check.result = result_;
372 client->OnSafeBrowsingResult(check);
375 GURL url_;
376 UrlCheckResult result_;
379 // Factory that creates FakeSafeBrowsingService instances.
380 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
381 public:
382 TestSafeBrowsingServiceFactory() :
383 most_recent_service_(NULL) { }
384 virtual ~TestSafeBrowsingServiceFactory() { }
386 virtual SafeBrowsingService* CreateSafeBrowsingService() OVERRIDE {
387 most_recent_service_ = new FakeSafeBrowsingService();
388 return most_recent_service_;
391 FakeSafeBrowsingService* most_recent_service() const {
392 return most_recent_service_;
395 private:
396 FakeSafeBrowsingService* most_recent_service_;
398 #endif
400 class FakeDevToolsClientHost : public DevToolsClientHost {
401 public:
402 FakeDevToolsClientHost() {}
403 virtual ~FakeDevToolsClientHost() {}
404 virtual void InspectedTabClosing() OVERRIDE {}
405 virtual void FrameNavigating(const std::string& url) OVERRIDE {}
406 virtual void DispatchOnInspectorFrontend(const std::string& msg) OVERRIDE {}
407 virtual void TabReplaced(TabContents* new_tab) OVERRIDE {}
410 } // namespace
412 class PrerenderBrowserTest : public InProcessBrowserTest {
413 public:
414 PrerenderBrowserTest()
415 : prerender_contents_factory_(NULL),
416 #if defined(ENABLE_SAFE_BROWSING)
417 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
418 #endif
419 use_https_src_server_(false),
420 call_javascript_(true),
421 loader_path_("files/prerender/prerender_loader.html") {
422 EnableDOMAutomation();
425 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
426 #if defined(ENABLE_SAFE_BROWSING)
427 SafeBrowsingService::RegisterFactory(safe_browsing_factory_.get());
428 #endif
431 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
432 command_line->AppendSwitchASCII(switches::kPrerenderMode,
433 switches::kPrerenderModeSwitchValueEnabled);
434 // This is needed to enable click-to-play without triggering a DCHECK.
435 // It does not actually enable click-to-play.
436 command_line->AppendSwitchASCII(switches::kEnableClickToPlay, "");
437 #if defined(OS_MACOSX)
438 // The plugins directory isn't read by default on the Mac, so it needs to be
439 // explicitly registered.
440 FilePath app_dir;
441 PathService::Get(chrome::DIR_APP, &app_dir);
442 command_line->AppendSwitchPath(
443 switches::kExtraPluginDir,
444 app_dir.Append(FILE_PATH_LITERAL("plugins")));
445 #endif
448 virtual void SetUpOnMainThread() OVERRIDE {
449 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPromptForDownload,
450 false);
451 // Increase the memory allowed in a prerendered page above normal settings.
452 // Debug build bots occasionally run against the default limit, and tests
453 // were failing because the prerender was canceled due to memory exhaustion.
454 // http://crbug.com/93076
455 prerender_manager()->mutable_config().max_bytes = 1000 * 1024 * 1024;
456 ASSERT_TRUE(test_server()->Start());
459 // Overload for a single expected final status
460 void PrerenderTestURL(const std::string& html_file,
461 FinalStatus expected_final_status,
462 int expected_number_of_loads) {
463 std::deque<FinalStatus> expected_final_status_queue(1,
464 expected_final_status);
465 PrerenderTestURL(html_file,
466 expected_final_status_queue,
467 expected_number_of_loads);
470 void PrerenderTestURL(
471 const std::string& html_file,
472 const std::deque<FinalStatus>& expected_final_status_queue,
473 int expected_number_of_loads) {
474 PrerenderTestURLImpl(test_server()->GetURL(html_file),
475 expected_final_status_queue,
476 expected_number_of_loads);
479 void PrerenderTestURL(
480 const GURL& url,
481 FinalStatus expected_final_status,
482 int expected_number_of_loads) {
483 std::deque<FinalStatus> expected_final_status_queue(1,
484 expected_final_status);
485 PrerenderTestURLImpl(url,
486 expected_final_status_queue,
487 expected_number_of_loads);
490 void NavigateToDestURL() const {
491 NavigateToDestURLWithDisposition(CURRENT_TAB);
494 // Opens the url in a new tab, with no opener.
495 void NavigateToDestURLWithDisposition(
496 WindowOpenDisposition disposition) const {
497 NavigateToURLImpl(dest_url_, disposition);
500 void OpenDestURLViaClick() const {
501 OpenDestURLWithJSImpl("Click()");
504 void OpenDestURLViaClickTarget() const {
505 OpenDestURLWithJSImpl("ClickTarget()");
508 void OpenDestURLViaClickNewWindow() const {
509 OpenDestURLWithJSImpl("ShiftClick()");
512 void OpenDestURLViaClickNewForegroundTab() const {
513 #if defined(OS_MACOSX)
514 OpenDestURLWithJSImpl("MetaShiftClick()");
515 #else
516 OpenDestURLWithJSImpl("CtrlShiftClick()");
517 #endif
520 void OpenDestURLViaClickNewBackgroundTab() const {
521 TestPrerenderContents* prerender_contents = GetPrerenderContents();
522 ASSERT_TRUE(prerender_contents != NULL);
523 prerender_contents->set_should_be_shown(false);
524 #if defined(OS_MACOSX)
525 OpenDestURLWithJSImpl("MetaClick()");
526 #else
527 OpenDestURLWithJSImpl("CtrlClick()");
528 #endif
531 void OpenDestURLViaWindowOpen() const {
532 OpenDestURLWithJSImpl("WindowOpen()");
535 void ClickToNextPageAfterPrerender(Browser* browser) {
536 ui_test_utils::WindowedNotificationObserver new_page_observer(
537 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
538 content::NotificationService::AllSources());
539 RenderViewHost* render_view_host =
540 browser->GetSelectedTabContents()->GetRenderViewHost();
541 render_view_host->ExecuteJavascriptInWebFrame(
542 string16(),
543 ASCIIToUTF16("ClickOpenLink()"));
544 new_page_observer.Wait();
547 void NavigateToNextPageAfterPrerender(Browser* browser) {
548 ui_test_utils::NavigateToURL(
549 browser,
550 test_server()->GetURL("files/prerender/prerender_page.html"));
553 void NavigateToDestUrlAndWaitForPassTitle() {
554 string16 expected_title = ASCIIToUTF16("PASS");
555 ui_test_utils::TitleWatcher title_watcher(
556 GetPrerenderContents()->prerender_contents()->tab_contents(),
557 expected_title);
558 NavigateToDestURL();
559 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
562 // Called after the prerendered page has been navigated to and then away from.
563 // Navigates back through the history to the prerendered page.
564 void GoBackToPrerender(Browser* browser) {
565 ui_test_utils::WindowedNotificationObserver back_nav_observer(
566 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
567 content::NotificationService::AllSources());
568 browser->GoBack(CURRENT_TAB);
569 back_nav_observer.Wait();
570 bool original_prerender_page = false;
571 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
572 browser->GetSelectedTabContents()->GetRenderViewHost(), L"",
573 L"window.domAutomationController.send(IsOriginalPrerenderPage())",
574 &original_prerender_page));
575 EXPECT_TRUE(original_prerender_page);
578 // Goes back to the page that was active before the prerender was swapped
579 // in. This must be called when the prerendered page is the current page
580 // in the active tab.
581 void GoBackToPageBeforePrerender(Browser* browser) {
582 TabContents* tab = browser->GetSelectedTabContents();
583 ASSERT_TRUE(tab);
584 EXPECT_FALSE(tab->IsLoading());
585 ui_test_utils::WindowedNotificationObserver back_nav_observer(
586 content::NOTIFICATION_LOAD_STOP,
587 content::Source<NavigationController>(&tab->GetController()));
588 browser->GoBack(CURRENT_TAB);
589 back_nav_observer.Wait();
590 bool js_result;
591 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
592 tab->GetRenderViewHost(), L"",
593 L"window.domAutomationController.send(DidBackToOriginalPagePass())",
594 &js_result));
595 EXPECT_TRUE(js_result);
598 // Should be const but test_server()->GetURL(...) is not const.
599 void NavigateToURL(const std::string& dest_html_file) {
600 GURL dest_url = test_server()->GetURL(dest_html_file);
601 NavigateToURLImpl(dest_url, CURRENT_TAB);
604 bool UrlIsInPrerenderManager(const std::string& html_file) {
605 GURL dest_url = test_server()->GetURL(html_file);
606 return (prerender_manager()->FindEntry(dest_url) != NULL);
609 bool UrlIsInPrerenderManager(const GURL& url) {
610 return (prerender_manager()->FindEntry(url) != NULL);
613 bool UrlIsPendingInPrerenderManager(const std::string& html_file) {
614 GURL dest_url = test_server()->GetURL(html_file);
615 return prerender_manager()->IsPendingEntry(dest_url);
618 void set_use_https_src(bool use_https_src_server) {
619 use_https_src_server_ = use_https_src_server;
622 void DisableJavascriptCalls() {
623 call_javascript_ = false;
626 TaskManagerModel* model() const {
627 return TaskManager::GetInstance()->model();
630 PrerenderManager* prerender_manager() const {
631 Profile* profile = browser()->GetSelectedTabContentsWrapper()->profile();
632 PrerenderManager* prerender_manager =
633 PrerenderManagerFactory::GetForProfile(profile);
634 return prerender_manager;
637 // Returns length of |prerender_manager_|'s history, or -1 on failure.
638 int GetHistoryLength() const {
639 scoped_ptr<DictionaryValue> prerender_dict(
640 static_cast<DictionaryValue*>(prerender_manager()->GetAsValue()));
641 if (!prerender_dict.get())
642 return -1;
643 ListValue* history_list;
644 if (!prerender_dict->GetList("history", &history_list))
645 return -1;
646 return static_cast<int>(history_list->GetSize());
649 #if defined(ENABLE_SAFE_BROWSING)
650 FakeSafeBrowsingService* GetSafeBrowsingService() {
651 return safe_browsing_factory_->most_recent_service();
653 #endif
655 TestPrerenderContents* GetPrerenderContents() const {
656 return static_cast<TestPrerenderContents*>(
657 prerender_manager()->FindEntry(dest_url_));
660 void set_loader_path(const std::string& path) {
661 loader_path_ = path;
664 GURL GetCrossDomainTestUrl(const std::string& path) {
665 static const std::string secondary_domain = "www.foo.com";
666 host_resolver()->AddRule(secondary_domain, "127.0.0.1");
667 std::string url_str(base::StringPrintf(
668 "http://%s:%d/%s",
669 secondary_domain.c_str(),
670 test_server()->host_port_pair().port(),
671 path.c_str()));
672 return GURL(url_str);
675 private:
676 void PrerenderTestURLImpl(
677 const GURL& url,
678 const std::deque<FinalStatus>& expected_final_status_queue,
679 int expected_number_of_loads) {
680 dest_url_ = url;
682 std::vector<net::TestServer::StringPair> replacement_text;
683 replacement_text.push_back(
684 make_pair("REPLACE_WITH_PRERENDER_URL", dest_url_.spec()));
685 std::string replacement_path;
686 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
687 loader_path_,
688 replacement_text,
689 &replacement_path));
691 net::TestServer* src_server = test_server();
692 scoped_ptr<net::TestServer> https_src_server;
693 if (use_https_src_server_) {
694 https_src_server.reset(
695 new net::TestServer(net::TestServer::TYPE_HTTPS,
696 FilePath(FILE_PATH_LITERAL("chrome/test/data"))));
697 ASSERT_TRUE(https_src_server->Start());
698 src_server = https_src_server.get();
700 GURL src_url = src_server->GetURL(replacement_path);
702 ASSERT_TRUE(prerender_manager());
703 prerender_manager()->mutable_config().rate_limit_enabled = false;
704 prerender_manager()->mutable_config().https_allowed = true;
705 ASSERT_TRUE(prerender_contents_factory_ == NULL);
706 prerender_contents_factory_ =
707 new WaitForLoadPrerenderContentsFactory(expected_number_of_loads,
708 expected_final_status_queue);
709 prerender_manager()->SetPrerenderContentsFactory(
710 prerender_contents_factory_);
711 FinalStatus expected_final_status = expected_final_status_queue.front();
713 // ui_test_utils::NavigateToURL uses its own observer and message loop.
714 // Since the test needs to wait until the prerendered page has stopped
715 // loading, rather than the page directly navigated to, need to
716 // handle browser navigation directly.
717 browser()->OpenURL(
718 src_url, GURL(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED);
720 ui_test_utils::RunMessageLoop();
722 TestPrerenderContents* prerender_contents = GetPrerenderContents();
724 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status)) {
725 ASSERT_TRUE(prerender_contents != NULL);
726 EXPECT_EQ(FINAL_STATUS_MAX, prerender_contents->final_status());
728 if (call_javascript_ && expected_number_of_loads > 0) {
729 // Check if page behaves as expected while in prerendered state.
730 bool prerender_test_result = false;
731 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
732 prerender_contents->render_view_host_mutable(), L"",
733 L"window.domAutomationController.send(DidPrerenderPass())",
734 &prerender_test_result));
735 EXPECT_TRUE(prerender_test_result);
737 } else {
738 // In the failure case, we should have removed |dest_url_| from the
739 // prerender_manager. We ignore dummy PrerenderContents (as indicated
740 // by not having started).
741 EXPECT_TRUE(prerender_contents == NULL ||
742 !prerender_contents->prerendering_has_started());
746 void NavigateToURLImpl(const GURL& dest_url,
747 WindowOpenDisposition disposition) const {
748 // Make sure in navigating we have a URL to use in the PrerenderManager.
749 ASSERT_TRUE(GetPrerenderContents() != NULL);
751 // If opening the page in a background tab, it won't be shown when swapped
752 // in.
753 if (disposition == NEW_BACKGROUND_TAB)
754 GetPrerenderContents()->set_should_be_shown(false);
756 // In the case of zero loads, need to wait for the page load to complete
757 // before running any Javascript.
758 scoped_ptr<ui_test_utils::WindowedNotificationObserver> page_load_observer;
759 TabContents* tab_contents =
760 GetPrerenderContents()->prerender_contents()->tab_contents();
761 if (GetPrerenderContents()->number_of_loads() == 0) {
762 page_load_observer.reset(
763 new ui_test_utils::WindowedNotificationObserver(
764 content::NOTIFICATION_LOAD_STOP,
765 content::Source<NavigationController>(
766 &tab_contents->GetController())));
769 // ui_test_utils::NavigateToURL waits until DidStopLoading is called on
770 // the current tab. As that tab is going to end up deleted, and may never
771 // finish loading before that happens, exit the message loop on the deletion
772 // of the used prerender contents instead.
774 // As PrerenderTestURL waits until the prerendered page has completely
775 // loaded, there is no race between loading |dest_url| and swapping the
776 // prerendered TabContents into the tab.
777 ui_test_utils::NavigateToURLWithDisposition(
778 browser(), dest_url, disposition, ui_test_utils::BROWSER_TEST_NONE);
779 ui_test_utils::RunMessageLoop();
781 // Make sure the PrerenderContents found earlier was used or removed.
782 EXPECT_TRUE(GetPrerenderContents() == NULL);
784 if (call_javascript_) {
785 if (page_load_observer.get())
786 page_load_observer->Wait();
788 bool display_test_result = false;
789 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
790 tab_contents->GetRenderViewHost(), L"",
791 L"window.domAutomationController.send(DidDisplayPass())",
792 &display_test_result));
793 EXPECT_TRUE(display_test_result);
797 // Opens the prerendered page using javascript functions in the
798 // loader page. |javascript_function_name| should be a 0 argument function
799 // which is invoked.
800 void OpenDestURLWithJSImpl(const std::string& javascript_function_name)
801 const {
802 TestPrerenderContents* prerender_contents = GetPrerenderContents();
803 ASSERT_TRUE(prerender_contents != NULL);
805 RenderViewHost* render_view_host =
806 browser()->GetSelectedTabContents()->GetRenderViewHost();
807 render_view_host->ExecuteJavascriptInWebFrame(
808 string16(),
809 ASCIIToUTF16(javascript_function_name));
811 // Run message loop until the prerender contents is destroyed.
812 ui_test_utils::RunMessageLoop();
815 WaitForLoadPrerenderContentsFactory* prerender_contents_factory_;
816 #if defined(ENABLE_SAFE_BROWSING)
817 scoped_ptr<TestSafeBrowsingServiceFactory> safe_browsing_factory_;
818 #endif
819 GURL dest_url_;
820 bool use_https_src_server_;
821 bool call_javascript_;
822 std::string loader_path_;
825 // Checks that a page is correctly prerendered in the case of a
826 // <link rel=prerender> tag and then loaded into a tab in response to a
827 // navigation.
828 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) {
829 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
830 NavigateToDestURL();
833 // Checks that the visibility API works.
834 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderVisibility) {
835 PrerenderTestURL("files/prerender/prerender_visibility.html",
836 FINAL_STATUS_USED,
838 NavigateToDestURL();
841 // Checks that the visibility API works when the prerender is quickly opened
842 // in a new tab before it stops loading.
843 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderVisibilityQuickSwitch) {
844 PrerenderTestURL("files/prerender/prerender_visibility_quick.html",
845 FINAL_STATUS_USED, 0);
846 NavigateToDestURL();
849 // Checks that the prerendering of a page is canceled correctly when a
850 // Javascript alert is called.
851 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertBeforeOnload) {
852 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html",
853 FINAL_STATUS_JAVASCRIPT_ALERT,
857 // Checks that the prerendering of a page is canceled correctly when a
858 // Javascript alert is called.
859 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertAfterOnload) {
860 PrerenderTestURL("files/prerender/prerender_alert_after_onload.html",
861 FINAL_STATUS_JAVASCRIPT_ALERT,
865 // Checks that plugins are not loaded while a page is being preloaded, but
866 // are loaded when the page is displayed.
867 #if defined(USE_AURA)
868 // http://crbug.com/103496
869 #define MAYBE_PrerenderDelayLoadPlugin DISABLED_PrerenderDelayLoadPlugin
870 #elif defined(OS_MACOSX)
871 // http://crbug.com/100514
872 #define MAYBE_PrerenderDelayLoadPlugin FAILS_PrerenderDelayLoadPlugin
873 #else
874 #define MAYBE_PrerenderDelayLoadPlugin PrerenderDelayLoadPlugin
875 #endif
876 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, MAYBE_PrerenderDelayLoadPlugin) {
877 PrerenderTestURL("files/prerender/plugin_delay_load.html",
878 FINAL_STATUS_USED,
880 NavigateToDestURL();
883 // Checks that plugins are not loaded on prerendering pages when click-to-play
884 // is enabled.
885 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickToPlay) {
886 // Enable click-to-play.
887 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
888 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK);
890 PrerenderTestURL("files/prerender/prerender_plugin_click_to_play.html",
891 FINAL_STATUS_USED,
893 NavigateToDestURL();
896 // Checks that plugins in an iframe are not loaded while a page is
897 // being preloaded, but are loaded when the page is displayed.
898 #if defined(USE_AURA)
899 // http://crbug.com/103496
900 #define MAYBE_PrerenderIframeDelayLoadPlugin \
901 DISABLED_PrerenderIframeDelayLoadPlugin
902 #elif defined(OS_MACOSX)
903 // http://crbug.com/100514
904 #define MAYBE_PrerenderIframeDelayLoadPlugin \
905 FAILS_PrerenderIframeDelayLoadPlugin
906 #else
907 #define MAYBE_PrerenderIframeDelayLoadPlugin PrerenderIframeDelayLoadPlugin
908 #endif
909 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
910 MAYBE_PrerenderIframeDelayLoadPlugin) {
911 PrerenderTestURL("files/prerender/prerender_iframe_plugin_delay_load.html",
912 FINAL_STATUS_USED,
914 NavigateToDestURL();
917 // Renders a page that contains a prerender link to a page that contains an
918 // iframe with a source that requires http authentication. This should not
919 // prerender successfully.
920 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttpAuthentication) {
921 PrerenderTestURL("files/prerender/prerender_http_auth_container.html",
922 FINAL_STATUS_AUTH_NEEDED,
926 // Checks that client-issued redirects work with prerendering.
927 // This version navigates to the page which issues the redirection, rather
928 // than the final destination page.
929 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
930 PrerenderClientRedirectNavigateToFirst) {
931 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
932 FINAL_STATUS_USED,
934 NavigateToDestURL();
937 // Checks that client-issued redirects work with prerendering.
938 // This version navigates to the final destination page, rather than the
939 // page which does the redirection.
940 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
941 PrerenderClientRedirectNavigateToSecond) {
942 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
943 FINAL_STATUS_USED,
945 NavigateToURL("files/prerender/prerender_page.html");
948 // Checks that a prerender for an https will prevent a prerender from happening.
949 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHttps) {
950 net::TestServer https_server(net::TestServer::TYPE_HTTPS,
951 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
952 ASSERT_TRUE(https_server.Start());
953 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
954 PrerenderTestURL(https_url,
955 FINAL_STATUS_USED,
957 NavigateToDestURL();
960 // Checks that client-issued redirects to an https page will cancel prerenders.
961 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClientRedirectToHttps) {
962 net::TestServer https_server(net::TestServer::TYPE_HTTPS,
963 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
964 ASSERT_TRUE(https_server.Start());
965 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
966 PrerenderTestURL(CreateClientRedirect(https_url.spec()),
967 FINAL_STATUS_USED,
969 NavigateToDestURL();
972 // Checks that client-issued redirects within an iframe in a prerendered
973 // page will not count as an "alias" for the prerendered page.
974 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClientRedirectInIframe) {
975 std::string redirect_path = CreateClientRedirect(
976 "/files/prerender/prerender_embedded_content.html");
977 std::vector<net::TestServer::StringPair> replacement_text;
978 replacement_text.push_back(
979 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
980 std::string replacement_path;
981 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
982 "files/prerender/prerender_with_iframe.html",
983 replacement_text,
984 &replacement_path));
985 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 2);
986 EXPECT_FALSE(UrlIsInPrerenderManager(
987 "files/prerender/prerender_embedded_content.html"));
988 NavigateToDestURL();
991 // Checks that client-issued redirects within an iframe in a prerendered
992 // page to an https page will not cancel the prerender, nor will it
993 // count as an "alias" for the prerendered page.
994 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
995 PrerenderClientRedirectToHttpsInIframe) {
996 net::TestServer https_server(net::TestServer::TYPE_HTTPS,
997 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
998 ASSERT_TRUE(https_server.Start());
999 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
1000 std::string redirect_path = CreateClientRedirect(https_url.spec());
1001 std::vector<net::TestServer::StringPair> replacement_text;
1002 replacement_text.push_back(
1003 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
1004 std::string replacement_path;
1005 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1006 "files/prerender/prerender_with_iframe.html",
1007 replacement_text,
1008 &replacement_path));
1009 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 2);
1010 EXPECT_FALSE(UrlIsInPrerenderManager(https_url));
1011 NavigateToDestURL();
1014 // Checks that server-issued redirects work with prerendering.
1015 // This version navigates to the page which issues the redirection, rather
1016 // than the final destination page.
1017 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1018 PrerenderServerRedirectNavigateToFirst) {
1019 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
1020 FINAL_STATUS_USED,
1022 NavigateToDestURL();
1025 // Checks that server-issued redirects work with prerendering.
1026 // This version navigates to the final destination page, rather than the
1027 // page which does the redirection.
1028 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1029 PrerenderServerRedirectNavigateToSecond) {
1030 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
1031 FINAL_STATUS_USED,
1033 NavigateToURL("files/prerender/prerender_page.html");
1036 // Checks that server-issued redirects from an http to an https
1037 // location will cancel prerendering.
1038 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1039 PrerenderServerRedirectToHttps) {
1040 net::TestServer https_server(net::TestServer::TYPE_HTTPS,
1041 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1042 ASSERT_TRUE(https_server.Start());
1043 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
1044 PrerenderTestURL(CreateServerRedirect(https_url.spec()),
1045 FINAL_STATUS_USED,
1047 NavigateToDestURL();
1050 // Checks that server-issued redirects within an iframe in a prerendered
1051 // page will not count as an "alias" for the prerendered page.
1052 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderServerRedirectInIframe) {
1053 std::string redirect_path = CreateServerRedirect(
1054 "/files/prerender/prerender_embedded_content.html");
1055 std::vector<net::TestServer::StringPair> replacement_text;
1056 replacement_text.push_back(
1057 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
1058 std::string replacement_path;
1059 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1060 "files/prerender/prerender_with_iframe.html",
1061 replacement_text,
1062 &replacement_path));
1063 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
1064 EXPECT_FALSE(UrlIsInPrerenderManager(
1065 "files/prerender/prerender_embedded_content.html"));
1066 NavigateToDestURL();
1069 // Checks that server-issued redirects within an iframe in a prerendered
1070 // page to an https page will not cancel the prerender, nor will it
1071 // count as an "alias" for the prerendered page.
1072 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1073 PrerenderServerRedirectToHttpsInIframe) {
1074 net::TestServer https_server(net::TestServer::TYPE_HTTPS,
1075 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1076 ASSERT_TRUE(https_server.Start());
1077 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
1078 std::string redirect_path = CreateServerRedirect(https_url.spec());
1079 std::vector<net::TestServer::StringPair> replacement_text;
1080 replacement_text.push_back(
1081 std::make_pair("REPLACE_WITH_URL", "/" + redirect_path));
1082 std::string replacement_path;
1083 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1084 "files/prerender/prerender_with_iframe.html",
1085 replacement_text,
1086 &replacement_path));
1087 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
1088 EXPECT_FALSE(UrlIsInPrerenderManager(https_url));
1089 NavigateToDestURL();
1092 // Prerenders a page that contains an automatic download triggered through an
1093 // iframe. This should not prerender successfully.
1094 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadIframe) {
1095 PrerenderTestURL("files/prerender/prerender_download_iframe.html",
1096 FINAL_STATUS_DOWNLOAD,
1100 // Prerenders a page that contains an automatic download triggered through
1101 // Javascript changing the window.location. This should not prerender
1102 // successfully
1103 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadLocation) {
1104 PrerenderTestURL(CreateClientRedirect("files/download-test1.lib"),
1105 FINAL_STATUS_DOWNLOAD,
1109 // Prerenders a page that contains an automatic download triggered through a
1110 // client-issued redirect. This should not prerender successfully.
1111 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDownloadClientRedirect) {
1112 PrerenderTestURL("files/prerender/prerender_download_refresh.html",
1113 FINAL_STATUS_DOWNLOAD,
1117 // Checks that the referrer is set when prerendering.
1118 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderReferrer) {
1119 PrerenderTestURL("files/prerender/prerender_referrer.html",
1120 FINAL_STATUS_USED,
1122 NavigateToDestURL();
1125 // Checks that the referrer is not set when prerendering and the source page is
1126 // HTTPS.
1127 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoSSLReferrer) {
1128 set_use_https_src(true);
1129 PrerenderTestURL("files/prerender/prerender_no_referrer.html",
1130 FINAL_STATUS_USED,
1132 NavigateToDestURL();
1135 // Checks that popups on a prerendered page cause cancellation.
1136 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPopup) {
1137 PrerenderTestURL("files/prerender/prerender_popup.html",
1138 FINAL_STATUS_CREATE_NEW_WINDOW,
1142 // Checks that renderers using excessive memory will be terminated.
1143 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderExcessiveMemory) {
1144 ASSERT_TRUE(prerender_manager());
1145 prerender_manager()->mutable_config().max_bytes = 30 * 1024 * 1024;
1146 PrerenderTestURL("files/prerender/prerender_excessive_memory.html",
1147 FINAL_STATUS_MEMORY_LIMIT_EXCEEDED,
1151 // Checks shutdown code while a prerender is active.
1152 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderQuickQuit) {
1153 PrerenderTestURL("files/prerender/prerender_page.html",
1154 FINAL_STATUS_APP_TERMINATING,
1158 // Checks that we don't prerender in an infinite loop.
1159 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderInfiniteLoop) {
1160 const char* const kHtmlFileA = "files/prerender/prerender_infinite_a.html";
1161 const char* const kHtmlFileB = "files/prerender/prerender_infinite_b.html";
1163 std::deque<FinalStatus> expected_final_status_queue;
1164 expected_final_status_queue.push_back(FINAL_STATUS_USED);
1165 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
1167 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
1168 ASSERT_TRUE(GetPrerenderContents());
1169 GetPrerenderContents()->WaitForPendingPrerenders(1u);
1171 // Next url should be in pending list but not an active entry.
1172 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
1173 EXPECT_TRUE(UrlIsPendingInPrerenderManager(kHtmlFileB));
1175 NavigateToDestURL();
1177 // Make sure the PrerenderContents for the next url is now in the manager
1178 // and not pending.
1179 EXPECT_TRUE(UrlIsInPrerenderManager(kHtmlFileB));
1180 EXPECT_FALSE(UrlIsPendingInPrerenderManager(kHtmlFileB));
1183 // Checks that we don't prerender in an infinite loop and multiple links are
1184 // handled correctly.
1185 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderInfiniteLoopMultiple) {
1186 const char* const kHtmlFileA =
1187 "files/prerender/prerender_infinite_a_multiple.html";
1188 const char* const kHtmlFileB =
1189 "files/prerender/prerender_infinite_b_multiple.html";
1190 const char* const kHtmlFileC =
1191 "files/prerender/prerender_infinite_c_multiple.html";
1193 // We need to set the final status to expect here before starting any
1194 // prerenders. We set them on a queue so whichever we see first is expected to
1195 // be evicted, and the second should stick around until we exit.
1196 std::deque<FinalStatus> expected_final_status_queue;
1197 expected_final_status_queue.push_back(FINAL_STATUS_USED);
1198 expected_final_status_queue.push_back(FINAL_STATUS_EVICTED);
1199 expected_final_status_queue.push_back(FINAL_STATUS_APP_TERMINATING);
1201 PrerenderTestURL(kHtmlFileA, expected_final_status_queue, 1);
1202 ASSERT_TRUE(GetPrerenderContents());
1203 GetPrerenderContents()->WaitForPendingPrerenders(2u);
1205 // Next url should be in pending list but not an active entry.
1206 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileB));
1207 EXPECT_FALSE(UrlIsInPrerenderManager(kHtmlFileC));
1208 EXPECT_TRUE(UrlIsPendingInPrerenderManager(kHtmlFileB));
1209 EXPECT_TRUE(UrlIsPendingInPrerenderManager(kHtmlFileC));
1211 NavigateToDestURL();
1213 // Make sure the PrerenderContents for the next urls are now in the manager
1214 // and not pending. One and only one of the URLs (the last seen) should be the
1215 // active entry.
1216 bool url_b_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileB);
1217 bool url_c_is_active_prerender = UrlIsInPrerenderManager(kHtmlFileC);
1218 EXPECT_TRUE((url_b_is_active_prerender || url_c_is_active_prerender) &&
1219 !(url_b_is_active_prerender && url_c_is_active_prerender));
1220 EXPECT_FALSE(UrlIsPendingInPrerenderManager(kHtmlFileB));
1221 EXPECT_FALSE(UrlIsPendingInPrerenderManager(kHtmlFileC));
1224 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderTaskManager) {
1225 // Show the task manager. This populates the model.
1226 browser()->window()->ShowTaskManager();
1227 // Wait for the model of task manager to start.
1228 TaskManagerBrowserTestUtil::WaitForResourceChange(2);
1230 // Start with two resources.
1231 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
1233 // One of the resources that has a TabContents associated with it should have
1234 // the Prerender prefix.
1235 const string16 prefix =
1236 l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX, string16());
1237 string16 prerender_title;
1238 int num_prerender_tabs = 0;
1240 for (int i = 0; i < model()->ResourceCount(); ++i) {
1241 if (model()->GetResourceTabContents(i)) {
1242 prerender_title = model()->GetResourceTitle(i);
1243 if (StartsWith(prerender_title, prefix, true))
1244 ++num_prerender_tabs;
1247 EXPECT_EQ(1, num_prerender_tabs);
1248 const string16 prerender_page_title = prerender_title.substr(prefix.length());
1250 NavigateToDestURL();
1252 // There should be no tabs with the Prerender prefix.
1253 const string16 tab_prefix =
1254 l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, string16());
1255 num_prerender_tabs = 0;
1256 int num_tabs_with_prerender_page_title = 0;
1257 for (int i = 0; i < model()->ResourceCount(); ++i) {
1258 if (model()->GetResourceTabContents(i)) {
1259 string16 tab_title = model()->GetResourceTitle(i);
1260 if (StartsWith(tab_title, prefix, true)) {
1261 ++num_prerender_tabs;
1262 } else {
1263 EXPECT_TRUE(StartsWith(tab_title, tab_prefix, true));
1265 // The prerender tab should now be a normal tab but the title should be
1266 // the same. Depending on timing, there may be more than one of these.
1267 const string16 tab_page_title = tab_title.substr(tab_prefix.length());
1268 if (prerender_page_title.compare(tab_page_title) == 0)
1269 ++num_tabs_with_prerender_page_title;
1273 EXPECT_EQ(0, num_prerender_tabs);
1275 // We may have deleted the prerender tab, but the swapped in tab should be
1276 // active.
1277 EXPECT_GE(num_tabs_with_prerender_page_title, 1);
1278 EXPECT_LE(num_tabs_with_prerender_page_title, 2);
1281 // Checks that audio loads are deferred on prerendering.
1282 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Audio) {
1283 PrerenderTestURL("files/prerender/prerender_html5_audio.html",
1284 FINAL_STATUS_USED,
1286 NavigateToDestUrlAndWaitForPassTitle();
1289 // Checks that audio loads are deferred on prerendering and played back when
1290 // the prerender is swapped in if autoplay is set.
1291 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioAutoplay) {
1292 PrerenderTestURL("files/prerender/prerender_html5_audio_autoplay.html",
1293 FINAL_STATUS_USED,
1295 NavigateToDestUrlAndWaitForPassTitle();
1298 // Checks that audio loads are deferred on prerendering and played back when
1299 // the prerender is swapped in if js starts playing.
1300 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5AudioJsplay) {
1301 PrerenderTestURL("files/prerender/prerender_html5_audio_jsplay.html",
1302 FINAL_STATUS_USED,
1304 NavigateToDestUrlAndWaitForPassTitle();
1307 // Checks that video loads are deferred on prerendering.
1308 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5Video) {
1309 PrerenderTestURL("files/prerender/prerender_html5_video.html",
1310 FINAL_STATUS_USED,
1312 NavigateToDestUrlAndWaitForPassTitle();
1315 // Checks that video tags inserted by javascript are deferred and played
1316 // correctly on swap in.
1317 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoJs) {
1318 PrerenderTestURL("files/prerender/prerender_html5_video_script.html",
1319 FINAL_STATUS_USED,
1321 NavigateToDestUrlAndWaitForPassTitle();
1324 // Checks for correct network events by using a busy sleep the javascript.
1325 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderHTML5VideoNetwork) {
1326 PrerenderTestURL("files/prerender/prerender_html5_video_network.html",
1327 FINAL_STATUS_USED,
1329 NavigateToDestUrlAndWaitForPassTitle();
1332 // Checks that scripts can retrieve the correct window size while prerendering.
1333 #if defined(TOOLKIT_VIEWS)
1334 // TODO(beng): Widget hierarchy split causes this to fail http://crbug.com/82363
1335 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, DISABLED_PrerenderWindowSize) {
1336 #else
1337 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderWindowSize) {
1338 #endif
1339 PrerenderTestURL("files/prerender/prerender_size.html",
1340 FINAL_STATUS_USED,
1342 NavigateToDestURL();
1345 // Checks that prerenderers will terminate when the RenderView crashes.
1346 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderRendererCrash) {
1347 PrerenderTestURL("files/prerender/prerender_page.html",
1348 FINAL_STATUS_RENDERER_CRASHED,
1351 // Navigate to about:crash and then wait for the renderer to crash.
1352 ASSERT_TRUE(GetPrerenderContents());
1353 ASSERT_TRUE(GetPrerenderContents()->prerender_contents());
1354 GetPrerenderContents()->prerender_contents()->tab_contents()->GetController().
1355 LoadURL(
1356 GURL(chrome::kAboutCrashURL),
1357 content::Referrer(),
1358 content::PAGE_TRANSITION_TYPED,
1359 std::string());
1360 ui_test_utils::RunMessageLoop();
1363 // Checks that we correctly use a prerendered page when navigating to a
1364 // fragment.
1365 // DISABLED: http://crbug.com/84154
1366 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1367 DISABLED_PrerenderPageNavigateFragment) {
1368 PrerenderTestURL("files/prerender/prerender_fragment.html",
1369 FINAL_STATUS_FRAGMENT_MISMATCH,
1371 NavigateToURL("files/prerender/prerender_fragment.html#fragment");
1374 // Checks that we correctly use a prerendered page when we prerender a fragment
1375 // but navigate to the main page.
1376 // http://crbug.com/83901
1377 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1378 DISABLED_PrerenderFragmentNavigatePage) {
1379 PrerenderTestURL("files/prerender/prerender_fragment.html#fragment",
1380 FINAL_STATUS_FRAGMENT_MISMATCH,
1382 NavigateToURL("files/prerender/prerender_fragment.html");
1385 // Checks that we correctly use a prerendered page when we prerender a fragment
1386 // but navigate to a different fragment on the same page.
1387 // DISABLED: http://crbug.com/84154
1388 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1389 DISABLED_PrerenderFragmentNavigateFragment) {
1390 PrerenderTestURL("files/prerender/prerender_fragment.html#other_fragment",
1391 FINAL_STATUS_FRAGMENT_MISMATCH,
1393 NavigateToURL("files/prerender/prerender_fragment.html#fragment");
1396 // Checks that we correctly use a prerendered page when the page uses a client
1397 // redirect to refresh from a fragment on the same page.
1398 // http://crbug.com/83901
1399 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1400 DISABLED_PrerenderClientRedirectFromFragment) {
1401 PrerenderTestURL(
1402 CreateClientRedirect("files/prerender/prerender_fragment.html#fragment"),
1403 FINAL_STATUS_FRAGMENT_MISMATCH,
1405 NavigateToURL("files/prerender/prerender_fragment.html");
1408 // Checks that we correctly use a prerendered page when the page uses a crient
1409 // redirect to refresh to a fragment on the same page.
1410 // DISABLED: http://crbug.com/84154
1411 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1412 DISABLED_PrerenderClientRedirectToFragment) {
1413 PrerenderTestURL(
1414 CreateClientRedirect("files/prerender/prerender_fragment.html"),
1415 FINAL_STATUS_FRAGMENT_MISMATCH,
1417 NavigateToURL("files/prerender/prerender_fragment.html#fragment");
1420 // Checks that we correctly use a prerendered page when the page uses JS to set
1421 // the window.location.hash to a fragment on the same page.
1422 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1423 PrerenderPageChangeFragmentLocationHash) {
1424 PrerenderTestURL("files/prerender/prerender_fragment_location_hash.html",
1425 FINAL_STATUS_USED,
1427 NavigateToURL("files/prerender/prerender_fragment_location_hash.html");
1430 // Checks that prerendering a PNG works correctly.
1431 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImagePng) {
1432 DisableJavascriptCalls();
1433 PrerenderTestURL("files/prerender/image.png", FINAL_STATUS_USED, 1);
1434 NavigateToDestURL();
1437 // Checks that prerendering a JPG works correctly.
1438 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderImageJpeg) {
1439 DisableJavascriptCalls();
1440 PrerenderTestURL("files/prerender/image.jpeg", FINAL_STATUS_USED, 1);
1441 NavigateToDestURL();
1444 // Checks that a prerender of a CRX will result in a cancellation due to
1445 // download.
1446 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCrx) {
1447 PrerenderTestURL("files/prerender/extension.crx", FINAL_STATUS_DOWNLOAD, 1);
1450 // Checks that xhr GET requests allow prerenders.
1451 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrGet) {
1452 PrerenderTestURL("files/prerender/prerender_xhr_get.html",
1453 FINAL_STATUS_USED,
1455 NavigateToDestURL();
1458 // Checks that xhr HEAD requests allow prerenders.
1459 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrHead) {
1460 PrerenderTestURL("files/prerender/prerender_xhr_head.html",
1461 FINAL_STATUS_USED,
1463 NavigateToDestURL();
1466 // Checks that xhr OPTIONS requests allow prerenders.
1467 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrOptions) {
1468 PrerenderTestURL("files/prerender/prerender_xhr_options.html",
1469 FINAL_STATUS_USED,
1471 NavigateToDestURL();
1474 // Checks that xhr TRACE requests allow prerenders.
1475 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrTrace) {
1476 PrerenderTestURL("files/prerender/prerender_xhr_trace.html",
1477 FINAL_STATUS_USED,
1479 NavigateToDestURL();
1482 // Checks that xhr POST requests allow prerenders.
1483 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPost) {
1484 PrerenderTestURL("files/prerender/prerender_xhr_post.html",
1485 FINAL_STATUS_USED,
1487 NavigateToDestURL();
1490 // Checks that xhr PUT cancels prerenders.
1491 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrPut) {
1492 PrerenderTestURL("files/prerender/prerender_xhr_put.html",
1493 FINAL_STATUS_INVALID_HTTP_METHOD,
1497 // Checks that xhr DELETE cancels prerenders.
1498 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrDelete) {
1499 PrerenderTestURL("files/prerender/prerender_xhr_delete.html",
1500 FINAL_STATUS_INVALID_HTTP_METHOD,
1504 // Checks that a top-level page which would trigger an SSL error is canceled.
1505 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorTopLevel) {
1506 net::TestServer::HTTPSOptions https_options;
1507 https_options.server_certificate =
1508 net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME;
1509 net::TestServer https_server(https_options,
1510 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1511 ASSERT_TRUE(https_server.Start());
1512 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
1513 PrerenderTestURL(https_url,
1514 FINAL_STATUS_SSL_ERROR,
1518 // Checks that an SSL error that comes from a subresource does not cancel
1519 // the page. Non-main-frame requests are simply cancelled if they run into
1520 // an SSL problem.
1521 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorSubresource) {
1522 net::TestServer::HTTPSOptions https_options;
1523 https_options.server_certificate =
1524 net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME;
1525 net::TestServer https_server(https_options,
1526 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1527 ASSERT_TRUE(https_server.Start());
1528 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
1529 std::vector<net::TestServer::StringPair> replacement_text;
1530 replacement_text.push_back(
1531 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
1532 std::string replacement_path;
1533 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1534 "files/prerender/prerender_with_image.html",
1535 replacement_text,
1536 &replacement_path));
1537 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
1538 NavigateToDestURL();
1541 // Checks that an SSL error that comes from an iframe does not cancel
1542 // the page. Non-main-frame requests are simply cancelled if they run into
1543 // an SSL problem.
1544 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLErrorIframe) {
1545 net::TestServer::HTTPSOptions https_options;
1546 https_options.server_certificate =
1547 net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME;
1548 net::TestServer https_server(https_options,
1549 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1550 ASSERT_TRUE(https_server.Start());
1551 GURL https_url = https_server.GetURL(
1552 "files/prerender/prerender_embedded_content.html");
1553 std::vector<net::TestServer::StringPair> replacement_text;
1554 replacement_text.push_back(
1555 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
1556 std::string replacement_path;
1557 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1558 "files/prerender/prerender_with_iframe.html",
1559 replacement_text,
1560 &replacement_path));
1561 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1);
1562 NavigateToDestURL();
1565 // Checks that we cancel correctly when window.print() is called.
1566 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPrint) {
1567 PrerenderTestURL("files/prerender/prerender_print.html",
1568 FINAL_STATUS_WINDOW_PRINT,
1572 // Checks that if a page is opened in a new window by javascript and both the
1573 // pages are in the same domain, the prerendered page is not used.
1574 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1575 PrerenderSameDomainWindowOpenerWindowOpen) {
1576 PrerenderTestURL("files/prerender/prerender_page.html",
1577 FINAL_STATUS_WINDOW_OPENER,
1579 OpenDestURLViaWindowOpen();
1582 // Checks that if a page is opened due to click on a href with target="_blank"
1583 // and both pages are in the same domain the prerendered page is not used.
1584 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1585 PrerenderSameDomainWindowOpenerClickTarget) {
1586 PrerenderTestURL("files/prerender/prerender_page.html",
1587 FINAL_STATUS_WINDOW_OPENER,
1589 OpenDestURLViaClickTarget();
1592 // Checks that a top-level page which would normally request an SSL client
1593 // certificate will never be seen since it's an https top-level resource.
1594 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLClientCertTopLevel) {
1595 net::TestServer::HTTPSOptions https_options;
1596 https_options.request_client_certificate = true;
1597 net::TestServer https_server(https_options,
1598 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1599 ASSERT_TRUE(https_server.Start());
1600 GURL https_url = https_server.GetURL("files/prerender/prerender_page.html");
1601 PrerenderTestURL(https_url, FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED, 1);
1604 // Checks that an SSL Client Certificate request that originates from a
1605 // subresource will cancel the prerendered page.
1606 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1607 PrerenderSSLClientCertSubresource) {
1608 net::TestServer::HTTPSOptions https_options;
1609 https_options.request_client_certificate = true;
1610 net::TestServer https_server(https_options,
1611 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1612 ASSERT_TRUE(https_server.Start());
1613 GURL https_url = https_server.GetURL("files/prerender/image.jpeg");
1614 std::vector<net::TestServer::StringPair> replacement_text;
1615 replacement_text.push_back(
1616 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec()));
1617 std::string replacement_path;
1618 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1619 "files/prerender/prerender_with_image.html",
1620 replacement_text,
1621 &replacement_path));
1622 PrerenderTestURL(replacement_path,
1623 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
1627 // Checks that an SSL Client Certificate request that originates from an
1628 // iframe will cancel the prerendered page.
1629 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSSLClientCertIframe) {
1630 net::TestServer::HTTPSOptions https_options;
1631 https_options.request_client_certificate = true;
1632 net::TestServer https_server(https_options,
1633 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1634 ASSERT_TRUE(https_server.Start());
1635 GURL https_url = https_server.GetURL(
1636 "files/prerender/prerender_embedded_content.html");
1637 std::vector<net::TestServer::StringPair> replacement_text;
1638 replacement_text.push_back(
1639 std::make_pair("REPLACE_WITH_URL", https_url.spec()));
1640 std::string replacement_path;
1641 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1642 "files/prerender/prerender_with_iframe.html",
1643 replacement_text,
1644 &replacement_path));
1645 PrerenderTestURL(replacement_path,
1646 FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED,
1650 #if defined(ENABLE_SAFE_BROWSING)
1651 // Ensures that we do not prerender pages with a safe browsing
1652 // interstitial.
1653 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingTopLevel) {
1654 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
1655 GetSafeBrowsingService()->SetResultForUrl(
1656 url, SafeBrowsingService::URL_MALWARE);
1657 PrerenderTestURL("files/prerender/prerender_page.html",
1658 FINAL_STATUS_SAFE_BROWSING, 1);
1661 // Ensures that server redirects to a malware page will cancel prerenders.
1662 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1663 PrerenderSafeBrowsingServerRedirect) {
1664 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
1665 GetSafeBrowsingService()->SetResultForUrl(
1666 url, SafeBrowsingService::URL_MALWARE);
1667 PrerenderTestURL(CreateServerRedirect("files/prerender/prerender_page.html"),
1668 FINAL_STATUS_SAFE_BROWSING,
1672 // Ensures that client redirects to a malware page will cancel prerenders.
1673 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1674 PrerenderSafeBrowsingClientRedirect) {
1675 GURL url = test_server()->GetURL("files/prerender/prerender_page.html");
1676 GetSafeBrowsingService()->SetResultForUrl(
1677 url, SafeBrowsingService::URL_MALWARE);
1678 PrerenderTestURL(CreateClientRedirect("files/prerender/prerender_page.html"),
1679 FINAL_STATUS_SAFE_BROWSING,
1683 // Ensures that we do not prerender pages which have a malware subresource.
1684 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingSubresource) {
1685 GURL image_url = test_server()->GetURL("files/prerender/image.jpeg");
1686 GetSafeBrowsingService()->SetResultForUrl(
1687 image_url, SafeBrowsingService::URL_MALWARE);
1688 std::vector<net::TestServer::StringPair> replacement_text;
1689 replacement_text.push_back(
1690 std::make_pair("REPLACE_WITH_IMAGE_URL", image_url.spec()));
1691 std::string replacement_path;
1692 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1693 "files/prerender/prerender_with_image.html",
1694 replacement_text,
1695 &replacement_path));
1696 PrerenderTestURL(replacement_path,
1697 FINAL_STATUS_SAFE_BROWSING,
1701 // Ensures that we do not prerender pages which have a malware iframe.
1702 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSafeBrowsingIframe) {
1703 GURL iframe_url = test_server()->GetURL(
1704 "files/prerender/prerender_embedded_content.html");
1705 GetSafeBrowsingService()->SetResultForUrl(
1706 iframe_url, SafeBrowsingService::URL_MALWARE);
1707 std::vector<net::TestServer::StringPair> replacement_text;
1708 replacement_text.push_back(
1709 std::make_pair("REPLACE_WITH_URL", iframe_url.spec()));
1710 std::string replacement_path;
1711 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements(
1712 "files/prerender/prerender_with_iframe.html",
1713 replacement_text,
1714 &replacement_path));
1715 PrerenderTestURL(replacement_path,
1716 FINAL_STATUS_SAFE_BROWSING,
1720 #endif
1722 // Checks that a local storage read will not cause prerender to fail.
1723 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageRead) {
1724 PrerenderTestURL("files/prerender/prerender_localstorage_read.html",
1725 FINAL_STATUS_USED,
1727 NavigateToDestURL();
1730 // Checks that a local storage write will not cause prerender to fail.
1731 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderLocalStorageWrite) {
1732 PrerenderTestURL("files/prerender/prerender_localstorage_write.html",
1733 FINAL_STATUS_USED,
1735 NavigateToDestURL();
1738 // Checks that the favicon is properly loaded on prerender.
1739 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderFavicon) {
1740 PrerenderTestURL("files/prerender/prerender_favicon.html",
1741 FINAL_STATUS_USED,
1743 TestPrerenderContents* prerender_contents = GetPrerenderContents();
1744 ASSERT_TRUE(prerender_contents != NULL);
1745 ui_test_utils::WindowedNotificationObserver favicon_update_watcher(
1746 chrome::NOTIFICATION_FAVICON_UPDATED,
1747 content::Source<TabContents>(prerender_contents->prerender_contents()->
1748 tab_contents()));
1749 NavigateToDestURL();
1750 favicon_update_watcher.Wait();
1753 // Checks that when a prerendered page is swapped in to a referring page, the
1754 // unload handlers on the referring page are executed.
1755 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderUnload) {
1756 set_loader_path("files/prerender/prerender_loader_with_unload.html");
1757 PrerenderTestURL("files/prerender/prerender_page.html", FINAL_STATUS_USED, 1);
1758 string16 expected_title = ASCIIToUTF16("Unloaded");
1759 ui_test_utils::TitleWatcher title_watcher(browser()->GetSelectedTabContents(),
1760 expected_title);
1761 NavigateToDestURL();
1762 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
1765 // Checks that when the history is cleared, prerendering is cancelled and
1766 // prerendering history is cleared.
1767 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearHistory) {
1768 PrerenderTestURL("files/prerender/prerender_page.html",
1769 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
1772 // Post a task to clear the history, and run the message loop until it
1773 // destroys the prerender.
1774 MessageLoop::current()->PostTask(
1775 FROM_HERE,
1776 base::Bind(&ClearBrowsingData, browser(),
1777 BrowsingDataRemover::REMOVE_HISTORY));
1778 ui_test_utils::RunMessageLoop();
1780 // Make sure prerender history was cleared.
1781 EXPECT_EQ(0, GetHistoryLength());
1784 // Checks that when the cache is cleared, prerenders are cancelled but
1785 // prerendering history is not cleared.
1786 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClearCache) {
1787 PrerenderTestURL("files/prerender/prerender_page.html",
1788 FINAL_STATUS_CACHE_OR_HISTORY_CLEARED,
1791 // Post a task to clear the cache, and run the message loop until it
1792 // destroys the prerender.
1793 MessageLoop::current()->PostTask(FROM_HERE,
1794 base::Bind(&ClearBrowsingData, browser(),
1795 BrowsingDataRemover::REMOVE_CACHE));
1796 ui_test_utils::RunMessageLoop();
1798 // Make sure prerender history was not cleared. Not a vital behavior, but
1799 // used to compare with PrerenderClearHistory test.
1800 EXPECT_EQ(1, GetHistoryLength());
1803 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderCancelAll) {
1804 PrerenderTestURL("files/prerender/prerender_page.html",
1805 FINAL_STATUS_CANCELLED,
1807 // Post a task to cancel all the prerenders.
1808 MessageLoop::current()->PostTask(
1809 FROM_HERE,
1810 base::Bind(&CancelAllPrerenders, prerender_manager()));
1811 ui_test_utils::RunMessageLoop();
1812 EXPECT_TRUE(GetPrerenderContents() == NULL);
1815 // Prerendering and history tests.
1816 // The prerendered page is navigated to in several ways [navigate via
1817 // omnibox, click on link, key-modified click to open in background tab, etc],
1818 // followed by a navigation to another page from the prerendered page, followed
1819 // by a back navigation.
1821 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNavigateClickGoBack) {
1822 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
1823 FINAL_STATUS_USED,
1825 NavigateToDestURL();
1826 ClickToNextPageAfterPrerender(browser());
1827 GoBackToPrerender(browser());
1830 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1831 PrerenderNavigateNavigateGoBack) {
1832 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
1833 FINAL_STATUS_USED,
1835 NavigateToDestURL();
1836 NavigateToNextPageAfterPrerender(browser());
1837 GoBackToPrerender(browser());
1840 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickClickGoBack) {
1841 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
1842 FINAL_STATUS_USED,
1844 OpenDestURLViaClick();
1845 ClickToNextPageAfterPrerender(browser());
1846 GoBackToPrerender(browser());
1849 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNavigateGoBack) {
1850 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
1851 FINAL_STATUS_USED,
1853 OpenDestURLViaClick();
1854 NavigateToNextPageAfterPrerender(browser());
1855 GoBackToPrerender(browser());
1858 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewWindow) {
1859 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
1860 FINAL_STATUS_SESSION_STORAGE_NAMESPACE_MISMATCH,
1862 OpenDestURLViaClickNewWindow();
1865 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewForegroundTab) {
1866 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
1867 FINAL_STATUS_SESSION_STORAGE_NAMESPACE_MISMATCH,
1869 OpenDestURLViaClickNewForegroundTab();
1872 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderClickNewBackgroundTab) {
1873 PrerenderTestURL("files/prerender/prerender_page_with_link.html",
1874 FINAL_STATUS_SESSION_STORAGE_NAMESPACE_MISMATCH,
1876 OpenDestURLViaClickNewBackgroundTab();
1879 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
1880 NavigateToPrerenderedPageWhenDevToolsAttached) {
1881 DisableJavascriptCalls();
1882 TabContents* tab_contents = browser()->GetSelectedTabContents();
1883 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
1884 tab_contents->GetRenderViewHost());
1885 DevToolsManager* manager = DevToolsManager::GetInstance();
1886 FakeDevToolsClientHost client_host;
1887 manager->RegisterDevToolsClientHostFor(agent, &client_host);
1888 const char* url = "files/prerender/prerender_page.html";
1889 PrerenderTestURL(url, FINAL_STATUS_DEVTOOLS_ATTACHED, 1);
1890 NavigateToURL(url);
1891 manager->ClientHostClosing(&client_host);
1894 // Validate that the sessionStorage namespace remains the same when swapping
1895 // in a prerendered page.
1896 // http://crbug.com/103563
1897 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, DISABLED_PrerenderSessionStorage) {
1898 set_loader_path("files/prerender/prerender_loader_with_session_storage.html");
1899 PrerenderTestURL(GetCrossDomainTestUrl("files/prerender/prerender_page.html"),
1900 FINAL_STATUS_USED,
1902 NavigateToDestURL();
1903 GoBackToPageBeforePrerender(browser());
1906 } // namespace prerender