Chromium Code Reviews| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <map> | 6 #include <map> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 for (size_t j = 0; j < update.GetPagesSize(); ++j) { | 130 for (size_t j = 0; j < update.GetPagesSize(); ++j) { |
| 131 int actual_page_num = expected_page_nums_in_update[j]; | 131 int actual_page_num = expected_page_nums_in_update[j]; |
| 132 EXPECT_EQ(distiller_data->page_urls[actual_page_num], | 132 EXPECT_EQ(distiller_data->page_urls[actual_page_num], |
| 133 update.GetDistilledPage(j).url()); | 133 update.GetDistilledPage(j).url()); |
| 134 EXPECT_EQ(distiller_data->content[actual_page_num], | 134 EXPECT_EQ(distiller_data->content[actual_page_num], |
| 135 update.GetDistilledPage(j).html()); | 135 update.GetDistilledPage(j).html()); |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 | 139 |
| 140 string GenerateNextPageUrl(size_t page_num, size_t pages_size, | |
| 141 string url_prefix) { | |
|
nyquist
2014/10/24 20:04:02
How about using const reference to url_prefix here
kuan
2014/10/24 20:57:32
Done. of course, too much java recently and forgo
| |
| 142 return page_num + 1 < pages_size ? | |
| 143 url_prefix + base::IntToString(page_num + 1) : ""; | |
| 144 } | |
| 145 | |
| 146 string GeneratePrevPageUrl(size_t page_num, size_t pages_size, | |
|
nyquist
2014/10/24 20:04:02
It seems like pages_size might not be needed for t
kuan
2014/10/24 20:57:32
Done.
| |
| 147 string url_prefix) { | |
| 148 return page_num > 0 ? url_prefix + base::IntToString(page_num - 1) : ""; | |
| 149 } | |
| 150 | |
| 140 scoped_ptr<MultipageDistillerData> CreateMultipageDistillerDataWithoutImages( | 151 scoped_ptr<MultipageDistillerData> CreateMultipageDistillerDataWithoutImages( |
| 141 size_t pages_size) { | 152 size_t pages_size) { |
| 142 scoped_ptr<MultipageDistillerData> result(new MultipageDistillerData()); | 153 scoped_ptr<MultipageDistillerData> result(new MultipageDistillerData()); |
| 143 string url_prefix = "http://a.com/"; | 154 string url_prefix = kURL; |
| 144 for (size_t page_num = 0; page_num < pages_size; ++page_num) { | 155 for (size_t page_num = 0; page_num < pages_size; ++page_num) { |
| 145 result->page_urls.push_back(url_prefix + base::IntToString(page_num)); | 156 result->page_urls.push_back(url_prefix + base::IntToString(page_num)); |
| 146 result->content.push_back("Content for page:" + | 157 result->content.push_back("Content for page:" + |
| 147 base::IntToString(page_num)); | 158 base::IntToString(page_num)); |
| 148 result->image_ids.push_back(vector<int>()); | 159 result->image_ids.push_back(vector<int>()); |
| 149 string next_page_url = (page_num + 1 < pages_size) | 160 string next_page_url = |
| 150 ? url_prefix + base::IntToString(page_num + 1) | 161 GenerateNextPageUrl(page_num, pages_size, url_prefix); |
| 151 : ""; | |
| 152 string prev_page_url = | 162 string prev_page_url = |
| 153 (page_num > 0) ? result->page_urls[page_num - 1] : ""; | 163 GeneratePrevPageUrl(page_num, pages_size, url_prefix); |
|
nyquist
2014/10/24 20:04:02
If you end up removing pages_size here, maybe move
kuan
2014/10/24 20:57:32
Done.
| |
| 154 scoped_ptr<base::Value> distilled_value = | 164 scoped_ptr<base::Value> distilled_value = |
| 155 CreateDistilledValueReturnedFromJS(kTitle, | 165 CreateDistilledValueReturnedFromJS(kTitle, |
| 156 result->content[page_num], | 166 result->content[page_num], |
| 157 result->image_ids[page_num], | 167 result->image_ids[page_num], |
| 158 next_page_url, | 168 next_page_url, |
| 159 prev_page_url); | 169 prev_page_url); |
| 160 result->distilled_values.push_back(distilled_value.release()); | 170 result->distilled_values.push_back(distilled_value.release()); |
| 161 } | 171 } |
| 162 return result.Pass(); | 172 return result.Pass(); |
| 163 } | 173 } |
| 164 | 174 |
| 165 void VerifyArticleProtoMatchesMultipageData( | 175 void VerifyArticleProtoMatchesMultipageData( |
| 166 const dom_distiller::DistilledArticleProto* article_proto, | 176 const dom_distiller::DistilledArticleProto* article_proto, |
| 167 const MultipageDistillerData* distiller_data, | 177 const MultipageDistillerData* distiller_data, |
| 168 size_t pages_size) { | 178 size_t distilled_pages_size, |
| 169 ASSERT_EQ(pages_size, static_cast<size_t>(article_proto->pages_size())); | 179 size_t total_pages_size) { |
| 180 ASSERT_EQ(distilled_pages_size, | |
| 181 static_cast<size_t>(article_proto->pages_size())); | |
| 170 EXPECT_EQ(kTitle, article_proto->title()); | 182 EXPECT_EQ(kTitle, article_proto->title()); |
| 171 for (size_t page_num = 0; page_num < pages_size; ++page_num) { | 183 string url_prefix = kURL; |
| 184 for (size_t page_num = 0; page_num < distilled_pages_size; ++page_num) { | |
| 172 const dom_distiller::DistilledPageProto& page = | 185 const dom_distiller::DistilledPageProto& page = |
| 173 article_proto->pages(page_num); | 186 article_proto->pages(page_num); |
| 174 EXPECT_EQ(distiller_data->content[page_num], page.html()); | 187 EXPECT_EQ(distiller_data->content[page_num], page.html()); |
| 175 EXPECT_EQ(distiller_data->page_urls[page_num], page.url()); | 188 EXPECT_EQ(distiller_data->page_urls[page_num], page.url()); |
| 176 EXPECT_EQ(distiller_data->image_ids[page_num].size(), | 189 EXPECT_EQ(distiller_data->image_ids[page_num].size(), |
| 177 static_cast<size_t>(page.image_size())); | 190 static_cast<size_t>(page.image_size())); |
| 178 const vector<int>& image_ids_for_page = distiller_data->image_ids[page_num]; | 191 const vector<int>& image_ids_for_page = distiller_data->image_ids[page_num]; |
| 179 for (size_t img_num = 0; img_num < image_ids_for_page.size(); ++img_num) { | 192 for (size_t img_num = 0; img_num < image_ids_for_page.size(); ++img_num) { |
| 180 EXPECT_EQ(kImageData[image_ids_for_page[img_num]], | 193 EXPECT_EQ(kImageData[image_ids_for_page[img_num]], |
| 181 page.image(img_num).data()); | 194 page.image(img_num).data()); |
| 182 EXPECT_EQ(GetImageName(page_num + 1, img_num), | 195 EXPECT_EQ(GetImageName(page_num + 1, img_num), |
| 183 page.image(img_num).name()); | 196 page.image(img_num).name()); |
| 184 } | 197 } |
| 198 string expected_next_page_url = | |
| 199 GenerateNextPageUrl(page_num, total_pages_size, url_prefix); | |
| 200 string expected_prev_page_url = | |
| 201 GeneratePrevPageUrl(page_num, total_pages_size, url_prefix); | |
| 202 EXPECT_EQ(expected_next_page_url, page.pagination_info().next_page()); | |
| 203 EXPECT_EQ(expected_prev_page_url, page.pagination_info().prev_page()); | |
| 185 } | 204 } |
| 186 } | 205 } |
| 187 | 206 |
| 188 } // namespace | 207 } // namespace |
| 189 | 208 |
| 190 namespace dom_distiller { | 209 namespace dom_distiller { |
| 191 | 210 |
| 192 using test::MockDistillerPage; | 211 using test::MockDistillerPage; |
| 193 using test::MockDistillerPageFactory; | 212 using test::MockDistillerPageFactory; |
| 194 | 213 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 383 distiller_data->image_ids.push_back(image_indices); | 402 distiller_data->image_ids.push_back(image_indices); |
| 384 } | 403 } |
| 385 | 404 |
| 386 distiller_.reset( | 405 distiller_.reset( |
| 387 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 406 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 388 DistillPage( | 407 DistillPage( |
| 389 distiller_data->page_urls[0], | 408 distiller_data->page_urls[0], |
| 390 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0).Pass()); | 409 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0).Pass()); |
| 391 base::MessageLoop::current()->RunUntilIdle(); | 410 base::MessageLoop::current()->RunUntilIdle(); |
| 392 VerifyArticleProtoMatchesMultipageData( | 411 VerifyArticleProtoMatchesMultipageData( |
| 393 article_proto_.get(), distiller_data.get(), kNumPages); | 412 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
| 394 } | 413 } |
| 395 | 414 |
| 396 TEST_F(DistillerTest, DistillLinkLoop) { | 415 TEST_F(DistillerTest, DistillLinkLoop) { |
| 397 base::MessageLoopForUI loop; | 416 base::MessageLoopForUI loop; |
| 398 // Create a loop, the next page is same as the current page. This could | 417 // Create a loop, the next page is same as the current page. This could |
| 399 // happen if javascript misparses a next page link. | 418 // happen if javascript misparses a next page link. |
| 400 scoped_ptr<base::Value> result = | 419 scoped_ptr<base::Value> result = |
| 401 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); | 420 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); |
| 402 distiller_.reset( | 421 distiller_.reset( |
| 403 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 422 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 491 base::Value::CreateNullValue()); | 510 base::Value::CreateNullValue()); |
| 492 // Expect only calls till the failed page number. | 511 // Expect only calls till the failed page number. |
| 493 distiller_.reset( | 512 distiller_.reset( |
| 494 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 513 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 495 DistillPage(distiller_data->page_urls[0], | 514 DistillPage(distiller_data->page_urls[0], |
| 496 CreateMockDistillerPages( | 515 CreateMockDistillerPages( |
| 497 distiller_data.get(), failed_page_num + 1, 0).Pass()); | 516 distiller_data.get(), failed_page_num + 1, 0).Pass()); |
| 498 base::MessageLoop::current()->RunUntilIdle(); | 517 base::MessageLoop::current()->RunUntilIdle(); |
| 499 EXPECT_EQ(kTitle, article_proto_->title()); | 518 EXPECT_EQ(kTitle, article_proto_->title()); |
| 500 VerifyArticleProtoMatchesMultipageData( | 519 VerifyArticleProtoMatchesMultipageData( |
| 501 article_proto_.get(), distiller_data.get(), failed_page_num); | 520 article_proto_.get(), distiller_data.get(), failed_page_num, kNumPages); |
| 502 } | 521 } |
| 503 | 522 |
| 504 TEST_F(DistillerTest, DistillPreviousPage) { | 523 TEST_F(DistillerTest, DistillPreviousPage) { |
| 505 base::MessageLoopForUI loop; | 524 base::MessageLoopForUI loop; |
| 506 const size_t kNumPages = 8; | 525 const size_t kNumPages = 8; |
| 507 | 526 |
| 508 // The page number of the article on which distillation starts. | 527 // The page number of the article on which distillation starts. |
| 509 int start_page_num = 3; | 528 int start_page_num = 3; |
| 510 scoped_ptr<MultipageDistillerData> distiller_data = | 529 scoped_ptr<MultipageDistillerData> distiller_data = |
| 511 CreateMultipageDistillerDataWithoutImages(kNumPages); | 530 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 512 | 531 |
| 513 distiller_.reset( | 532 distiller_.reset( |
| 514 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); | 533 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 515 DistillPage(distiller_data->page_urls[start_page_num], | 534 DistillPage(distiller_data->page_urls[start_page_num], |
| 516 CreateMockDistillerPages( | 535 CreateMockDistillerPages( |
| 517 distiller_data.get(), kNumPages, start_page_num).Pass()); | 536 distiller_data.get(), kNumPages, start_page_num).Pass()); |
| 518 base::MessageLoop::current()->RunUntilIdle(); | 537 base::MessageLoop::current()->RunUntilIdle(); |
| 519 VerifyArticleProtoMatchesMultipageData( | 538 VerifyArticleProtoMatchesMultipageData( |
| 520 article_proto_.get(), distiller_data.get(), kNumPages); | 539 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
| 521 } | 540 } |
| 522 | 541 |
| 523 TEST_F(DistillerTest, IncrementalUpdates) { | 542 TEST_F(DistillerTest, IncrementalUpdates) { |
| 524 base::MessageLoopForUI loop; | 543 base::MessageLoopForUI loop; |
| 525 const size_t kNumPages = 8; | 544 const size_t kNumPages = 8; |
| 526 | 545 |
| 527 // The page number of the article on which distillation starts. | 546 // The page number of the article on which distillation starts. |
| 528 int start_page_num = 3; | 547 int start_page_num = 3; |
| 529 scoped_ptr<MultipageDistillerData> distiller_data = | 548 scoped_ptr<MultipageDistillerData> distiller_data = |
| 530 CreateMultipageDistillerDataWithoutImages(kNumPages); | 549 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 555 DistillPage(distiller_data->page_urls[start_page_num], | 574 DistillPage(distiller_data->page_urls[start_page_num], |
| 556 CreateMockDistillerPages( | 575 CreateMockDistillerPages( |
| 557 distiller_data.get(), kNumPages, start_page_num).Pass()); | 576 distiller_data.get(), kNumPages, start_page_num).Pass()); |
| 558 base::MessageLoop::current()->RunUntilIdle(); | 577 base::MessageLoop::current()->RunUntilIdle(); |
| 559 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 578 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
| 560 | 579 |
| 561 in_sequence_updates_.clear(); | 580 in_sequence_updates_.clear(); |
| 562 | 581 |
| 563 // Should still be able to access article and pages. | 582 // Should still be able to access article and pages. |
| 564 VerifyArticleProtoMatchesMultipageData( | 583 VerifyArticleProtoMatchesMultipageData( |
| 565 article_proto_.get(), distiller_data.get(), kNumPages); | 584 article_proto_.get(), distiller_data.get(), kNumPages, kNumPages); |
| 566 } | 585 } |
| 567 | 586 |
| 568 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { | 587 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { |
| 569 base::MessageLoopForUI loop; | 588 base::MessageLoopForUI loop; |
| 570 const size_t kNumPages = 8; | 589 const size_t kNumPages = 8; |
| 571 scoped_ptr<MultipageDistillerData> distiller_data = | 590 scoped_ptr<MultipageDistillerData> distiller_data = |
| 572 CreateMultipageDistillerDataWithoutImages(kNumPages); | 591 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 573 // The page number of the article on which distillation starts. | 592 // The page number of the article on which distillation starts. |
| 574 int start_page_num = 3; | 593 int start_page_num = 3; |
| 575 | 594 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 626 | 645 |
| 627 ASSERT_TRUE(distiller_page); | 646 ASSERT_TRUE(distiller_page); |
| 628 // Post the task to execute javascript and then delete the distiller. | 647 // Post the task to execute javascript and then delete the distiller. |
| 629 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); | 648 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); |
| 630 distiller_.reset(); | 649 distiller_.reset(); |
| 631 | 650 |
| 632 base::MessageLoop::current()->RunUntilIdle(); | 651 base::MessageLoop::current()->RunUntilIdle(); |
| 633 } | 652 } |
| 634 | 653 |
| 635 } // namespace dom_distiller | 654 } // namespace dom_distiller |
| OLD | NEW |