Chromium Code Reviews| Index: components/dom_distiller/core/distiller_unittest.cc |
| diff --git a/components/dom_distiller/core/distiller_unittest.cc b/components/dom_distiller/core/distiller_unittest.cc |
| index 3865aefa4d60fbcec500aa27e4df8a1fdf89fb8f..16b8d23a31d34bb1f8404dc681bdb7b730e96c71 100644 |
| --- a/components/dom_distiller/core/distiller_unittest.cc |
| +++ b/components/dom_distiller/core/distiller_unittest.cc |
| @@ -28,6 +28,8 @@ using::testing::Return; |
| using::testing::_; |
| namespace { |
| +const size_t kMaxPagesInArticle = 10; |
|
cjhopman
2014/02/15 02:44:15
nit: this can just be moved into the CheckMaxPageL
shashi
2014/02/15 03:15:36
Done.
|
| + |
| const char kTitle[] = "Title"; |
| const char kContent[] = "Content"; |
| const char kURL[] = "http://a.com/"; |
| @@ -117,7 +119,6 @@ class MockDistillerPageFactory : public DistillerPageFactory { |
| } |
| }; |
| - |
| class DistillerTest : public testing::Test { |
| public: |
| virtual ~DistillerTest() {} |
| @@ -290,4 +291,96 @@ TEST_F(DistillerTest, DistillLinkLoop) { |
| EXPECT_EQ(article_proto_->pages_size(), 1); |
| } |
| +TEST_F(DistillerTest, CheckMaxPageLimit) { |
|
cjhopman
2014/02/15 02:44:15
Nit: maybe test that an article that is exactly th
shashi
2014/02/15 03:15:36
Done.
|
| + base::MessageLoopForUI loop; |
| + string page_urls[kMaxPagesInArticle]; |
| + scoped_ptr<base::ListValue> list[kMaxPagesInArticle]; |
| + |
| + // Note: Next page url of the last page of article is set. So distiller will |
| + // try to do kMaxPagesInArticle + 1 calls if the max article limit does not |
| + // work. |
| + string url_prefix = "http://a.com/"; |
| + for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) { |
| + page_urls[page_num] = url_prefix + base::IntToString(page_num + 1); |
| + string content = "Content for page:" + base::IntToString(page_num); |
| + string next_page_url = url_prefix + base::IntToString(page_num + 2); |
| + list[page_num] = CreateDistilledValueReturnedFromJS( |
| + kTitle, content, vector<int>(), next_page_url); |
| + } |
| + |
| + EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
| + .WillOnce(CreateMockDistillerPages( |
| + list, page_urls, static_cast<int>(kMaxPagesInArticle))); |
| + |
| + distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
| + |
| + distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
| + |
| + distiller_->Init(); |
| + distiller_->DistillPage( |
| + GURL(page_urls[0]), |
| + base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_EQ(kTitle, article_proto_->title()); |
| + EXPECT_EQ(kMaxPagesInArticle, |
| + static_cast<size_t>(article_proto_->pages_size())); |
| +} |
| + |
| +TEST_F(DistillerTest, SinglePageDistillationFailure) { |
| + base::MessageLoopForUI loop; |
| + // To simulate failure return a null value. |
| + scoped_ptr<base::Value> nullValue(base::Value::CreateNullValue()); |
| + EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
| + .WillOnce(CreateMockDistillerPage(nullValue.get(), GURL(kURL))); |
| + distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
| + distiller_->Init(); |
| + distiller_->DistillPage( |
| + GURL(kURL), |
| + base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_EQ("", article_proto_->title()); |
| + EXPECT_EQ(0, article_proto_->pages_size()); |
| +} |
| + |
| +TEST_F(DistillerTest, MultiplePagesDistillationFailure) { |
| + base::MessageLoopForUI loop; |
| + const int kNumPages = 8; |
| + string content[kNumPages]; |
| + string page_urls[kNumPages]; |
| + scoped_ptr<base::Value> distilled_values[kNumPages]; |
| + // The page number of the failed page. |
| + int failed_page_num = 3; |
| + string url_prefix = "http://a.com/"; |
| + for (int page_num = 0; page_num < kNumPages; ++page_num) { |
| + page_urls[page_num] = url_prefix + base::IntToString(page_num); |
| + content[page_num] = "Content for page:" + base::IntToString(page_num); |
| + string next_page_url = url_prefix + base::IntToString(page_num + 1); |
| + if (page_num != failed_page_num) { |
| + distilled_values[page_num] = CreateDistilledValueReturnedFromJS( |
| + kTitle, content[page_num], vector<int>(), next_page_url); |
| + } else { |
| + distilled_values[page_num].reset(base::Value::CreateNullValue()); |
| + } |
| + } |
| + |
| + // Expect only calls till the failed page number. |
| + EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
| + .WillOnce(CreateMockDistillerPages( |
| + distilled_values, page_urls, failed_page_num + 1)); |
| + |
| + distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
| + distiller_->Init(); |
| + distiller_->DistillPage( |
| + GURL(page_urls[0]), |
| + base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); |
| + base::MessageLoop::current()->RunUntilIdle(); |
| + EXPECT_EQ(kTitle, article_proto_->title()); |
| + EXPECT_EQ(article_proto_->pages_size(), failed_page_num); |
| + for (int page_num = 0; page_num < failed_page_num; ++page_num) { |
| + const DistilledPageProto& page = article_proto_->pages(page_num); |
| + EXPECT_EQ(content[page_num], page.html()); |
| + EXPECT_EQ(page_urls[page_num], page.url()); |
| + } |
| +} |
| + |
| } // namespace dom_distiller |