Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: components/dom_distiller/core/distiller_unittest.cc

Issue 678543002: add pagination info to DistilledPageProto (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(const std::string& url_prefix, size_t page_num,
141 size_t pages_size) {
142 return page_num + 1 < pages_size ?
143 url_prefix + base::IntToString(page_num + 1) : "";
144 }
145
146 string GeneratePrevPageUrl(const std::string& url_prefix, size_t page_num) {
147 return page_num > 0 ? url_prefix + base::IntToString(page_num - 1) : "";
148 }
149
140 scoped_ptr<MultipageDistillerData> CreateMultipageDistillerDataWithoutImages( 150 scoped_ptr<MultipageDistillerData> CreateMultipageDistillerDataWithoutImages(
141 size_t pages_size) { 151 size_t pages_size) {
142 scoped_ptr<MultipageDistillerData> result(new MultipageDistillerData()); 152 scoped_ptr<MultipageDistillerData> result(new MultipageDistillerData());
143 string url_prefix = "http://a.com/"; 153 string url_prefix = kURL;
144 for (size_t page_num = 0; page_num < pages_size; ++page_num) { 154 for (size_t page_num = 0; page_num < pages_size; ++page_num) {
145 result->page_urls.push_back(url_prefix + base::IntToString(page_num)); 155 result->page_urls.push_back(url_prefix + base::IntToString(page_num));
146 result->content.push_back("Content for page:" + 156 result->content.push_back("Content for page:" +
147 base::IntToString(page_num)); 157 base::IntToString(page_num));
148 result->image_ids.push_back(vector<int>()); 158 result->image_ids.push_back(vector<int>());
149 string next_page_url = (page_num + 1 < pages_size) 159 string next_page_url =
150 ? url_prefix + base::IntToString(page_num + 1) 160 GenerateNextPageUrl(url_prefix, page_num, pages_size);
151 : "";
152 string prev_page_url = 161 string prev_page_url =
153 (page_num > 0) ? result->page_urls[page_num - 1] : ""; 162 GeneratePrevPageUrl(url_prefix, page_num);
154 scoped_ptr<base::Value> distilled_value = 163 scoped_ptr<base::Value> distilled_value =
155 CreateDistilledValueReturnedFromJS(kTitle, 164 CreateDistilledValueReturnedFromJS(kTitle,
156 result->content[page_num], 165 result->content[page_num],
157 result->image_ids[page_num], 166 result->image_ids[page_num],
158 next_page_url, 167 next_page_url,
159 prev_page_url); 168 prev_page_url);
160 result->distilled_values.push_back(distilled_value.release()); 169 result->distilled_values.push_back(distilled_value.release());
161 } 170 }
162 return result.Pass(); 171 return result.Pass();
163 } 172 }
164 173
165 void VerifyArticleProtoMatchesMultipageData( 174 void VerifyArticleProtoMatchesMultipageData(
166 const dom_distiller::DistilledArticleProto* article_proto, 175 const dom_distiller::DistilledArticleProto* article_proto,
167 const MultipageDistillerData* distiller_data, 176 const MultipageDistillerData* distiller_data,
168 size_t pages_size) { 177 size_t distilled_pages_size,
169 ASSERT_EQ(pages_size, static_cast<size_t>(article_proto->pages_size())); 178 size_t total_pages_size) {
179 ASSERT_EQ(distilled_pages_size,
180 static_cast<size_t>(article_proto->pages_size()));
170 EXPECT_EQ(kTitle, article_proto->title()); 181 EXPECT_EQ(kTitle, article_proto->title());
171 for (size_t page_num = 0; page_num < pages_size; ++page_num) { 182 std::string url_prefix = kURL;
183 for (size_t page_num = 0; page_num < distilled_pages_size; ++page_num) {
172 const dom_distiller::DistilledPageProto& page = 184 const dom_distiller::DistilledPageProto& page =
173 article_proto->pages(page_num); 185 article_proto->pages(page_num);
174 EXPECT_EQ(distiller_data->content[page_num], page.html()); 186 EXPECT_EQ(distiller_data->content[page_num], page.html());
175 EXPECT_EQ(distiller_data->page_urls[page_num], page.url()); 187 EXPECT_EQ(distiller_data->page_urls[page_num], page.url());
176 EXPECT_EQ(distiller_data->image_ids[page_num].size(), 188 EXPECT_EQ(distiller_data->image_ids[page_num].size(),
177 static_cast<size_t>(page.image_size())); 189 static_cast<size_t>(page.image_size()));
178 const vector<int>& image_ids_for_page = distiller_data->image_ids[page_num]; 190 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) { 191 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]], 192 EXPECT_EQ(kImageData[image_ids_for_page[img_num]],
181 page.image(img_num).data()); 193 page.image(img_num).data());
182 EXPECT_EQ(GetImageName(page_num + 1, img_num), 194 EXPECT_EQ(GetImageName(page_num + 1, img_num),
183 page.image(img_num).name()); 195 page.image(img_num).name());
184 } 196 }
197 std::string expected_next_page_url =
198 GenerateNextPageUrl(url_prefix, page_num, total_pages_size);
199 std::string expected_prev_page_url =
200 GeneratePrevPageUrl(url_prefix, page_num);
201 EXPECT_EQ(expected_next_page_url, page.pagination_info().next_page());
202 EXPECT_EQ(expected_prev_page_url, page.pagination_info().prev_page());
203 EXPECT_FALSE(page.pagination_info().has_canonical_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
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
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
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
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
OLDNEW
« no previous file with comments | « components/dom_distiller/core/distiller.cc ('k') | components/dom_distiller/core/proto/distilled_page.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698