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 <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using std::vector; | 24 using std::vector; |
25 using std::string; | 25 using std::string; |
26 using::testing::Invoke; | 26 using::testing::Invoke; |
27 using::testing::Return; | 27 using::testing::Return; |
28 using::testing::_; | 28 using::testing::_; |
29 | 29 |
30 namespace { | 30 namespace { |
| 31 const size_t kMaxPagesInArticle = 10; |
| 32 |
31 const char kTitle[] = "Title"; | 33 const char kTitle[] = "Title"; |
32 const char kContent[] = "Content"; | 34 const char kContent[] = "Content"; |
33 const char kURL[] = "http://a.com/"; | 35 const char kURL[] = "http://a.com/"; |
34 const size_t kTotalImages = 2; | 36 const size_t kTotalImages = 2; |
35 const char* kImageURLs[kTotalImages] = {"http://a.com/img1.jpg", | 37 const char* kImageURLs[kTotalImages] = {"http://a.com/img1.jpg", |
36 "http://a.com/img2.jpg"}; | 38 "http://a.com/img2.jpg"}; |
37 const char* kImageData[kTotalImages] = {"abcde", "12345"}; | 39 const char* kImageData[kTotalImages] = {"abcde", "12345"}; |
38 | 40 |
39 const string GetImageName(int page_num, int image_num) { | 41 const string GetImageName(int page_num, int image_num) { |
40 return base::IntToString(page_num) + "_" + base::IntToString(image_num); | 42 return base::IntToString(page_num) + "_" + base::IntToString(image_num); |
41 } | 43 } |
42 | 44 |
43 scoped_ptr<base::ListValue> CreateDistilledValueReturnedFromJS( | 45 scoped_ptr<base::ListValue> CreateDistilledValueReturnedFromJS( |
44 const string& title, | 46 const string& title, |
45 const string& content, | 47 const string& content, |
46 const vector<int>& image_indices, | 48 const vector<int>& image_indices, |
47 const string& next_page_url) { | 49 const string& next_page_url) { |
48 scoped_ptr<base::ListValue> list(new base::ListValue()); | 50 scoped_ptr<base::ListValue> list(new base::ListValue()); |
49 | 51 |
50 list->AppendString(title); | 52 list->AppendString(title); |
51 list->AppendString(content); | 53 list->AppendString(content); |
52 list->AppendString(next_page_url); | 54 list->AppendString(next_page_url); |
53 for (size_t i = 0; i < image_indices.size(); ++i) { | 55 for (size_t i = 0; i < image_indices.size(); ++i) { |
54 list->AppendString(kImageURLs[image_indices[i]]); | 56 list->AppendString(kImageURLs[image_indices[i]]); |
55 } | 57 } |
56 return list.Pass(); | 58 return list.Pass(); |
57 } | 59 } |
58 | 60 |
| 61 class TestDistillerWithMaxPageLimit : public dom_distiller::DistillerImpl { |
| 62 public: |
| 63 TestDistillerWithMaxPageLimit( |
| 64 const dom_distiller::DistillerPageFactory& distiller_page_factory, |
| 65 const dom_distiller::DistillerURLFetcherFactory& |
| 66 distiller_url_fetcher_factory) |
| 67 : DistillerImpl(distiller_page_factory, distiller_url_fetcher_factory) { |
| 68 } |
| 69 |
| 70 protected: |
| 71 virtual size_t GetMaxNumPagesInArticle() const OVERRIDE { |
| 72 return kMaxPagesInArticle; |
| 73 } |
| 74 }; |
| 75 |
59 } // namespace | 76 } // namespace |
60 | 77 |
61 namespace dom_distiller { | 78 namespace dom_distiller { |
62 | 79 |
63 class TestDistillerURLFetcher : public DistillerURLFetcher { | 80 class TestDistillerURLFetcher : public DistillerURLFetcher { |
64 public: | 81 public: |
65 TestDistillerURLFetcher() : DistillerURLFetcher(NULL) { | 82 TestDistillerURLFetcher() : DistillerURLFetcher(NULL) { |
66 responses_[kImageURLs[0]] = string(kImageData[0]); | 83 responses_[kImageURLs[0]] = string(kImageData[0]); |
67 responses_[kImageURLs[1]] = string(kImageData[1]); | 84 responses_[kImageURLs[1]] = string(kImageData[1]); |
68 } | 85 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 MOCK_CONST_METHOD1( | 127 MOCK_CONST_METHOD1( |
111 CreateDistillerPageMock, | 128 CreateDistillerPageMock, |
112 DistillerPage*(DistillerPage::Delegate* delegate)); | 129 DistillerPage*(DistillerPage::Delegate* delegate)); |
113 | 130 |
114 virtual scoped_ptr<DistillerPage> CreateDistillerPage( | 131 virtual scoped_ptr<DistillerPage> CreateDistillerPage( |
115 DistillerPage::Delegate* delegate) const OVERRIDE { | 132 DistillerPage::Delegate* delegate) const OVERRIDE { |
116 return scoped_ptr<DistillerPage>(CreateDistillerPageMock(delegate)); | 133 return scoped_ptr<DistillerPage>(CreateDistillerPageMock(delegate)); |
117 } | 134 } |
118 }; | 135 }; |
119 | 136 |
120 | |
121 class DistillerTest : public testing::Test { | 137 class DistillerTest : public testing::Test { |
122 public: | 138 public: |
123 virtual ~DistillerTest() {} | 139 virtual ~DistillerTest() {} |
124 void OnDistillPageDone(scoped_ptr<DistilledArticleProto> proto) { | 140 void OnDistillPageDone(scoped_ptr<DistilledArticleProto> proto) { |
125 article_proto_ = proto.Pass(); | 141 article_proto_ = proto.Pass(); |
126 } | 142 } |
127 | 143 |
128 protected: | 144 protected: |
129 scoped_ptr<DistillerImpl> distiller_; | 145 scoped_ptr<DistillerImpl> distiller_; |
130 scoped_ptr<DistilledArticleProto> article_proto_; | 146 scoped_ptr<DistilledArticleProto> article_proto_; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 299 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
284 distiller_->Init(); | 300 distiller_->Init(); |
285 distiller_->DistillPage( | 301 distiller_->DistillPage( |
286 GURL(kURL), | 302 GURL(kURL), |
287 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | 303 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); |
288 base::MessageLoop::current()->RunUntilIdle(); | 304 base::MessageLoop::current()->RunUntilIdle(); |
289 EXPECT_EQ(kTitle, article_proto_->title()); | 305 EXPECT_EQ(kTitle, article_proto_->title()); |
290 EXPECT_EQ(article_proto_->pages_size(), 1); | 306 EXPECT_EQ(article_proto_->pages_size(), 1); |
291 } | 307 } |
292 | 308 |
| 309 TEST_F(DistillerTest, CheckMaxPageLimit) { |
| 310 base::MessageLoopForUI loop; |
| 311 string page_urls[kMaxPagesInArticle]; |
| 312 scoped_ptr<base::ListValue> list[kMaxPagesInArticle]; |
| 313 |
| 314 // Note: Next page url of the last page of article is set. So distiller will |
| 315 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not |
| 316 // work. |
| 317 string url_prefix = "http://a.com/"; |
| 318 for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) { |
| 319 page_urls[page_num] = url_prefix + base::IntToString(page_num + 1); |
| 320 string content = "Content for page:" + base::IntToString(page_num); |
| 321 string next_page_url = url_prefix + base::IntToString(page_num + 2); |
| 322 list[page_num] = CreateDistilledValueReturnedFromJS( |
| 323 kTitle, content, vector<int>(), next_page_url); |
| 324 } |
| 325 |
| 326 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
| 327 .WillOnce(CreateMockDistillerPages( |
| 328 list, page_urls, static_cast<int>(kMaxPagesInArticle))); |
| 329 |
| 330 distiller_.reset( |
| 331 new TestDistillerWithMaxPageLimit(page_factory_, url_fetcher_factory_)); |
| 332 distiller_->Init(); |
| 333 distiller_->DistillPage( |
| 334 GURL(page_urls[0]), |
| 335 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); |
| 336 base::MessageLoop::current()->RunUntilIdle(); |
| 337 EXPECT_EQ(kTitle, article_proto_->title()); |
| 338 EXPECT_EQ(kMaxPagesInArticle, |
| 339 static_cast<size_t>(article_proto_->pages_size())); |
| 340 } |
| 341 |
| 342 TEST_F(DistillerTest, SinglePageDistillationFailure) { |
| 343 base::MessageLoopForUI loop; |
| 344 // To simulate failure set the value returned as |
| 345 scoped_ptr<base::Value> emptyValue(base::Value::CreateNullValue()); |
| 346 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
| 347 .WillOnce(CreateMockDistillerPage(emptyValue.get(), GURL(kURL))); |
| 348 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
| 349 distiller_->Init(); |
| 350 distiller_->DistillPage( |
| 351 GURL(kURL), |
| 352 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); |
| 353 base::MessageLoop::current()->RunUntilIdle(); |
| 354 EXPECT_EQ("", article_proto_->title()); |
| 355 EXPECT_EQ(0, article_proto_->pages_size()); |
| 356 } |
| 357 |
| 358 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { |
| 359 base::MessageLoopForUI loop; |
| 360 const int kNumPages = 8; |
| 361 string content[kNumPages]; |
| 362 string page_urls[kNumPages]; |
| 363 scoped_ptr<base::Value> distilled_values[kNumPages]; |
| 364 // The page number of the failed page. |
| 365 int failed_page_no = 3; |
| 366 string url_prefix = "http://a.com/"; |
| 367 for (int page_no = 0; page_no < kNumPages; ++page_no) { |
| 368 page_urls[page_no] = url_prefix + base::IntToString(page_no); |
| 369 content[page_no] = "Content for page:" + base::IntToString(page_no); |
| 370 string next_page_url = url_prefix + base::IntToString(page_no + 1); |
| 371 if (page_no != failed_page_no) { |
| 372 distilled_values[page_no] = CreateDistilledValueReturnedFromJS( |
| 373 kTitle, content[page_no], vector<int>(), next_page_url); |
| 374 } else { |
| 375 distilled_values[page_no].reset(base::Value::CreateNullValue()); |
| 376 } |
| 377 } |
| 378 |
| 379 // Expect only calls till the failed page number. |
| 380 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
| 381 .WillOnce(CreateMockDistillerPages( |
| 382 distilled_values, page_urls, failed_page_no + 1)); |
| 383 |
| 384 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
| 385 distiller_->Init(); |
| 386 distiller_->DistillPage( |
| 387 GURL(page_urls[0]), |
| 388 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); |
| 389 base::MessageLoop::current()->RunUntilIdle(); |
| 390 EXPECT_EQ(kTitle, article_proto_->title()); |
| 391 EXPECT_EQ(article_proto_->pages_size(), failed_page_no); |
| 392 for (int page_no = 0; page_no < failed_page_no; ++page_no) { |
| 393 const DistilledPageProto& page = article_proto_->pages(page_no); |
| 394 EXPECT_EQ(content[page_no], page.html()); |
| 395 EXPECT_EQ(page_urls[page_no], page.url()); |
| 396 } |
| 397 } |
| 398 |
293 } // namespace dom_distiller | 399 } // namespace dom_distiller |
OLD | NEW |