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/content_settings/host_content_settings_map.h" | 10 #include "chrome/browser/content_settings/host_content_settings_map.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 std::string CreateClientRedirect(const std::string& dest_url) { | 46 std::string CreateClientRedirect(const std::string& dest_url) { |
47 const char* const kClientRedirectBase = "client-redirect?"; | 47 const char* const kClientRedirectBase = "client-redirect?"; |
48 return kClientRedirectBase + dest_url; | 48 return kClientRedirectBase + dest_url; |
49 } | 49 } |
50 | 50 |
51 std::string CreateServerRedirect(const std::string& dest_url) { | 51 std::string CreateServerRedirect(const std::string& dest_url) { |
52 const char* const kServerRedirectBase = "server-redirect?"; | 52 const char* const kServerRedirectBase = "server-redirect?"; |
53 return kServerRedirectBase + dest_url; | 53 return kServerRedirectBase + dest_url; |
54 } | 54 } |
55 | 55 |
| 56 // Returns true iff the final status is one in which the prerendered |
| 57 // page should prerender correctly. The page still may not be used. |
| 58 bool ShouldRenderPrerenderedPageCorrectly(FinalStatus status) { |
| 59 switch (status) { |
| 60 case FINAL_STATUS_USED: |
| 61 case FINAL_STATUS_WINDOW_OPENER: |
| 62 return true; |
| 63 default: |
| 64 return false; |
| 65 } |
| 66 } |
| 67 |
56 // PrerenderContents that stops the UI message loop on DidStopLoading(). | 68 // PrerenderContents that stops the UI message loop on DidStopLoading(). |
57 class TestPrerenderContents : public PrerenderContents { | 69 class TestPrerenderContents : public PrerenderContents { |
58 public: | 70 public: |
59 TestPrerenderContents( | 71 TestPrerenderContents( |
60 PrerenderManager* prerender_manager, | 72 PrerenderManager* prerender_manager, |
61 Profile* profile, | 73 Profile* profile, |
62 const GURL& url, | 74 const GURL& url, |
63 const GURL& referrer, | 75 const GURL& referrer, |
64 int number_of_loads, | 76 int number_of_loads, |
65 FinalStatus expected_final_status) | 77 FinalStatus expected_final_status) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // on quit. | 118 // on quit. |
107 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING) | 119 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING) |
108 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; | 120 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; |
109 | 121 |
110 PrerenderContents::OnRenderViewGone(status, exit_code); | 122 PrerenderContents::OnRenderViewGone(status, exit_code); |
111 } | 123 } |
112 | 124 |
113 virtual void DidStopLoading() OVERRIDE { | 125 virtual void DidStopLoading() OVERRIDE { |
114 PrerenderContents::DidStopLoading(); | 126 PrerenderContents::DidStopLoading(); |
115 ++number_of_loads_; | 127 ++number_of_loads_; |
116 if (expected_final_status_ == FINAL_STATUS_USED && | 128 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status_) && |
117 number_of_loads_ >= expected_number_of_loads_) { | 129 number_of_loads_ >= expected_number_of_loads_) { |
118 MessageLoopForUI::current()->Quit(); | 130 MessageLoopForUI::current()->Quit(); |
119 } else if (expected_final_status_ == FINAL_STATUS_RENDERER_CRASHED) { | 131 } else if (expected_final_status_ == FINAL_STATUS_RENDERER_CRASHED) { |
120 // Crash the render process. This has to be done here because | 132 // Crash the render process. This has to be done here because |
121 // a prerender navigating to about:crash is cancelled with | 133 // a prerender navigating to about:crash is cancelled with |
122 // "FINAL_STATUS_HTTPS". Even if this were worked around, | 134 // "FINAL_STATUS_HTTPS". Even if this were worked around, |
123 // about:crash can't be navigated to by a normal webpage. | 135 // about:crash can't be navigated to by a normal webpage. |
124 render_view_host_mutable()->NavigateToURL(GURL("about:crash")); | 136 render_view_host_mutable()->NavigateToURL(GURL("about:crash")); |
125 } | 137 } |
126 } | 138 } |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 ASSERT_TRUE(test_server()->Start()); | 285 ASSERT_TRUE(test_server()->Start()); |
274 std::deque<FinalStatus> expected_final_status_queue(1, | 286 std::deque<FinalStatus> expected_final_status_queue(1, |
275 expected_final_status); | 287 expected_final_status); |
276 PrerenderTestURLImpl(url, expected_final_status_queue, total_navigations); | 288 PrerenderTestURLImpl(url, expected_final_status_queue, total_navigations); |
277 } | 289 } |
278 | 290 |
279 void NavigateToDestURL() const { | 291 void NavigateToDestURL() const { |
280 NavigateToURLImpl(dest_url_); | 292 NavigateToURLImpl(dest_url_); |
281 } | 293 } |
282 | 294 |
| 295 void OpenDestUrlInNewWindowViaJs() const { |
| 296 // Make sure in navigating we have a URL to use in the PrerenderManager. |
| 297 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); |
| 298 |
| 299 bool open_window_result = false; |
| 300 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 301 browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 302 L"window.domAutomationController.send(JsOpenLinkInNewWindow())", |
| 303 &open_window_result)); |
| 304 EXPECT_TRUE(open_window_result); |
| 305 } |
| 306 |
| 307 void OpenDestUrlInNewWindowViaClick() const { |
| 308 // Make sure in navigating we have a URL to use in the PrerenderManager. |
| 309 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); |
| 310 |
| 311 bool click_prerendered_link_result = false; |
| 312 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 313 browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 314 L"window.domAutomationController.send(ClickOpenLinkInNewWindow())", |
| 315 &click_prerendered_link_result)); |
| 316 EXPECT_TRUE(click_prerendered_link_result); |
| 317 } |
| 318 |
283 // Should be const but test_server()->GetURL(...) is not const. | 319 // Should be const but test_server()->GetURL(...) is not const. |
284 void NavigateToURL(const std::string& dest_html_file) { | 320 void NavigateToURL(const std::string& dest_html_file) { |
285 GURL dest_url = test_server()->GetURL(dest_html_file); | 321 GURL dest_url = test_server()->GetURL(dest_html_file); |
286 NavigateToURLImpl(dest_url); | 322 NavigateToURLImpl(dest_url); |
287 } | 323 } |
288 | 324 |
289 bool UrlIsInPrerenderManager(const std::string& html_file) { | 325 bool UrlIsInPrerenderManager(const std::string& html_file) { |
290 GURL dest_url = test_server()->GetURL(html_file); | 326 GURL dest_url = test_server()->GetURL(html_file); |
291 return (prerender_manager()->FindEntry(dest_url) != NULL); | 327 return (prerender_manager()->FindEntry(dest_url) != NULL); |
292 } | 328 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 // handle browser navigation directly. | 399 // handle browser navigation directly. |
364 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 400 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); |
365 | 401 |
366 TestPrerenderContents* prerender_contents = NULL; | 402 TestPrerenderContents* prerender_contents = NULL; |
367 ui_test_utils::RunMessageLoop(); | 403 ui_test_utils::RunMessageLoop(); |
368 | 404 |
369 prerender_contents = | 405 prerender_contents = |
370 static_cast<TestPrerenderContents*>( | 406 static_cast<TestPrerenderContents*>( |
371 prerender_manager()->FindEntry(dest_url_)); | 407 prerender_manager()->FindEntry(dest_url_)); |
372 | 408 |
373 switch (expected_final_status) { | 409 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status)) { |
374 case FINAL_STATUS_USED: { | 410 ASSERT_TRUE(prerender_contents != NULL); |
375 ASSERT_TRUE(prerender_contents != NULL); | 411 EXPECT_EQ(FINAL_STATUS_MAX, prerender_contents->final_status()); |
376 | 412 |
377 if (call_javascript_) { | 413 if (call_javascript_) { |
378 // Check if page behaves as expected while in prerendered state. | 414 // Check if page behaves as expected while in prerendered state. |
379 bool prerender_test_result = false; | 415 bool prerender_test_result = false; |
380 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 416 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
381 prerender_contents->render_view_host_mutable(), L"", | 417 prerender_contents->render_view_host_mutable(), L"", |
382 L"window.domAutomationController.send(DidPrerenderPass())", | 418 L"window.domAutomationController.send(DidPrerenderPass())", |
383 &prerender_test_result)); | 419 &prerender_test_result)); |
384 EXPECT_TRUE(prerender_test_result); | 420 EXPECT_TRUE(prerender_test_result); |
385 } | |
386 break; | |
387 } | 421 } |
388 default: | 422 } else { |
389 // In the failure case, we should have removed dest_url_ from the | 423 // In the failure case, we should have removed dest_url_ from the |
390 // prerender_manager. | 424 // prerender_manager. |
391 EXPECT_TRUE(prerender_contents == NULL); | 425 EXPECT_TRUE(prerender_contents == NULL); |
392 break; | |
393 } | 426 } |
394 } | 427 } |
395 | 428 |
396 void NavigateToURLImpl(const GURL& dest_url) const { | 429 void NavigateToURLImpl(const GURL& dest_url) const { |
397 // Make sure in navigating we have a URL to use in the PrerenderManager. | 430 // Make sure in navigating we have a URL to use in the PrerenderManager. |
398 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | 431 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); |
399 | 432 |
400 // ui_test_utils::NavigateToURL waits until DidStopLoading is called on | 433 // ui_test_utils::NavigateToURL waits until DidStopLoading is called on |
401 // the current tab. As that tab is going to end up deleted, and may never | 434 // the current tab. As that tab is going to end up deleted, and may never |
402 // finish loading before that happens, exit the message loop on the deletion | 435 // finish loading before that happens, exit the message loop on the deletion |
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1078 NavigateToDestURL(); | 1111 NavigateToDestURL(); |
1079 } | 1112 } |
1080 | 1113 |
1081 // Checks that we cancel correctly when window.print() is called. | 1114 // Checks that we cancel correctly when window.print() is called. |
1082 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPrint) { | 1115 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPrint) { |
1083 PrerenderTestURL("files/prerender/prerender_print.html", | 1116 PrerenderTestURL("files/prerender/prerender_print.html", |
1084 FINAL_STATUS_WINDOW_PRINT, | 1117 FINAL_STATUS_WINDOW_PRINT, |
1085 1); | 1118 1); |
1086 } | 1119 } |
1087 | 1120 |
| 1121 // The following test has been commented out because it exposes a bug in the |
| 1122 // way ui_test_utils::ExecuteJavaScriptAndExtractBool method works which can |
| 1123 // cause this test to fail sometimes. |
| 1124 // |
| 1125 // Checks that if a page is opened in a new window by javascript the |
| 1126 // prerendered page is not used. |
| 1127 // IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, |
| 1128 // PrerenderWindowOpenerJsOpenInNewPageTest) { |
| 1129 // PrerenderTestURL("files/prerender/prerender_page.html", |
| 1130 // FINAL_STATUS_WINDOW_OPENER, |
| 1131 // 1); |
| 1132 // OpenDestUrlInNewWindowViaJs(); |
| 1133 // } |
| 1134 |
| 1135 // Checks that if a page is opened due to click on a href with target="_blank" |
| 1136 // the prerendered page is not used. |
| 1137 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, |
| 1138 PrerenderWindowOpenerClickOpenInNewPageTest) { |
| 1139 PrerenderTestURL("files/prerender/prerender_page.html", |
| 1140 FINAL_STATUS_WINDOW_OPENER, |
| 1141 1); |
| 1142 OpenDestUrlInNewWindowViaClick(); |
| 1143 } |
| 1144 |
| 1145 // TODO(shishir): Add a test for the case when the page having the |
| 1146 // prerendering link already has an opener set. |
| 1147 |
1088 } // namespace prerender | 1148 } // namespace prerender |
OLD | NEW |