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

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

Issue 167963003: Support for distilling prior pages in an article. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 10 months 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 | Annotate | Revision Log
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 <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 26 matching lines...) Expand all
37 const char* kImageData[kTotalImages] = {"abcde", "12345"}; 37 const char* kImageData[kTotalImages] = {"abcde", "12345"};
38 38
39 const string GetImageName(int page_num, int image_num) { 39 const string GetImageName(int page_num, int image_num) {
40 return base::IntToString(page_num) + "_" + base::IntToString(image_num); 40 return base::IntToString(page_num) + "_" + base::IntToString(image_num);
41 } 41 }
42 42
43 scoped_ptr<base::ListValue> CreateDistilledValueReturnedFromJS( 43 scoped_ptr<base::ListValue> CreateDistilledValueReturnedFromJS(
44 const string& title, 44 const string& title,
45 const string& content, 45 const string& content,
46 const vector<int>& image_indices, 46 const vector<int>& image_indices,
47 const string& next_page_url) { 47 const string& next_page_url,
48 const string& prev_page_url = "") {
48 scoped_ptr<base::ListValue> list(new base::ListValue()); 49 scoped_ptr<base::ListValue> list(new base::ListValue());
49 50
50 list->AppendString(title); 51 list->AppendString(title);
51 list->AppendString(content); 52 list->AppendString(content);
52 list->AppendString(next_page_url); 53 list->AppendString(next_page_url);
54 list->AppendString(prev_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
59 } // namespace 61 } // namespace
60 62
61 namespace dom_distiller { 63 namespace dom_distiller {
62 64
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); 142 MockDistillerPage* distiller_page = new MockDistillerPage(delegate);
141 EXPECT_CALL(*distiller_page, InitImpl()); 143 EXPECT_CALL(*distiller_page, InitImpl());
142 EXPECT_CALL(*distiller_page, LoadURLImpl(kurl)) 144 EXPECT_CALL(*distiller_page, LoadURLImpl(kurl))
143 .WillOnce(testing::InvokeWithoutArgs(distiller_page, 145 .WillOnce(testing::InvokeWithoutArgs(distiller_page,
144 &DistillerPage::OnLoadURLDone)); 146 &DistillerPage::OnLoadURLDone));
145 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)).WillOnce( 147 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)).WillOnce(
146 DistillerPageOnExecuteJavaScriptDone(distiller_page, kurl, list)); 148 DistillerPageOnExecuteJavaScriptDone(distiller_page, kurl, list));
147 return distiller_page; 149 return distiller_page;
148 } 150 }
149 151
150 ACTION_P3(CreateMockDistillerPages, lists, kurls, num_pages) { 152 ACTION_P4(CreateMockDistillerPages, lists, kurls, num_pages, start_page_num) {
151 DistillerPage::Delegate* delegate = arg0; 153 DistillerPage::Delegate* delegate = arg0;
152 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); 154 MockDistillerPage* distiller_page = new MockDistillerPage(delegate);
153 EXPECT_CALL(*distiller_page, InitImpl()); 155 EXPECT_CALL(*distiller_page, InitImpl());
154 { 156 {
155 testing::InSequence s; 157 testing::InSequence s;
158 // Distiller prefers distilling past pages first. E.g. when distillation
159 // starts on page 2 then pages are distilled in the order: 2, 1, 0, 3, 4.
160 vector<int> page_nums;
161 for (int page = start_page_num; page >= 0; --page)
162 page_nums.push_back(page);
163 for (int page = start_page_num + 1; page < num_pages; ++page)
164 page_nums.push_back(page);
156 165
157 for (int page = 0; page < num_pages; ++page) { 166 for (size_t page_num = 0; page_num < page_nums.size(); ++page_num) {
167 int page = page_nums[page_num];
158 GURL url = GURL(kurls[page]); 168 GURL url = GURL(kurls[page]);
159 EXPECT_CALL(*distiller_page, LoadURLImpl(url)) 169 EXPECT_CALL(*distiller_page, LoadURLImpl(url))
160 .WillOnce(testing::InvokeWithoutArgs(distiller_page, 170 .WillOnce(testing::InvokeWithoutArgs(distiller_page,
161 &DistillerPage::OnLoadURLDone)); 171 &DistillerPage::OnLoadURLDone));
162 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)) 172 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_))
163 .WillOnce(DistillerPageOnExecuteJavaScriptDone( 173 .WillOnce(DistillerPageOnExecuteJavaScriptDone(
164 distiller_page, url, lists[page].get())); 174 distiller_page, url, lists[page].get()));
165 } 175 }
166 } 176 }
167 return distiller_page; 177 return distiller_page;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 for (int i = 0; i < kNumPages; ++i) { 248 for (int i = 0; i < kNumPages; ++i) {
239 string next_page_url = ""; 249 string next_page_url = "";
240 if (i + 1 < kNumPages) 250 if (i + 1 < kNumPages)
241 next_page_url = page_urls[i + 1]; 251 next_page_url = page_urls[i + 1];
242 252
243 list[i] = CreateDistilledValueReturnedFromJS( 253 list[i] = CreateDistilledValueReturnedFromJS(
244 kTitle, content[i], image_indices[i], next_page_url); 254 kTitle, content[i], image_indices[i], next_page_url);
245 } 255 }
246 256
247 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) 257 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
248 .WillOnce(CreateMockDistillerPages(list, page_urls, kNumPages)); 258 .WillOnce(CreateMockDistillerPages(list, page_urls, kNumPages, 0));
249 259
250 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); 260 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
251 distiller_->Init(); 261 distiller_->Init();
252 distiller_->DistillPage( 262 distiller_->DistillPage(
253 GURL(page_urls[0]), 263 GURL(page_urls[0]),
254 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); 264 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
255 base::MessageLoop::current()->RunUntilIdle(); 265 base::MessageLoop::current()->RunUntilIdle();
256 EXPECT_EQ(kTitle, article_proto_->title()); 266 EXPECT_EQ(kTitle, article_proto_->title());
257 EXPECT_EQ(article_proto_->pages_size(), kNumPages); 267 EXPECT_EQ(article_proto_->pages_size(), kNumPages);
258 for (int page_num = 0; page_num < kNumPages; ++page_num) { 268 for (int page_num = 0; page_num < kNumPages; ++page_num) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) { 312 for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) {
303 page_urls[page_num] = url_prefix + base::IntToString(page_num + 1); 313 page_urls[page_num] = url_prefix + base::IntToString(page_num + 1);
304 string content = "Content for page:" + base::IntToString(page_num); 314 string content = "Content for page:" + base::IntToString(page_num);
305 string next_page_url = url_prefix + base::IntToString(page_num + 2); 315 string next_page_url = url_prefix + base::IntToString(page_num + 2);
306 list[page_num] = CreateDistilledValueReturnedFromJS( 316 list[page_num] = CreateDistilledValueReturnedFromJS(
307 kTitle, content, vector<int>(), next_page_url); 317 kTitle, content, vector<int>(), next_page_url);
308 } 318 }
309 319
310 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) 320 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
311 .WillOnce(CreateMockDistillerPages( 321 .WillOnce(CreateMockDistillerPages(
312 list, page_urls, static_cast<int>(kMaxPagesInArticle))); 322 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0));
313 323
314 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); 324 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
315 325
316 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); 326 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle);
317 327
318 distiller_->Init(); 328 distiller_->Init();
319 distiller_->DistillPage( 329 distiller_->DistillPage(
320 GURL(page_urls[0]), 330 GURL(page_urls[0]),
321 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); 331 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
322 base::MessageLoop::current()->RunUntilIdle(); 332 base::MessageLoop::current()->RunUntilIdle();
323 EXPECT_EQ(kTitle, article_proto_->title()); 333 EXPECT_EQ(kTitle, article_proto_->title());
324 EXPECT_EQ(kMaxPagesInArticle, 334 EXPECT_EQ(kMaxPagesInArticle,
325 static_cast<size_t>(article_proto_->pages_size())); 335 static_cast<size_t>(article_proto_->pages_size()));
326 336
327 // Now check if distilling an article with exactly the page limit works by 337 // Now check if distilling an article with exactly the page limit works by
328 // resetting the next page url of the last page of the article. 338 // resetting the next page url of the last page of the article.
329 list[kMaxPagesInArticle - 1] = 339 list[kMaxPagesInArticle - 1] =
330 CreateDistilledValueReturnedFromJS(kTitle, "Content", vector<int>(), ""); 340 CreateDistilledValueReturnedFromJS(kTitle, "Content", vector<int>(), "");
331 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) 341 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
332 .WillOnce(CreateMockDistillerPages( 342 .WillOnce(CreateMockDistillerPages(
333 list, page_urls, static_cast<int>(kMaxPagesInArticle))); 343 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0));
334 344
335 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); 345 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
336 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); 346 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle);
347
348 distiller_->Init();
349 distiller_->DistillPage(
350 GURL(page_urls[0]),
351 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
352 base::MessageLoop::current()->RunUntilIdle();
353 EXPECT_EQ(kTitle, article_proto_->title());
354 EXPECT_EQ(kMaxPagesInArticle,
355 static_cast<size_t>(article_proto_->pages_size()));
356
357 // Now check if distilling an article with exactly the page limit works by
358 // resetting the next page url of the last page of the article.
359 list[kMaxPagesInArticle - 1] =
360 CreateDistilledValueReturnedFromJS(kTitle, "Content", vector<int>(), "");
361 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
362 .WillOnce(CreateMockDistillerPages(
363 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0));
364
365 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
366 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle);
337 367
338 distiller_->Init(); 368 distiller_->Init();
339 distiller_->DistillPage( 369 distiller_->DistillPage(
340 GURL(page_urls[0]), 370 GURL(page_urls[0]),
341 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); 371 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
342 base::MessageLoop::current()->RunUntilIdle(); 372 base::MessageLoop::current()->RunUntilIdle();
343 EXPECT_EQ(kTitle, article_proto_->title()); 373 EXPECT_EQ(kTitle, article_proto_->title());
344 EXPECT_EQ(kMaxPagesInArticle, 374 EXPECT_EQ(kMaxPagesInArticle,
345 static_cast<size_t>(article_proto_->pages_size())); 375 static_cast<size_t>(article_proto_->pages_size()));
346 } 376 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 distilled_values[page_num] = CreateDistilledValueReturnedFromJS( 408 distilled_values[page_num] = CreateDistilledValueReturnedFromJS(
379 kTitle, content[page_num], vector<int>(), next_page_url); 409 kTitle, content[page_num], vector<int>(), next_page_url);
380 } else { 410 } else {
381 distilled_values[page_num].reset(base::Value::CreateNullValue()); 411 distilled_values[page_num].reset(base::Value::CreateNullValue());
382 } 412 }
383 } 413 }
384 414
385 // Expect only calls till the failed page number. 415 // Expect only calls till the failed page number.
386 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) 416 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
387 .WillOnce(CreateMockDistillerPages( 417 .WillOnce(CreateMockDistillerPages(
388 distilled_values, page_urls, failed_page_num + 1)); 418 distilled_values, page_urls, failed_page_num + 1, 0));
389 419
390 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); 420 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
391 distiller_->Init(); 421 distiller_->Init();
392 distiller_->DistillPage( 422 distiller_->DistillPage(
393 GURL(page_urls[0]), 423 GURL(page_urls[0]),
394 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); 424 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
395 base::MessageLoop::current()->RunUntilIdle(); 425 base::MessageLoop::current()->RunUntilIdle();
396 EXPECT_EQ(kTitle, article_proto_->title()); 426 EXPECT_EQ(kTitle, article_proto_->title());
397 EXPECT_EQ(article_proto_->pages_size(), failed_page_num); 427 EXPECT_EQ(article_proto_->pages_size(), failed_page_num);
398 for (int page_num = 0; page_num < failed_page_num; ++page_num) { 428 for (int page_num = 0; page_num < failed_page_num; ++page_num) {
399 const DistilledPageProto& page = article_proto_->pages(page_num); 429 const DistilledPageProto& page = article_proto_->pages(page_num);
400 EXPECT_EQ(content[page_num], page.html()); 430 EXPECT_EQ(content[page_num], page.html());
401 EXPECT_EQ(page_urls[page_num], page.url()); 431 EXPECT_EQ(page_urls[page_num], page.url());
402 } 432 }
403 } 433 }
404 434
435 TEST_F(DistillerTest, DistillPreviousPage) {
436 base::MessageLoopForUI loop;
437 const int kNumPages = 8;
438 string content[kNumPages];
439 string page_urls[kNumPages];
440 scoped_ptr<base::Value> distilled_values[kNumPages];
441
442 // The page number of the article on which distillation starts.
443 int start_page_number = 3;
444 string url_prefix = "http://a.com/";
445 for (int page_no = 0; page_no < kNumPages; ++page_no) {
446 page_urls[page_no] = url_prefix + base::IntToString(page_no);
447 content[page_no] = "Content for page:" + base::IntToString(page_no);
448 string next_page_url = (page_no + 1 < kNumPages)
449 ? url_prefix + base::IntToString(page_no + 1)
450 : "";
451 string prev_page_url = (page_no > 0) ? page_urls[page_no - 1] : "";
452 distilled_values[page_no] = CreateDistilledValueReturnedFromJS(
453 kTitle, content[page_no], vector<int>(), next_page_url, prev_page_url);
454 }
455
456 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
457 .WillOnce(CreateMockDistillerPages(
458 distilled_values, page_urls, kNumPages, start_page_number));
459
460 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
461 distiller_->Init();
462 distiller_->DistillPage(
463 GURL(page_urls[start_page_number]),
464 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
465 base::MessageLoop::current()->RunUntilIdle();
466 EXPECT_EQ(kTitle, article_proto_->title());
467 EXPECT_EQ(kNumPages, article_proto_->pages_size());
468 for (int page_no = 0; page_no < kNumPages; ++page_no) {
469 const DistilledPageProto& page = article_proto_->pages(page_no);
470 EXPECT_EQ(content[page_no], page.html());
471 EXPECT_EQ(page_urls[page_no], page.url());
472 }
473 }
474
405 } // namespace dom_distiller 475 } // namespace dom_distiller
OLDNEW
« no previous file with comments | « components/dom_distiller/core/distiller.cc ('k') | components/dom_distiller/core/page_distiller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698