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 pre prerendered correctly. The page still may not be used. | |
cbentzel
2011/05/04 17:10:15
Nit: prerender correctly rather than pre prerender
Shishir
2011/05/05 23:09:54
Done.
| |
54 bool ShouldRenderPrerenderedPageCorrectly(FinalStatus status) { | |
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 // on quit. | 106 // on quit. |
95 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING) | 107 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING) |
96 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; | 108 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; |
97 | 109 |
98 PrerenderContents::RenderViewGone(); | 110 PrerenderContents::RenderViewGone(); |
99 } | 111 } |
100 | 112 |
101 virtual void DidStopLoading() OVERRIDE { | 113 virtual void DidStopLoading() OVERRIDE { |
102 PrerenderContents::DidStopLoading(); | 114 PrerenderContents::DidStopLoading(); |
103 ++number_of_loads_; | 115 ++number_of_loads_; |
104 if (expected_final_status_ == FINAL_STATUS_USED && | 116 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status_) && |
105 number_of_loads_ >= expected_number_of_loads_) { | 117 number_of_loads_ >= expected_number_of_loads_) { |
106 MessageLoopForUI::current()->Quit(); | 118 MessageLoopForUI::current()->Quit(); |
107 } else if (expected_final_status_ == FINAL_STATUS_RENDERER_CRASHED) { | 119 } else if (expected_final_status_ == FINAL_STATUS_RENDERER_CRASHED) { |
108 // Crash the render process. This has to be done here because | 120 // Crash the render process. This has to be done here because |
109 // a prerender navigating to about:crash is cancelled with | 121 // a prerender navigating to about:crash is cancelled with |
110 // "FINAL_STATUS_HTTPS". Even if this were worked around, | 122 // "FINAL_STATUS_HTTPS". Even if this were worked around, |
111 // about:crash can't be navigated to by a normal webpage. | 123 // about:crash can't be navigated to by a normal webpage. |
112 render_view_host()->NavigateToURL(GURL("about:crash")); | 124 render_view_host()->NavigateToURL(GURL("about:crash")); |
113 } | 125 } |
114 } | 126 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 ASSERT_TRUE(test_server()->Start()); | 222 ASSERT_TRUE(test_server()->Start()); |
211 std::deque<FinalStatus> expected_final_status_queue(1, | 223 std::deque<FinalStatus> expected_final_status_queue(1, |
212 expected_final_status); | 224 expected_final_status); |
213 PrerenderTestURLImpl(url, expected_final_status_queue, total_navigations); | 225 PrerenderTestURLImpl(url, expected_final_status_queue, total_navigations); |
214 } | 226 } |
215 | 227 |
216 void NavigateToDestURL() const { | 228 void NavigateToDestURL() const { |
217 NavigateToURLImpl(dest_url_); | 229 NavigateToURLImpl(dest_url_); |
218 } | 230 } |
219 | 231 |
232 void OpenDestUrlInNewWindowViaJs() const { | |
233 OpenDestUrlInNewWindowViaJsImpl(); | |
234 } | |
235 | |
236 void OpenDestUrlInNewWindowViaClick() const { | |
237 OpenDestUrlInNewWindowViaClickImpl(); | |
238 } | |
239 | |
220 // Should be const but test_server()->GetURL(...) is not const. | 240 // Should be const but test_server()->GetURL(...) is not const. |
221 void NavigateToURL(const std::string& dest_html_file) { | 241 void NavigateToURL(const std::string& dest_html_file) { |
222 GURL dest_url = test_server()->GetURL(dest_html_file); | 242 GURL dest_url = test_server()->GetURL(dest_html_file); |
223 NavigateToURLImpl(dest_url); | 243 NavigateToURLImpl(dest_url); |
224 } | 244 } |
225 | 245 |
226 bool UrlIsInPrerenderManager(const std::string& html_file) { | 246 bool UrlIsInPrerenderManager(const std::string& html_file) { |
227 GURL dest_url = test_server()->GetURL(html_file); | 247 GURL dest_url = test_server()->GetURL(html_file); |
228 return (prerender_manager()->FindEntry(dest_url) != NULL); | 248 return (prerender_manager()->FindEntry(dest_url) != NULL); |
229 } | 249 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 // handle browser navigation directly. | 320 // handle browser navigation directly. |
301 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 321 browser()->OpenURL(src_url, GURL(), CURRENT_TAB, PageTransition::TYPED); |
302 | 322 |
303 TestPrerenderContents* prerender_contents = NULL; | 323 TestPrerenderContents* prerender_contents = NULL; |
304 ui_test_utils::RunMessageLoop(); | 324 ui_test_utils::RunMessageLoop(); |
305 | 325 |
306 prerender_contents = | 326 prerender_contents = |
307 static_cast<TestPrerenderContents*>( | 327 static_cast<TestPrerenderContents*>( |
308 prerender_manager()->FindEntry(dest_url_)); | 328 prerender_manager()->FindEntry(dest_url_)); |
309 | 329 |
310 switch (expected_final_status) { | 330 if (ShouldRenderPrerenderedPageCorrectly(expected_final_status)) { |
311 case FINAL_STATUS_USED: { | 331 ASSERT_TRUE(prerender_contents != NULL); |
312 ASSERT_TRUE(prerender_contents != NULL); | |
313 | 332 |
314 if (call_javascript_) { | 333 if (call_javascript_) { |
315 // Check if page behaves as expected while in prerendered state. | 334 // Check if page behaves as expected while in prerendered state. |
316 bool prerender_test_result = false; | 335 bool prerender_test_result = false; |
317 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 336 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
318 prerender_contents->render_view_host(), L"", | 337 prerender_contents->render_view_host(), L"", |
319 L"window.domAutomationController.send(DidPrerenderPass())", | 338 L"window.domAutomationController.send(DidPrerenderPass())", |
320 &prerender_test_result)); | 339 &prerender_test_result)); |
321 EXPECT_TRUE(prerender_test_result); | 340 EXPECT_TRUE(prerender_test_result); |
322 } | |
323 break; | |
324 } | 341 } |
325 default: | 342 } else { |
326 // In the failure case, we should have removed dest_url_ from the | 343 // In the failure case, we should have removed dest_url_ from the |
327 // prerender_manager. | 344 // prerender_manager. |
328 EXPECT_TRUE(prerender_contents == NULL); | 345 EXPECT_TRUE(prerender_contents == NULL); |
329 break; | |
330 } | 346 } |
331 } | 347 } |
332 | 348 |
349 void OpenDestUrlInNewWindowViaClickImpl() const { | |
350 // Make sure in navigating we have a URL to use in the PrerenderManager. | |
351 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | |
352 | |
353 bool click_prerendered_link_result = false; | |
354 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
355 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
356 L"window.domAutomationController.send(ClickOpenPrerenderedLinkInNewWindo w())", | |
357 &click_prerendered_link_result)); | |
358 EXPECT_TRUE(click_prerendered_link_result); | |
359 } | |
360 | |
361 void OpenDestUrlInNewWindowViaJsImpl() const { | |
362 // Make sure in navigating we have a URL to use in the PrerenderManager. | |
363 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | |
364 | |
365 bool open_window_result = false; | |
366 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | |
367 browser()->GetSelectedTabContents()->render_view_host(), L"", | |
368 L"window.domAutomationController.send(JsOpenPrerenderedLinkInNewWindow() )", | |
369 &open_window_result)); | |
370 EXPECT_TRUE(open_window_result); | |
371 } | |
372 | |
333 void NavigateToURLImpl(const GURL& dest_url) const { | 373 void NavigateToURLImpl(const GURL& dest_url) const { |
334 // Make sure in navigating we have a URL to use in the PrerenderManager. | 374 // Make sure in navigating we have a URL to use in the PrerenderManager. |
335 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); | 375 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) != NULL); |
336 | 376 |
337 ui_test_utils::NavigateToURL(browser(), dest_url); | 377 ui_test_utils::NavigateToURL(browser(), dest_url); |
338 | 378 |
339 // Make sure the PrerenderContents found earlier was used or removed. | 379 // Make sure the PrerenderContents found earlier was used or removed. |
340 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) == NULL); | 380 EXPECT_TRUE(prerender_manager()->FindEntry(dest_url_) == NULL); |
341 | 381 |
342 if (call_javascript_) { | 382 if (call_javascript_) { |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
879 1); | 919 1); |
880 } | 920 } |
881 | 921 |
882 // Checks that xhr DELETE cancels prerenders. | 922 // Checks that xhr DELETE cancels prerenders. |
883 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrDelete) { | 923 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderXhrDelete) { |
884 PrerenderTestURL("files/prerender/prerender_xhr_delete.html", | 924 PrerenderTestURL("files/prerender/prerender_xhr_delete.html", |
885 FINAL_STATUS_INVALID_HTTP_METHOD, | 925 FINAL_STATUS_INVALID_HTTP_METHOD, |
886 1); | 926 1); |
887 } | 927 } |
888 | 928 |
929 // Checks that if a page is opened in a new window by javascript the | |
930 // prerendered page is not used. | |
931 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, | |
932 PrerenderWindowOpenerJsOpenInNewPageTest) { | |
933 PrerenderTestURL("files/prerender/prerender_page.html", | |
934 FINAL_STATUS_WINDOW_OPENER, | |
935 1); | |
936 OpenDestUrlInNewWindowViaJs(); | |
cbentzel
2011/05/04 17:10:15
Should these also do a normal NavigateToURL and ma
Shishir
2011/05/05 23:09:54
As discussed we dont use the prerendered page.
| |
937 } | |
938 | |
939 // Checks that if a page is opened due to click on a href with target="_blank" | |
940 // the prerendered page is not used. | |
941 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, | |
942 PrerenderWindowOpenerClickOpenInNewPageTest) { | |
943 PrerenderTestURL("files/prerender/prerender_page.html", | |
944 FINAL_STATUS_WINDOW_OPENER, | |
945 1); | |
946 OpenDestUrlInNewWindowViaClick(); | |
947 } | |
948 | |
949 // TODO(shishir): Add a test for the case when the page having the | |
950 // prerendering link already has an opener set. | |
951 | |
889 } // namespace prerender | 952 } // namespace prerender |
OLD | NEW |