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 "components/dom_distiller/core/distiller.h" | 5 #include "components/dom_distiller/core/distiller.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <utility> | 13 #include <utility> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
17 #include "base/bind_helpers.h" | 17 #include "base/bind_helpers.h" |
18 #include "base/location.h" | 18 #include "base/location.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 21 #include "base/run_loop.h" |
21 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
22 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
23 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
24 #include "base/values.h" | 25 #include "base/values.h" |
25 #include "components/dom_distiller/core/article_distillation_update.h" | 26 #include "components/dom_distiller/core/article_distillation_update.h" |
26 #include "components/dom_distiller/core/distiller_page.h" | 27 #include "components/dom_distiller/core/distiller_page.h" |
27 #include "components/dom_distiller/core/fake_distiller_page.h" | 28 #include "components/dom_distiller/core/fake_distiller_page.h" |
28 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 29 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
29 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 30 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
30 #include "net/url_request/url_request_context_getter.h" | 31 #include "net/url_request/url_request_context_getter.h" |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 return std::unique_ptr<DistillerPage>(distiller_page); | 341 return std::unique_ptr<DistillerPage>(distiller_page); |
341 } | 342 } |
342 | 343 |
343 TEST_F(DistillerTest, DistillPage) { | 344 TEST_F(DistillerTest, DistillPage) { |
344 base::MessageLoopForUI loop; | 345 base::MessageLoopForUI loop; |
345 std::unique_ptr<base::Value> result = | 346 std::unique_ptr<base::Value> result = |
346 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 347 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
347 distiller_.reset( | 348 distiller_.reset( |
348 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 349 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
349 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); | 350 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); |
350 base::MessageLoop::current()->RunUntilIdle(); | 351 base::RunLoop().RunUntilIdle(); |
351 EXPECT_EQ(kTitle, article_proto_->title()); | 352 EXPECT_EQ(kTitle, article_proto_->title()); |
352 ASSERT_EQ(article_proto_->pages_size(), 1); | 353 ASSERT_EQ(article_proto_->pages_size(), 1); |
353 const DistilledPageProto& first_page = article_proto_->pages(0); | 354 const DistilledPageProto& first_page = article_proto_->pages(0); |
354 EXPECT_EQ(kContent, first_page.html()); | 355 EXPECT_EQ(kContent, first_page.html()); |
355 EXPECT_EQ(kURL, first_page.url()); | 356 EXPECT_EQ(kURL, first_page.url()); |
356 } | 357 } |
357 | 358 |
358 TEST_F(DistillerTest, DistillPageWithDebugInfo) { | 359 TEST_F(DistillerTest, DistillPageWithDebugInfo) { |
359 base::MessageLoopForUI loop; | 360 base::MessageLoopForUI loop; |
360 DomDistillerResult dd_result; | 361 DomDistillerResult dd_result; |
361 dd_result.mutable_debug_info()->set_log(kDebugLog); | 362 dd_result.mutable_debug_info()->set_log(kDebugLog); |
362 std::unique_ptr<base::Value> result = | 363 std::unique_ptr<base::Value> result = |
363 dom_distiller::proto::json::DomDistillerResult::WriteToValue(dd_result); | 364 dom_distiller::proto::json::DomDistillerResult::WriteToValue(dd_result); |
364 distiller_.reset( | 365 distiller_.reset( |
365 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 366 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
366 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); | 367 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); |
367 base::MessageLoop::current()->RunUntilIdle(); | 368 base::RunLoop().RunUntilIdle(); |
368 const DistilledPageProto& first_page = article_proto_->pages(0); | 369 const DistilledPageProto& first_page = article_proto_->pages(0); |
369 EXPECT_EQ(kDebugLog, first_page.debug_info().log()); | 370 EXPECT_EQ(kDebugLog, first_page.debug_info().log()); |
370 } | 371 } |
371 | 372 |
372 void SetTimingEntry(TimingEntry* entry, const std::string& name, double time) { | 373 void SetTimingEntry(TimingEntry* entry, const std::string& name, double time) { |
373 entry->set_name(name); | 374 entry->set_name(name); |
374 entry->set_time(time); | 375 entry->set_time(time); |
375 } | 376 } |
376 | 377 |
377 TEST_F(DistillerTest, DistillPageWithTimingInfo) { | 378 TEST_F(DistillerTest, DistillPageWithTimingInfo) { |
378 base::MessageLoopForUI loop; | 379 base::MessageLoopForUI loop; |
379 DomDistillerResult dd_result; | 380 DomDistillerResult dd_result; |
380 dd_result.mutable_timing_info()->set_total_time(1.0); | 381 dd_result.mutable_timing_info()->set_total_time(1.0); |
381 dd_result.mutable_timing_info()->set_markup_parsing_time(2.0); | 382 dd_result.mutable_timing_info()->set_markup_parsing_time(2.0); |
382 dd_result.mutable_timing_info()->set_document_construction_time(3.0); | 383 dd_result.mutable_timing_info()->set_document_construction_time(3.0); |
383 dd_result.mutable_timing_info()->set_article_processing_time(4.0); | 384 dd_result.mutable_timing_info()->set_article_processing_time(4.0); |
384 dd_result.mutable_timing_info()->set_formatting_time(5.0); | 385 dd_result.mutable_timing_info()->set_formatting_time(5.0); |
385 SetTimingEntry( | 386 SetTimingEntry( |
386 dd_result.mutable_timing_info()->add_other_times(), "time0", 6.0); | 387 dd_result.mutable_timing_info()->add_other_times(), "time0", 6.0); |
387 SetTimingEntry( | 388 SetTimingEntry( |
388 dd_result.mutable_timing_info()->add_other_times(), "time1", 7.0); | 389 dd_result.mutable_timing_info()->add_other_times(), "time1", 7.0); |
389 std::unique_ptr<base::Value> result = | 390 std::unique_ptr<base::Value> result = |
390 dom_distiller::proto::json::DomDistillerResult::WriteToValue(dd_result); | 391 dom_distiller::proto::json::DomDistillerResult::WriteToValue(dd_result); |
391 distiller_.reset( | 392 distiller_.reset( |
392 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 393 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
393 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); | 394 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); |
394 base::MessageLoop::current()->RunUntilIdle(); | 395 base::RunLoop().RunUntilIdle(); |
395 const DistilledPageProto& first_page = article_proto_->pages(0); | 396 const DistilledPageProto& first_page = article_proto_->pages(0); |
396 std::map<std::string, double> timings; | 397 std::map<std::string, double> timings; |
397 for (int i = 0; i < first_page.timing_info_size(); ++i) { | 398 for (int i = 0; i < first_page.timing_info_size(); ++i) { |
398 DistilledPageProto::TimingInfo timing = first_page.timing_info(i); | 399 DistilledPageProto::TimingInfo timing = first_page.timing_info(i); |
399 timings[timing.name()] = timing.time(); | 400 timings[timing.name()] = timing.time(); |
400 } | 401 } |
401 EXPECT_EQ(7u, timings.size()); | 402 EXPECT_EQ(7u, timings.size()); |
402 EXPECT_EQ(1.0, timings["total"]); | 403 EXPECT_EQ(1.0, timings["total"]); |
403 EXPECT_EQ(2.0, timings["markup_parsing"]); | 404 EXPECT_EQ(2.0, timings["markup_parsing"]); |
404 EXPECT_EQ(3.0, timings["document_construction"]); | 405 EXPECT_EQ(3.0, timings["document_construction"]); |
405 EXPECT_EQ(4.0, timings["article_processing"]); | 406 EXPECT_EQ(4.0, timings["article_processing"]); |
406 EXPECT_EQ(5.0, timings["formatting"]); | 407 EXPECT_EQ(5.0, timings["formatting"]); |
407 EXPECT_EQ(6.0, timings["time0"]); | 408 EXPECT_EQ(6.0, timings["time0"]); |
408 EXPECT_EQ(7.0, timings["time1"]); | 409 EXPECT_EQ(7.0, timings["time1"]); |
409 } | 410 } |
410 | 411 |
411 TEST_F(DistillerTest, DistillPageWithImages) { | 412 TEST_F(DistillerTest, DistillPageWithImages) { |
412 base::MessageLoopForUI loop; | 413 base::MessageLoopForUI loop; |
413 vector<int> image_indices; | 414 vector<int> image_indices; |
414 image_indices.push_back(0); | 415 image_indices.push_back(0); |
415 image_indices.push_back(1); | 416 image_indices.push_back(1); |
416 image_indices.push_back(2); | 417 image_indices.push_back(2); |
417 std::unique_ptr<base::Value> result = | 418 std::unique_ptr<base::Value> result = |
418 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); | 419 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); |
419 distiller_.reset( | 420 distiller_.reset( |
420 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 421 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
421 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); | 422 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); |
422 base::MessageLoop::current()->RunUntilIdle(); | 423 base::RunLoop().RunUntilIdle(); |
423 EXPECT_EQ(kTitle, article_proto_->title()); | 424 EXPECT_EQ(kTitle, article_proto_->title()); |
424 ASSERT_EQ(article_proto_->pages_size(), 1); | 425 ASSERT_EQ(article_proto_->pages_size(), 1); |
425 const DistilledPageProto& first_page = article_proto_->pages(0); | 426 const DistilledPageProto& first_page = article_proto_->pages(0); |
426 EXPECT_EQ(kContent, first_page.html()); | 427 EXPECT_EQ(kContent, first_page.html()); |
427 EXPECT_EQ(kURL, first_page.url()); | 428 EXPECT_EQ(kURL, first_page.url()); |
428 ASSERT_EQ(2, first_page.image_size()); | 429 ASSERT_EQ(2, first_page.image_size()); |
429 EXPECT_EQ(kImageData[0], first_page.image(0).data()); | 430 EXPECT_EQ(kImageData[0], first_page.image(0).data()); |
430 EXPECT_EQ(kImageURLs[0], first_page.image(0).url()); | 431 EXPECT_EQ(kImageURLs[0], first_page.image(0).url()); |
431 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); | 432 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); |
432 EXPECT_EQ(kImageData[1], first_page.image(1).data()); | 433 EXPECT_EQ(kImageData[1], first_page.image(1).data()); |
(...skipping 17 matching lines...) Expand all Loading... |
450 image_indices.push_back(next_image_number); | 451 image_indices.push_back(next_image_number); |
451 next_image_number = (next_image_number + 1) % kTotalImages; | 452 next_image_number = (next_image_number + 1) % kTotalImages; |
452 } | 453 } |
453 distiller_data->image_ids.push_back(image_indices); | 454 distiller_data->image_ids.push_back(image_indices); |
454 } | 455 } |
455 | 456 |
456 distiller_.reset( | 457 distiller_.reset( |
457 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 458 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
458 DistillPage(distiller_data->page_urls[0], | 459 DistillPage(distiller_data->page_urls[0], |
459 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0)); | 460 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0)); |
460 base::MessageLoop::current()->RunUntilIdle(); | 461 base::RunLoop().RunUntilIdle(); |
461 VerifyArticleProtoMatchesMultipageData( | 462 VerifyArticleProtoMatchesMultipageData( |
462 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); | 463 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
463 } | 464 } |
464 | 465 |
465 TEST_F(DistillerTest, DistillLinkLoop) { | 466 TEST_F(DistillerTest, DistillLinkLoop) { |
466 base::MessageLoopForUI loop; | 467 base::MessageLoopForUI loop; |
467 // Create a loop, the next page is same as the current page. This could | 468 // Create a loop, the next page is same as the current page. This could |
468 // happen if javascript misparses a next page link. | 469 // happen if javascript misparses a next page link. |
469 std::unique_ptr<base::Value> result = | 470 std::unique_ptr<base::Value> result = |
470 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); | 471 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); |
471 distiller_.reset( | 472 distiller_.reset( |
472 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 473 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
473 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); | 474 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL))); |
474 base::MessageLoop::current()->RunUntilIdle(); | 475 base::RunLoop().RunUntilIdle(); |
475 EXPECT_EQ(kTitle, article_proto_->title()); | 476 EXPECT_EQ(kTitle, article_proto_->title()); |
476 EXPECT_EQ(article_proto_->pages_size(), 1); | 477 EXPECT_EQ(article_proto_->pages_size(), 1); |
477 } | 478 } |
478 | 479 |
479 TEST_F(DistillerTest, CheckMaxPageLimitExtraPage) { | 480 TEST_F(DistillerTest, CheckMaxPageLimitExtraPage) { |
480 base::MessageLoopForUI loop; | 481 base::MessageLoopForUI loop; |
481 const size_t kMaxPagesInArticle = 10; | 482 const size_t kMaxPagesInArticle = 10; |
482 std::unique_ptr<MultipageDistillerData> distiller_data = | 483 std::unique_ptr<MultipageDistillerData> distiller_data = |
483 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); | 484 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); |
484 | 485 |
485 // Note: Next page url of the last page of article is set. So distiller will | 486 // Note: Next page url of the last page of article is set. So distiller will |
486 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not | 487 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not |
487 // work. | 488 // work. |
488 std::unique_ptr<base::Value> last_page_data = | 489 std::unique_ptr<base::Value> last_page_data = |
489 CreateDistilledValueReturnedFromJS( | 490 CreateDistilledValueReturnedFromJS( |
490 kTitle, distiller_data->content[kMaxPagesInArticle - 1], | 491 kTitle, distiller_data->content[kMaxPagesInArticle - 1], |
491 vector<int>(), "", distiller_data->page_urls[kMaxPagesInArticle - 2]); | 492 vector<int>(), "", distiller_data->page_urls[kMaxPagesInArticle - 2]); |
492 | 493 |
493 distiller_data->distilled_values.pop_back(); | 494 distiller_data->distilled_values.pop_back(); |
494 distiller_data->distilled_values.push_back(last_page_data.release()); | 495 distiller_data->distilled_values.push_back(last_page_data.release()); |
495 | 496 |
496 distiller_.reset( | 497 distiller_.reset( |
497 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 498 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
498 | 499 |
499 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 500 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
500 | 501 |
501 DistillPage( | 502 DistillPage( |
502 distiller_data->page_urls[0], | 503 distiller_data->page_urls[0], |
503 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); | 504 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); |
504 base::MessageLoop::current()->RunUntilIdle(); | 505 base::RunLoop().RunUntilIdle(); |
505 EXPECT_EQ(kTitle, article_proto_->title()); | 506 EXPECT_EQ(kTitle, article_proto_->title()); |
506 EXPECT_EQ(kMaxPagesInArticle, | 507 EXPECT_EQ(kMaxPagesInArticle, |
507 static_cast<size_t>(article_proto_->pages_size())); | 508 static_cast<size_t>(article_proto_->pages_size())); |
508 } | 509 } |
509 | 510 |
510 TEST_F(DistillerTest, CheckMaxPageLimitExactLimit) { | 511 TEST_F(DistillerTest, CheckMaxPageLimitExactLimit) { |
511 base::MessageLoopForUI loop; | 512 base::MessageLoopForUI loop; |
512 const size_t kMaxPagesInArticle = 10; | 513 const size_t kMaxPagesInArticle = 10; |
513 std::unique_ptr<MultipageDistillerData> distiller_data = | 514 std::unique_ptr<MultipageDistillerData> distiller_data = |
514 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); | 515 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); |
515 | 516 |
516 distiller_.reset( | 517 distiller_.reset( |
517 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 518 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
518 | 519 |
519 // Check if distilling an article with exactly the page limit works. | 520 // Check if distilling an article with exactly the page limit works. |
520 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 521 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
521 | 522 |
522 DistillPage( | 523 DistillPage( |
523 distiller_data->page_urls[0], | 524 distiller_data->page_urls[0], |
524 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); | 525 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); |
525 base::MessageLoop::current()->RunUntilIdle(); | 526 base::RunLoop().RunUntilIdle(); |
526 EXPECT_EQ(kTitle, article_proto_->title()); | 527 EXPECT_EQ(kTitle, article_proto_->title()); |
527 EXPECT_EQ(kMaxPagesInArticle, | 528 EXPECT_EQ(kMaxPagesInArticle, |
528 static_cast<size_t>(article_proto_->pages_size())); | 529 static_cast<size_t>(article_proto_->pages_size())); |
529 } | 530 } |
530 | 531 |
531 TEST_F(DistillerTest, SinglePageDistillationFailure) { | 532 TEST_F(DistillerTest, SinglePageDistillationFailure) { |
532 base::MessageLoopForUI loop; | 533 base::MessageLoopForUI loop; |
533 // To simulate failure return a null value. | 534 // To simulate failure return a null value. |
534 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); | 535 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); |
535 distiller_.reset( | 536 distiller_.reset( |
536 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 537 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
537 DistillPage(kURL, CreateMockDistillerPage(null_value.get(), GURL(kURL))); | 538 DistillPage(kURL, CreateMockDistillerPage(null_value.get(), GURL(kURL))); |
538 base::MessageLoop::current()->RunUntilIdle(); | 539 base::RunLoop().RunUntilIdle(); |
539 EXPECT_EQ("", article_proto_->title()); | 540 EXPECT_EQ("", article_proto_->title()); |
540 EXPECT_EQ(0, article_proto_->pages_size()); | 541 EXPECT_EQ(0, article_proto_->pages_size()); |
541 } | 542 } |
542 | 543 |
543 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { | 544 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { |
544 base::MessageLoopForUI loop; | 545 base::MessageLoopForUI loop; |
545 const size_t kNumPages = 8; | 546 const size_t kNumPages = 8; |
546 std::unique_ptr<MultipageDistillerData> distiller_data = | 547 std::unique_ptr<MultipageDistillerData> distiller_data = |
547 CreateMultipageDistillerDataWithoutImages(kNumPages); | 548 CreateMultipageDistillerDataWithoutImages(kNumPages); |
548 | 549 |
549 // The page number of the failed page. | 550 // The page number of the failed page. |
550 size_t failed_page_num = 3; | 551 size_t failed_page_num = 3; |
551 // reset distilled data of the failed page. | 552 // reset distilled data of the failed page. |
552 distiller_data->distilled_values.erase( | 553 distiller_data->distilled_values.erase( |
553 distiller_data->distilled_values.begin() + failed_page_num); | 554 distiller_data->distilled_values.begin() + failed_page_num); |
554 distiller_data->distilled_values.insert( | 555 distiller_data->distilled_values.insert( |
555 distiller_data->distilled_values.begin() + failed_page_num, | 556 distiller_data->distilled_values.begin() + failed_page_num, |
556 base::Value::CreateNullValue().release()); | 557 base::Value::CreateNullValue().release()); |
557 // Expect only calls till the failed page number. | 558 // Expect only calls till the failed page number. |
558 distiller_.reset( | 559 distiller_.reset( |
559 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 560 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
560 DistillPage( | 561 DistillPage( |
561 distiller_data->page_urls[0], | 562 distiller_data->page_urls[0], |
562 CreateMockDistillerPages(distiller_data.get(), failed_page_num + 1, 0)); | 563 CreateMockDistillerPages(distiller_data.get(), failed_page_num + 1, 0)); |
563 base::MessageLoop::current()->RunUntilIdle(); | 564 base::RunLoop().RunUntilIdle(); |
564 EXPECT_EQ(kTitle, article_proto_->title()); | 565 EXPECT_EQ(kTitle, article_proto_->title()); |
565 VerifyArticleProtoMatchesMultipageData( | 566 VerifyArticleProtoMatchesMultipageData( |
566 article_proto_.get(), distiller_data.get(), failed_page_num, kNumPages); | 567 article_proto_.get(), distiller_data.get(), failed_page_num, kNumPages); |
567 } | 568 } |
568 | 569 |
569 TEST_F(DistillerTest, DistillMultiplePagesFirstEmpty) { | 570 TEST_F(DistillerTest, DistillMultiplePagesFirstEmpty) { |
570 base::MessageLoopForUI loop; | 571 base::MessageLoopForUI loop; |
571 const size_t kNumPages = 8; | 572 const size_t kNumPages = 8; |
572 std::unique_ptr<MultipageDistillerData> distiller_data = | 573 std::unique_ptr<MultipageDistillerData> distiller_data = |
573 CreateMultipageDistillerDataWithoutImages(kNumPages); | 574 CreateMultipageDistillerDataWithoutImages(kNumPages); |
574 | 575 |
575 // The first page has no content. | 576 // The first page has no content. |
576 const size_t empty_page_num = 0; | 577 const size_t empty_page_num = 0; |
577 distiller_data->content[empty_page_num] = ""; | 578 distiller_data->content[empty_page_num] = ""; |
578 std::unique_ptr<base::Value> distilled_value = | 579 std::unique_ptr<base::Value> distilled_value = |
579 CreateDistilledValueReturnedFromJS(kTitle, "", vector<int>(), | 580 CreateDistilledValueReturnedFromJS(kTitle, "", vector<int>(), |
580 GenerateNextPageUrl(kURL, empty_page_num, kNumPages), | 581 GenerateNextPageUrl(kURL, empty_page_num, kNumPages), |
581 GeneratePrevPageUrl(kURL, empty_page_num)); | 582 GeneratePrevPageUrl(kURL, empty_page_num)); |
582 // Reset distilled data of the first page. | 583 // Reset distilled data of the first page. |
583 distiller_data->distilled_values.erase( | 584 distiller_data->distilled_values.erase( |
584 distiller_data->distilled_values.begin() + empty_page_num); | 585 distiller_data->distilled_values.begin() + empty_page_num); |
585 distiller_data->distilled_values.insert( | 586 distiller_data->distilled_values.insert( |
586 distiller_data->distilled_values.begin() + empty_page_num, | 587 distiller_data->distilled_values.begin() + empty_page_num, |
587 distilled_value.release()); | 588 distilled_value.release()); |
588 | 589 |
589 distiller_.reset( | 590 distiller_.reset( |
590 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 591 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
591 DistillPage(distiller_data->page_urls[0], | 592 DistillPage(distiller_data->page_urls[0], |
592 CreateMockDistillerPages(distiller_data.get(), 1, 0)); | 593 CreateMockDistillerPages(distiller_data.get(), 1, 0)); |
593 base::MessageLoop::current()->RunUntilIdle(); | 594 base::RunLoop().RunUntilIdle(); |
594 // If the first page has no content, stop fetching the next page. | 595 // If the first page has no content, stop fetching the next page. |
595 EXPECT_EQ(1, article_proto_->pages_size()); | 596 EXPECT_EQ(1, article_proto_->pages_size()); |
596 VerifyArticleProtoMatchesMultipageData( | 597 VerifyArticleProtoMatchesMultipageData( |
597 article_proto_.get(), distiller_data.get(), 1, 1); | 598 article_proto_.get(), distiller_data.get(), 1, 1); |
598 } | 599 } |
599 | 600 |
600 TEST_F(DistillerTest, DistillMultiplePagesSecondEmpty) { | 601 TEST_F(DistillerTest, DistillMultiplePagesSecondEmpty) { |
601 base::MessageLoopForUI loop; | 602 base::MessageLoopForUI loop; |
602 const size_t kNumPages = 8; | 603 const size_t kNumPages = 8; |
603 std::unique_ptr<MultipageDistillerData> distiller_data = | 604 std::unique_ptr<MultipageDistillerData> distiller_data = |
(...skipping 10 matching lines...) Expand all Loading... |
614 distiller_data->distilled_values.erase( | 615 distiller_data->distilled_values.erase( |
615 distiller_data->distilled_values.begin() + empty_page_num); | 616 distiller_data->distilled_values.begin() + empty_page_num); |
616 distiller_data->distilled_values.insert( | 617 distiller_data->distilled_values.insert( |
617 distiller_data->distilled_values.begin() + empty_page_num, | 618 distiller_data->distilled_values.begin() + empty_page_num, |
618 distilled_value.release()); | 619 distilled_value.release()); |
619 | 620 |
620 distiller_.reset( | 621 distiller_.reset( |
621 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 622 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
622 DistillPage(distiller_data->page_urls[0], | 623 DistillPage(distiller_data->page_urls[0], |
623 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0)); | 624 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0)); |
624 base::MessageLoop::current()->RunUntilIdle(); | 625 base::RunLoop().RunUntilIdle(); |
625 | 626 |
626 VerifyArticleProtoMatchesMultipageData( | 627 VerifyArticleProtoMatchesMultipageData( |
627 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); | 628 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
628 } | 629 } |
629 | 630 |
630 TEST_F(DistillerTest, DistillPreviousPage) { | 631 TEST_F(DistillerTest, DistillPreviousPage) { |
631 base::MessageLoopForUI loop; | 632 base::MessageLoopForUI loop; |
632 const size_t kNumPages = 8; | 633 const size_t kNumPages = 8; |
633 | 634 |
634 // The page number of the article on which distillation starts. | 635 // The page number of the article on which distillation starts. |
635 int start_page_num = 3; | 636 int start_page_num = 3; |
636 std::unique_ptr<MultipageDistillerData> distiller_data = | 637 std::unique_ptr<MultipageDistillerData> distiller_data = |
637 CreateMultipageDistillerDataWithoutImages(kNumPages); | 638 CreateMultipageDistillerDataWithoutImages(kNumPages); |
638 | 639 |
639 distiller_.reset( | 640 distiller_.reset( |
640 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 641 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
641 DistillPage(distiller_data->page_urls[start_page_num], | 642 DistillPage(distiller_data->page_urls[start_page_num], |
642 CreateMockDistillerPages(distiller_data.get(), kNumPages, | 643 CreateMockDistillerPages(distiller_data.get(), kNumPages, |
643 start_page_num)); | 644 start_page_num)); |
644 base::MessageLoop::current()->RunUntilIdle(); | 645 base::RunLoop().RunUntilIdle(); |
645 VerifyArticleProtoMatchesMultipageData( | 646 VerifyArticleProtoMatchesMultipageData( |
646 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); | 647 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
647 } | 648 } |
648 | 649 |
649 TEST_F(DistillerTest, IncrementalUpdates) { | 650 TEST_F(DistillerTest, IncrementalUpdates) { |
650 base::MessageLoopForUI loop; | 651 base::MessageLoopForUI loop; |
651 const size_t kNumPages = 8; | 652 const size_t kNumPages = 8; |
652 | 653 |
653 // The page number of the article on which distillation starts. | 654 // The page number of the article on which distillation starts. |
654 int start_page_num = 3; | 655 int start_page_num = 3; |
655 std::unique_ptr<MultipageDistillerData> distiller_data = | 656 std::unique_ptr<MultipageDistillerData> distiller_data = |
656 CreateMultipageDistillerDataWithoutImages(kNumPages); | 657 CreateMultipageDistillerDataWithoutImages(kNumPages); |
657 | 658 |
658 distiller_.reset( | 659 distiller_.reset( |
659 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 660 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
660 DistillPage(distiller_data->page_urls[start_page_num], | 661 DistillPage(distiller_data->page_urls[start_page_num], |
661 CreateMockDistillerPages(distiller_data.get(), kNumPages, | 662 CreateMockDistillerPages(distiller_data.get(), kNumPages, |
662 start_page_num)); | 663 start_page_num)); |
663 base::MessageLoop::current()->RunUntilIdle(); | 664 base::RunLoop().RunUntilIdle(); |
664 EXPECT_EQ(kTitle, article_proto_->title()); | 665 EXPECT_EQ(kTitle, article_proto_->title()); |
665 ASSERT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); | 666 ASSERT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); |
666 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 667 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
667 | 668 |
668 VerifyIncrementalUpdatesMatch( | 669 VerifyIncrementalUpdatesMatch( |
669 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); | 670 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); |
670 } | 671 } |
671 | 672 |
672 TEST_F(DistillerTest, IncrementalUpdatesDoNotDeleteFinalArticle) { | 673 TEST_F(DistillerTest, IncrementalUpdatesDoNotDeleteFinalArticle) { |
673 base::MessageLoopForUI loop; | 674 base::MessageLoopForUI loop; |
674 const size_t kNumPages = 8; | 675 const size_t kNumPages = 8; |
675 int start_page_num = 3; | 676 int start_page_num = 3; |
676 std::unique_ptr<MultipageDistillerData> distiller_data = | 677 std::unique_ptr<MultipageDistillerData> distiller_data = |
677 CreateMultipageDistillerDataWithoutImages(kNumPages); | 678 CreateMultipageDistillerDataWithoutImages(kNumPages); |
678 | 679 |
679 distiller_.reset( | 680 distiller_.reset( |
680 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 681 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
681 DistillPage(distiller_data->page_urls[start_page_num], | 682 DistillPage(distiller_data->page_urls[start_page_num], |
682 CreateMockDistillerPages(distiller_data.get(), kNumPages, | 683 CreateMockDistillerPages(distiller_data.get(), kNumPages, |
683 start_page_num)); | 684 start_page_num)); |
684 base::MessageLoop::current()->RunUntilIdle(); | 685 base::RunLoop().RunUntilIdle(); |
685 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 686 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
686 | 687 |
687 in_sequence_updates_.clear(); | 688 in_sequence_updates_.clear(); |
688 | 689 |
689 // Should still be able to access article and pages. | 690 // Should still be able to access article and pages. |
690 VerifyArticleProtoMatchesMultipageData( | 691 VerifyArticleProtoMatchesMultipageData( |
691 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); | 692 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
692 } | 693 } |
693 | 694 |
694 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { | 695 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { |
695 base::MessageLoopForUI loop; | 696 base::MessageLoopForUI loop; |
696 const size_t kNumPages = 8; | 697 const size_t kNumPages = 8; |
697 std::unique_ptr<MultipageDistillerData> distiller_data = | 698 std::unique_ptr<MultipageDistillerData> distiller_data = |
698 CreateMultipageDistillerDataWithoutImages(kNumPages); | 699 CreateMultipageDistillerDataWithoutImages(kNumPages); |
699 // The page number of the article on which distillation starts. | 700 // The page number of the article on which distillation starts. |
700 int start_page_num = 3; | 701 int start_page_num = 3; |
701 | 702 |
702 distiller_.reset( | 703 distiller_.reset( |
703 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 704 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
704 DistillPage(distiller_data->page_urls[start_page_num], | 705 DistillPage(distiller_data->page_urls[start_page_num], |
705 CreateMockDistillerPages(distiller_data.get(), kNumPages, | 706 CreateMockDistillerPages(distiller_data.get(), kNumPages, |
706 start_page_num)); | 707 start_page_num)); |
707 base::MessageLoop::current()->RunUntilIdle(); | 708 base::RunLoop().RunUntilIdle(); |
708 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 709 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
709 EXPECT_EQ(kTitle, article_proto_->title()); | 710 EXPECT_EQ(kTitle, article_proto_->title()); |
710 ASSERT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); | 711 ASSERT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); |
711 | 712 |
712 // Delete the article. | 713 // Delete the article. |
713 article_proto_.reset(); | 714 article_proto_.reset(); |
714 VerifyIncrementalUpdatesMatch( | 715 VerifyIncrementalUpdatesMatch( |
715 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); | 716 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); |
716 } | 717 } |
717 | 718 |
718 TEST_F(DistillerTest, CancelWithDelayedImageFetchCallback) { | 719 TEST_F(DistillerTest, CancelWithDelayedImageFetchCallback) { |
719 base::MessageLoopForUI loop; | 720 base::MessageLoopForUI loop; |
720 vector<int> image_indices; | 721 vector<int> image_indices; |
721 image_indices.push_back(0); | 722 image_indices.push_back(0); |
722 std::unique_ptr<base::Value> distilled_value = | 723 std::unique_ptr<base::Value> distilled_value = |
723 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); | 724 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); |
724 TestDistillerURLFetcher* delayed_fetcher = new TestDistillerURLFetcher(true); | 725 TestDistillerURLFetcher* delayed_fetcher = new TestDistillerURLFetcher(true); |
725 MockDistillerURLFetcherFactory mock_url_fetcher_factory; | 726 MockDistillerURLFetcherFactory mock_url_fetcher_factory; |
726 EXPECT_CALL(mock_url_fetcher_factory, CreateDistillerURLFetcher()) | 727 EXPECT_CALL(mock_url_fetcher_factory, CreateDistillerURLFetcher()) |
727 .WillOnce(Return(delayed_fetcher)); | 728 .WillOnce(Return(delayed_fetcher)); |
728 distiller_.reset( | 729 distiller_.reset( |
729 new DistillerImpl(mock_url_fetcher_factory, DomDistillerOptions())); | 730 new DistillerImpl(mock_url_fetcher_factory, DomDistillerOptions())); |
730 DistillPage(kURL, CreateMockDistillerPage(distilled_value.get(), GURL(kURL))); | 731 DistillPage(kURL, CreateMockDistillerPage(distilled_value.get(), GURL(kURL))); |
731 base::MessageLoop::current()->RunUntilIdle(); | 732 base::RunLoop().RunUntilIdle(); |
732 | 733 |
733 // Post callback from the url fetcher and then delete the distiller. | 734 // Post callback from the url fetcher and then delete the distiller. |
734 delayed_fetcher->PostCallbackTask(); | 735 delayed_fetcher->PostCallbackTask(); |
735 distiller_.reset(); | 736 distiller_.reset(); |
736 | 737 |
737 base::MessageLoop::current()->RunUntilIdle(); | 738 base::RunLoop().RunUntilIdle(); |
738 } | 739 } |
739 | 740 |
740 TEST_F(DistillerTest, CancelWithDelayedJSCallback) { | 741 TEST_F(DistillerTest, CancelWithDelayedJSCallback) { |
741 base::MessageLoopForUI loop; | 742 base::MessageLoopForUI loop; |
742 std::unique_ptr<base::Value> distilled_value = | 743 std::unique_ptr<base::Value> distilled_value = |
743 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 744 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
744 MockDistillerPage* distiller_page = NULL; | 745 MockDistillerPage* distiller_page = NULL; |
745 distiller_.reset( | 746 distiller_.reset( |
746 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 747 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
747 DistillPage(kURL, | 748 DistillPage(kURL, |
748 CreateMockDistillerPageWithPendingJSCallback(&distiller_page, | 749 CreateMockDistillerPageWithPendingJSCallback(&distiller_page, |
749 GURL(kURL))); | 750 GURL(kURL))); |
750 base::MessageLoop::current()->RunUntilIdle(); | 751 base::RunLoop().RunUntilIdle(); |
751 | 752 |
752 ASSERT_TRUE(distiller_page); | 753 ASSERT_TRUE(distiller_page); |
753 // Post the task to execute javascript and then delete the distiller. | 754 // Post the task to execute javascript and then delete the distiller. |
754 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); | 755 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); |
755 distiller_.reset(); | 756 distiller_.reset(); |
756 | 757 |
757 base::MessageLoop::current()->RunUntilIdle(); | 758 base::RunLoop().RunUntilIdle(); |
758 } | 759 } |
759 | 760 |
760 } // namespace dom_distiller | 761 } // namespace dom_distiller |
OLD | NEW |