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/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "components/dom_distiller/core/article_distillation_update.h" | 17 #include "components/dom_distiller/core/article_distillation_update.h" |
18 #include "components/dom_distiller/core/distiller.h" | 18 #include "components/dom_distiller/core/distiller.h" |
19 #include "components/dom_distiller/core/distiller_page.h" | 19 #include "components/dom_distiller/core/distiller_page.h" |
| 20 #include "components/dom_distiller/core/fake_distiller_page.h" |
20 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 21 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
21 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 22 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
22 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 using std::vector; | 27 using std::vector; |
27 using std::string; | 28 using std::string; |
28 using ::testing::Invoke; | 29 using ::testing::Invoke; |
29 using ::testing::Return; | 30 using ::testing::Return; |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 EXPECT_EQ(GetImageName(page_num + 1, img_num), | 175 EXPECT_EQ(GetImageName(page_num + 1, img_num), |
175 page.image(img_num).name()); | 176 page.image(img_num).name()); |
176 } | 177 } |
177 } | 178 } |
178 } | 179 } |
179 | 180 |
180 } // namespace | 181 } // namespace |
181 | 182 |
182 namespace dom_distiller { | 183 namespace dom_distiller { |
183 | 184 |
| 185 using test::MockDistillerPage; |
| 186 using test::MockDistillerPageFactory; |
| 187 |
184 class TestDistillerURLFetcher : public DistillerURLFetcher { | 188 class TestDistillerURLFetcher : public DistillerURLFetcher { |
185 public: | 189 public: |
186 explicit TestDistillerURLFetcher(bool delay_fetch) | 190 explicit TestDistillerURLFetcher(bool delay_fetch) |
187 : DistillerURLFetcher(NULL), delay_fetch_(delay_fetch) { | 191 : DistillerURLFetcher(NULL), delay_fetch_(delay_fetch) { |
188 responses_[kImageURLs[0]] = string(kImageData[0]); | 192 responses_[kImageURLs[0]] = string(kImageData[0]); |
189 responses_[kImageURLs[1]] = string(kImageData[1]); | 193 responses_[kImageURLs[1]] = string(kImageData[1]); |
190 } | 194 } |
191 | 195 |
192 virtual void FetchURL(const string& url, | 196 virtual void FetchURL(const string& url, |
193 const URLFetcherCallback& callback) OVERRIDE { | 197 const URLFetcherCallback& callback) OVERRIDE { |
(...skipping 23 matching lines...) Expand all Loading... |
217 TestDistillerURLFetcherFactory() : DistillerURLFetcherFactory(NULL) {} | 221 TestDistillerURLFetcherFactory() : DistillerURLFetcherFactory(NULL) {} |
218 | 222 |
219 virtual ~TestDistillerURLFetcherFactory() {} | 223 virtual ~TestDistillerURLFetcherFactory() {} |
220 virtual DistillerURLFetcher* CreateDistillerURLFetcher() const OVERRIDE { | 224 virtual DistillerURLFetcher* CreateDistillerURLFetcher() const OVERRIDE { |
221 return new TestDistillerURLFetcher(false); | 225 return new TestDistillerURLFetcher(false); |
222 } | 226 } |
223 }; | 227 }; |
224 | 228 |
225 class MockDistillerURLFetcherFactory : public DistillerURLFetcherFactory { | 229 class MockDistillerURLFetcherFactory : public DistillerURLFetcherFactory { |
226 public: | 230 public: |
227 MockDistillerURLFetcherFactory() | 231 MockDistillerURLFetcherFactory() : DistillerURLFetcherFactory(NULL) {} |
228 : DistillerURLFetcherFactory(NULL) {} | |
229 virtual ~MockDistillerURLFetcherFactory() {} | 232 virtual ~MockDistillerURLFetcherFactory() {} |
230 | 233 |
231 MOCK_CONST_METHOD0(CreateDistillerURLFetcher, DistillerURLFetcher*()); | 234 MOCK_CONST_METHOD0(CreateDistillerURLFetcher, DistillerURLFetcher*()); |
232 }; | 235 }; |
233 | 236 |
234 class MockDistillerPage : public DistillerPage { | |
235 public: | |
236 MOCK_METHOD2(DistillPageImpl, void(const GURL& gurl, const string& script)); | |
237 | |
238 MockDistillerPage() {} | |
239 }; | |
240 | |
241 class MockDistillerPageFactory : public DistillerPageFactory { | |
242 public: | |
243 MOCK_CONST_METHOD0(CreateDistillerPageMock, DistillerPage*()); | |
244 | |
245 virtual scoped_ptr<DistillerPage> CreateDistillerPage() const OVERRIDE { | |
246 return scoped_ptr<DistillerPage>(CreateDistillerPageMock()); | |
247 } | |
248 }; | |
249 | |
250 class DistillerTest : public testing::Test { | 237 class DistillerTest : public testing::Test { |
251 public: | 238 public: |
252 virtual ~DistillerTest() {} | 239 virtual ~DistillerTest() {} |
253 void OnDistillArticleDone(scoped_ptr<DistilledArticleProto> proto) { | 240 void OnDistillArticleDone(scoped_ptr<DistilledArticleProto> proto) { |
254 article_proto_ = proto.Pass(); | 241 article_proto_ = proto.Pass(); |
255 } | 242 } |
256 | 243 |
257 void OnDistillArticleUpdate(const ArticleDistillationUpdate& article_update) { | 244 void OnDistillArticleUpdate(const ArticleDistillationUpdate& article_update) { |
258 in_sequence_updates_.push_back(article_update); | 245 in_sequence_updates_.push_back(article_update); |
259 } | 246 } |
260 | 247 |
261 void DistillPage(const std::string& url) { | 248 void DistillPage(const std::string& url, |
| 249 scoped_ptr<DistillerPage> distiller_page) { |
262 distiller_->DistillPage(GURL(url), | 250 distiller_->DistillPage(GURL(url), |
| 251 distiller_page.Pass(), |
263 base::Bind(&DistillerTest::OnDistillArticleDone, | 252 base::Bind(&DistillerTest::OnDistillArticleDone, |
264 base::Unretained(this)), | 253 base::Unretained(this)), |
265 base::Bind(&DistillerTest::OnDistillArticleUpdate, | 254 base::Bind(&DistillerTest::OnDistillArticleUpdate, |
266 base::Unretained(this))); | 255 base::Unretained(this))); |
267 } | 256 } |
268 | 257 |
269 protected: | 258 protected: |
270 scoped_ptr<DistillerImpl> distiller_; | 259 scoped_ptr<DistillerImpl> distiller_; |
271 scoped_ptr<DistilledArticleProto> article_proto_; | 260 scoped_ptr<DistilledArticleProto> article_proto_; |
272 std::vector<ArticleDistillationUpdate> in_sequence_updates_; | 261 std::vector<ArticleDistillationUpdate> in_sequence_updates_; |
273 MockDistillerPageFactory page_factory_; | 262 MockDistillerPageFactory page_factory_; |
274 TestDistillerURLFetcherFactory url_fetcher_factory_; | 263 TestDistillerURLFetcherFactory url_fetcher_factory_; |
275 }; | 264 }; |
276 | 265 |
277 ACTION_P3(DistillerPageOnDistillationDone, distiller_page, url, list) { | 266 ACTION_P3(DistillerPageOnDistillationDone, distiller_page, url, list) { |
278 distiller_page->OnDistillationDone(url, list); | 267 distiller_page->OnDistillationDone(url, list); |
279 } | 268 } |
280 | 269 |
281 ACTION_P2(CreateMockDistillerPage, list, kurl) { | 270 scoped_ptr<DistillerPage> CreateMockDistillerPage(const base::Value* list, |
| 271 const GURL& url) { |
282 MockDistillerPage* distiller_page = new MockDistillerPage(); | 272 MockDistillerPage* distiller_page = new MockDistillerPage(); |
283 EXPECT_CALL(*distiller_page, DistillPageImpl(kurl, _)) | 273 EXPECT_CALL(*distiller_page, DistillPageImpl(url, _)) |
284 .WillOnce( | 274 .WillOnce(DistillerPageOnDistillationDone(distiller_page, url, list)); |
285 DistillerPageOnDistillationDone(distiller_page, kurl, list)); | 275 return scoped_ptr<DistillerPage>(distiller_page).Pass(); |
286 return distiller_page; | |
287 } | 276 } |
288 | 277 |
289 ACTION_P2(CreateMockDistillerPageWithPendingJSCallback, | 278 scoped_ptr<DistillerPage> CreateMockDistillerPageWithPendingJSCallback( |
290 distiller_page_ptr, | 279 MockDistillerPage** distiller_page_ptr, |
291 kurl) { | 280 const GURL& url) { |
292 MockDistillerPage* distiller_page = new MockDistillerPage(); | 281 MockDistillerPage* distiller_page = new MockDistillerPage(); |
293 *distiller_page_ptr = distiller_page; | 282 *distiller_page_ptr = distiller_page; |
294 EXPECT_CALL(*distiller_page, DistillPageImpl(kurl, _)); | 283 EXPECT_CALL(*distiller_page, DistillPageImpl(url, _)); |
295 return distiller_page; | 284 return scoped_ptr<DistillerPage>(distiller_page).Pass(); |
296 } | 285 } |
297 | 286 |
298 ACTION_P3(CreateMockDistillerPages, | 287 scoped_ptr<DistillerPage> CreateMockDistillerPages( |
299 distiller_data, | 288 MultipageDistillerData* distiller_data, |
300 pages_size, | 289 size_t pages_size, |
301 start_page_num) { | 290 int start_page_num) { |
302 MockDistillerPage* distiller_page = new MockDistillerPage(); | 291 MockDistillerPage* distiller_page = new MockDistillerPage(); |
303 { | 292 { |
304 testing::InSequence s; | 293 testing::InSequence s; |
305 vector<int> page_nums = GetPagesInSequence(start_page_num, pages_size); | 294 vector<int> page_nums = GetPagesInSequence(start_page_num, pages_size); |
306 for (size_t page_num = 0; page_num < pages_size; ++page_num) { | 295 for (size_t page_num = 0; page_num < pages_size; ++page_num) { |
307 int page = page_nums[page_num]; | 296 int page = page_nums[page_num]; |
308 GURL url = GURL(distiller_data->page_urls[page]); | 297 GURL url = GURL(distiller_data->page_urls[page]); |
309 EXPECT_CALL(*distiller_page, DistillPageImpl(url, _)) | 298 EXPECT_CALL(*distiller_page, DistillPageImpl(url, _)) |
310 .WillOnce(DistillerPageOnDistillationDone( | 299 .WillOnce(DistillerPageOnDistillationDone( |
311 distiller_page, url, distiller_data->distilled_values[page])); | 300 distiller_page, url, distiller_data->distilled_values[page])); |
312 } | 301 } |
313 } | 302 } |
314 return distiller_page; | 303 return scoped_ptr<DistillerPage>(distiller_page).Pass(); |
315 } | 304 } |
316 | 305 |
317 TEST_F(DistillerTest, DistillPage) { | 306 TEST_F(DistillerTest, DistillPage) { |
318 base::MessageLoopForUI loop; | 307 base::MessageLoopForUI loop; |
319 scoped_ptr<base::ListValue> list = | 308 scoped_ptr<base::ListValue> list = |
320 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 309 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
321 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 310 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
322 .WillOnce(CreateMockDistillerPage(list.get(), GURL(kURL))); | 311 DistillPage(kURL, CreateMockDistillerPage(list.get(), GURL(kURL)).Pass()); |
323 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
324 DistillPage(kURL); | |
325 base::MessageLoop::current()->RunUntilIdle(); | 312 base::MessageLoop::current()->RunUntilIdle(); |
326 EXPECT_EQ(kTitle, article_proto_->title()); | 313 EXPECT_EQ(kTitle, article_proto_->title()); |
327 EXPECT_EQ(article_proto_->pages_size(), 1); | 314 EXPECT_EQ(article_proto_->pages_size(), 1); |
328 const DistilledPageProto& first_page = article_proto_->pages(0); | 315 const DistilledPageProto& first_page = article_proto_->pages(0); |
329 EXPECT_EQ(kContent, first_page.html()); | 316 EXPECT_EQ(kContent, first_page.html()); |
330 EXPECT_EQ(kURL, first_page.url()); | 317 EXPECT_EQ(kURL, first_page.url()); |
331 } | 318 } |
332 | 319 |
333 TEST_F(DistillerTest, DistillPageWithImages) { | 320 TEST_F(DistillerTest, DistillPageWithImages) { |
334 base::MessageLoopForUI loop; | 321 base::MessageLoopForUI loop; |
335 vector<int> image_indices; | 322 vector<int> image_indices; |
336 image_indices.push_back(0); | 323 image_indices.push_back(0); |
337 image_indices.push_back(1); | 324 image_indices.push_back(1); |
338 scoped_ptr<base::ListValue> list = | 325 scoped_ptr<base::ListValue> list = |
339 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); | 326 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); |
340 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 327 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
341 .WillOnce(CreateMockDistillerPage(list.get(), GURL(kURL))); | 328 DistillPage(kURL, CreateMockDistillerPage(list.get(), GURL(kURL)).Pass()); |
342 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
343 DistillPage(kURL); | |
344 base::MessageLoop::current()->RunUntilIdle(); | 329 base::MessageLoop::current()->RunUntilIdle(); |
345 EXPECT_EQ(kTitle, article_proto_->title()); | 330 EXPECT_EQ(kTitle, article_proto_->title()); |
346 EXPECT_EQ(article_proto_->pages_size(), 1); | 331 EXPECT_EQ(article_proto_->pages_size(), 1); |
347 const DistilledPageProto& first_page = article_proto_->pages(0); | 332 const DistilledPageProto& first_page = article_proto_->pages(0); |
348 EXPECT_EQ(kContent, first_page.html()); | 333 EXPECT_EQ(kContent, first_page.html()); |
349 EXPECT_EQ(kURL, first_page.url()); | 334 EXPECT_EQ(kURL, first_page.url()); |
350 EXPECT_EQ(2, first_page.image_size()); | 335 EXPECT_EQ(2, first_page.image_size()); |
351 EXPECT_EQ(kImageData[0], first_page.image(0).data()); | 336 EXPECT_EQ(kImageData[0], first_page.image(0).data()); |
352 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); | 337 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); |
353 EXPECT_EQ(kImageData[1], first_page.image(1).data()); | 338 EXPECT_EQ(kImageData[1], first_page.image(1).data()); |
(...skipping 12 matching lines...) Expand all Loading... |
366 // Each page has different number of images. | 351 // Each page has different number of images. |
367 size_t tot_images = (page_num + kTotalImages) % (kTotalImages + 1); | 352 size_t tot_images = (page_num + kTotalImages) % (kTotalImages + 1); |
368 vector<int> image_indices; | 353 vector<int> image_indices; |
369 for (size_t img_num = 0; img_num < tot_images; img_num++) { | 354 for (size_t img_num = 0; img_num < tot_images; img_num++) { |
370 image_indices.push_back(next_image_number); | 355 image_indices.push_back(next_image_number); |
371 next_image_number = (next_image_number + 1) % kTotalImages; | 356 next_image_number = (next_image_number + 1) % kTotalImages; |
372 } | 357 } |
373 distiller_data->image_ids.push_back(image_indices); | 358 distiller_data->image_ids.push_back(image_indices); |
374 } | 359 } |
375 | 360 |
376 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 361 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
377 .WillOnce(CreateMockDistillerPages(distiller_data.get(), kNumPages, 0)); | 362 DistillPage( |
378 | 363 distiller_data->page_urls[0], |
379 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 364 CreateMockDistillerPages(distiller_data.get(), kNumPages, 0).Pass()); |
380 DistillPage(distiller_data->page_urls[0]); | |
381 base::MessageLoop::current()->RunUntilIdle(); | 365 base::MessageLoop::current()->RunUntilIdle(); |
382 VerifyArticleProtoMatchesMultipageData( | 366 VerifyArticleProtoMatchesMultipageData( |
383 article_proto_.get(), distiller_data.get(), kNumPages); | 367 article_proto_.get(), distiller_data.get(), kNumPages); |
384 } | 368 } |
385 | 369 |
386 TEST_F(DistillerTest, DistillLinkLoop) { | 370 TEST_F(DistillerTest, DistillLinkLoop) { |
387 base::MessageLoopForUI loop; | 371 base::MessageLoopForUI loop; |
388 // Create a loop, the next page is same as the current page. This could | 372 // Create a loop, the next page is same as the current page. This could |
389 // happen if javascript misparses a next page link. | 373 // happen if javascript misparses a next page link. |
390 scoped_ptr<base::ListValue> list = | 374 scoped_ptr<base::ListValue> list = |
391 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); | 375 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); |
392 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 376 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
393 .WillOnce(CreateMockDistillerPage(list.get(), GURL(kURL))); | 377 DistillPage(kURL, CreateMockDistillerPage(list.get(), GURL(kURL)).Pass()); |
394 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
395 DistillPage(kURL); | |
396 base::MessageLoop::current()->RunUntilIdle(); | 378 base::MessageLoop::current()->RunUntilIdle(); |
397 EXPECT_EQ(kTitle, article_proto_->title()); | 379 EXPECT_EQ(kTitle, article_proto_->title()); |
398 EXPECT_EQ(article_proto_->pages_size(), 1); | 380 EXPECT_EQ(article_proto_->pages_size(), 1); |
399 } | 381 } |
400 | 382 |
401 TEST_F(DistillerTest, CheckMaxPageLimitExtraPage) { | 383 TEST_F(DistillerTest, CheckMaxPageLimitExtraPage) { |
402 base::MessageLoopForUI loop; | 384 base::MessageLoopForUI loop; |
403 const size_t kMaxPagesInArticle = 10; | 385 const size_t kMaxPagesInArticle = 10; |
404 scoped_ptr<MultipageDistillerData> distiller_data = | 386 scoped_ptr<MultipageDistillerData> distiller_data = |
405 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); | 387 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); |
406 | 388 |
407 // Note: Next page url of the last page of article is set. So distiller will | 389 // Note: Next page url of the last page of article is set. So distiller will |
408 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not | 390 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not |
409 // work. | 391 // work. |
410 scoped_ptr<base::ListValue> last_page_data = | 392 scoped_ptr<base::ListValue> last_page_data = |
411 CreateDistilledValueReturnedFromJS( | 393 CreateDistilledValueReturnedFromJS( |
412 kTitle, | 394 kTitle, |
413 distiller_data->content[kMaxPagesInArticle - 1], | 395 distiller_data->content[kMaxPagesInArticle - 1], |
414 vector<int>(), | 396 vector<int>(), |
415 "", | 397 "", |
416 distiller_data->page_urls[kMaxPagesInArticle - 2]); | 398 distiller_data->page_urls[kMaxPagesInArticle - 2]); |
417 | 399 |
418 distiller_data->distilled_values.pop_back(); | 400 distiller_data->distilled_values.pop_back(); |
419 distiller_data->distilled_values.push_back(last_page_data.release()); | 401 distiller_data->distilled_values.push_back(last_page_data.release()); |
420 | 402 |
421 EXPECT_CALL(page_factory_, CreateDistillerPageMock()).WillOnce( | 403 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
422 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); | |
423 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
424 | 404 |
425 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 405 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
426 | 406 |
427 DistillPage(distiller_data->page_urls[0]); | 407 DistillPage(distiller_data->page_urls[0], |
| 408 CreateMockDistillerPages( |
| 409 distiller_data.get(), kMaxPagesInArticle, 0).Pass()); |
428 base::MessageLoop::current()->RunUntilIdle(); | 410 base::MessageLoop::current()->RunUntilIdle(); |
429 EXPECT_EQ(kTitle, article_proto_->title()); | 411 EXPECT_EQ(kTitle, article_proto_->title()); |
430 EXPECT_EQ(kMaxPagesInArticle, | 412 EXPECT_EQ(kMaxPagesInArticle, |
431 static_cast<size_t>(article_proto_->pages_size())); | 413 static_cast<size_t>(article_proto_->pages_size())); |
432 } | 414 } |
433 | 415 |
434 TEST_F(DistillerTest, CheckMaxPageLimitExactLimit) { | 416 TEST_F(DistillerTest, CheckMaxPageLimitExactLimit) { |
435 base::MessageLoopForUI loop; | 417 base::MessageLoopForUI loop; |
436 const size_t kMaxPagesInArticle = 10; | 418 const size_t kMaxPagesInArticle = 10; |
437 scoped_ptr<MultipageDistillerData> distiller_data = | 419 scoped_ptr<MultipageDistillerData> distiller_data = |
438 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); | 420 CreateMultipageDistillerDataWithoutImages(kMaxPagesInArticle); |
439 | 421 |
440 EXPECT_CALL(page_factory_, CreateDistillerPageMock()).WillOnce( | 422 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
441 CreateMockDistillerPages(distiller_data.get(), kMaxPagesInArticle, 0)); | |
442 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
443 | 423 |
444 // Check if distilling an article with exactly the page limit works. | 424 // Check if distilling an article with exactly the page limit works. |
445 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 425 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
446 | 426 |
447 DistillPage(distiller_data->page_urls[0]); | 427 DistillPage(distiller_data->page_urls[0], |
| 428 CreateMockDistillerPages( |
| 429 distiller_data.get(), kMaxPagesInArticle, 0).Pass()); |
448 base::MessageLoop::current()->RunUntilIdle(); | 430 base::MessageLoop::current()->RunUntilIdle(); |
449 EXPECT_EQ(kTitle, article_proto_->title()); | 431 EXPECT_EQ(kTitle, article_proto_->title()); |
450 EXPECT_EQ(kMaxPagesInArticle, | 432 EXPECT_EQ(kMaxPagesInArticle, |
451 static_cast<size_t>(article_proto_->pages_size())); | 433 static_cast<size_t>(article_proto_->pages_size())); |
452 } | 434 } |
453 | 435 |
454 TEST_F(DistillerTest, SinglePageDistillationFailure) { | 436 TEST_F(DistillerTest, SinglePageDistillationFailure) { |
455 base::MessageLoopForUI loop; | 437 base::MessageLoopForUI loop; |
456 // To simulate failure return a null value. | 438 // To simulate failure return a null value. |
457 scoped_ptr<base::Value> nullValue(base::Value::CreateNullValue()); | 439 scoped_ptr<base::Value> nullValue(base::Value::CreateNullValue()); |
458 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 440 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
459 .WillOnce(CreateMockDistillerPage(nullValue.get(), GURL(kURL))); | 441 DistillPage(kURL, |
460 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 442 CreateMockDistillerPage(nullValue.get(), GURL(kURL)).Pass()); |
461 DistillPage(kURL); | |
462 base::MessageLoop::current()->RunUntilIdle(); | 443 base::MessageLoop::current()->RunUntilIdle(); |
463 EXPECT_EQ("", article_proto_->title()); | 444 EXPECT_EQ("", article_proto_->title()); |
464 EXPECT_EQ(0, article_proto_->pages_size()); | 445 EXPECT_EQ(0, article_proto_->pages_size()); |
465 } | 446 } |
466 | 447 |
467 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { | 448 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { |
468 base::MessageLoopForUI loop; | 449 base::MessageLoopForUI loop; |
469 const size_t kNumPages = 8; | 450 const size_t kNumPages = 8; |
470 scoped_ptr<MultipageDistillerData> distiller_data = | 451 scoped_ptr<MultipageDistillerData> distiller_data = |
471 CreateMultipageDistillerDataWithoutImages(kNumPages); | 452 CreateMultipageDistillerDataWithoutImages(kNumPages); |
472 | 453 |
473 // The page number of the failed page. | 454 // The page number of the failed page. |
474 size_t failed_page_num = 3; | 455 size_t failed_page_num = 3; |
475 // reset distilled data of the failed page. | 456 // reset distilled data of the failed page. |
476 distiller_data->distilled_values.erase( | 457 distiller_data->distilled_values.erase( |
477 distiller_data->distilled_values.begin() + failed_page_num); | 458 distiller_data->distilled_values.begin() + failed_page_num); |
478 distiller_data->distilled_values.insert( | 459 distiller_data->distilled_values.insert( |
479 distiller_data->distilled_values.begin() + failed_page_num, | 460 distiller_data->distilled_values.begin() + failed_page_num, |
480 base::Value::CreateNullValue()); | 461 base::Value::CreateNullValue()); |
481 // Expect only calls till the failed page number. | 462 // Expect only calls till the failed page number. |
482 EXPECT_CALL(page_factory_, CreateDistillerPageMock()).WillOnce( | 463 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
483 CreateMockDistillerPages(distiller_data.get(), failed_page_num + 1, 0)); | 464 DistillPage(distiller_data->page_urls[0], |
484 | 465 CreateMockDistillerPages( |
485 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 466 distiller_data.get(), failed_page_num + 1, 0).Pass()); |
486 DistillPage(distiller_data->page_urls[0]); | |
487 base::MessageLoop::current()->RunUntilIdle(); | 467 base::MessageLoop::current()->RunUntilIdle(); |
488 EXPECT_EQ(kTitle, article_proto_->title()); | 468 EXPECT_EQ(kTitle, article_proto_->title()); |
489 VerifyArticleProtoMatchesMultipageData( | 469 VerifyArticleProtoMatchesMultipageData( |
490 article_proto_.get(), distiller_data.get(), failed_page_num); | 470 article_proto_.get(), distiller_data.get(), failed_page_num); |
491 } | 471 } |
492 | 472 |
493 TEST_F(DistillerTest, DistillPreviousPage) { | 473 TEST_F(DistillerTest, DistillPreviousPage) { |
494 base::MessageLoopForUI loop; | 474 base::MessageLoopForUI loop; |
495 const size_t kNumPages = 8; | 475 const size_t kNumPages = 8; |
496 | 476 |
497 // The page number of the article on which distillation starts. | 477 // The page number of the article on which distillation starts. |
498 int start_page_num = 3; | 478 int start_page_num = 3; |
499 scoped_ptr<MultipageDistillerData> distiller_data = | 479 scoped_ptr<MultipageDistillerData> distiller_data = |
500 CreateMultipageDistillerDataWithoutImages(kNumPages); | 480 CreateMultipageDistillerDataWithoutImages(kNumPages); |
501 | 481 |
502 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 482 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
503 .WillOnce(CreateMockDistillerPages( | 483 DistillPage(distiller_data->page_urls[start_page_num], |
504 distiller_data.get(), kNumPages, start_page_num)); | 484 CreateMockDistillerPages( |
505 | 485 distiller_data.get(), kNumPages, start_page_num).Pass()); |
506 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
507 DistillPage(distiller_data->page_urls[start_page_num]); | |
508 base::MessageLoop::current()->RunUntilIdle(); | 486 base::MessageLoop::current()->RunUntilIdle(); |
509 VerifyArticleProtoMatchesMultipageData( | 487 VerifyArticleProtoMatchesMultipageData( |
510 article_proto_.get(), distiller_data.get(), kNumPages); | 488 article_proto_.get(), distiller_data.get(), kNumPages); |
511 } | 489 } |
512 | 490 |
513 TEST_F(DistillerTest, IncrementalUpdates) { | 491 TEST_F(DistillerTest, IncrementalUpdates) { |
514 base::MessageLoopForUI loop; | 492 base::MessageLoopForUI loop; |
515 const size_t kNumPages = 8; | 493 const size_t kNumPages = 8; |
516 | 494 |
517 // The page number of the article on which distillation starts. | 495 // The page number of the article on which distillation starts. |
518 int start_page_num = 3; | 496 int start_page_num = 3; |
519 scoped_ptr<MultipageDistillerData> distiller_data = | 497 scoped_ptr<MultipageDistillerData> distiller_data = |
520 CreateMultipageDistillerDataWithoutImages(kNumPages); | 498 CreateMultipageDistillerDataWithoutImages(kNumPages); |
521 | 499 |
522 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 500 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
523 .WillOnce(CreateMockDistillerPages( | 501 DistillPage(distiller_data->page_urls[start_page_num], |
524 distiller_data.get(), kNumPages, start_page_num)); | 502 CreateMockDistillerPages( |
525 | 503 distiller_data.get(), kNumPages, start_page_num).Pass()); |
526 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
527 DistillPage(distiller_data->page_urls[start_page_num]); | |
528 base::MessageLoop::current()->RunUntilIdle(); | 504 base::MessageLoop::current()->RunUntilIdle(); |
529 EXPECT_EQ(kTitle, article_proto_->title()); | 505 EXPECT_EQ(kTitle, article_proto_->title()); |
530 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); | 506 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); |
531 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 507 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
532 | 508 |
533 VerifyIncrementalUpdatesMatch( | 509 VerifyIncrementalUpdatesMatch( |
534 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); | 510 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); |
535 } | 511 } |
536 | 512 |
537 TEST_F(DistillerTest, IncrementalUpdatesDoNotDeleteFinalArticle) { | 513 TEST_F(DistillerTest, IncrementalUpdatesDoNotDeleteFinalArticle) { |
538 base::MessageLoopForUI loop; | 514 base::MessageLoopForUI loop; |
539 const size_t kNumPages = 8; | 515 const size_t kNumPages = 8; |
540 int start_page_num = 3; | 516 int start_page_num = 3; |
541 scoped_ptr<MultipageDistillerData> distiller_data = | 517 scoped_ptr<MultipageDistillerData> distiller_data = |
542 CreateMultipageDistillerDataWithoutImages(kNumPages); | 518 CreateMultipageDistillerDataWithoutImages(kNumPages); |
543 | 519 |
544 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 520 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
545 .WillOnce(CreateMockDistillerPages( | 521 DistillPage(distiller_data->page_urls[start_page_num], |
546 distiller_data.get(), kNumPages, start_page_num)); | 522 CreateMockDistillerPages( |
547 | 523 distiller_data.get(), kNumPages, start_page_num).Pass()); |
548 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
549 DistillPage(distiller_data->page_urls[start_page_num]); | |
550 base::MessageLoop::current()->RunUntilIdle(); | 524 base::MessageLoop::current()->RunUntilIdle(); |
551 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 525 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
552 | 526 |
553 in_sequence_updates_.clear(); | 527 in_sequence_updates_.clear(); |
554 | 528 |
555 // Should still be able to access article and pages. | 529 // Should still be able to access article and pages. |
556 VerifyArticleProtoMatchesMultipageData( | 530 VerifyArticleProtoMatchesMultipageData( |
557 article_proto_.get(), distiller_data.get(), kNumPages); | 531 article_proto_.get(), distiller_data.get(), kNumPages); |
558 } | 532 } |
559 | 533 |
560 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { | 534 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { |
561 base::MessageLoopForUI loop; | 535 base::MessageLoopForUI loop; |
562 const size_t kNumPages = 8; | 536 const size_t kNumPages = 8; |
563 scoped_ptr<MultipageDistillerData> distiller_data = | 537 scoped_ptr<MultipageDistillerData> distiller_data = |
564 CreateMultipageDistillerDataWithoutImages(kNumPages); | 538 CreateMultipageDistillerDataWithoutImages(kNumPages); |
565 // The page number of the article on which distillation starts. | 539 // The page number of the article on which distillation starts. |
566 int start_page_num = 3; | 540 int start_page_num = 3; |
567 | 541 |
568 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 542 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
569 .WillOnce(CreateMockDistillerPages( | 543 DistillPage(distiller_data->page_urls[start_page_num], |
570 distiller_data.get(), kNumPages, start_page_num)); | 544 CreateMockDistillerPages( |
571 | 545 distiller_data.get(), kNumPages, start_page_num).Pass()); |
572 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
573 DistillPage(distiller_data->page_urls[start_page_num]); | |
574 base::MessageLoop::current()->RunUntilIdle(); | 546 base::MessageLoop::current()->RunUntilIdle(); |
575 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); | 547 EXPECT_EQ(kNumPages, in_sequence_updates_.size()); |
576 EXPECT_EQ(kTitle, article_proto_->title()); | 548 EXPECT_EQ(kTitle, article_proto_->title()); |
577 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); | 549 EXPECT_EQ(kNumPages, static_cast<size_t>(article_proto_->pages_size())); |
578 | 550 |
579 // Delete the article. | 551 // Delete the article. |
580 article_proto_.reset(); | 552 article_proto_.reset(); |
581 VerifyIncrementalUpdatesMatch( | 553 VerifyIncrementalUpdatesMatch( |
582 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); | 554 distiller_data.get(), kNumPages, in_sequence_updates_, start_page_num); |
583 } | 555 } |
584 | 556 |
585 TEST_F(DistillerTest, CancelWithDelayedImageFetchCallback) { | 557 TEST_F(DistillerTest, CancelWithDelayedImageFetchCallback) { |
586 base::MessageLoopForUI loop; | 558 base::MessageLoopForUI loop; |
587 vector<int> image_indices; | 559 vector<int> image_indices; |
588 image_indices.push_back(0); | 560 image_indices.push_back(0); |
589 scoped_ptr<base::ListValue> distilled_value = | 561 scoped_ptr<base::ListValue> distilled_value = |
590 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); | 562 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); |
591 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | |
592 .WillOnce(CreateMockDistillerPage(distilled_value.get(), GURL(kURL))); | |
593 | |
594 TestDistillerURLFetcher* delayed_fetcher = new TestDistillerURLFetcher(true); | 563 TestDistillerURLFetcher* delayed_fetcher = new TestDistillerURLFetcher(true); |
595 MockDistillerURLFetcherFactory url_fetcher_factory; | 564 MockDistillerURLFetcherFactory url_fetcher_factory; |
596 EXPECT_CALL(url_fetcher_factory, CreateDistillerURLFetcher()) | 565 EXPECT_CALL(url_fetcher_factory, CreateDistillerURLFetcher()) |
597 .WillOnce(Return(delayed_fetcher)); | 566 .WillOnce(Return(delayed_fetcher)); |
598 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory)); | 567 distiller_.reset(new DistillerImpl(url_fetcher_factory)); |
599 DistillPage(kURL); | 568 DistillPage( |
| 569 kURL, CreateMockDistillerPage(distilled_value.get(), GURL(kURL)).Pass()); |
600 base::MessageLoop::current()->RunUntilIdle(); | 570 base::MessageLoop::current()->RunUntilIdle(); |
601 | 571 |
602 // Post callback from the url fetcher and then delete the distiller. | 572 // Post callback from the url fetcher and then delete the distiller. |
603 delayed_fetcher->PostCallbackTask(); | 573 delayed_fetcher->PostCallbackTask(); |
604 distiller_.reset(); | 574 distiller_.reset(); |
605 | 575 |
606 base::MessageLoop::current()->RunUntilIdle(); | 576 base::MessageLoop::current()->RunUntilIdle(); |
607 } | 577 } |
608 | 578 |
609 TEST_F(DistillerTest, CancelWithDelayedJSCallback) { | 579 TEST_F(DistillerTest, CancelWithDelayedJSCallback) { |
610 base::MessageLoopForUI loop; | 580 base::MessageLoopForUI loop; |
611 scoped_ptr<base::ListValue> distilled_value = | 581 scoped_ptr<base::ListValue> distilled_value = |
612 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 582 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
613 MockDistillerPage* distiller_page = NULL; | 583 MockDistillerPage* distiller_page = NULL; |
614 EXPECT_CALL(page_factory_, CreateDistillerPageMock()) | 584 distiller_.reset(new DistillerImpl(url_fetcher_factory_)); |
615 .WillOnce(CreateMockDistillerPageWithPendingJSCallback(&distiller_page, | 585 DistillPage(kURL, |
616 GURL(kURL))); | 586 CreateMockDistillerPageWithPendingJSCallback(&distiller_page, |
617 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 587 GURL(kURL))); |
618 DistillPage(kURL); | |
619 base::MessageLoop::current()->RunUntilIdle(); | 588 base::MessageLoop::current()->RunUntilIdle(); |
620 | 589 |
621 ASSERT_TRUE(distiller_page); | 590 ASSERT_TRUE(distiller_page); |
622 // Post the task to execute javascript and then delete the distiller. | 591 // Post the task to execute javascript and then delete the distiller. |
623 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); | 592 distiller_page->OnDistillationDone(GURL(kURL), distilled_value.get()); |
624 distiller_.reset(); | 593 distiller_.reset(); |
625 | 594 |
626 base::MessageLoop::current()->RunUntilIdle(); | 595 base::MessageLoop::current()->RunUntilIdle(); |
627 } | 596 } |
628 | 597 |
629 } // namespace dom_distiller | 598 } // namespace dom_distiller |
OLD | NEW |