| 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> |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 } | 94 } |
| 95 | 95 |
| 96 struct MultipageDistillerData { | 96 struct MultipageDistillerData { |
| 97 public: | 97 public: |
| 98 MultipageDistillerData() {} | 98 MultipageDistillerData() {} |
| 99 ~MultipageDistillerData() {} | 99 ~MultipageDistillerData() {} |
| 100 vector<string> page_urls; | 100 vector<string> page_urls; |
| 101 vector<string> content; | 101 vector<string> content; |
| 102 vector<vector<int> > image_ids; | 102 vector<vector<int> > image_ids; |
| 103 // The Javascript values returned by mock distiller. | 103 // The Javascript values returned by mock distiller. |
| 104 ScopedVector<base::Value> distilled_values; | 104 std::vector<std::unique_ptr<base::Value>> distilled_values; |
| 105 | 105 |
| 106 private: | 106 private: |
| 107 DISALLOW_COPY_AND_ASSIGN(MultipageDistillerData); | 107 DISALLOW_COPY_AND_ASSIGN(MultipageDistillerData); |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 void VerifyIncrementalUpdatesMatch( | 110 void VerifyIncrementalUpdatesMatch( |
| 111 const MultipageDistillerData* distiller_data, | 111 const MultipageDistillerData* distiller_data, |
| 112 int num_pages_in_article, | 112 int num_pages_in_article, |
| 113 const vector<dom_distiller::ArticleDistillationUpdate>& incremental_updates, | 113 const vector<dom_distiller::ArticleDistillationUpdate>& incremental_updates, |
| 114 int start_page_num) { | 114 int start_page_num) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 base::SizeTToString(page_num)); | 169 base::SizeTToString(page_num)); |
| 170 result->image_ids.push_back(vector<int>()); | 170 result->image_ids.push_back(vector<int>()); |
| 171 string next_page_url = | 171 string next_page_url = |
| 172 GenerateNextPageUrl(url_prefix, page_num, pages_size); | 172 GenerateNextPageUrl(url_prefix, page_num, pages_size); |
| 173 string prev_page_url = | 173 string prev_page_url = |
| 174 GeneratePrevPageUrl(url_prefix, page_num); | 174 GeneratePrevPageUrl(url_prefix, page_num); |
| 175 std::unique_ptr<base::Value> distilled_value = | 175 std::unique_ptr<base::Value> distilled_value = |
| 176 CreateDistilledValueReturnedFromJS(kTitle, result->content[page_num], | 176 CreateDistilledValueReturnedFromJS(kTitle, result->content[page_num], |
| 177 result->image_ids[page_num], | 177 result->image_ids[page_num], |
| 178 next_page_url, prev_page_url); | 178 next_page_url, prev_page_url); |
| 179 result->distilled_values.push_back(distilled_value.release()); | 179 result->distilled_values.push_back(std::move(distilled_value)); |
| 180 } | 180 } |
| 181 return result; | 181 return result; |
| 182 } | 182 } |
| 183 | 183 |
| 184 void VerifyArticleProtoMatchesMultipageData( | 184 void VerifyArticleProtoMatchesMultipageData( |
| 185 const dom_distiller::DistilledArticleProto* article_proto, | 185 const dom_distiller::DistilledArticleProto* article_proto, |
| 186 const MultipageDistillerData* distiller_data, | 186 const MultipageDistillerData* distiller_data, |
| 187 size_t distilled_pages_size, | 187 size_t distilled_pages_size, |
| 188 size_t total_pages_size) { | 188 size_t total_pages_size) { |
| 189 ASSERT_EQ(distilled_pages_size, | 189 ASSERT_EQ(distilled_pages_size, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 int start_page_num) { | 328 int start_page_num) { |
| 329 MockDistillerPage* distiller_page = new MockDistillerPage(); | 329 MockDistillerPage* distiller_page = new MockDistillerPage(); |
| 330 { | 330 { |
| 331 testing::InSequence s; | 331 testing::InSequence s; |
| 332 vector<int> page_nums = GetPagesInSequence(start_page_num, pages_size); | 332 vector<int> page_nums = GetPagesInSequence(start_page_num, pages_size); |
| 333 for (size_t page_num = 0; page_num < pages_size; ++page_num) { | 333 for (size_t page_num = 0; page_num < pages_size; ++page_num) { |
| 334 int page = page_nums[page_num]; | 334 int page = page_nums[page_num]; |
| 335 GURL url = GURL(distiller_data->page_urls[page]); | 335 GURL url = GURL(distiller_data->page_urls[page]); |
| 336 EXPECT_CALL(*distiller_page, DistillPageImpl(url, _)) | 336 EXPECT_CALL(*distiller_page, DistillPageImpl(url, _)) |
| 337 .WillOnce(DistillerPageOnDistillationDone( | 337 .WillOnce(DistillerPageOnDistillationDone( |
| 338 distiller_page, url, distiller_data->distilled_values[page])); | 338 distiller_page, url, |
| 339 distiller_data->distilled_values[page].get())); |
| 339 } | 340 } |
| 340 } | 341 } |
| 341 return std::unique_ptr<DistillerPage>(distiller_page); | 342 return std::unique_ptr<DistillerPage>(distiller_page); |
| 342 } | 343 } |
| 343 | 344 |
| 344 TEST_F(DistillerTest, DistillPage) { | 345 TEST_F(DistillerTest, DistillPage) { |
| 345 base::MessageLoopForUI loop; | 346 base::MessageLoopForUI loop; |
| 346 std::unique_ptr<base::Value> result = | 347 std::unique_ptr<base::Value> result = |
| 347 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 348 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
| 348 distiller_.reset( | 349 distiller_.reset( |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 | 486 |
| 486 // Note: Next page url of the last page of article is set. So distiller will | 487 // Note: Next page url of the last page of article is set. So distiller will |
| 487 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not | 488 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not |
| 488 // work. | 489 // work. |
| 489 std::unique_ptr<base::Value> last_page_data = | 490 std::unique_ptr<base::Value> last_page_data = |
| 490 CreateDistilledValueReturnedFromJS( | 491 CreateDistilledValueReturnedFromJS( |
| 491 kTitle, distiller_data->content[kMaxPagesInArticle - 1], | 492 kTitle, distiller_data->content[kMaxPagesInArticle - 1], |
| 492 vector<int>(), "", distiller_data->page_urls[kMaxPagesInArticle - 2]); | 493 vector<int>(), "", distiller_data->page_urls[kMaxPagesInArticle - 2]); |
| 493 | 494 |
| 494 distiller_data->distilled_values.pop_back(); | 495 distiller_data->distilled_values.pop_back(); |
| 495 distiller_data->distilled_values.push_back(last_page_data.release()); | 496 distiller_data->distilled_values.push_back(std::move(last_page_data)); |
| 496 | 497 |
| 497 distiller_.reset( | 498 distiller_.reset( |
| 498 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 499 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 499 | 500 |
| 500 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 501 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
| 501 | 502 |
| 502 DistillPage( | 503 DistillPage( |
| 503 distiller_data->page_urls[0], | 504 distiller_data->page_urls[0], |
| 504 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); | 505 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); |
| 505 base::RunLoop().RunUntilIdle(); | 506 base::RunLoop().RunUntilIdle(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 std::unique_ptr<MultipageDistillerData> distiller_data = | 548 std::unique_ptr<MultipageDistillerData> distiller_data = |
| 548 CreateMultipageDistillerDataWithoutImages(kNumPages); | 549 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 549 | 550 |
| 550 // The page number of the failed page. | 551 // The page number of the failed page. |
| 551 size_t failed_page_num = 3; | 552 size_t failed_page_num = 3; |
| 552 // reset distilled data of the failed page. | 553 // reset distilled data of the failed page. |
| 553 distiller_data->distilled_values.erase( | 554 distiller_data->distilled_values.erase( |
| 554 distiller_data->distilled_values.begin() + failed_page_num); | 555 distiller_data->distilled_values.begin() + failed_page_num); |
| 555 distiller_data->distilled_values.insert( | 556 distiller_data->distilled_values.insert( |
| 556 distiller_data->distilled_values.begin() + failed_page_num, | 557 distiller_data->distilled_values.begin() + failed_page_num, |
| 557 base::Value::CreateNullValue().release()); | 558 base::Value::CreateNullValue()); |
| 558 // Expect only calls till the failed page number. | 559 // Expect only calls till the failed page number. |
| 559 distiller_.reset( | 560 distiller_.reset( |
| 560 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 561 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 561 DistillPage( | 562 DistillPage( |
| 562 distiller_data->page_urls[0], | 563 distiller_data->page_urls[0], |
| 563 CreateMockDistillerPages(distiller_data.get(), failed_page_num + 1, 0)); | 564 CreateMockDistillerPages(distiller_data.get(), failed_page_num + 1, 0)); |
| 564 base::RunLoop().RunUntilIdle(); | 565 base::RunLoop().RunUntilIdle(); |
| 565 EXPECT_EQ(kTitle, article_proto_->title()); | 566 EXPECT_EQ(kTitle, article_proto_->title()); |
| 566 VerifyArticleProtoMatchesMultipageData( | 567 VerifyArticleProtoMatchesMultipageData( |
| 567 article_proto_.get(), distiller_data.get(), failed_page_num, kNumPages); | 568 article_proto_.get(), distiller_data.get(), failed_page_num, kNumPages); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 578 distiller_data->content[empty_page_num] = ""; | 579 distiller_data->content[empty_page_num] = ""; |
| 579 std::unique_ptr<base::Value> distilled_value = | 580 std::unique_ptr<base::Value> distilled_value = |
| 580 CreateDistilledValueReturnedFromJS(kTitle, "", vector<int>(), | 581 CreateDistilledValueReturnedFromJS(kTitle, "", vector<int>(), |
| 581 GenerateNextPageUrl(kURL, empty_page_num, kNumPages), | 582 GenerateNextPageUrl(kURL, empty_page_num, kNumPages), |
| 582 GeneratePrevPageUrl(kURL, empty_page_num)); | 583 GeneratePrevPageUrl(kURL, empty_page_num)); |
| 583 // Reset distilled data of the first page. | 584 // Reset distilled data of the first page. |
| 584 distiller_data->distilled_values.erase( | 585 distiller_data->distilled_values.erase( |
| 585 distiller_data->distilled_values.begin() + empty_page_num); | 586 distiller_data->distilled_values.begin() + empty_page_num); |
| 586 distiller_data->distilled_values.insert( | 587 distiller_data->distilled_values.insert( |
| 587 distiller_data->distilled_values.begin() + empty_page_num, | 588 distiller_data->distilled_values.begin() + empty_page_num, |
| 588 distilled_value.release()); | 589 std::move(distilled_value)); |
| 589 | 590 |
| 590 distiller_.reset( | 591 distiller_.reset( |
| 591 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 592 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 592 DistillPage(distiller_data->page_urls[0], | 593 DistillPage(distiller_data->page_urls[0], |
| 593 CreateMockDistillerPages(distiller_data.get(), 1, 0)); | 594 CreateMockDistillerPages(distiller_data.get(), 1, 0)); |
| 594 base::RunLoop().RunUntilIdle(); | 595 base::RunLoop().RunUntilIdle(); |
| 595 // If the first page has no content, stop fetching the next page. | 596 // If the first page has no content, stop fetching the next page. |
| 596 EXPECT_EQ(1, article_proto_->pages_size()); | 597 EXPECT_EQ(1, article_proto_->pages_size()); |
| 597 VerifyArticleProtoMatchesMultipageData( | 598 VerifyArticleProtoMatchesMultipageData( |
| 598 article_proto_.get(), distiller_data.get(), 1, 1); | 599 article_proto_.get(), distiller_data.get(), 1, 1); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 609 distiller_data->content[empty_page_num] = ""; | 610 distiller_data->content[empty_page_num] = ""; |
| 610 std::unique_ptr<base::Value> distilled_value = | 611 std::unique_ptr<base::Value> distilled_value = |
| 611 CreateDistilledValueReturnedFromJS(kTitle, "", vector<int>(), | 612 CreateDistilledValueReturnedFromJS(kTitle, "", vector<int>(), |
| 612 GenerateNextPageUrl(kURL, empty_page_num, kNumPages), | 613 GenerateNextPageUrl(kURL, empty_page_num, kNumPages), |
| 613 GeneratePrevPageUrl(kURL, empty_page_num)); | 614 GeneratePrevPageUrl(kURL, empty_page_num)); |
| 614 // Reset distilled data of the second page. | 615 // Reset distilled data of the second page. |
| 615 distiller_data->distilled_values.erase( | 616 distiller_data->distilled_values.erase( |
| 616 distiller_data->distilled_values.begin() + empty_page_num); | 617 distiller_data->distilled_values.begin() + empty_page_num); |
| 617 distiller_data->distilled_values.insert( | 618 distiller_data->distilled_values.insert( |
| 618 distiller_data->distilled_values.begin() + empty_page_num, | 619 distiller_data->distilled_values.begin() + empty_page_num, |
| 619 distilled_value.release()); | 620 std::move(distilled_value)); |
| 620 | 621 |
| 621 distiller_.reset( | 622 distiller_.reset( |
| 622 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 623 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 623 DistillPage(distiller_data->page_urls[0], | 624 DistillPage(distiller_data->page_urls[0], |
| 624 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0)); | 625 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0)); |
| 625 base::RunLoop().RunUntilIdle(); | 626 base::RunLoop().RunUntilIdle(); |
| 626 | 627 |
| 627 VerifyArticleProtoMatchesMultipageData( | 628 VerifyArticleProtoMatchesMultipageData( |
| 628 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); | 629 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
| 629 } | 630 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 | 753 |
| 753 ASSERT_TRUE(distiller_page); | 754 ASSERT_TRUE(distiller_page); |
| 754 // Post the task to execute javascript and then delete the distiller. | 755 // Post the task to execute javascript and then delete the distiller. |
| 755 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); | 756 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); |
| 756 distiller_.reset(); | 757 distiller_.reset(); |
| 757 | 758 |
| 758 base::RunLoop().RunUntilIdle(); | 759 base::RunLoop().RunUntilIdle(); |
| 759 } | 760 } |
| 760 | 761 |
| 761 } // namespace dom_distiller | 762 } // namespace dom_distiller |
| OLD | NEW |