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 <map> | 6 #include <map> |
6 #include <string> | 7 #include <string> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "components/dom_distiller/core/article_distillation_update.h" | |
16 #include "components/dom_distiller/core/distiller.h" | 18 #include "components/dom_distiller/core/distiller.h" |
17 #include "components/dom_distiller/core/distiller_page.h" | 19 #include "components/dom_distiller/core/distiller_page.h" |
18 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 20 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
19 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 21 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
20 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
23 | 25 |
24 using std::vector; | 26 using std::vector; |
25 using std::string; | 27 using std::string; |
(...skipping 25 matching lines...) Expand all Loading... | |
51 list->AppendString(title); | 53 list->AppendString(title); |
52 list->AppendString(content); | 54 list->AppendString(content); |
53 list->AppendString(next_page_url); | 55 list->AppendString(next_page_url); |
54 list->AppendString(prev_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 |
63 // Return the sequence in which Distiller will distill pages. | |
64 // Note: ignores any delays due to fetching images etc. | |
65 vector<int> get_pages_in_sequence(int start_page_num, int num_pages) { | |
66 // Distiller prefers distilling past pages first. E.g. when distillation | |
67 // starts on page 2 then pages are distilled in the order: 2, 1, 0, 3, 4. | |
68 vector<int> page_nums; | |
69 for (int page = start_page_num; page >= 0; --page) | |
70 page_nums.push_back(page); | |
71 for (int page = start_page_num + 1; page < num_pages; ++page) | |
72 page_nums.push_back(page); | |
73 return page_nums; | |
74 } | |
75 | |
61 } // namespace | 76 } // namespace |
62 | 77 |
63 namespace dom_distiller { | 78 namespace dom_distiller { |
64 | 79 |
65 class TestDistillerURLFetcher : public DistillerURLFetcher { | 80 class TestDistillerURLFetcher : public DistillerURLFetcher { |
66 public: | 81 public: |
67 TestDistillerURLFetcher() : DistillerURLFetcher(NULL) { | 82 TestDistillerURLFetcher() : DistillerURLFetcher(NULL) { |
68 responses_[kImageURLs[0]] = string(kImageData[0]); | 83 responses_[kImageURLs[0]] = string(kImageData[0]); |
69 responses_[kImageURLs[1]] = string(kImageData[1]); | 84 responses_[kImageURLs[1]] = string(kImageData[1]); |
70 } | 85 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 | 130 |
116 virtual scoped_ptr<DistillerPage> CreateDistillerPage( | 131 virtual scoped_ptr<DistillerPage> CreateDistillerPage( |
117 DistillerPage::Delegate* delegate) const OVERRIDE { | 132 DistillerPage::Delegate* delegate) const OVERRIDE { |
118 return scoped_ptr<DistillerPage>(CreateDistillerPageMock(delegate)); | 133 return scoped_ptr<DistillerPage>(CreateDistillerPageMock(delegate)); |
119 } | 134 } |
120 }; | 135 }; |
121 | 136 |
122 class DistillerTest : public testing::Test { | 137 class DistillerTest : public testing::Test { |
123 public: | 138 public: |
124 virtual ~DistillerTest() {} | 139 virtual ~DistillerTest() {} |
125 void OnDistillPageDone(scoped_ptr<DistilledArticleProto> proto) { | 140 void OnDistillArticleDone(scoped_ptr<DistilledArticleProto> proto) { |
126 article_proto_ = proto.Pass(); | 141 article_proto_ = proto.Pass(); |
127 } | 142 } |
128 | 143 |
144 void OnDistillArticleUpdate(const ArticleDistillationUpdate& article_update) { | |
145 in_sequence_updates_.push_back(article_update); | |
146 } | |
147 | |
148 void DistillPage(const std::string& url) { | |
149 distiller_->DistillPage(GURL(url), | |
150 base::Bind(&DistillerTest::OnDistillArticleDone, | |
151 base::Unretained(this)), | |
152 base::Bind(&DistillerTest::OnDistillArticleUpdate, | |
153 base::Unretained(this))); | |
154 } | |
155 | |
129 protected: | 156 protected: |
130 scoped_ptr<DistillerImpl> distiller_; | 157 scoped_ptr<DistillerImpl> distiller_; |
131 scoped_ptr<DistilledArticleProto> article_proto_; | 158 scoped_ptr<DistilledArticleProto> article_proto_; |
159 std::vector<ArticleDistillationUpdate> in_sequence_updates_; | |
132 MockDistillerPageFactory page_factory_; | 160 MockDistillerPageFactory page_factory_; |
133 TestDistillerURLFetcherFactory url_fetcher_factory_; | 161 TestDistillerURLFetcherFactory url_fetcher_factory_; |
134 }; | 162 }; |
135 | 163 |
136 ACTION_P3(DistillerPageOnExecuteJavaScriptDone, distiller_page, url, list) { | 164 ACTION_P3(DistillerPageOnExecuteJavaScriptDone, distiller_page, url, list) { |
137 distiller_page->OnExecuteJavaScriptDone(url, list); | 165 distiller_page->OnExecuteJavaScriptDone(url, list); |
138 } | 166 } |
139 | 167 |
140 ACTION_P2(CreateMockDistillerPage, list, kurl) { | 168 ACTION_P2(CreateMockDistillerPage, list, kurl) { |
141 DistillerPage::Delegate* delegate = arg0; | 169 DistillerPage::Delegate* delegate = arg0; |
142 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); | 170 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); |
143 EXPECT_CALL(*distiller_page, InitImpl()); | 171 EXPECT_CALL(*distiller_page, InitImpl()); |
144 EXPECT_CALL(*distiller_page, LoadURLImpl(kurl)) | 172 EXPECT_CALL(*distiller_page, LoadURLImpl(kurl)) |
145 .WillOnce(testing::InvokeWithoutArgs(distiller_page, | 173 .WillOnce(testing::InvokeWithoutArgs(distiller_page, |
146 &DistillerPage::OnLoadURLDone)); | 174 &DistillerPage::OnLoadURLDone)); |
147 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)).WillOnce( | 175 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)).WillOnce( |
148 DistillerPageOnExecuteJavaScriptDone(distiller_page, kurl, list)); | 176 DistillerPageOnExecuteJavaScriptDone(distiller_page, kurl, list)); |
149 return distiller_page; | 177 return distiller_page; |
150 } | 178 } |
151 | 179 |
152 ACTION_P4(CreateMockDistillerPages, lists, kurls, num_pages, start_page_num) { | 180 ACTION_P4(CreateMockDistillerPages, lists, kurls, num_pages, start_page_num) { |
153 DistillerPage::Delegate* delegate = arg0; | 181 DistillerPage::Delegate* delegate = arg0; |
154 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); | 182 MockDistillerPage* distiller_page = new MockDistillerPage(delegate); |
155 EXPECT_CALL(*distiller_page, InitImpl()); | 183 EXPECT_CALL(*distiller_page, InitImpl()); |
156 { | 184 { |
157 testing::InSequence s; | 185 testing::InSequence s; |
158 // Distiller prefers distilling past pages first. E.g. when distillation | 186 vector<int> page_nums = get_pages_in_sequence(start_page_num, num_pages); |
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); | |
165 | |
166 for (size_t page_num = 0; page_num < page_nums.size(); ++page_num) { | 187 for (size_t page_num = 0; page_num < page_nums.size(); ++page_num) { |
167 int page = page_nums[page_num]; | 188 int page = page_nums[page_num]; |
168 GURL url = GURL(kurls[page]); | 189 GURL url = GURL(kurls[page]); |
169 EXPECT_CALL(*distiller_page, LoadURLImpl(url)) | 190 EXPECT_CALL(*distiller_page, LoadURLImpl(url)) |
170 .WillOnce(testing::InvokeWithoutArgs(distiller_page, | 191 .WillOnce(testing::InvokeWithoutArgs(distiller_page, |
171 &DistillerPage::OnLoadURLDone)); | 192 &DistillerPage::OnLoadURLDone)); |
172 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)) | 193 EXPECT_CALL(*distiller_page, ExecuteJavaScriptImpl(_)) |
173 .WillOnce(DistillerPageOnExecuteJavaScriptDone( | 194 .WillOnce(DistillerPageOnExecuteJavaScriptDone( |
174 distiller_page, url, lists[page].get())); | 195 distiller_page, url, lists[page].get())); |
175 } | 196 } |
176 } | 197 } |
177 return distiller_page; | 198 return distiller_page; |
178 } | 199 } |
179 | 200 |
201 ACTION_P5(VerifyIncrementalUpdatesMatch, | |
cjhopman
2014/02/28 02:38:14
Why is this an action? It isn't ever used as an ac
shashi
2014/03/02 02:53:40
Changed to function.
| |
202 start_page_num, | |
203 kNumPages, | |
cjhopman
2014/02/28 02:38:14
nit: the capitalization of this is wrong
shashi
2014/03/02 02:53:40
Done.
| |
204 expected_page_urls, | |
205 expected_content, | |
206 incremental_updates) { | |
207 vector<int> page_nums = get_pages_in_sequence(start_page_num, kNumPages); | |
cjhopman
2014/02/28 02:38:14
This function contains the following names:
start_
shashi
2014/03/02 02:53:40
Done.
| |
208 // Updates should contain a list of pages. Pages in a update should be in | |
209 // the correct ascending page order regardless of start_page_num. | |
210 // E.g. if distillation starts at page 2 of a 3 page article, the updates | |
211 // will be [ [2], [1, 2], [1, 2, 3]]. This example assumes that image fetches | |
212 // do not delay distillation of a page. There can be scenarios when image | |
213 // fetch delays distillation of a page (E.g. 1 is delayed due to image fetches | |
214 // so updates can be in this order [ [2], [2,3], [1,2,3]]. | |
215 for (int page_num = 0; page_num < incremental_updates.size(); ++page_num) { | |
cjhopman
2014/02/28 02:38:14
This seems like the index of an incremental update
cjhopman
2014/02/28 02:38:14
incremental_updates is a vector, why not use an it
shashi
2014/03/02 02:53:40
Done.
shashi
2014/03/02 02:53:40
Done.
shashi
2014/03/02 02:53:40
Done.
| |
216 size_t num_pages = incremental_updates[page_num].getPagesSize(); | |
217 EXPECT_EQ(static_cast<size_t>(page_num + 1), num_pages); | |
218 vector<int> update_pages(page_nums.begin(), page_nums.begin() + num_pages); | |
cjhopman
2014/02/28 02:38:14
I'd rename this: expected_pages_in_update or some
shashi
2014/03/02 02:53:40
Done.
| |
219 std::sort(update_pages.begin(), update_pages.end()); | |
220 | |
221 // If we already got the first page then there is no previous page. | |
222 EXPECT_EQ((update_pages[0] != 0), | |
223 incremental_updates[page_num].hasPrevPage()); | |
224 | |
225 // if we already got the last page then there is no next page. | |
226 EXPECT_EQ((*update_pages.rbegin()) != kNumPages - 1, | |
227 incremental_updates[page_num].hasNextPage()); | |
228 for (size_t j = 0; j < num_pages; ++j) { | |
229 int actual_page_num = update_pages[j]; | |
230 EXPECT_EQ(expected_page_urls[actual_page_num], | |
231 incremental_updates[page_num].getDistilledPage(j)->data.url()); | |
232 EXPECT_EQ(expected_content[actual_page_num], | |
233 incremental_updates[page_num].getDistilledPage(j)->data.html()); | |
234 } | |
235 } | |
236 } | |
237 | |
180 TEST_F(DistillerTest, DistillPage) { | 238 TEST_F(DistillerTest, DistillPage) { |
181 base::MessageLoopForUI loop; | 239 base::MessageLoopForUI loop; |
182 scoped_ptr<base::ListValue> list = | 240 scoped_ptr<base::ListValue> list = |
183 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); | 241 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), ""); |
184 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 242 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
185 .WillOnce(CreateMockDistillerPage(list.get(), GURL(kURL))); | 243 .WillOnce(CreateMockDistillerPage(list.get(), GURL(kURL))); |
186 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 244 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
187 distiller_->Init(); | 245 distiller_->Init(); |
188 distiller_->DistillPage( | 246 DistillPage(kURL); |
189 GURL(kURL), | |
190 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
191 base::MessageLoop::current()->RunUntilIdle(); | 247 base::MessageLoop::current()->RunUntilIdle(); |
192 EXPECT_EQ(kTitle, article_proto_->title()); | 248 EXPECT_EQ(kTitle, article_proto_->title()); |
193 EXPECT_EQ(article_proto_->pages_size(), 1); | 249 EXPECT_EQ(article_proto_->pages_size(), 1); |
194 const DistilledPageProto& first_page = article_proto_->pages(0); | 250 const DistilledPageProto& first_page = article_proto_->pages(0); |
195 EXPECT_EQ(kContent, first_page.html()); | 251 EXPECT_EQ(kContent, first_page.html()); |
196 EXPECT_EQ(kURL, first_page.url()); | 252 EXPECT_EQ(kURL, first_page.url()); |
197 } | 253 } |
198 | 254 |
199 TEST_F(DistillerTest, DistillPageWithImages) { | 255 TEST_F(DistillerTest, DistillPageWithImages) { |
200 base::MessageLoopForUI loop; | 256 base::MessageLoopForUI loop; |
201 vector<int> image_indices; | 257 vector<int> image_indices; |
202 image_indices.push_back(0); | 258 image_indices.push_back(0); |
203 image_indices.push_back(1); | 259 image_indices.push_back(1); |
204 scoped_ptr<base::ListValue> list = | 260 scoped_ptr<base::ListValue> list = |
205 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); | 261 CreateDistilledValueReturnedFromJS(kTitle, kContent, image_indices, ""); |
206 EXPECT_CALL(page_factory_, | 262 EXPECT_CALL(page_factory_, |
207 CreateDistillerPageMock(_)).WillOnce( | 263 CreateDistillerPageMock(_)).WillOnce( |
208 CreateMockDistillerPage(list.get(), GURL(kURL))); | 264 CreateMockDistillerPage(list.get(), GURL(kURL))); |
209 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 265 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
210 distiller_->Init(); | 266 distiller_->Init(); |
211 distiller_->DistillPage( | 267 DistillPage(kURL); |
212 GURL(kURL), | |
213 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
214 base::MessageLoop::current()->RunUntilIdle(); | 268 base::MessageLoop::current()->RunUntilIdle(); |
215 EXPECT_EQ(kTitle, article_proto_->title()); | 269 EXPECT_EQ(kTitle, article_proto_->title()); |
216 EXPECT_EQ(article_proto_->pages_size(), 1); | 270 EXPECT_EQ(article_proto_->pages_size(), 1); |
217 const DistilledPageProto& first_page = article_proto_->pages(0); | 271 const DistilledPageProto& first_page = article_proto_->pages(0); |
218 EXPECT_EQ(kContent, first_page.html()); | 272 EXPECT_EQ(kContent, first_page.html()); |
219 EXPECT_EQ(kURL, first_page.url()); | 273 EXPECT_EQ(kURL, first_page.url()); |
220 EXPECT_EQ(2, first_page.image_size()); | 274 EXPECT_EQ(2, first_page.image_size()); |
221 EXPECT_EQ(kImageData[0], first_page.image(0).data()); | 275 EXPECT_EQ(kImageData[0], first_page.image(0).data()); |
222 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); | 276 EXPECT_EQ(GetImageName(1, 0), first_page.image(0).name()); |
223 EXPECT_EQ(kImageData[1], first_page.image(1).data()); | 277 EXPECT_EQ(kImageData[1], first_page.image(1).data()); |
(...skipping 28 matching lines...) Expand all Loading... | |
252 | 306 |
253 list[i] = CreateDistilledValueReturnedFromJS( | 307 list[i] = CreateDistilledValueReturnedFromJS( |
254 kTitle, content[i], image_indices[i], next_page_url); | 308 kTitle, content[i], image_indices[i], next_page_url); |
255 } | 309 } |
256 | 310 |
257 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 311 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
258 .WillOnce(CreateMockDistillerPages(list, page_urls, kNumPages, 0)); | 312 .WillOnce(CreateMockDistillerPages(list, page_urls, kNumPages, 0)); |
259 | 313 |
260 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 314 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
261 distiller_->Init(); | 315 distiller_->Init(); |
262 distiller_->DistillPage( | 316 DistillPage(page_urls[0]); |
263 GURL(page_urls[0]), | |
264 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
265 base::MessageLoop::current()->RunUntilIdle(); | 317 base::MessageLoop::current()->RunUntilIdle(); |
266 EXPECT_EQ(kTitle, article_proto_->title()); | 318 EXPECT_EQ(kTitle, article_proto_->title()); |
267 EXPECT_EQ(article_proto_->pages_size(), kNumPages); | 319 EXPECT_EQ(article_proto_->pages_size(), kNumPages); |
268 for (int page_num = 0; page_num < kNumPages; ++page_num) { | 320 for (int page_num = 0; page_num < kNumPages; ++page_num) { |
269 const DistilledPageProto& page = article_proto_->pages(page_num); | 321 const DistilledPageProto& page = article_proto_->pages(page_num); |
270 EXPECT_EQ(content[page_num], page.html()); | 322 EXPECT_EQ(content[page_num], page.html()); |
271 EXPECT_EQ(page_urls[page_num], page.url()); | 323 EXPECT_EQ(page_urls[page_num], page.url()); |
272 EXPECT_EQ(image_indices[page_num].size(), | 324 EXPECT_EQ(image_indices[page_num].size(), |
273 static_cast<size_t>(page.image_size())); | 325 static_cast<size_t>(page.image_size())); |
274 for (size_t img_num = 0; img_num < image_indices[page_num].size(); | 326 for (size_t img_num = 0; img_num < image_indices[page_num].size(); |
275 ++img_num) { | 327 ++img_num) { |
276 EXPECT_EQ(kImageData[image_indices[page_num][img_num]], | 328 EXPECT_EQ(kImageData[image_indices[page_num][img_num]], |
277 page.image(img_num).data()); | 329 page.image(img_num).data()); |
278 EXPECT_EQ(GetImageName(page_num + 1, img_num), | 330 EXPECT_EQ(GetImageName(page_num + 1, img_num), |
279 page.image(img_num).name()); | 331 page.image(img_num).name()); |
280 } | 332 } |
281 } | 333 } |
282 } | 334 } |
283 | 335 |
284 TEST_F(DistillerTest, DistillLinkLoop) { | 336 TEST_F(DistillerTest, DistillLinkLoop) { |
285 base::MessageLoopForUI loop; | 337 base::MessageLoopForUI loop; |
286 // Create a loop, the next page is same as the current page. This could | 338 // Create a loop, the next page is same as the current page. This could |
287 // happen if javascript misparses a next page link. | 339 // happen if javascript misparses a next page link. |
288 scoped_ptr<base::ListValue> list = | 340 scoped_ptr<base::ListValue> list = |
289 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); | 341 CreateDistilledValueReturnedFromJS(kTitle, kContent, vector<int>(), kURL); |
290 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 342 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
291 .WillOnce(CreateMockDistillerPage(list.get(), GURL(kURL))); | 343 .WillOnce(CreateMockDistillerPage(list.get(), GURL(kURL))); |
292 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 344 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
293 distiller_->Init(); | 345 distiller_->Init(); |
294 distiller_->DistillPage( | 346 DistillPage(kURL); |
295 GURL(kURL), | |
296 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
297 base::MessageLoop::current()->RunUntilIdle(); | 347 base::MessageLoop::current()->RunUntilIdle(); |
298 EXPECT_EQ(kTitle, article_proto_->title()); | 348 EXPECT_EQ(kTitle, article_proto_->title()); |
299 EXPECT_EQ(article_proto_->pages_size(), 1); | 349 EXPECT_EQ(article_proto_->pages_size(), 1); |
300 } | 350 } |
301 | 351 |
302 TEST_F(DistillerTest, CheckMaxPageLimit) { | 352 TEST_F(DistillerTest, CheckMaxPageLimit) { |
cjhopman
2014/02/28 02:38:14
Just noticed: This should probably be three separa
shashi
2014/03/02 02:53:40
Good catch, just realized that I messed up the reb
| |
303 base::MessageLoopForUI loop; | 353 base::MessageLoopForUI loop; |
304 const size_t kMaxPagesInArticle = 10; | 354 const size_t kMaxPagesInArticle = 10; |
305 string page_urls[kMaxPagesInArticle]; | 355 string page_urls[kMaxPagesInArticle]; |
306 scoped_ptr<base::ListValue> list[kMaxPagesInArticle]; | 356 scoped_ptr<base::ListValue> list[kMaxPagesInArticle]; |
307 | 357 |
308 // Note: Next page url of the last page of article is set. So distiller will | 358 // Note: Next page url of the last page of article is set. So distiller will |
309 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not | 359 // try to do kMaxPagesInArticle + 1 calls if the max article limit does not |
310 // work. | 360 // work. |
311 string url_prefix = "http://a.com/"; | 361 string url_prefix = "http://a.com/"; |
312 for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) { | 362 for (size_t page_num = 0; page_num < kMaxPagesInArticle; ++page_num) { |
313 page_urls[page_num] = url_prefix + base::IntToString(page_num + 1); | 363 page_urls[page_num] = url_prefix + base::IntToString(page_num + 1); |
314 string content = "Content for page:" + base::IntToString(page_num); | 364 string content = "Content for page:" + base::IntToString(page_num); |
315 string next_page_url = url_prefix + base::IntToString(page_num + 2); | 365 string next_page_url = url_prefix + base::IntToString(page_num + 2); |
316 list[page_num] = CreateDistilledValueReturnedFromJS( | 366 list[page_num] = CreateDistilledValueReturnedFromJS( |
317 kTitle, content, vector<int>(), next_page_url); | 367 kTitle, content, vector<int>(), next_page_url); |
318 } | 368 } |
319 | 369 |
320 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 370 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
321 .WillOnce(CreateMockDistillerPages( | 371 .WillOnce(CreateMockDistillerPages( |
322 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0)); | 372 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0)); |
323 | 373 |
324 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 374 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
325 | 375 |
326 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 376 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
327 | 377 |
328 distiller_->Init(); | 378 distiller_->Init(); |
329 distiller_->DistillPage( | 379 DistillPage(page_urls[0]); |
330 GURL(page_urls[0]), | |
331 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
332 base::MessageLoop::current()->RunUntilIdle(); | 380 base::MessageLoop::current()->RunUntilIdle(); |
333 EXPECT_EQ(kTitle, article_proto_->title()); | 381 EXPECT_EQ(kTitle, article_proto_->title()); |
334 EXPECT_EQ(kMaxPagesInArticle, | 382 EXPECT_EQ(kMaxPagesInArticle, |
335 static_cast<size_t>(article_proto_->pages_size())); | 383 static_cast<size_t>(article_proto_->pages_size())); |
336 | 384 |
337 // Now check if distilling an article with exactly the page limit works by | 385 // Now check if distilling an article with exactly the page limit works by |
338 // resetting the next page url of the last page of the article. | 386 // resetting the next page url of the last page of the article. |
339 list[kMaxPagesInArticle - 1] = | 387 list[kMaxPagesInArticle - 1] = |
340 CreateDistilledValueReturnedFromJS(kTitle, "Content", vector<int>(), ""); | 388 CreateDistilledValueReturnedFromJS(kTitle, "Content", vector<int>(), ""); |
341 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 389 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
342 .WillOnce(CreateMockDistillerPages( | 390 .WillOnce(CreateMockDistillerPages( |
343 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0)); | 391 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0)); |
344 | 392 |
345 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 393 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
346 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 394 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
347 | 395 |
348 distiller_->Init(); | 396 distiller_->Init(); |
349 distiller_->DistillPage( | 397 DistillPage(page_urls[0]); |
350 GURL(page_urls[0]), | |
351 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
352 base::MessageLoop::current()->RunUntilIdle(); | 398 base::MessageLoop::current()->RunUntilIdle(); |
353 EXPECT_EQ(kTitle, article_proto_->title()); | 399 EXPECT_EQ(kTitle, article_proto_->title()); |
354 EXPECT_EQ(kMaxPagesInArticle, | 400 EXPECT_EQ(kMaxPagesInArticle, |
355 static_cast<size_t>(article_proto_->pages_size())); | 401 static_cast<size_t>(article_proto_->pages_size())); |
356 | 402 |
357 // Now check if distilling an article with exactly the page limit works by | 403 // 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. | 404 // resetting the next page url of the last page of the article. |
359 list[kMaxPagesInArticle - 1] = | 405 list[kMaxPagesInArticle - 1] = |
360 CreateDistilledValueReturnedFromJS(kTitle, "Content", vector<int>(), ""); | 406 CreateDistilledValueReturnedFromJS(kTitle, "Content", vector<int>(), ""); |
361 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 407 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
362 .WillOnce(CreateMockDistillerPages( | 408 .WillOnce(CreateMockDistillerPages( |
363 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0)); | 409 list, page_urls, static_cast<int>(kMaxPagesInArticle), 0)); |
364 | 410 |
365 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 411 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
366 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); | 412 distiller_->SetMaxNumPagesInArticle(kMaxPagesInArticle); |
367 | 413 |
368 distiller_->Init(); | 414 distiller_->Init(); |
369 distiller_->DistillPage( | 415 DistillPage(page_urls[0]); |
370 GURL(page_urls[0]), | |
371 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
372 base::MessageLoop::current()->RunUntilIdle(); | 416 base::MessageLoop::current()->RunUntilIdle(); |
373 EXPECT_EQ(kTitle, article_proto_->title()); | 417 EXPECT_EQ(kTitle, article_proto_->title()); |
374 EXPECT_EQ(kMaxPagesInArticle, | 418 EXPECT_EQ(kMaxPagesInArticle, |
375 static_cast<size_t>(article_proto_->pages_size())); | 419 static_cast<size_t>(article_proto_->pages_size())); |
376 } | 420 } |
377 | 421 |
378 TEST_F(DistillerTest, SinglePageDistillationFailure) { | 422 TEST_F(DistillerTest, SinglePageDistillationFailure) { |
379 base::MessageLoopForUI loop; | 423 base::MessageLoopForUI loop; |
380 // To simulate failure return a null value. | 424 // To simulate failure return a null value. |
381 scoped_ptr<base::Value> nullValue(base::Value::CreateNullValue()); | 425 scoped_ptr<base::Value> nullValue(base::Value::CreateNullValue()); |
382 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 426 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
383 .WillOnce(CreateMockDistillerPage(nullValue.get(), GURL(kURL))); | 427 .WillOnce(CreateMockDistillerPage(nullValue.get(), GURL(kURL))); |
384 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 428 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
385 distiller_->Init(); | 429 distiller_->Init(); |
386 distiller_->DistillPage( | 430 DistillPage(kURL); |
387 GURL(kURL), | |
388 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
389 base::MessageLoop::current()->RunUntilIdle(); | 431 base::MessageLoop::current()->RunUntilIdle(); |
390 EXPECT_EQ("", article_proto_->title()); | 432 EXPECT_EQ("", article_proto_->title()); |
391 EXPECT_EQ(0, article_proto_->pages_size()); | 433 EXPECT_EQ(0, article_proto_->pages_size()); |
392 } | 434 } |
393 | 435 |
394 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { | 436 TEST_F(DistillerTest, MultiplePagesDistillationFailure) { |
395 base::MessageLoopForUI loop; | 437 base::MessageLoopForUI loop; |
396 const int kNumPages = 8; | 438 const int kNumPages = 8; |
397 string content[kNumPages]; | 439 string content[kNumPages]; |
398 string page_urls[kNumPages]; | 440 string page_urls[kNumPages]; |
(...skipping 13 matching lines...) Expand all Loading... | |
412 } | 454 } |
413 } | 455 } |
414 | 456 |
415 // Expect only calls till the failed page number. | 457 // Expect only calls till the failed page number. |
416 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 458 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
417 .WillOnce(CreateMockDistillerPages( | 459 .WillOnce(CreateMockDistillerPages( |
418 distilled_values, page_urls, failed_page_num + 1, 0)); | 460 distilled_values, page_urls, failed_page_num + 1, 0)); |
419 | 461 |
420 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 462 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
421 distiller_->Init(); | 463 distiller_->Init(); |
422 distiller_->DistillPage( | 464 DistillPage(page_urls[0]); |
423 GURL(page_urls[0]), | |
424 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
425 base::MessageLoop::current()->RunUntilIdle(); | 465 base::MessageLoop::current()->RunUntilIdle(); |
426 EXPECT_EQ(kTitle, article_proto_->title()); | 466 EXPECT_EQ(kTitle, article_proto_->title()); |
427 EXPECT_EQ(article_proto_->pages_size(), failed_page_num); | 467 EXPECT_EQ(article_proto_->pages_size(), failed_page_num); |
428 for (int page_num = 0; page_num < failed_page_num; ++page_num) { | 468 for (int page_num = 0; page_num < failed_page_num; ++page_num) { |
429 const DistilledPageProto& page = article_proto_->pages(page_num); | 469 const DistilledPageProto& page = article_proto_->pages(page_num); |
430 EXPECT_EQ(content[page_num], page.html()); | 470 EXPECT_EQ(content[page_num], page.html()); |
431 EXPECT_EQ(page_urls[page_num], page.url()); | 471 EXPECT_EQ(page_urls[page_num], page.url()); |
432 } | 472 } |
433 } | 473 } |
434 | 474 |
435 TEST_F(DistillerTest, DistillPreviousPage) { | 475 TEST_F(DistillerTest, DistillPreviousPage) { |
436 base::MessageLoopForUI loop; | 476 base::MessageLoopForUI loop; |
437 const int kNumPages = 8; | 477 const int kNumPages = 8; |
438 string content[kNumPages]; | 478 string content[kNumPages]; |
439 string page_urls[kNumPages]; | 479 string page_urls[kNumPages]; |
440 scoped_ptr<base::Value> distilled_values[kNumPages]; | 480 scoped_ptr<base::Value> distilled_values[kNumPages]; |
441 | 481 |
442 // The page number of the article on which distillation starts. | 482 // The page number of the article on which distillation starts. |
443 int start_page_number = 3; | 483 int start_page_num = 3; |
444 string url_prefix = "http://a.com/"; | 484 string url_prefix = "http://a.com/"; |
445 for (int page_no = 0; page_no < kNumPages; ++page_no) { | 485 for (int page_num = 0; page_num < kNumPages; ++page_num) { |
446 page_urls[page_no] = url_prefix + base::IntToString(page_no); | 486 page_urls[page_num] = url_prefix + base::IntToString(page_num); |
447 content[page_no] = "Content for page:" + base::IntToString(page_no); | 487 content[page_num] = "Content for page:" + base::IntToString(page_num); |
448 string next_page_url = (page_no + 1 < kNumPages) | 488 string next_page_url = (page_num + 1 < kNumPages) |
449 ? url_prefix + base::IntToString(page_no + 1) | 489 ? url_prefix + base::IntToString(page_num + 1) |
450 : ""; | 490 : ""; |
451 string prev_page_url = (page_no > 0) ? page_urls[page_no - 1] : ""; | 491 string prev_page_url = (page_num > 0) ? page_urls[page_num - 1] : ""; |
452 distilled_values[page_no] = CreateDistilledValueReturnedFromJS( | 492 distilled_values[page_num] = CreateDistilledValueReturnedFromJS( |
453 kTitle, content[page_no], vector<int>(), next_page_url, prev_page_url); | 493 kTitle, content[page_num], vector<int>(), next_page_url, prev_page_url); |
454 } | 494 } |
455 | 495 |
456 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | 496 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) |
457 .WillOnce(CreateMockDistillerPages( | 497 .WillOnce(CreateMockDistillerPages( |
458 distilled_values, page_urls, kNumPages, start_page_number)); | 498 distilled_values, page_urls, kNumPages, start_page_num)); |
459 | 499 |
460 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | 500 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); |
461 distiller_->Init(); | 501 distiller_->Init(); |
462 distiller_->DistillPage( | 502 DistillPage(page_urls[start_page_num]); |
463 GURL(page_urls[start_page_number]), | |
464 base::Bind(&DistillerTest::OnDistillPageDone, base::Unretained(this))); | |
465 base::MessageLoop::current()->RunUntilIdle(); | 503 base::MessageLoop::current()->RunUntilIdle(); |
466 EXPECT_EQ(kTitle, article_proto_->title()); | 504 EXPECT_EQ(kTitle, article_proto_->title()); |
467 EXPECT_EQ(kNumPages, article_proto_->pages_size()); | 505 EXPECT_EQ(kNumPages, article_proto_->pages_size()); |
468 for (int page_no = 0; page_no < kNumPages; ++page_no) { | 506 for (int page_num = 0; page_num < kNumPages; ++page_num) { |
469 const DistilledPageProto& page = article_proto_->pages(page_no); | 507 const DistilledPageProto& page = article_proto_->pages(page_num); |
470 EXPECT_EQ(content[page_no], page.html()); | 508 EXPECT_EQ(content[page_num], page.html()); |
471 EXPECT_EQ(page_urls[page_no], page.url()); | 509 EXPECT_EQ(page_urls[page_num], page.url()); |
472 } | 510 } |
473 } | 511 } |
474 | 512 |
513 TEST_F(DistillerTest, IncrementalUpdates) { | |
514 base::MessageLoopForUI loop; | |
515 const int kNumPages = 8; | |
516 string content[kNumPages]; | |
517 string page_urls[kNumPages]; | |
518 scoped_ptr<base::Value> distilled_values[kNumPages]; | |
519 | |
520 // The page number of the article on which distillation starts. | |
521 int start_page_num = 3; | |
522 string url_prefix = "http://a.com/"; | |
523 for (int page_num = 0; page_num < kNumPages; ++page_num) { | |
524 page_urls[page_num] = url_prefix + base::IntToString(page_num); | |
525 content[page_num] = "Content for page:" + base::IntToString(page_num); | |
526 string next_page_url = (page_num + 1 < kNumPages) | |
527 ? url_prefix + base::IntToString(page_num + 1) | |
528 : ""; | |
529 string prev_page_url = (page_num > 0) ? page_urls[page_num - 1] : ""; | |
530 distilled_values[page_num] = CreateDistilledValueReturnedFromJS( | |
531 kTitle, content[page_num], vector<int>(), next_page_url, prev_page_url); | |
532 } | |
533 | |
534 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | |
535 .WillOnce(CreateMockDistillerPages( | |
536 distilled_values, page_urls, kNumPages, start_page_num)); | |
537 | |
538 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
539 distiller_->Init(); | |
540 DistillPage(page_urls[start_page_num]); | |
541 base::MessageLoop::current()->RunUntilIdle(); | |
542 EXPECT_EQ(kTitle, article_proto_->title()); | |
543 EXPECT_EQ(kNumPages, article_proto_->pages_size()); | |
544 EXPECT_EQ(static_cast<size_t>(kNumPages), in_sequence_updates_.size()); | |
545 | |
546 VerifyIncrementalUpdatesMatch( | |
547 start_page_num, kNumPages, page_urls, content, in_sequence_updates_); | |
548 } | |
549 | |
550 TEST_F(DistillerTest, IncrementalUpdatesDoNotDeleteFinalArticle) { | |
551 base::MessageLoopForUI loop; | |
552 const int kNumPages = 8; | |
553 string content[kNumPages]; | |
554 string page_urls[kNumPages]; | |
555 scoped_ptr<base::Value> distilled_values[kNumPages]; | |
556 | |
557 // The page number of the article on which distillation starts. | |
558 int start_page_num = 3; | |
559 string url_prefix = "http://a.com/"; | |
560 for (int page_num = 0; page_num < kNumPages; ++page_num) { | |
561 page_urls[page_num] = url_prefix + base::IntToString(page_num); | |
562 content[page_num] = "Content for page:" + base::IntToString(page_num); | |
563 string next_page_url = (page_num + 1 < kNumPages) | |
564 ? url_prefix + base::IntToString(page_num + 1) | |
565 : ""; | |
566 string prev_page_url = (page_num > 0) ? page_urls[page_num - 1] : ""; | |
567 distilled_values[page_num] = CreateDistilledValueReturnedFromJS( | |
568 kTitle, content[page_num], vector<int>(), next_page_url, prev_page_url); | |
569 } | |
570 | |
571 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | |
572 .WillOnce(CreateMockDistillerPages( | |
573 distilled_values, page_urls, kNumPages, start_page_num)); | |
574 | |
575 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
576 distiller_->Init(); | |
577 DistillPage(page_urls[start_page_num]); | |
578 base::MessageLoop::current()->RunUntilIdle(); | |
579 EXPECT_EQ(static_cast<size_t>(kNumPages), in_sequence_updates_.size()); | |
580 | |
581 in_sequence_updates_.clear(); | |
582 | |
583 // Should still be able to access article and pages. | |
584 EXPECT_EQ(kTitle, article_proto_->title()); | |
585 EXPECT_EQ(kNumPages, article_proto_->pages_size()); | |
586 for (int page_num = 0; page_num < kNumPages; ++page_num) { | |
587 const DistilledPageProto& page = article_proto_->pages(page_num); | |
588 EXPECT_EQ(content[page_num], page.html()); | |
589 EXPECT_EQ(page_urls[page_num], page.url()); | |
590 } | |
591 } | |
592 | |
593 TEST_F(DistillerTest, DeletingArticleDoesNotInterfereWithUpdates) { | |
594 base::MessageLoopForUI loop; | |
595 const int kNumPages = 8; | |
596 string content[kNumPages]; | |
597 string page_urls[kNumPages]; | |
598 scoped_ptr<base::Value> distilled_values[kNumPages]; | |
599 | |
600 // The page number of the article on which distillation starts. | |
601 int start_page_num = 3; | |
602 string url_prefix = "http://a.com/"; | |
603 for (int page_num = 0; page_num < kNumPages; ++page_num) { | |
604 page_urls[page_num] = url_prefix + base::IntToString(page_num); | |
605 content[page_num] = "Content for page:" + base::IntToString(page_num); | |
606 string next_page_url = (page_num + 1 < kNumPages) | |
607 ? url_prefix + base::IntToString(page_num + 1) | |
608 : ""; | |
609 string prev_page_url = (page_num > 0) ? page_urls[page_num - 1] : ""; | |
610 distilled_values[page_num] = CreateDistilledValueReturnedFromJS( | |
611 kTitle, content[page_num], vector<int>(), next_page_url, prev_page_url); | |
612 } | |
cjhopman
2014/02/28 02:38:14
This setup code seems to be repeated three times.
shashi
2014/03/02 02:53:40
Good point, done, though it might make it slightly
| |
613 | |
614 EXPECT_CALL(page_factory_, CreateDistillerPageMock(_)) | |
615 .WillOnce(CreateMockDistillerPages( | |
616 distilled_values, page_urls, kNumPages, start_page_num)); | |
617 | |
618 distiller_.reset(new DistillerImpl(page_factory_, url_fetcher_factory_)); | |
619 distiller_->Init(); | |
620 DistillPage(page_urls[start_page_num]); | |
621 base::MessageLoop::current()->RunUntilIdle(); | |
622 EXPECT_EQ(static_cast<size_t>(kNumPages), in_sequence_updates_.size()); | |
623 EXPECT_EQ(kTitle, article_proto_->title()); | |
624 EXPECT_EQ(kNumPages, article_proto_->pages_size()); | |
625 | |
626 // Delete the article. | |
627 article_proto_.reset(); | |
628 VerifyIncrementalUpdatesMatch( | |
629 start_page_num, kNumPages, page_urls, content, in_sequence_updates_); | |
630 } | |
631 | |
475 } // namespace dom_distiller | 632 } // namespace dom_distiller |
OLD | NEW |