| 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" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/path_service.h" |
| 15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "components/dom_distiller/core/article_distillation_update.h" | 18 #include "components/dom_distiller/core/article_distillation_update.h" |
| 18 #include "components/dom_distiller/core/distiller.h" | 19 #include "components/dom_distiller/core/distiller.h" |
| 19 #include "components/dom_distiller/core/distiller_page.h" | 20 #include "components/dom_distiller/core/distiller_page.h" |
| 20 #include "components/dom_distiller/core/fake_distiller_page.h" | 21 #include "components/dom_distiller/core/fake_distiller_page.h" |
| 21 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 22 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 22 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 23 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
| 23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/dom_distiller_js/dom_distiller.pb.h" | 27 #include "third_party/dom_distiller_js/dom_distiller.pb.h" |
| 27 #include "third_party/dom_distiller_js/dom_distiller_json_converter.h" | 28 #include "third_party/dom_distiller_js/dom_distiller_json_converter.h" |
| 29 #include "ui/base/resource/resource_bundle.h" |
| 28 | 30 |
| 29 using std::vector; | 31 using std::vector; |
| 30 using std::string; | 32 using std::string; |
| 31 using ::testing::Invoke; | 33 using ::testing::Invoke; |
| 32 using ::testing::Return; | 34 using ::testing::Return; |
| 33 using ::testing::_; | 35 using ::testing::_; |
| 34 | 36 |
| 37 using dom_distiller::proto::DomDistillerOptions; |
| 38 using dom_distiller::proto::DomDistillerResult; |
| 39 |
| 35 namespace { | 40 namespace { |
| 36 const char kTitle[] = "Title"; | 41 const char kTitle[] = "Title"; |
| 37 const char kContent[] = "Content"; | 42 const char kContent[] = "Content"; |
| 38 const char kURL[] = "http://a.com/"; | 43 const char kURL[] = "http://a.com/"; |
| 39 const size_t kTotalImages = 2; | 44 const size_t kTotalImages = 2; |
| 40 const char* kImageURLs[kTotalImages] = {"http://a.com/img1.jpg", | 45 const char* kImageURLs[kTotalImages] = {"http://a.com/img1.jpg", |
| 41 "http://a.com/img2.jpg"}; | 46 "http://a.com/img2.jpg"}; |
| 42 const char* kImageData[kTotalImages] = {"abcde", "12345"}; | 47 const char* kImageData[kTotalImages] = {"abcde", "12345"}; |
| 43 | 48 |
| 44 const string GetImageName(int page_num, int image_num) { | 49 const string GetImageName(int page_num, int image_num) { |
| 45 return base::IntToString(page_num) + "_" + base::IntToString(image_num); | 50 return base::IntToString(page_num) + "_" + base::IntToString(image_num); |
| 46 } | 51 } |
| 47 | 52 |
| 48 scoped_ptr<base::Value> CreateDistilledValueReturnedFromJS( | 53 scoped_ptr<base::Value> CreateDistilledValueReturnedFromJS( |
| 49 const string& title, | 54 const string& title, |
| 50 const string& content, | 55 const string& content, |
| 51 const vector<int>& image_indices, | 56 const vector<int>& image_indices, |
| 52 const string& next_page_url, | 57 const string& next_page_url, |
| 53 const string& prev_page_url = "") { | 58 const string& prev_page_url = "") { |
| 54 dom_distiller::proto::DomDistillerResult result; | 59 DomDistillerResult result; |
| 55 result.set_title(title); | 60 result.set_title(title); |
| 56 result.mutable_distilled_content()->set_html(content); | 61 result.mutable_distilled_content()->set_html(content); |
| 57 result.mutable_pagination_info()->set_next_page(next_page_url); | 62 result.mutable_pagination_info()->set_next_page(next_page_url); |
| 58 result.mutable_pagination_info()->set_prev_page(prev_page_url); | 63 result.mutable_pagination_info()->set_prev_page(prev_page_url); |
| 59 | 64 |
| 60 for (size_t i = 0; i < image_indices.size(); ++i) { | 65 for (size_t i = 0; i < image_indices.size(); ++i) { |
| 61 result.add_image_urls(kImageURLs[image_indices[i]]); | 66 result.add_image_urls(kImageURLs[image_indices[i]]); |
| 62 } | 67 } |
| 63 | 68 |
| 64 return dom_distiller::proto::json::DomDistillerResult::WriteToValue(result); | 69 return dom_distiller::proto::json::DomDistillerResult::WriteToValue(result); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 const vector<int>& image_ids_for_page = distiller_data->image_ids[page_num]; | 179 const vector<int>& image_ids_for_page = distiller_data->image_ids[page_num]; |
| 175 for (size_t img_num = 0; img_num < image_ids_for_page.size(); ++img_num) { | 180 for (size_t img_num = 0; img_num < image_ids_for_page.size(); ++img_num) { |
| 176 EXPECT_EQ(kImageData[image_ids_for_page[img_num]], | 181 EXPECT_EQ(kImageData[image_ids_for_page[img_num]], |
| 177 page.image(img_num).data()); | 182 page.image(img_num).data()); |
| 178 EXPECT_EQ(GetImageName(page_num + 1, img_num), | 183 EXPECT_EQ(GetImageName(page_num + 1, img_num), |
| 179 page.image(img_num).name()); | 184 page.image(img_num).name()); |
| 180 } | 185 } |
| 181 } | 186 } |
| 182 } | 187 } |
| 183 | 188 |
| 189 void AddComponentsResources() { |
| 190 base::FilePath pak_file; |
| 191 base::FilePath pak_dir; |
| 192 PathService::Get(base::DIR_MODULE, &pak_dir); |
| 193 pak_file = pak_dir.Append(FILE_PATH_LITERAL("components_resources.pak")); |
| 194 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| 195 pak_file, ui::SCALE_FACTOR_NONE); |
| 196 } |
| 197 |
| 184 } // namespace | 198 } // namespace |
| 185 | 199 |
| 186 namespace dom_distiller { | 200 namespace dom_distiller { |
| 187 | 201 |
| 188 using test::MockDistillerPage; | 202 using test::MockDistillerPage; |
| 189 using test::MockDistillerPageFactory; | 203 using test::MockDistillerPageFactory; |
| 190 | 204 |
| 191 class TestDistillerURLFetcher : public DistillerURLFetcher { | 205 class TestDistillerURLFetcher : public DistillerURLFetcher { |
| 192 public: | 206 public: |
| 193 explicit TestDistillerURLFetcher(bool delay_fetch) | 207 explicit TestDistillerURLFetcher(bool delay_fetch) |
| 194 : DistillerURLFetcher(NULL), delay_fetch_(delay_fetch) { | 208 : DistillerURLFetcher(NULL), delay_fetch_(delay_fetch) { |
| 195 responses_[kImageURLs[0]] = string(kImageData[0]); | 209 responses_[kImageURLs[0]] = string(kImageData[0]); |
| 196 responses_[kImageURLs[1]] = string(kImageData[1]); | 210 responses_[kImageURLs[1]] = string(kImageData[1]); |
| 197 } | 211 } |
| 198 | 212 |
| 199 virtual void FetchURL(const string& url, | 213 virtual void FetchURL(const string& url, |
| 200 const URLFetcherCallback& callback) OVERRIDE { | 214 const URLFetcherCallback& callback) OVERRIDE { |
| 201 DCHECK(callback_.is_null()); | 215 ASSERT_FALSE(callback.is_null()); |
| 202 url_ = url; | 216 url_ = url; |
| 203 callback_ = callback; | 217 callback_ = callback; |
| 204 if (!delay_fetch_) { | 218 if (!delay_fetch_) { |
| 205 PostCallbackTask(); | 219 PostCallbackTask(); |
| 206 } | 220 } |
| 207 } | 221 } |
| 208 | 222 |
| 209 void PostCallbackTask() { | 223 void PostCallbackTask() { |
| 210 ASSERT_TRUE(base::MessageLoop::current()); | 224 ASSERT_TRUE(base::MessageLoop::current()); |
| 225 ASSERT_FALSE(callback_.is_null()); |
| 211 base::MessageLoop::current()->PostTask( | 226 base::MessageLoop::current()->PostTask( |
| 212 FROM_HERE, base::Bind(callback_, responses_[url_])); | 227 FROM_HERE, base::Bind(callback_, responses_[url_])); |
| 213 } | 228 } |
| 214 | 229 |
| 215 private: | 230 private: |
| 216 std::map<string, string> responses_; | 231 std::map<string, string> responses_; |
| 217 string url_; | 232 string url_; |
| 218 URLFetcherCallback callback_; | 233 URLFetcherCallback callback_; |
| 219 bool delay_fetch_; | 234 bool delay_fetch_; |
| 220 }; | 235 }; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 234 MockDistillerURLFetcherFactory() : DistillerURLFetcherFactory(NULL) {} | 249 MockDistillerURLFetcherFactory() : DistillerURLFetcherFactory(NULL) {} |
| 235 virtual ~MockDistillerURLFetcherFactory() {} | 250 virtual ~MockDistillerURLFetcherFactory() {} |
| 236 | 251 |
| 237 MOCK_CONST_METHOD0(CreateDistillerURLFetcher, DistillerURLFetcher*()); | 252 MOCK_CONST_METHOD0(CreateDistillerURLFetcher, DistillerURLFetcher*()); |
| 238 }; | 253 }; |
| 239 | 254 |
| 240 class DistillerTest : public testing::Test { | 255 class DistillerTest : public testing::Test { |
| 241 public: | 256 public: |
| 242 virtual ~DistillerTest() {} | 257 virtual ~DistillerTest() {} |
| 243 | 258 |
| 259 virtual void SetUp() OVERRIDE { |
| 260 AddComponentsResources(); |
| 261 } |
| 262 |
| 244 void OnDistillArticleDone(scoped_ptr<DistilledArticleProto> proto) { | 263 void OnDistillArticleDone(scoped_ptr<DistilledArticleProto> proto) { |
| 245 article_proto_ = proto.Pass(); | 264 article_proto_ = proto.Pass(); |
| 246 } | 265 } |
| 247 | 266 |
| 248 void OnDistillArticleUpdate(const ArticleDistillationUpdate& article_update) { | 267 void OnDistillArticleUpdate(const ArticleDistillationUpdate& article_update) { |
| 249 in_sequence_updates_.push_back(article_update); | 268 in_sequence_updates_.push_back(article_update); |
| 250 } | 269 } |
| 251 | 270 |
| 252 void DistillPage(const std::string& url, | 271 void DistillPage(const std::string& url, |
| 253 scoped_ptr<DistillerPage> distiller_page) { | 272 scoped_ptr<DistillerPage> distiller_page) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 distiller_page, url, distiller_data->distilled_values[page])); | 323 distiller_page, url, distiller_data->distilled_values[page])); |
| 305 } | 324 } |
| 306 } | 325 } |
| 307 return scoped_ptr<DistillerPage>(distiller_page).Pass(); | 326 return scoped_ptr<DistillerPage>(distiller_page).Pass(); |
| 308 } | 327 } |
| 309 | 328 |
| 310 TEST_F(DistillerTest, DistillPage) { | 329 TEST_F(DistillerTest, DistillPage) { |
| 311 base::MessageLoopForUI loop; | 330 base::MessageLoopForUI loop; |
| 312 scoped_ptr<base::Value> result = | 331 scoped_ptr<base::Value> result = |
| 313 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 332 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
| 314 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 333 distiller_.reset( |
| 334 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 315 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL)).Pass()); | 335 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL)).Pass()); |
| 316 base::MessageLoop::current()->RunUntilIdle(); | 336 base::MessageLoop::current()->RunUntilIdle(); |
| 317 EXPECT_EQ(kTitle, article_proto_->title()); | 337 EXPECT_EQ(kTitle, article_proto_->title()); |
| 318 EXPECT_EQ(article_proto_->pages_size(), 1); | 338 EXPECT_EQ(article_proto_->pages_size(), 1); |
| 319 const DistilledPageProto& first_page = article_proto_->pages(0); | 339 const DistilledPageProto& first_page = article_proto_->pages(0); |
| 320 EXPECT_EQ(kContent, first_page.html()); | 340 EXPECT_EQ(kContent, first_page.html()); |
| 321 EXPECT_EQ(kURL, first_page.url()); | 341 EXPECT_EQ(kURL, first_page.url()); |
| 322 } | 342 } |
| 323 | 343 |
| 324 TEST_F(DistillerTest, DistillPageWithImages) { | 344 TEST_F(DistillerTest, DistillPageWithImages) { |
| 325 base::MessageLoopForUI loop; | 345 base::MessageLoopForUI loop; |
| 326 vector<int> image_indices; | 346 vector<int> image_indices; |
| 327 image_indices.push_back(0); | 347 image_indices.push_back(0); |
| 328 image_indices.push_back(1); | 348 image_indices.push_back(1); |
| 329 scoped_ptr<base::Value> result = | 349 scoped_ptr<base::Value> result = |
| 330 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); | 350 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); |
| 331 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 351 distiller_.reset( |
| 352 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 332 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL)).Pass()); | 353 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL)).Pass()); |
| 333 base::MessageLoop::current()->RunUntilIdle(); | 354 base::MessageLoop::current()->RunUntilIdle(); |
| 334 EXPECT_EQ(kTitle, article_proto_->title()); | 355 EXPECT_EQ(kTitle, article_proto_->title()); |
| 335 EXPECT_EQ(article_proto_->pages_size(), 1); | 356 EXPECT_EQ(article_proto_->pages_size(), 1); |
| 336 const DistilledPageProto& first_page = article_proto_->pages(0); | 357 const DistilledPageProto& first_page = article_proto_->pages(0); |
| 337 EXPECT_EQ(kContent, first_page.html()); | 358 EXPECT_EQ(kContent, first_page.html()); |
| 338 EXPECT_EQ(kURL, first_page.url()); | 359 EXPECT_EQ(kURL, first_page.url()); |
| 339 EXPECT_EQ(2, first_page.image_size()); | 360 EXPECT_EQ(2, first_page.image_size()); |
| 340 EXPECT_EQ(kImageData[0], first_page.image(0).data()); | 361 EXPECT_EQ(kImageData[0], first_page.image(0).data()); |
| 341 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); | 362 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 355 // Each page has different number of images. | 376 // Each page has different number of images. |
| 356 size_t tot_images = (page_num + kTotalImages) % (kTotalImages + 1); | 377 size_t tot_images = (page_num + kTotalImages) % (kTotalImages + 1); |
| 357 vector<int> image_indices; | 378 vector<int> image_indices; |
| 358 for (size_t img_num = 0; img_num < tot_images; img_num++) { | 379 for (size_t img_num = 0; img_num < tot_images; img_num++) { |
| 359 image_indices.push_back(next_image_number); | 380 image_indices.push_back(next_image_number); |
| 360 next_image_number = (next_image_number + 1) % kTotalImages; | 381 next_image_number = (next_image_number + 1) % kTotalImages; |
| 361 } | 382 } |
| 362 distiller_data->image_ids.push_back(image_indices); | 383 distiller_data->image_ids.push_back(image_indices); |
| 363 } | 384 } |
| 364 | 385 |
| 365 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 386 distiller_.reset( |
| 387 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 366 DistillPage( | 388 DistillPage( |
| 367 distiller_data->page_urls[0], | 389 distiller_data->page_urls[0], |
| 368 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0).Pass()); | 390 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0).Pass()); |
| 369 base::MessageLoop::current()->RunUntilIdle(); | 391 base::MessageLoop::current()->RunUntilIdle(); |
| 370 VerifyArticleProtoMatchesMultipageData( | 392 VerifyArticleProtoMatchesMultipageData( |
| 371 article_proto_.get(), distiller_data.get(), kNumPages); | 393 article_proto_.get(), distiller_data.get(), kNumPages); |
| 372 } | 394 } |
| 373 | 395 |
| 374 TEST_F(DistillerTest, DistillLinkLoop) { | 396 TEST_F(DistillerTest, DistillLinkLoop) { |
| 375 base::MessageLoopForUI loop; | 397 base::MessageLoopForUI loop; |
| 376 // Create a loop, the next page is same as the current page. This could | 398 // Create a loop, the next page is same as the current page. This could |
| 377 // happen if javascript misparses a next page link. | 399 // happen if javascript misparses a next page link. |
| 378 scoped_ptr<base::Value> result = | 400 scoped_ptr<base::Value> result = |
| 379 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); | 401 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); |
| 380 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 402 distiller_.reset( |
| 403 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 381 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL)).Pass()); | 404 DistillPage(kURL, CreateMockDistillerPage(result.get(), GURL(kURL)).Pass()); |
| 382 base::MessageLoop::current()->RunUntilIdle(); | 405 base::MessageLoop::current()->RunUntilIdle(); |
| 383 EXPECT_EQ(kTitle, article_proto_->title()); | 406 EXPECT_EQ(kTitle, article_proto_->title()); |
| 384 EXPECT_EQ(article_proto_->pages_size(), 1); | 407 EXPECT_EQ(article_proto_->pages_size(), 1); |
| 385 } | 408 } |
| 386 | 409 |
| 387 TEST_F(DistillerTest, CheckMaxPageLimitExtraPage) { | 410 TEST_F(DistillerTest, CheckMaxPageLimitExtraPage) { |
| 388 base::MessageLoopForUI loop; | 411 base::MessageLoopForUI loop; |
| 389 const size_t kMaxPagesInArticle = 10; | 412 const size_t kMaxPagesInArticle = 10; |
| 390 scoped_ptr<MultipageDistillerData> distiller_data = | 413 scoped_ptr<MultipageDistillerData> distiller_data = |
| 391 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); | 414 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); |
| 392 | 415 |
| 393 // Note: Next page url of the last page of article is set. So distiller will | 416 // Note: Next page url of the last page of article is set. So distiller will |
| 394 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not | 417 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not |
| 395 // work. | 418 // work. |
| 396 scoped_ptr<base::Value> last_page_data = | 419 scoped_ptr<base::Value> last_page_data = |
| 397 CreateDistilledValueReturnedFromJS( | 420 CreateDistilledValueReturnedFromJS( |
| 398 kTitle, | 421 kTitle, |
| 399 distiller_data->content[kMaxPagesInArticle - 1], | 422 distiller_data->content[kMaxPagesInArticle - 1], |
| 400 vector<int>(), | 423 vector<int>(), |
| 401 "", | 424 "", |
| 402 distiller_data->page_urls[kMaxPagesInArticle - 2]); | 425 distiller_data->page_urls[kMaxPagesInArticle - 2]); |
| 403 | 426 |
| 404 distiller_data->distilled_values.pop_back(); | 427 distiller_data->distilled_values.pop_back(); |
| 405 distiller_data->distilled_values.push_back(last_page_data.release()); | 428 distiller_data->distilled_values.push_back(last_page_data.release()); |
| 406 | 429 |
| 407 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 430 distiller_.reset( |
| 431 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 408 | 432 |
| 409 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 433 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
| 410 | 434 |
| 411 DistillPage(distiller_data->page_urls[0], | 435 DistillPage(distiller_data->page_urls[0], |
| 412 CreateMockDistillerPages( | 436 CreateMockDistillerPages( |
| 413 distiller_data.get(), kMaxPagesInArticle, 0).Pass()); | 437 distiller_data.get(), kMaxPagesInArticle, 0).Pass()); |
| 414 base::MessageLoop::current()->RunUntilIdle(); | 438 base::MessageLoop::current()->RunUntilIdle(); |
| 415 EXPECT_EQ(kTitle, article_proto_->title()); | 439 EXPECT_EQ(kTitle, article_proto_->title()); |
| 416 EXPECT_EQ(kMaxPagesInArticle, | 440 EXPECT_EQ(kMaxPagesInArticle, |
| 417 static_cast<size_t>(article_proto_->pages_size())); | 441 static_cast<size_t>(article_proto_->pages_size())); |
| 418 } | 442 } |
| 419 | 443 |
| 420 TEST_F(DistillerTest, CheckMaxPageLimitExactLimit) { | 444 TEST_F(DistillerTest, CheckMaxPageLimitExactLimit) { |
| 421 base::MessageLoopForUI loop; | 445 base::MessageLoopForUI loop; |
| 422 const size_t kMaxPagesInArticle = 10; | 446 const size_t kMaxPagesInArticle = 10; |
| 423 scoped_ptr<MultipageDistillerData> distiller_data = | 447 scoped_ptr<MultipageDistillerData> distiller_data = |
| 424 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); | 448 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); |
| 425 | 449 |
| 426 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 450 distiller_.reset( |
| 451 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 427 | 452 |
| 428 // Check if distilling an article with exactly the page limit works. | 453 // Check if distilling an article with exactly the page limit works. |
| 429 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 454 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
| 430 | 455 |
| 431 DistillPage(distiller_data->page_urls[0], | 456 DistillPage(distiller_data->page_urls[0], |
| 432 CreateMockDistillerPages( | 457 CreateMockDistillerPages( |
| 433 distiller_data.get(), kMaxPagesInArticle, 0).Pass()); | 458 distiller_data.get(), kMaxPagesInArticle, 0).Pass()); |
| 434 base::MessageLoop::current()->RunUntilIdle(); | 459 base::MessageLoop::current()->RunUntilIdle(); |
| 435 EXPECT_EQ(kTitle, article_proto_->title()); | 460 EXPECT_EQ(kTitle, article_proto_->title()); |
| 436 EXPECT_EQ(kMaxPagesInArticle, | 461 EXPECT_EQ(kMaxPagesInArticle, |
| 437 static_cast<size_t>(article_proto_->pages_size())); | 462 static_cast<size_t>(article_proto_->pages_size())); |
| 438 } | 463 } |
| 439 | 464 |
| 440 TEST_F(DistillerTest, SinglePageDistillationFailure) { | 465 TEST_F(DistillerTest, SinglePageDistillationFailure) { |
| 441 base::MessageLoopForUI loop; | 466 base::MessageLoopForUI loop; |
| 442 // To simulate failure return a null value. | 467 // To simulate failure return a null value. |
| 443 scoped_ptr<base::Value> nullValue(base::Value::CreateNullValue()); | 468 scoped_ptr<base::Value> nullValue(base::Value::CreateNullValue()); |
| 444 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 469 distiller_.reset( |
| 470 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 445 DistillPage(kURL, | 471 DistillPage(kURL, |
| 446 CreateMockDistillerPage(nullValue.get(), GURL(kURL)).Pass()); | 472 CreateMockDistillerPage(nullValue.get(), GURL(kURL)).Pass()); |
| 447 base::MessageLoop::current()->RunUntilIdle(); | 473 base::MessageLoop::current()->RunUntilIdle(); |
| 448 EXPECT_EQ("", article_proto_->title()); | 474 EXPECT_EQ("", article_proto_->title()); |
| 449 EXPECT_EQ(0, article_proto_->pages_size()); | 475 EXPECT_EQ(0, article_proto_->pages_size()); |
| 450 } | 476 } |
| 451 | 477 |
| 452 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { | 478 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { |
| 453 base::MessageLoopForUI loop; | 479 base::MessageLoopForUI loop; |
| 454 const size_t kNumPages = 8; | 480 const size_t kNumPages = 8; |
| 455 scoped_ptr<MultipageDistillerData> distiller_data = | 481 scoped_ptr<MultipageDistillerData> distiller_data = |
| 456 CreateMultipageDistillerDataWithoutImages(kNumPages); | 482 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 457 | 483 |
| 458 // The page number of the failed page. | 484 // The page number of the failed page. |
| 459 size_t failed_page_num = 3; | 485 size_t failed_page_num = 3; |
| 460 // reset distilled data of the failed page. | 486 // reset distilled data of the failed page. |
| 461 distiller_data->distilled_values.erase( | 487 distiller_data->distilled_values.erase( |
| 462 distiller_data->distilled_values.begin() + failed_page_num); | 488 distiller_data->distilled_values.begin() + failed_page_num); |
| 463 distiller_data->distilled_values.insert( | 489 distiller_data->distilled_values.insert( |
| 464 distiller_data->distilled_values.begin() + failed_page_num, | 490 distiller_data->distilled_values.begin() + failed_page_num, |
| 465 base::Value::CreateNullValue()); | 491 base::Value::CreateNullValue()); |
| 466 // Expect only calls till the failed page number. | 492 // Expect only calls till the failed page number. |
| 467 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 493 distiller_.reset( |
| 494 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 468 DistillPage(distiller_data->page_urls[0], | 495 DistillPage(distiller_data->page_urls[0], |
| 469 CreateMockDistillerPages( | 496 CreateMockDistillerPages( |
| 470 distiller_data.get(), failed_page_num + 1, 0).Pass()); | 497 distiller_data.get(), failed_page_num + 1, 0).Pass()); |
| 471 base::MessageLoop::current()->RunUntilIdle(); | 498 base::MessageLoop::current()->RunUntilIdle(); |
| 472 EXPECT_EQ(kTitle, article_proto_->title()); | 499 EXPECT_EQ(kTitle, article_proto_->title()); |
| 473 VerifyArticleProtoMatchesMultipageData( | 500 VerifyArticleProtoMatchesMultipageData( |
| 474 article_proto_.get(), distiller_data.get(), failed_page_num); | 501 article_proto_.get(), distiller_data.get(), failed_page_num); |
| 475 } | 502 } |
| 476 | 503 |
| 477 TEST_F(DistillerTest, DistillPreviousPage) { | 504 TEST_F(DistillerTest, DistillPreviousPage) { |
| 478 base::MessageLoopForUI loop; | 505 base::MessageLoopForUI loop; |
| 479 const size_t kNumPages = 8; | 506 const size_t kNumPages = 8; |
| 480 | 507 |
| 481 // The page number of the article on which distillation starts. | 508 // The page number of the article on which distillation starts. |
| 482 int start_page_num = 3; | 509 int start_page_num = 3; |
| 483 scoped_ptr<MultipageDistillerData> distiller_data = | 510 scoped_ptr<MultipageDistillerData> distiller_data = |
| 484 CreateMultipageDistillerDataWithoutImages(kNumPages); | 511 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 485 | 512 |
| 486 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 513 distiller_.reset( |
| 514 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 487 DistillPage(distiller_data->page_urls[start_page_num], | 515 DistillPage(distiller_data->page_urls[start_page_num], |
| 488 CreateMockDistillerPages( | 516 CreateMockDistillerPages( |
| 489 distiller_data.get(), kNumPages, start_page_num).Pass()); | 517 distiller_data.get(), kNumPages, start_page_num).Pass()); |
| 490 base::MessageLoop::current()->RunUntilIdle(); | 518 base::MessageLoop::current()->RunUntilIdle(); |
| 491 VerifyArticleProtoMatchesMultipageData( | 519 VerifyArticleProtoMatchesMultipageData( |
| 492 article_proto_.get(), distiller_data.get(), kNumPages); | 520 article_proto_.get(), distiller_data.get(), kNumPages); |
| 493 } | 521 } |
| 494 | 522 |
| 495 TEST_F(DistillerTest, IncrementalUpdates) { | 523 TEST_F(DistillerTest, IncrementalUpdates) { |
| 496 base::MessageLoopForUI loop; | 524 base::MessageLoopForUI loop; |
| 497 const size_t kNumPages = 8; | 525 const size_t kNumPages = 8; |
| 498 | 526 |
| 499 // The page number of the article on which distillation starts. | 527 // The page number of the article on which distillation starts. |
| 500 int start_page_num = 3; | 528 int start_page_num = 3; |
| 501 scoped_ptr<MultipageDistillerData> distiller_data = | 529 scoped_ptr<MultipageDistillerData> distiller_data = |
| 502 CreateMultipageDistillerDataWithoutImages(kNumPages); | 530 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 503 | 531 |
| 504 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 532 distiller_.reset( |
| 533 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 505 DistillPage(distiller_data->page_urls[start_page_num], | 534 DistillPage(distiller_data->page_urls[start_page_num], |
| 506 CreateMockDistillerPages( | 535 CreateMockDistillerPages( |
| 507 distiller_data.get(), kNumPages, start_page_num).Pass()); | 536 distiller_data.get(), kNumPages, start_page_num).Pass()); |
| 508 base::MessageLoop::current()->RunUntilIdle(); | 537 base::MessageLoop::current()->RunUntilIdle(); |
| 509 EXPECT_EQ(kTitle, article_proto_->title()); | 538 EXPECT_EQ(kTitle, article_proto_->title()); |
| 510 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); | 539 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); |
| 511 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 540 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
| 512 | 541 |
| 513 VerifyIncrementalUpdatesMatch( | 542 VerifyIncrementalUpdatesMatch( |
| 514 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); | 543 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); |
| 515 } | 544 } |
| 516 | 545 |
| 517 TEST_F(DistillerTest, IncrementalUpdatesDoNotDeleteFinalArticle) { | 546 TEST_F(DistillerTest, IncrementalUpdatesDoNotDeleteFinalArticle) { |
| 518 base::MessageLoopForUI loop; | 547 base::MessageLoopForUI loop; |
| 519 const size_t kNumPages = 8; | 548 const size_t kNumPages = 8; |
| 520 int start_page_num = 3; | 549 int start_page_num = 3; |
| 521 scoped_ptr<MultipageDistillerData> distiller_data = | 550 scoped_ptr<MultipageDistillerData> distiller_data = |
| 522 CreateMultipageDistillerDataWithoutImages(kNumPages); | 551 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 523 | 552 |
| 524 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 553 distiller_.reset( |
| 554 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 525 DistillPage(distiller_data->page_urls[start_page_num], | 555 DistillPage(distiller_data->page_urls[start_page_num], |
| 526 CreateMockDistillerPages( | 556 CreateMockDistillerPages( |
| 527 distiller_data.get(), kNumPages, start_page_num).Pass()); | 557 distiller_data.get(), kNumPages, start_page_num).Pass()); |
| 528 base::MessageLoop::current()->RunUntilIdle(); | 558 base::MessageLoop::current()->RunUntilIdle(); |
| 529 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 559 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
| 530 | 560 |
| 531 in_sequence_updates_.clear(); | 561 in_sequence_updates_.clear(); |
| 532 | 562 |
| 533 // Should still be able to access article and pages. | 563 // Should still be able to access article and pages. |
| 534 VerifyArticleProtoMatchesMultipageData( | 564 VerifyArticleProtoMatchesMultipageData( |
| 535 article_proto_.get(), distiller_data.get(), kNumPages); | 565 article_proto_.get(), distiller_data.get(), kNumPages); |
| 536 } | 566 } |
| 537 | 567 |
| 538 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { | 568 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { |
| 539 base::MessageLoopForUI loop; | 569 base::MessageLoopForUI loop; |
| 540 const size_t kNumPages = 8; | 570 const size_t kNumPages = 8; |
| 541 scoped_ptr<MultipageDistillerData> distiller_data = | 571 scoped_ptr<MultipageDistillerData> distiller_data = |
| 542 CreateMultipageDistillerDataWithoutImages(kNumPages); | 572 CreateMultipageDistillerDataWithoutImages(kNumPages); |
| 543 // The page number of the article on which distillation starts. | 573 // The page number of the article on which distillation starts. |
| 544 int start_page_num = 3; | 574 int start_page_num = 3; |
| 545 | 575 |
| 546 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 576 distiller_.reset( |
| 577 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 547 DistillPage(distiller_data->page_urls[start_page_num], | 578 DistillPage(distiller_data->page_urls[start_page_num], |
| 548 CreateMockDistillerPages( | 579 CreateMockDistillerPages( |
| 549 distiller_data.get(), kNumPages, start_page_num).Pass()); | 580 distiller_data.get(), kNumPages, start_page_num).Pass()); |
| 550 base::MessageLoop::current()->RunUntilIdle(); | 581 base::MessageLoop::current()->RunUntilIdle(); |
| 551 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 582 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
| 552 EXPECT_EQ(kTitle, article_proto_->title()); | 583 EXPECT_EQ(kTitle, article_proto_->title()); |
| 553 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); | 584 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); |
| 554 | 585 |
| 555 // Delete the article. | 586 // Delete the article. |
| 556 article_proto_.reset(); | 587 article_proto_.reset(); |
| 557 VerifyIncrementalUpdatesMatch( | 588 VerifyIncrementalUpdatesMatch( |
| 558 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); | 589 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); |
| 559 } | 590 } |
| 560 | 591 |
| 561 TEST_F(DistillerTest, CancelWithDelayedImageFetchCallback) { | 592 TEST_F(DistillerTest, CancelWithDelayedImageFetchCallback) { |
| 562 base::MessageLoopForUI loop; | 593 base::MessageLoopForUI loop; |
| 563 vector<int> image_indices; | 594 vector<int> image_indices; |
| 564 image_indices.push_back(0); | 595 image_indices.push_back(0); |
| 565 scoped_ptr<base::Value> distilled_value = | 596 scoped_ptr<base::Value> distilled_value = |
| 566 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); | 597 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); |
| 567 TestDistillerURLFetcher* delayed_fetcher = new TestDistillerURLFetcher(true); | 598 TestDistillerURLFetcher* delayed_fetcher = new TestDistillerURLFetcher(true); |
| 568 MockDistillerURLFetcherFactory url_fetcher_factory; | 599 MockDistillerURLFetcherFactory mock_url_fetcher_factory; |
| 569 EXPECT_CALL(url_fetcher_factory, CreateDistillerURLFetcher()) | 600 EXPECT_CALL(mock_url_fetcher_factory, CreateDistillerURLFetcher()) |
| 570 .WillOnce(Return(delayed_fetcher)); | 601 .WillOnce(Return(delayed_fetcher)); |
| 571 distiller_.reset(new DistillerImpl(url_fetcher_factory)); | 602 distiller_.reset( |
| 603 new DistillerImpl(mock_url_fetcher_factory, DomDistillerOptions())); |
| 572 DistillPage( | 604 DistillPage( |
| 573 kURL, CreateMockDistillerPage(distilled_value.get(), GURL(kURL)).Pass()); | 605 kURL, CreateMockDistillerPage(distilled_value.get(), GURL(kURL)).Pass()); |
| 574 base::MessageLoop::current()->RunUntilIdle(); | 606 base::MessageLoop::current()->RunUntilIdle(); |
| 575 | 607 |
| 576 // Post callback from the url fetcher and then delete the distiller. | 608 // Post callback from the url fetcher and then delete the distiller. |
| 577 delayed_fetcher->PostCallbackTask(); | 609 delayed_fetcher->PostCallbackTask(); |
| 578 distiller_.reset(); | 610 distiller_.reset(); |
| 579 | 611 |
| 580 base::MessageLoop::current()->RunUntilIdle(); | 612 base::MessageLoop::current()->RunUntilIdle(); |
| 581 } | 613 } |
| 582 | 614 |
| 583 TEST_F(DistillerTest, CancelWithDelayedJSCallback) { | 615 TEST_F(DistillerTest, CancelWithDelayedJSCallback) { |
| 584 base::MessageLoopForUI loop; | 616 base::MessageLoopForUI loop; |
| 585 scoped_ptr<base::Value> distilled_value = | 617 scoped_ptr<base::Value> distilled_value = |
| 586 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 618 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
| 587 MockDistillerPage* distiller_page = NULL; | 619 MockDistillerPage* distiller_page = NULL; |
| 588 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); | 620 distiller_.reset( |
| 621 new DistillerImpl(url_fetcher_factory_, DomDistillerOptions())); |
| 589 DistillPage(kURL, | 622 DistillPage(kURL, |
| 590 CreateMockDistillerPageWithPendingJSCallback(&distiller_page, | 623 CreateMockDistillerPageWithPendingJSCallback(&distiller_page, |
| 591 GURL(kURL))); | 624 GURL(kURL))); |
| 592 base::MessageLoop::current()->RunUntilIdle(); | 625 base::MessageLoop::current()->RunUntilIdle(); |
| 593 | 626 |
| 594 ASSERT_TRUE(distiller_page); | 627 ASSERT_TRUE(distiller_page); |
| 595 // Post the task to execute javascript and then delete the distiller. | 628 // Post the task to execute javascript and then delete the distiller. |
| 596 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); | 629 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); |
| 597 distiller_.reset(); | 630 distiller_.reset(); |
| 598 | 631 |
| 599 base::MessageLoop::current()->RunUntilIdle(); | 632 base::MessageLoop::current()->RunUntilIdle(); |
| 600 } | 633 } |
| 601 | 634 |
| 602 } // namespace dom_distiller | 635 } // namespace dom_distiller |
| OLD | NEW |