Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <deque> | 5 #include <deque> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/prerender/prerender_contents.h" | 10 #include "chrome/browser/prerender/prerender_contents.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 std::string CreateClientRedirect(const std::string& dest_url) { | 42 std::string CreateClientRedirect(const std::string& dest_url) { |
| 43 const char* const kClientRedirectBase = "client-redirect?"; | 43 const char* const kClientRedirectBase = "client-redirect?"; |
| 44 return kClientRedirectBase + dest_url; | 44 return kClientRedirectBase + dest_url; |
| 45 } | 45 } |
| 46 | 46 |
| 47 std::string CreateServerRedirect(const std::string& dest_url) { | 47 std::string CreateServerRedirect(const std::string& dest_url) { |
| 48 const char* const kServerRedirectBase = "server-redirect?"; | 48 const char* const kServerRedirectBase = "server-redirect?"; |
| 49 return kServerRedirectBase + dest_url; | 49 return kServerRedirectBase + dest_url; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Returns true iff the final status is one in which the prerendered | |
| 53 // page should prerender correctly. The page still may not be used. | |
| 54 bool ShouldRenderPrerenderedPageCorrectly(FinalStatus status) { | |
|
cbentzel
2011/05/12 11:17:08
Is this needed anymore? The prerendered page is de
Shishir
2011/05/12 22:05:18
This is still needed as in the opener case, the pr
| |
| 55 switch (status) { | |
| 56 case FINAL_STATUS_USED: | |
| 57 case FINAL_STATUS_WINDOW_OPENER: | |
| 58 return true; | |
| 59 default: | |
| 60 return false; | |
| 61 } | |
| 62 } | |
| 63 | |
| 52 // PrerenderContents that stops the UI message loop on DidStopLoading(). | 64 // PrerenderContents that stops the UI message loop on DidStopLoading(). |
| 53 class TestPrerenderContents : public PrerenderContents { | 65 class TestPrerenderContents : public PrerenderContents { |
| 54 public: | 66 public: |
| 55 TestPrerenderContents( | 67 TestPrerenderContents( |
| 56 PrerenderManager* prerender_manager, | 68 PrerenderManager* prerender_manager, |
| 57 Profile* profile, | 69 Profile* profile, |
| 58 const GURL& url, | 70 const GURL& url, |
| 59 const GURL& referrer, | 71 const GURL& referrer, |
| 60 int number_of_loads, | 72 int number_of_loads, |
| 61 FinalStatus expected_final_status) | 73 FinalStatus expected_final_status) |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 82 // on quit. | 94 // on quit. |
| 83 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING) | 95 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING) |
| 84 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; | 96 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; |
| 85 | 97 |
| 86 PrerenderContents::OnRenderViewGone(status, exit_code); | 98 PrerenderContents::OnRenderViewGone(status, exit_code); |
| 87 } | 99 } |
| 88 | 100 |
| 89 virtual void DidStopLoading() OVERRIDE { | 101 virtual void DidStopLoading() OVERRIDE { |
| 90 PrerenderContents::DidStopLoading(); | 102 PrerenderContents::DidStopLoading(); |
| 91 ++number_of_loads_; | 103 ++number_of_loads_; |
| 92 if (expected_final_status_ == FINAL_STATUS_USED && | 104 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status_) && |
| 93 number_of_loads_ >= expected_number_of_loads_) { | 105 number_of_loads_ >= expected_number_of_loads_) { |
| 94 MessageLoopForUI::current()->Quit(); | 106 MessageLoopForUI::current()->Quit(); |
| 95 } else if (expected_final_status_ == FINAL_STATUS_RENDERER_CRASHED) { | 107 } else if (expected_final_status_ == FINAL_STATUS_RENDERER_CRASHED) { |
| 96 // Crash the render process. This has to be done here because | 108 // Crash the render process. This has to be done here because |
| 97 // a prerender navigating to about:crash is cancelled with | 109 // a prerender navigating to about:crash is cancelled with |
| 98 // "FINAL_STATUS_HTTPS". Even if this were worked around, | 110 // "FINAL_STATUS_HTTPS". Even if this were worked around, |
| 99 // about:crash can't be navigated to by a normal webpage. | 111 // about:crash can't be navigated to by a normal webpage. |
| 100 render_view_host_mutable()->NavigateToURL(GURL("about:crash")); | 112 render_view_host_mutable()->NavigateToURL(GURL("about:crash")); |
| 101 } | 113 } |
| 102 } | 114 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 ASSERT_TRUE(test_server()->Start()); | 216 ASSERT_TRUE(test_server()->Start()); |
| 205 std::deque<FinalStatus> expected_final_status_queue(1, | 217 std::deque<FinalStatus> expected_final_status_queue(1, |
| 206 expected_final_status); | 218 expected_final_status); |
| 207 PrerenderTestURLImpl(url, expected_final_status_queue, total_navigations); | 219 PrerenderTestURLImpl(url, expected_final_status_queue, total_navigations); |
| 208 } | 220 } |
| 209 | 221 |
| 210 void NavigateToDestURL() const { | 222 void NavigateToDestURL() const { |
| 211 NavigateToURLImpl(dest_url_); | 223 NavigateToURLImpl(dest_url_); |
| 212 } | 224 } |
| 213 | 225 |
| 226 void OpenDestUrlInNewWindowViaJs() const { | |
|
cbentzel
2011/05/12 11:17:08
In these two cases, you don't need to do the inter
Shishir
2011/05/12 22:05:18
Done.
| |
| 227 OpenDestUrlInNewWindowViaJsImpl(); | |
| 228 } | |
| 229 | |
| 230 void OpenDestUrlInNewWindowViaClick() const { | |
| 231 OpenDestUrlInNewWindowViaClickImpl(); | |
| 232 } | |
| 233 | |
| 214 // Should be const but test_server()->GetURL(...) is not const. | 234 // Should be const but test_server()->GetURL(...) is not const. |
| 215 void NavigateToURL(const std::string& dest_html_file) { | 235 void NavigateToURL(const std::string& dest_html_file) { |
| 216 GURL dest_url = test_server()->GetURL(dest_html_file); | 236 GURL dest_url = test_server()->GetURL(dest_html_file); |
| 217 NavigateToURLImpl(dest_url); | 237 NavigateToURLImpl(dest_url); |
| 218 } | 238 } |
| 219 | 239 |
| 220 bool UrlIsInPrerenderManager(const std::string& html_file) { | 240 bool UrlIsInPrerenderManager(const std::string& html_file) { |
| 221 GURL dest_url = test_server()->GetURL(html_file); | 241 GURL dest_url = test_server()->GetURL(html_file); |
| 222 return (prerender_manager()->FindEntry(dest_url) != NULL); | 242 return (prerender_manager()->FindEntry(dest_url) != NULL); |
| 223 } | 243 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 // handle browser navigation directly. | 314 // handle browser navigation directly. |
| 295 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 315 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); |
| 296 | 316 |
| 297 TestPrerenderContents* prerender_contents = NULL; | 317 TestPrerenderContents* prerender_contents = NULL; |
| 298 ui_test_utils::RunMessageLoop(); | 318 ui_test_utils::RunMessageLoop(); |
| 299 | 319 |
| 300 prerender_contents = | 320 prerender_contents = |
| 301 static_cast<TestPrerenderContents*>( | 321 static_cast<TestPrerenderContents*>( |
| 302 prerender_manager()->FindEntry(dest_url_)); | 322 prerender_manager()->FindEntry(dest_url_)); |
| 303 | 323 |
| 304 switch (expected_final_status) { | 324 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status)) { |
| 305 case FINAL_STATUS_USED: { | 325 ASSERT_TRUE(prerender_contents != NULL); |
| 306 ASSERT_TRUE(prerender_contents != NULL); | 326 EXPECT_EQ(prerender_contents->final_status(), FINAL_STATUS_MAX); |
|
sreeram
2011/05/12 02:36:41
Swap the arguments. In EXPECT_EQ, the first argume
Shishir
2011/05/12 22:05:18
Done.
| |
| 307 | 327 |
| 308 if (call_javascript_) { | 328 if (call_javascript_) { |
| 309 // Check if page behaves as expected while in prerendered state. | 329 // Check if page behaves as expected while in prerendered state. |
| 310 bool prerender_test_result = false; | 330 bool prerender_test_result = false; |
| 311 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 331 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 312 prerender_contents->render_view_host_mutable(), L"", | 332 prerender_contents->render_view_host_mutable(), L"", |
| 313 L"window.domAutomationController.send(DidPrerenderPass())", | 333 L"window.domAutomationController.send(DidPrerenderPass())", |
| 314 &prerender_test_result)); | 334 &prerender_test_result)); |
| 315 EXPECT_TRUE(prerender_test_result); | 335 EXPECT_TRUE(prerender_test_result); |
| 316 } | |
| 317 break; | |
| 318 } | 336 } |
| 319 default: | 337 } else { |
| 320 // In the failure case, we should have removed dest_url_ from the | 338 // In the failure case, we should have removed dest_url_ from the |
| 321 // prerender_manager. | 339 // prerender_manager. |
| 322 EXPECT_TRUE(prerender_contents == NULL); | 340 EXPECT_TRUE(prerender_contents == NULL); |
| 323 break; | |
| 324 } | 341 } |
| 325 } | 342 } |
| 326 | 343 |
| 344 void OpenDestUrlInNewWindowViaClickImpl() const { | |
| 345 // Make sure in navigating we have a URL to use in the PrerenderManager. | |
| 346 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | |
| 347 | |
| 348 bool click_prerendered_link_result = false; | |
| 349 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
| 350 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
| 351 L"window.domAutomationController.send(ClickOpenPrerenderedLinkInNewWindo w())", | |
|
cbentzel
2011/05/12 11:17:08
Nit: 80 char-line. However, this may be more reada
Shishir
2011/05/12 22:05:18
Done.
| |
| 352 &click_prerendered_link_result)); | |
| 353 EXPECT_TRUE(click_prerendered_link_result); | |
| 354 } | |
| 355 | |
| 356 void OpenDestUrlInNewWindowViaJsImpl() const { | |
| 357 // Make sure in navigating we have a URL to use in the PrerenderManager. | |
| 358 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | |
| 359 | |
| 360 bool open_window_result = false; | |
| 361 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
| 362 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
| 363 L"window.domAutomationController.send(JsOpenPrerenderedLinkInNewWindow() )", | |
| 364 &open_window_result)); | |
| 365 EXPECT_TRUE(open_window_result); | |
| 366 } | |
| 367 | |
| 327 void NavigateToURLImpl(const GURL& dest_url) const { | 368 void NavigateToURLImpl(const GURL& dest_url) const { |
| 328 // Make sure in navigating we have a URL to use in the PrerenderManager. | 369 // Make sure in navigating we have a URL to use in the PrerenderManager. |
| 329 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | 370 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); |
| 330 | 371 |
| 331 // ui_test_utils::NavigateToURL waits until DidStopLoading is called on | 372 // ui_test_utils::NavigateToURL waits until DidStopLoading is called on |
| 332 // the current tab. As that tab is going to end up deleted, and may never | 373 // the current tab. As that tab is going to end up deleted, and may never |
| 333 // finish loading before that happens, exit the message loop on the deletion | 374 // finish loading before that happens, exit the message loop on the deletion |
| 334 // of the used prerender contents instead. | 375 // of the used prerender contents instead. |
| 335 // | 376 // |
| 336 // As PrerenderTestURL waits until the prerendered page has completely | 377 // As PrerenderTestURL waits until the prerendered page has completely |
| (...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 946 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec())); | 987 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec())); |
| 947 std::string replacement_path; | 988 std::string replacement_path; |
| 948 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( | 989 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( |
| 949 "files/prerender/prerender_with_image.html", | 990 "files/prerender/prerender_with_image.html", |
| 950 replacement_text, | 991 replacement_text, |
| 951 &replacement_path)); | 992 &replacement_path)); |
| 952 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1); | 993 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1); |
| 953 NavigateToDestURL(); | 994 NavigateToDestURL(); |
| 954 } | 995 } |
| 955 | 996 |
| 997 // Checks that if a page is opened in a new window by javascript the | |
| 998 // prerendered page is not used. | |
| 999 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, | |
| 1000 PrerenderWindowOpenerJsOpenInNewPageTest) { | |
| 1001 PrerenderTestURL("files/prerender/prerender_page.html", | |
| 1002 FINAL_STATUS_WINDOW_OPENER, | |
| 1003 1); | |
| 1004 OpenDestUrlInNewWindowViaJs(); | |
| 1005 } | |
| 1006 | |
| 1007 // Checks that if a page is opened due to click on a href with target="_blank" | |
| 1008 // the prerendered page is not used. | |
| 1009 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, | |
| 1010 PrerenderWindowOpenerClickOpenInNewPageTest) { | |
| 1011 PrerenderTestURL("files/prerender/prerender_page.html", | |
| 1012 FINAL_STATUS_WINDOW_OPENER, | |
| 1013 1); | |
| 1014 OpenDestUrlInNewWindowViaClick(); | |
| 1015 } | |
| 1016 | |
| 1017 // TODO(shishir): Add a test for the case when the page having the | |
| 1018 // prerendering link already has an opener set. | |
| 1019 | |
| 956 } // namespace prerender | 1020 } // namespace prerender |
| OLD | NEW |