| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 10 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 150 |
| 151 // Use this class to be able to leak the WebContents, which is needed for when | 151 // Use this class to be able to leak the WebContents, which is needed for when |
| 152 // the current WebContents is used for distillation. | 152 // the current WebContents is used for distillation. |
| 153 class TestDistillerPageWebContents : public DistillerPageWebContents { | 153 class TestDistillerPageWebContents : public DistillerPageWebContents { |
| 154 public: | 154 public: |
| 155 TestDistillerPageWebContents( | 155 TestDistillerPageWebContents( |
| 156 content::BrowserContext* browser_context, | 156 content::BrowserContext* browser_context, |
| 157 const gfx::Size& render_view_size, | 157 const gfx::Size& render_view_size, |
| 158 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle, | 158 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle, |
| 159 bool expect_new_web_contents) | 159 bool expect_new_web_contents) |
| 160 : DistillerPageWebContents(browser_context, render_view_size, | 160 : DistillerPageWebContents(browser_context, |
| 161 render_view_size, |
| 161 optional_web_contents_handle.Pass()), | 162 optional_web_contents_handle.Pass()), |
| 162 expect_new_web_contents_(expect_new_web_contents), | 163 expect_new_web_contents_(expect_new_web_contents), |
| 163 new_web_contents_created_(false) {} | 164 new_web_contents_created_(false) {} |
| 164 | 165 |
| 165 void CreateNewWebContents(const GURL& url) override { | 166 void CreateNewWebContents(const GURL& url) override { |
| 166 ASSERT_EQ(true, expect_new_web_contents_); | 167 ASSERT_EQ(true, expect_new_web_contents_); |
| 167 new_web_contents_created_ = true; | 168 new_web_contents_created_ = true; |
| 168 DistillerPageWebContents::CreateNewWebContents(url); | 169 DistillerPageWebContents::CreateNewWebContents(url); |
| 169 } | 170 } |
| 170 | 171 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath); | 225 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath); |
| 225 run_loop.Run(); | 226 run_loop.Run(); |
| 226 | 227 |
| 227 // A relative link should've been updated. | 228 // A relative link should've been updated. |
| 228 EXPECT_THAT(distiller_result_->distilled_content().html(), | 229 EXPECT_THAT(distiller_result_->distilled_content().html(), |
| 229 ContainsRegex("src=\"http://127.0.0.1:.*/relativeimage.png\"")); | 230 ContainsRegex("src=\"http://127.0.0.1:.*/relativeimage.png\"")); |
| 230 EXPECT_THAT(distiller_result_->distilled_content().html(), | 231 EXPECT_THAT(distiller_result_->distilled_content().html(), |
| 231 HasSubstr("src=\"http://www.google.com/absoluteimage.png\"")); | 232 HasSubstr("src=\"http://www.google.com/absoluteimage.png\"")); |
| 232 } | 233 } |
| 233 | 234 |
| 234 | |
| 235 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeVideos) { | 235 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, HandlesRelativeVideos) { |
| 236 DistillerPageWebContents distiller_page( | 236 DistillerPageWebContents distiller_page( |
| 237 shell()->web_contents()->GetBrowserContext(), | 237 shell()->web_contents()->GetBrowserContext(), |
| 238 shell()->web_contents()->GetContainerBounds().size(), | 238 shell()->web_contents()->GetContainerBounds().size(), |
| 239 scoped_ptr<SourcePageHandleWebContents>()); | 239 scoped_ptr<SourcePageHandleWebContents>()); |
| 240 distiller_page_ = &distiller_page; | 240 distiller_page_ = &distiller_page; |
| 241 | 241 |
| 242 base::RunLoop run_loop; | 242 base::RunLoop run_loop; |
| 243 DistillPage(run_loop.QuitClosure(), kVideoArticlePath); | 243 DistillPage(run_loop.QuitClosure(), kVideoArticlePath); |
| 244 run_loop.Run(); | 244 run_loop.Run(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 Not(HasSubstr("Lorem ipsum"))); | 281 Not(HasSubstr("Lorem ipsum"))); |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 | 284 |
| 285 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, | 285 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 286 UsingCurrentWebContentsWrongUrl) { | 286 UsingCurrentWebContentsWrongUrl) { |
| 287 std::string url("/bogus"); | 287 std::string url("/bogus"); |
| 288 bool expect_new_web_contents = true; | 288 bool expect_new_web_contents = true; |
| 289 bool setup_main_frame_observer = true; | 289 bool setup_main_frame_observer = true; |
| 290 bool wait_for_document_loaded = true; | 290 bool wait_for_document_loaded = true; |
| 291 RunUseCurrentWebContentsTest(url, | 291 RunUseCurrentWebContentsTest(url, expect_new_web_contents, |
| 292 expect_new_web_contents, | |
| 293 setup_main_frame_observer, | 292 setup_main_frame_observer, |
| 294 wait_for_document_loaded); | 293 wait_for_document_loaded); |
| 295 } | 294 } |
| 296 | 295 |
| 297 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, | 296 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 298 UsingCurrentWebContentsNoMainFrameObserver) { | 297 UsingCurrentWebContentsNoMainFrameObserver) { |
| 299 std::string url(kSimpleArticlePath); | 298 std::string url(kSimpleArticlePath); |
| 300 bool expect_new_web_contents = true; | 299 bool expect_new_web_contents = true; |
| 301 bool setup_main_frame_observer = false; | 300 bool setup_main_frame_observer = false; |
| 302 bool wait_for_document_loaded = true; | 301 bool wait_for_document_loaded = true; |
| 303 RunUseCurrentWebContentsTest(url, | 302 RunUseCurrentWebContentsTest(url, expect_new_web_contents, |
| 304 expect_new_web_contents, | |
| 305 setup_main_frame_observer, | 303 setup_main_frame_observer, |
| 306 wait_for_document_loaded); | 304 wait_for_document_loaded); |
| 307 } | 305 } |
| 308 | 306 |
| 309 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, | 307 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 310 UsingCurrentWebContentsNotFinishedLoadingYet) { | 308 UsingCurrentWebContentsNotFinishedLoadingYet) { |
| 311 std::string url(kSimpleArticlePath); | 309 std::string url(kSimpleArticlePath); |
| 312 bool expect_new_web_contents = false; | 310 bool expect_new_web_contents = false; |
| 313 bool setup_main_frame_observer = true; | 311 bool setup_main_frame_observer = true; |
| 314 bool wait_for_document_loaded = false; | 312 bool wait_for_document_loaded = false; |
| 315 RunUseCurrentWebContentsTest(url, | 313 RunUseCurrentWebContentsTest(url, expect_new_web_contents, |
| 316 expect_new_web_contents, | |
| 317 setup_main_frame_observer, | 314 setup_main_frame_observer, |
| 318 wait_for_document_loaded); | 315 wait_for_document_loaded); |
| 319 } | 316 } |
| 320 | 317 |
| 321 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, | 318 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 322 UsingCurrentWebContentsReadyForDistillation) { | 319 UsingCurrentWebContentsReadyForDistillation) { |
| 323 std::string url(kSimpleArticlePath); | 320 std::string url(kSimpleArticlePath); |
| 324 bool expect_new_web_contents = false; | 321 bool expect_new_web_contents = false; |
| 325 bool setup_main_frame_observer = true; | 322 bool setup_main_frame_observer = true; |
| 326 bool wait_for_document_loaded = true; | 323 bool wait_for_document_loaded = true; |
| 327 RunUseCurrentWebContentsTest(url, | 324 RunUseCurrentWebContentsTest(url, expect_new_web_contents, |
| 328 expect_new_web_contents, | |
| 329 setup_main_frame_observer, | 325 setup_main_frame_observer, |
| 330 wait_for_document_loaded); | 326 wait_for_document_loaded); |
| 331 } | 327 } |
| 332 | 328 |
| 333 void DistillerPageWebContentsTest::RunUseCurrentWebContentsTest( | 329 void DistillerPageWebContentsTest::RunUseCurrentWebContentsTest( |
| 334 const std::string& url, | 330 const std::string& url, |
| 335 bool expect_new_web_contents, | 331 bool expect_new_web_contents, |
| 336 bool setup_main_frame_observer, | 332 bool setup_main_frame_observer, |
| 337 bool wait_for_document_loaded) { | 333 bool wait_for_document_loaded) { |
| 338 content::WebContents* current_web_contents = shell()->web_contents(); | 334 content::WebContents* current_web_contents = shell()->web_contents(); |
| 339 if (setup_main_frame_observer) { | 335 if (setup_main_frame_observer) { |
| 340 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( | 336 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( |
| 341 current_web_contents); | 337 current_web_contents); |
| 342 } | 338 } |
| 343 base::RunLoop url_loaded_runner; | 339 base::RunLoop url_loaded_runner; |
| 344 WebContentsMainFrameHelper main_frame_loaded(current_web_contents, | 340 WebContentsMainFrameHelper main_frame_loaded(current_web_contents, |
| 345 url_loaded_runner.QuitClosure(), | 341 url_loaded_runner.QuitClosure(), |
| 346 wait_for_document_loaded); | 342 wait_for_document_loaded); |
| 347 current_web_contents->GetController().LoadURL( | 343 current_web_contents->GetController().LoadURL( |
| 348 embedded_test_server()->GetURL(url), | 344 embedded_test_server()->GetURL(url), content::Referrer(), |
| 349 content::Referrer(), | 345 ui::PAGE_TRANSITION_TYPED, std::string()); |
| 350 ui::PAGE_TRANSITION_TYPED, | |
| 351 std::string()); | |
| 352 url_loaded_runner.Run(); | 346 url_loaded_runner.Run(); |
| 353 | 347 |
| 354 scoped_ptr<SourcePageHandleWebContents> source_page_handle( | 348 scoped_ptr<SourcePageHandleWebContents> source_page_handle( |
| 355 new SourcePageHandleWebContents(current_web_contents, false)); | 349 new SourcePageHandleWebContents(current_web_contents, false)); |
| 356 | 350 |
| 357 TestDistillerPageWebContents distiller_page( | 351 TestDistillerPageWebContents distiller_page( |
| 358 shell()->web_contents()->GetBrowserContext(), | 352 shell()->web_contents()->GetBrowserContext(), |
| 359 shell()->web_contents()->GetContainerBounds().size(), | 353 shell()->web_contents()->GetContainerBounds().size(), |
| 360 source_page_handle.Pass(), | 354 source_page_handle.Pass(), expect_new_web_contents); |
| 361 expect_new_web_contents); | |
| 362 distiller_page_ = &distiller_page; | 355 distiller_page_ = &distiller_page; |
| 363 | 356 |
| 364 base::RunLoop run_loop; | 357 base::RunLoop run_loop; |
| 365 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath); | 358 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath); |
| 366 run_loop.Run(); | 359 run_loop.Run(); |
| 367 | 360 |
| 368 // Sanity check of distillation process. | 361 // Sanity check of distillation process. |
| 369 EXPECT_EQ(expect_new_web_contents, distiller_page.new_web_contents_created()); | 362 EXPECT_EQ(expect_new_web_contents, distiller_page.new_web_contents_created()); |
| 370 EXPECT_EQ("Test Page Title", distiller_result_->title()); | 363 EXPECT_EQ("Test Page Title", distiller_result_->title()); |
| 371 } | 364 } |
| 372 | 365 |
| 373 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, | 366 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 374 PageDestroyedBeforeFinishDistillation) { | 367 PageDestroyedBeforeFinishDistillation) { |
| 375 | |
| 376 content::WebContents* current_web_contents = shell()->web_contents(); | 368 content::WebContents* current_web_contents = shell()->web_contents(); |
| 377 | 369 |
| 378 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( | 370 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( |
| 379 current_web_contents); | 371 current_web_contents); |
| 380 | 372 |
| 381 base::RunLoop url_loaded_runner; | 373 base::RunLoop url_loaded_runner; |
| 382 WebContentsMainFrameHelper main_frame_loaded(current_web_contents, | 374 WebContentsMainFrameHelper main_frame_loaded( |
| 383 url_loaded_runner.QuitClosure(), | 375 current_web_contents, url_loaded_runner.QuitClosure(), true); |
| 384 true); | |
| 385 current_web_contents->GetController().LoadURL( | 376 current_web_contents->GetController().LoadURL( |
| 386 embedded_test_server()->GetURL(kSimpleArticlePath), | 377 embedded_test_server()->GetURL(kSimpleArticlePath), content::Referrer(), |
| 387 content::Referrer(), | 378 ui::PAGE_TRANSITION_TYPED, std::string()); |
| 388 ui::PAGE_TRANSITION_TYPED, | |
| 389 std::string()); | |
| 390 url_loaded_runner.Run(); | 379 url_loaded_runner.Run(); |
| 391 | 380 |
| 392 scoped_ptr<SourcePageHandleWebContents> source_page_handle( | 381 scoped_ptr<SourcePageHandleWebContents> source_page_handle( |
| 393 new SourcePageHandleWebContents(current_web_contents, false)); | 382 new SourcePageHandleWebContents(current_web_contents, false)); |
| 394 | 383 |
| 395 TestDistillerPageWebContents* distiller_page( | 384 TestDistillerPageWebContents* distiller_page(new TestDistillerPageWebContents( |
| 396 new TestDistillerPageWebContents( | 385 current_web_contents->GetBrowserContext(), |
| 397 current_web_contents->GetBrowserContext(), | 386 current_web_contents->GetContainerBounds().size(), |
| 398 current_web_contents->GetContainerBounds().size(), | 387 source_page_handle.Pass(), false)); |
| 399 source_page_handle.Pass(), | |
| 400 false)); | |
| 401 distiller_page_ = distiller_page; | 388 distiller_page_ = distiller_page; |
| 402 | 389 |
| 403 base::RunLoop run_loop; | 390 base::RunLoop run_loop; |
| 404 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath); | 391 DistillPage(run_loop.QuitClosure(), kSimpleArticlePath); |
| 405 | 392 |
| 406 // It can not crash the loop when returning the result. | 393 // It can not crash the loop when returning the result. |
| 407 delete distiller_page_; | 394 delete distiller_page_; |
| 408 | 395 |
| 409 // Make sure the test ends when it does not crash. | 396 // Make sure the test ends when it does not crash. |
| 410 base::MessageLoop::current()->PostDelayedTask( | 397 base::MessageLoop::current()->PostDelayedTask( |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 465 |
| 479 { // Test empty content. | 466 { // Test empty content. |
| 480 scoped_ptr<DistilledArticleProto> article_proto( | 467 scoped_ptr<DistilledArticleProto> article_proto( |
| 481 new DistilledArticleProto()); | 468 new DistilledArticleProto()); |
| 482 (*(article_proto->add_pages())).set_html(""); | 469 (*(article_proto->add_pages())).set_html(""); |
| 483 std::string js = viewer::GetUnsafeArticleContentJs(article_proto.get()); | 470 std::string js = viewer::GetUnsafeArticleContentJs(article_proto.get()); |
| 484 EXPECT_THAT(js, HasSubstr(no_content)); | 471 EXPECT_THAT(js, HasSubstr(no_content)); |
| 485 } | 472 } |
| 486 } | 473 } |
| 487 | 474 |
| 488 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, | 475 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, TestPinch) { |
| 489 TestPinch) { | |
| 490 // Load the test file in content shell and wait until it has fully loaded. | 476 // Load the test file in content shell and wait until it has fully loaded. |
| 491 content::WebContents* web_contents = shell()->web_contents(); | 477 content::WebContents* web_contents = shell()->web_contents(); |
| 492 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( | 478 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( |
| 493 web_contents); | 479 web_contents); |
| 494 base::RunLoop url_loaded_runner; | 480 base::RunLoop url_loaded_runner; |
| 495 WebContentsMainFrameHelper main_frame_loaded(web_contents, | 481 WebContentsMainFrameHelper main_frame_loaded( |
| 496 url_loaded_runner.QuitClosure(), | 482 web_contents, url_loaded_runner.QuitClosure(), true); |
| 497 true); | |
| 498 web_contents->GetController().LoadURL( | 483 web_contents->GetController().LoadURL( |
| 499 embedded_test_server()->GetURL("/pinch_tester.html"), | 484 embedded_test_server()->GetURL("/pinch_tester.html"), content::Referrer(), |
| 500 content::Referrer(), | 485 ui::PAGE_TRANSITION_TYPED, std::string()); |
| 501 ui::PAGE_TRANSITION_TYPED, | |
| 502 std::string()); | |
| 503 url_loaded_runner.Run(); | 486 url_loaded_runner.Run(); |
| 504 | 487 |
| 505 // Execute the JS to run the tests, and wait until it has finished. | 488 // Execute the JS to run the tests, and wait until it has finished. |
| 506 base::RunLoop run_loop; | 489 base::RunLoop run_loop; |
| 507 web_contents->GetMainFrame()->ExecuteJavaScriptForTests( | 490 web_contents->GetMainFrame()->ExecuteJavaScriptForTests( |
| 508 base::UTF8ToUTF16("(function() {return pinchtest.run();})();"), | 491 base::UTF8ToUTF16("(function() {return pinchtest.run();})();"), |
| 509 base::Bind(&DistillerPageWebContentsTest::OnJsExecutionDone, | 492 base::Bind(&DistillerPageWebContentsTest::OnJsExecutionDone, |
| 510 base::Unretained(this), run_loop.QuitClosure())); | 493 base::Unretained(this), run_loop.QuitClosure())); |
| 511 run_loop.Run(); | 494 run_loop.Run(); |
| 512 | 495 |
| 513 // Convert to dictionary and parse the results. | 496 // Convert to dictionary and parse the results. |
| 514 const base::DictionaryValue* dict; | 497 const base::DictionaryValue* dict; |
| 515 ASSERT_TRUE(js_result_); | 498 ASSERT_TRUE(js_result_); |
| 516 ASSERT_TRUE(js_result_->GetAsDictionary(&dict)); | 499 ASSERT_TRUE(js_result_->GetAsDictionary(&dict)); |
| 517 | 500 |
| 518 ASSERT_TRUE(dict->HasKey("success")); | 501 ASSERT_TRUE(dict->HasKey("success")); |
| 519 bool success; | 502 bool success; |
| 520 ASSERT_TRUE(dict->GetBoolean("success", &success)); | 503 ASSERT_TRUE(dict->GetBoolean("success", &success)); |
| 521 EXPECT_TRUE(success); | 504 EXPECT_TRUE(success); |
| 522 } | 505 } |
| 523 | 506 |
| 524 } // namespace dom_distiller | 507 } // namespace dom_distiller |
| OLD | NEW |