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

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: 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 28 matching lines...) Expand all
39 const char* kImageData[kTotalImages] = {"abcde", "12345"}; 39 const char* kImageData[kTotalImages] = {"abcde", "12345"};
40 40
41 const string GetImageName(int page_num, int image_num) { 41 const string GetImageName(int page_num, int image_num) {
42 return base::IntToString(page_num) + "_" + base::IntToString(image_num); 42 return base::IntToString(page_num) + "_" + base::IntToString(image_num);
43 } 43 }
44 44
45 scoped_ptr<base::ListValue> CreateDistilledValueReturnedFromJS( 45 scoped_ptr<base::ListValue> CreateDistilledValueReturnedFromJS(
46 const string& title, 46 const string& title,
47 const string& content, 47 const string& content,
48 const vector<int>& image_indices, 48 const vector<int>& image_indices,
49 const string& next_page_url) { 49 const string& next_page_url,
50 const string& prev_page_url = "") {
50 scoped_ptr<base::ListValue> list(new base::ListValue()); 51 scoped_ptr<base::ListValue> list(new base::ListValue());
51 52
52 list->AppendString(title); 53 list->AppendString(title);
53 list->AppendString(content); 54 list->AppendString(content);
54 list->AppendString(next_page_url); 55 list->AppendString(next_page_url);
56 list->AppendString(prev_page_url);
55 for (size_t i = 0; i < image_indices.size(); ++i) { 57 for (size_t i = 0; i < image_indices.size(); ++i) {
56 list->AppendString(kImageURLs[image_indices[i]]); 58 list->AppendString(kImageURLs[image_indices[i]]);
57 } 59 }
58 return list.Pass(); 60 return list.Pass();
59 } 61 }
60 62
61 } // namespace 63 } // namespace
62 64
63 namespace dom_distiller { 65 namespace dom_distiller {
64 66
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); 144 MockDistillerPage* distiller_page = new MockDistillerPage(delegate);
143 EXPECT_CALL(*distiller_page, InitImpl()); 145 EXPECT_CALL(*distiller_page, InitImpl());
144 EXPECT_CALL(*distiller_page, LoadURLImpl(kurl)) 146 EXPECT_CALL(*distiller_page, LoadURLImpl(kurl))
145 .WillOnce(testing::InvokeWithoutArgs(distiller_page, 147 .WillOnce(testing::InvokeWithoutArgs(distiller_page,
146 &DistillerPage::OnLoadURLDone)); 148 &DistillerPage::OnLoadURLDone));
147 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)).WillOnce( 149 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)).WillOnce(
148 DistillerPageOnExecuteJavaScriptDone(distiller_page, kurl, list)); 150 DistillerPageOnExecuteJavaScriptDone(distiller_page, kurl, list));
149 return distiller_page; 151 return distiller_page;
150 } 152 }
151 153
152 ACTION_P3(CreateMockDistillerPages, lists, kurls, num_pages) { 154 ACTION_P4(CreateMockDistillerPages, lists, kurls, num_pages, start_page_num) {
153 DistillerPage::Delegate* delegate = arg0; 155 DistillerPage::Delegate* delegate = arg0;
154 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); 156 MockDistillerPage* distiller_page = new MockDistillerPage(delegate);
155 EXPECT_CALL(*distiller_page, InitImpl()); 157 EXPECT_CALL(*distiller_page, InitImpl());
156 { 158 {
157 testing::InSequence s; 159 testing::InSequence s;
160 // Distiller prefers distilling past pages first. E.g. when distillation
161 // starts on page 2 then pages are distilled in the order: 2, 1, 0, 3, 4.
162 vector<int> page_nums;
163 for (int page = start_page_num; page >= 0; --page)
164 page_nums.push_back(page);
165 for (int page = start_page_num + 1; page < num_pages; ++page)
166 page_nums.push_back(page);
158 167
159 for (int page = 0; page < num_pages; ++page) { 168 for (size_t page_num = 0; page_num < page_nums.size(); ++page_num) {
169 int page = page_nums[page_num];
160 GURL url = GURL(kurls[page]); 170 GURL url = GURL(kurls[page]);
161 EXPECT_CALL(*distiller_page, LoadURLImpl(url)) 171 EXPECT_CALL(*distiller_page, LoadURLImpl(url))
162 .WillOnce(testing::InvokeWithoutArgs(distiller_page, 172 .WillOnce(testing::InvokeWithoutArgs(distiller_page,
163 &DistillerPage::OnLoadURLDone)); 173 &DistillerPage::OnLoadURLDone));
164 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)) 174 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_))
165 .WillOnce(DistillerPageOnExecuteJavaScriptDone( 175 .WillOnce(DistillerPageOnExecuteJavaScriptDone(
166 distiller_page, url, lists[page].get())); 176 distiller_page, url, lists[page].get()));
167 } 177 }
168 } 178 }
169 return distiller_page; 179 return distiller_page;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 for (int i = 0; i < kNumPages; ++i) { 250 for (int i = 0; i < kNumPages; ++i) {
241 string next_page_url = ""; 251 string next_page_url = "";
242 if (i + 1 < kNumPages) 252 if (i + 1 < kNumPages)
243 next_page_url = page_urls[i + 1]; 253 next_page_url = page_urls[i + 1];
244 254
245 list[i] = CreateDistilledValueReturnedFromJS( 255 list[i] = CreateDistilledValueReturnedFromJS(
246 kTitle, content[i], image_indices[i], next_page_url); 256 kTitle, content[i], image_indices[i], next_page_url);
247 } 257 }
248 258
249 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) 259 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
250 .WillOnce(CreateMockDistillerPages(list, page_urls, kNumPages)); 260 .WillOnce(CreateMockDistillerPages(list, page_urls, kNumPages, 0));
251 261
252 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); 262 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
253 distiller_->Init(); 263 distiller_->Init();
254 distiller_->DistillPage( 264 distiller_->DistillPage(
255 GURL(page_urls[0]), 265 GURL(page_urls[0]),
256 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); 266 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
257 base::MessageLoop::current()->RunUntilIdle(); 267 base::MessageLoop::current()->RunUntilIdle();
258 EXPECT_EQ(kTitle, article_proto_->title()); 268 EXPECT_EQ(kTitle, article_proto_->title());
259 EXPECT_EQ(article_proto_->pages_size(), kNumPages); 269 EXPECT_EQ(article_proto_->pages_size(), kNumPages);
260 for (int page_num = 0; page_num < kNumPages; ++page_num) { 270 for (int page_num = 0; page_num < kNumPages; ++page_num) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) { 313 for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) {
304 page_urls[page_num] = url_prefix + base::IntToString(page_num + 1); 314 page_urls[page_num] = url_prefix + base::IntToString(page_num + 1);
305 string content = "Content for page:" + base::IntToString(page_num); 315 string content = "Content for page:" + base::IntToString(page_num);
306 string next_page_url = url_prefix + base::IntToString(page_num + 2); 316 string next_page_url = url_prefix + base::IntToString(page_num + 2);
307 list[page_num] = CreateDistilledValueReturnedFromJS( 317 list[page_num] = CreateDistilledValueReturnedFromJS(
308 kTitle, content, vector<int>(), next_page_url); 318 kTitle, content, vector<int>(), next_page_url);
309 } 319 }
310 320
311 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) 321 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
312 .WillOnce(CreateMockDistillerPages( 322 .WillOnce(CreateMockDistillerPages(
313 list, page_urls, static_cast<int>(kMaxPagesInArticle))); 323 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0));
314 324
315 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); 325 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
316 326
317 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); 327 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle);
318 328
319 distiller_->Init(); 329 distiller_->Init();
320 distiller_->DistillPage( 330 distiller_->DistillPage(
321 GURL(page_urls[0]), 331 GURL(page_urls[0]),
322 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); 332 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
323 base::MessageLoop::current()->RunUntilIdle(); 333 base::MessageLoop::current()->RunUntilIdle();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 distilled_values[page_num] = CreateDistilledValueReturnedFromJS( 369 distilled_values[page_num] = CreateDistilledValueReturnedFromJS(
360 kTitle, content[page_num], vector<int>(), next_page_url); 370 kTitle, content[page_num], vector<int>(), next_page_url);
361 } else { 371 } else {
362 distilled_values[page_num].reset(base::Value::CreateNullValue()); 372 distilled_values[page_num].reset(base::Value::CreateNullValue());
363 } 373 }
364 } 374 }
365 375
366 // Expect only calls till the failed page number. 376 // Expect only calls till the failed page number.
367 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) 377 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
368 .WillOnce(CreateMockDistillerPages( 378 .WillOnce(CreateMockDistillerPages(
369 distilled_values, page_urls, failed_page_num + 1)); 379 distilled_values, page_urls, failed_page_num + 1, 0));
370 380
371 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); 381 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
372 distiller_->Init(); 382 distiller_->Init();
373 distiller_->DistillPage( 383 distiller_->DistillPage(
374 GURL(page_urls[0]), 384 GURL(page_urls[0]),
375 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); 385 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
376 base::MessageLoop::current()->RunUntilIdle(); 386 base::MessageLoop::current()->RunUntilIdle();
377 EXPECT_EQ(kTitle, article_proto_->title()); 387 EXPECT_EQ(kTitle, article_proto_->title());
378 EXPECT_EQ(article_proto_->pages_size(), failed_page_num); 388 EXPECT_EQ(article_proto_->pages_size(), failed_page_num);
379 for (int page_num = 0; page_num < failed_page_num; ++page_num) { 389 for (int page_num = 0; page_num < failed_page_num; ++page_num) {
380 const DistilledPageProto& page = article_proto_->pages(page_num); 390 const DistilledPageProto& page = article_proto_->pages(page_num);
381 EXPECT_EQ(content[page_num], page.html()); 391 EXPECT_EQ(content[page_num], page.html());
382 EXPECT_EQ(page_urls[page_num], page.url()); 392 EXPECT_EQ(page_urls[page_num], page.url());
383 } 393 }
384 } 394 }
385 395
396 TEST_F(DistillerTest, DistillPreviousPage) {
397 base::MessageLoopForUI loop;
398 const int kNumPages = 8;
399 string content[kNumPages];
400 string page_urls[kNumPages];
401 scoped_ptr<base::Value> distilled_values[kNumPages];
402
403 // The page number of the article on which distillation starts.
404 int start_page_number = 3;
405 string url_prefix = "http://a.com/";
406 for (int page_no = 0; page_no < kNumPages; ++page_no) {
407 page_urls[page_no] = url_prefix + base::IntToString(page_no);
408 content[page_no] = "Content for page:" + base::IntToString(page_no);
409 string next_page_url = (page_no + 1 < kNumPages)
410 ? url_prefix + base::IntToString(page_no + 1)
411 : "";
412 string prev_page_url = (page_no > 0) ? page_urls[page_no - 1] : "";
413 distilled_values[page_no] = CreateDistilledValueReturnedFromJS(
414 kTitle, content[page_no], vector<int>(), next_page_url, prev_page_url);
415 }
416
417 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_))
418 .WillOnce(CreateMockDistillerPages(
419 distilled_values, page_urls, kNumPages, start_page_number));
420
421 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_));
422 distiller_->Init();
423 distiller_->DistillPage(
424 GURL(page_urls[start_page_number]),
425 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this)));
426 base::MessageLoop::current()->RunUntilIdle();
427 EXPECT_EQ(kTitle, article_proto_->title());
428 EXPECT_EQ(kNumPages, article_proto_->pages_size());
429 for (int page_no = 0; page_no < kNumPages; ++page_no) {
430 const DistilledPageProto& page = article_proto_->pages(page_no);
431 EXPECT_EQ(content[page_no], page.html());
432 EXPECT_EQ(page_urls[page_no], page.url());
433 }
434 }
435
386 } // namespace dom_distiller 436 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698