Chromium Code Reviews
|
| 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: | |
|
cbentzel
2011/01/21 18:37:02
I haven't really seen /* style commenting in Chrom
mmenke
2011/01/21 20:12:20
You're right. Hadn't really thought about it. Ch
| |
| 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 | |
| 30 namespace { | |
| 31 | |
| 32 // PrerenderContents that stops the UI message loop on DidStopLoading(). | |
| 33 class TestPrerenderContents : public PrerenderContents { | |
| 34 public: | |
| 35 TestPrerenderContents( | |
| 36 PrerenderManager* prerender_manager, Profile* profile, const GURL& url, | |
| 37 const std::vector<GURL>& alias_urls) | |
| 38 : PrerenderContents(prerender_manager, profile, url, alias_urls), | |
| 39 did_finish_loading_(false) { | |
| 40 } | |
| 41 | |
| 42 virtual void DidStopLoading() { | |
| 43 PrerenderContents::DidStopLoading(); | |
| 44 did_finish_loading_ = true; | |
| 45 MessageLoopForUI::current()->Quit(); | |
| 46 } | |
| 47 | |
| 48 bool did_finish_loading() { return did_finish_loading_; } | |
|
cbentzel
2011/01/21 18:37:02
Nit: const
mmenke
2011/01/21 20:12:20
Done.
| |
| 49 | |
| 50 private: | |
| 51 bool did_finish_loading_; | |
| 52 }; | |
| 53 | |
| 54 // PrerenderManager that uses TestPrerenderContents. | |
| 55 class WaitForLoadPrerenderContentsFactory : public PrerenderContents::Factory { | |
| 56 public: | |
| 57 virtual PrerenderContents* CreatePrerenderContents( | |
| 58 PrerenderManager* prerender_manager, Profile* profile, const GURL& url, | |
| 59 const std::vector<GURL>& alias_urls) { | |
|
cbentzel
2011/01/21 18:37:02
Nit: line up with PrerenderManager.
mmenke
2011/01/21 20:12:20
Done
| |
| 60 return new TestPrerenderContents(prerender_manager, profile, url, | |
| 61 alias_urls); | |
| 62 } | |
| 63 }; | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 class PrerenderBrowserTest : public InProcessBrowserTest { | |
| 68 public: | |
| 69 PrerenderBrowserTest() { | |
| 70 EnableDOMAutomation(); | |
| 71 } | |
| 72 | |
| 73 virtual void SetUpCommandLine(CommandLine* command_line) { | |
| 74 command_line->AppendSwitch(switches::kEnablePagePrerender); | |
| 75 #if defined(OS_MACOSX) | |
| 76 // The plugins directory isn't read by default on the Mac, so it needs to be | |
| 77 // explicitly registered. | |
| 78 FilePath app_dir; | |
| 79 PathService::Get(chrome::DIR_APP, &app_dir); | |
| 80 command_line->AppendSwitchPath( | |
| 81 switches::kExtraPluginDir, | |
| 82 app_dir.Append(FILE_PATH_LITERAL("plugins"))); | |
| 83 #endif | |
| 84 } | |
| 85 | |
| 86 void RunTestURL(const std::string& html_file) { | |
| 87 ASSERT_TRUE(test_server()->Start()); | |
| 88 | |
| 89 std::string src_path = "files/prerender/prerender_loader.html?"; | |
| 90 src_path.append(html_file); | |
| 91 std::string dest_path = "files/prerender/"; | |
| 92 dest_path.append(html_file); | |
| 93 | |
| 94 GURL src_url = test_server()->GetURL(src_path); | |
| 95 GURL dest_url = test_server()->GetURL(dest_path); | |
| 96 | |
| 97 Profile* profile = browser()->GetSelectedTabContents()->profile(); | |
| 98 PrerenderManager* prerender_manager = profile->GetPrerenderManager(); | |
| 99 ASSERT_TRUE(prerender_manager); | |
| 100 | |
| 101 // This is needed to exit the event loop once the prerendered page has | |
| 102 // stopped loading. | |
| 103 prerender_manager->SetPrerenderContentsFactory( | |
| 104 new WaitForLoadPrerenderContentsFactory()); | |
| 105 | |
| 106 // ui_test_utils::NavigateToURL uses its own observer and message loop. | |
| 107 // Since the test needs to wait until the prerendered page has stopped | |
| 108 // loading, rathather than the page directly navigated to, need to | |
| 109 // handle browser navigation directly. | |
| 110 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); | |
| 111 | |
| 112 ui_test_utils::RunMessageLoop(); | |
|
cbentzel
2011/01/21 18:37:02
So, if we break prerendering this will fail with a
mmenke
2011/01/21 20:12:20
Yes. I've thought about trying to get something t
| |
| 113 | |
| 114 TestPrerenderContents* prerender_contents = | |
| 115 static_cast<TestPrerenderContents*>( | |
| 116 prerender_manager->FindEntry(dest_url)); | |
| 117 | |
| 118 // Make sure the prefetech link was caught and the page was prerendered. | |
| 119 ASSERT_TRUE(prerender_contents != NULL); | |
| 120 ASSERT_TRUE(prerender_contents->did_finish_loading()); | |
| 121 | |
| 122 // Check if page behaves as expected while in prerendered state. | |
| 123 bool prerender_test_result; | |
| 124 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
| 125 prerender_contents->render_view_host(), L"", | |
| 126 L"window.domAutomationController.send(DidPrerenderPass())", | |
| 127 &prerender_test_result)); | |
| 128 EXPECT_TRUE(prerender_test_result); | |
| 129 | |
| 130 ui_test_utils::NavigateToURL(browser(), dest_url); | |
| 131 | |
| 132 // Make sure the PrerenderContents found earlier was used. | |
| 133 EXPECT_TRUE(prerender_manager->FindEntry(dest_url) == NULL); | |
| 134 | |
| 135 // Check if page behaved as expected when actually displayed. | |
| 136 bool display_test_result; | |
| 137 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
| 138 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
| 139 L"window.domAutomationController.send(DidDisplayPass())", | |
| 140 &display_test_result)); | |
| 141 EXPECT_TRUE(display_test_result); | |
| 142 } | |
| 143 }; | |
| 144 | |
| 145 // Checks that a page is correctly prerendered in the case of a | |
| 146 // <link rel=prefetch> tag and then loaded into a tab in response to a | |
| 147 // navigation. | |
| 148 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { | |
| 149 RunTestURL("prerender_page.html"); | |
| 150 } | |
| 151 | |
| 152 // Checks that plugins are not loaded while a page is being preloaded, but | |
| 153 // are loaded when the page is displayed. | |
| 154 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderDelayLoadPlugin) { | |
| 155 RunTestURL("plugin_delay_load.html"); | |
| 156 } | |
| OLD | NEW |