OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/command_line.h" |
| 6 #include "base/path_service.h" |
| 7 #include "chrome/browser/prerender/prerender_contents.h" |
| 8 #include "chrome/browser/prerender/prerender_manager.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/tab_contents/tab_contents.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/net/url_request_context_getter.h" |
| 15 #include "chrome/test/in_process_browser_test.h" |
| 16 #include "chrome/test/ui_test_utils.h" |
| 17 #include "net/url_request/url_request_context.h" |
| 18 |
| 19 // Prerender tests work as follows: |
| 20 // |
| 21 // A page with a prefetch link to the test page is loaded. Once prerendered, |
| 22 // its Javascript function DidPrerenderPass() is called, which returns true if |
| 23 // the page behaves as expected when prerendered. |
| 24 // |
| 25 // The prerendered page is then displayed on a tab. The Javascript function |
| 26 // DidDisplayPass() is called, and returns true if the page behaved as it |
| 27 // should while being displayed. |
| 28 |
| 29 namespace { |
| 30 |
| 31 // PrerenderContents that stops the UI message loop on DidStopLoading(). |
| 32 class TestPrerenderContents : public PrerenderContents { |
| 33 public: |
| 34 TestPrerenderContents( |
| 35 PrerenderManager* prerender_manager, Profile* profile, const GURL& url, |
| 36 const std::vector<GURL>& alias_urls) |
| 37 : PrerenderContents(prerender_manager, profile, url, alias_urls), |
| 38 did_finish_loading_(false) { |
| 39 } |
| 40 |
| 41 virtual void DidStopLoading() { |
| 42 PrerenderContents::DidStopLoading(); |
| 43 did_finish_loading_ = true; |
| 44 MessageLoopForUI::current()->Quit(); |
| 45 } |
| 46 |
| 47 bool did_finish_loading() const { return did_finish_loading_; } |
| 48 |
| 49 private: |
| 50 bool did_finish_loading_; |
| 51 }; |
| 52 |
| 53 // PrerenderManager that uses TestPrerenderContents. |
| 54 class WaitForLoadPrerenderContentsFactory : public PrerenderContents::Factory { |
| 55 public: |
| 56 virtual PrerenderContents* CreatePrerenderContents( |
| 57 PrerenderManager* prerender_manager, Profile* profile, const GURL& url, |
| 58 const std::vector<GURL>& alias_urls) { |
| 59 return new TestPrerenderContents(prerender_manager, profile, url, |
| 60 alias_urls); |
| 61 } |
| 62 }; |
| 63 |
| 64 } // namespace |
| 65 |
| 66 class PrerenderBrowserTest : public InProcessBrowserTest { |
| 67 public: |
| 68 PrerenderBrowserTest() { |
| 69 EnableDOMAutomation(); |
| 70 } |
| 71 |
| 72 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 73 command_line->AppendSwitch(switches::kEnablePagePrerender); |
| 74 #if defined(OS_MACOSX) |
| 75 // The plugins directory isn't read by default on the Mac, so it needs to be |
| 76 // explicitly registered. |
| 77 FilePath app_dir; |
| 78 PathService::Get(chrome::DIR_APP, &app_dir); |
| 79 command_line->AppendSwitchPath( |
| 80 switches::kExtraPluginDir, |
| 81 app_dir.Append(FILE_PATH_LITERAL("plugins"))); |
| 82 #endif |
| 83 } |
| 84 |
| 85 void RunTestURL(const std::string& html_file) { |
| 86 ASSERT_TRUE(test_server()->Start()); |
| 87 |
| 88 std::string src_path = "files/prerender/prerender_loader.html?"; |
| 89 src_path.append(html_file); |
| 90 std::string dest_path = "files/prerender/"; |
| 91 dest_path.append(html_file); |
| 92 |
| 93 GURL src_url = test_server()->GetURL(src_path); |
| 94 GURL dest_url = test_server()->GetURL(dest_path); |
| 95 |
| 96 Profile* profile = browser()->GetSelectedTabContents()->profile(); |
| 97 PrerenderManager* prerender_manager = profile->GetPrerenderManager(); |
| 98 ASSERT_TRUE(prerender_manager); |
| 99 |
| 100 // This is needed to exit the event loop once the prerendered page has |
| 101 // stopped loading. |
| 102 prerender_manager->SetPrerenderContentsFactory( |
| 103 new WaitForLoadPrerenderContentsFactory()); |
| 104 |
| 105 // ui_test_utils::NavigateToURL uses its own observer and message loop. |
| 106 // Since the test needs to wait until the prerendered page has stopped |
| 107 // loading, rathather than the page directly navigated to, need to |
| 108 // handle browser navigation directly. |
| 109 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); |
| 110 |
| 111 ui_test_utils::RunMessageLoop(); |
| 112 |
| 113 TestPrerenderContents* prerender_contents = |
| 114 static_cast<TestPrerenderContents*>( |
| 115 prerender_manager->FindEntry(dest_url)); |
| 116 |
| 117 // Make sure the prefetech link was caught and the page was prerendered. |
| 118 ASSERT_TRUE(prerender_contents != NULL); |
| 119 ASSERT_TRUE(prerender_contents->did_finish_loading()); |
| 120 |
| 121 // Check if page behaves as expected while in prerendered state. |
| 122 bool prerender_test_result; |
| 123 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 124 prerender_contents->render_view_host(), L"", |
| 125 L"window.domAutomationController.send(DidPrerenderPass())", |
| 126 &prerender_test_result)); |
| 127 EXPECT_TRUE(prerender_test_result); |
| 128 |
| 129 ui_test_utils::NavigateToURL(browser(), dest_url); |
| 130 |
| 131 // Make sure the PrerenderContents found earlier was used. |
| 132 EXPECT_TRUE(prerender_manager->FindEntry(dest_url) == NULL); |
| 133 |
| 134 // Check if page behaved as expected when actually displayed. |
| 135 bool display_test_result; |
| 136 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 137 browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 138 L"window.domAutomationController.send(DidDisplayPass())", |
| 139 &display_test_result)); |
| 140 EXPECT_TRUE(display_test_result); |
| 141 } |
| 142 }; |
| 143 |
| 144 // Checks that a page is correctly prerendered in the case of a |
| 145 // <link rel=prefetch> tag and then loaded into a tab in response to a |
| 146 // navigation. |
| 147 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { |
| 148 RunTestURL("prerender_page.html"); |
| 149 } |
OLD | NEW |