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) { | |
sreeram
2011/05/10 15:58:20
Naming suggestion: Perhaps call this DidPrerenderS
| |
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 { | |
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); | |
307 | 326 |
308 if (call_javascript_) { | 327 if (call_javascript_) { |
309 // Check if page behaves as expected while in prerendered state. | 328 // Check if page behaves as expected while in prerendered state. |
310 bool prerender_test_result = false; | 329 bool prerender_test_result = false; |
311 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 330 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
312 prerender_contents->render_view_host_mutable(), L"", | 331 prerender_contents->render_view_host_mutable(), L"", |
313 L"window.domAutomationController.send(DidPrerenderPass())", | 332 L"window.domAutomationController.send(DidPrerenderPass())", |
314 &prerender_test_result)); | 333 &prerender_test_result)); |
315 EXPECT_TRUE(prerender_test_result); | 334 EXPECT_TRUE(prerender_test_result); |
316 } | |
317 break; | |
318 } | 335 } |
319 default: | 336 } else { |
320 // In the failure case, we should have removed dest_url_ from the | 337 // In the failure case, we should have removed dest_url_ from the |
321 // prerender_manager. | 338 // prerender_manager. |
322 EXPECT_TRUE(prerender_contents == NULL); | 339 EXPECT_TRUE(prerender_contents == NULL); |
323 break; | |
324 } | 340 } |
325 } | 341 } |
326 | 342 |
343 void OpenDestUrlInNewWindowViaClickImpl() const { | |
344 // Make sure in navigating we have a URL to use in the PrerenderManager. | |
345 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | |
346 | |
347 bool click_prerendered_link_result = false; | |
348 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
349 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
350 L"window.domAutomationController.send(ClickOpenPrerenderedLinkInNewWindo w())", | |
351 &click_prerendered_link_result)); | |
352 EXPECT_TRUE(click_prerendered_link_result); | |
353 } | |
354 | |
355 void OpenDestUrlInNewWindowViaJsImpl() const { | |
356 // Make sure in navigating we have a URL to use in the PrerenderManager. | |
357 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | |
358 | |
359 bool open_window_result = false; | |
360 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
361 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
362 L"window.domAutomationController.send(JsOpenPrerenderedLinkInNewWindow() )", | |
363 &open_window_result)); | |
364 EXPECT_TRUE(open_window_result); | |
365 } | |
366 | |
327 void NavigateToURLImpl(const GURL& dest_url) const { | 367 void NavigateToURLImpl(const GURL& dest_url) const { |
328 // Make sure in navigating we have a URL to use in the PrerenderManager. | 368 // Make sure in navigating we have a URL to use in the PrerenderManager. |
329 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | 369 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); |
330 | 370 |
331 // ui_test_utils::NavigateToURL waits until DidStopLoading is called on | 371 // 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 | 372 // 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 | 373 // finish loading before that happens, exit the message loop on the deletion |
334 // of the used prerender contents instead. | 374 // of the used prerender contents instead. |
335 // | 375 // |
336 // As PrerenderTestURL waits until the prerendered page has completely | 376 // As PrerenderTestURL waits until the prerendered page has completely |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
940 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec())); | 980 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec())); |
941 std::string replacement_path; | 981 std::string replacement_path; |
942 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( | 982 ASSERT_TRUE(net::TestServer::GetFilePathWithReplacements( |
943 "files/prerender/prerender_with_image.html", | 983 "files/prerender/prerender_with_image.html", |
944 replacement_text, | 984 replacement_text, |
945 &replacement_path)); | 985 &replacement_path)); |
946 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1); | 986 PrerenderTestURL(replacement_path, FINAL_STATUS_USED, 1); |
947 NavigateToDestURL(); | 987 NavigateToDestURL(); |
948 } | 988 } |
949 | 989 |
990 // Checks that if a page is opened in a new window by javascript the | |
991 // prerendered page is not used. | |
992 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, | |
993 PrerenderWindowOpenerJsOpenInNewPageTest) { | |
994 PrerenderTestURL("files/prerender/prerender_page.html", | |
995 FINAL_STATUS_WINDOW_OPENER, | |
996 1); | |
997 OpenDestUrlInNewWindowViaJs(); | |
sreeram
2011/05/10 15:58:20
Seems to me that you should do this:
queue.pus
sreeram
2011/05/10 17:01:16
If the queue thing doesn't work (because the final
Shishir
2011/05/11 22:20:06
Added a check in PrerenderTestURLImpl
On 2011/05/
| |
998 } | |
999 | |
1000 // Checks that if a page is opened due to click on a href with target="_blank" | |
1001 // the prerendered page is not used. | |
1002 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, | |
1003 PrerenderWindowOpenerClickOpenInNewPageTest) { | |
1004 PrerenderTestURL("files/prerender/prerender_page.html", | |
1005 FINAL_STATUS_WINDOW_OPENER, | |
1006 1); | |
1007 OpenDestUrlInNewWindowViaClick(); | |
1008 } | |
1009 | |
1010 // TODO(shishir): Add a test for the case when the page having the | |
1011 // prerendering link already has an opener set. | |
1012 | |
950 } // namespace prerender | 1013 } // namespace prerender |
OLD | NEW |