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 "components/dom_distiller/core/task_tracker.h" | 5 #include "components/dom_distiller/core/task_tracker.h" |
6 | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "components/dom_distiller/core/article_distillation_update.h" | 8 #include "components/dom_distiller/core/article_distillation_update.h" |
9 #include "components/dom_distiller/core/article_entry.h" | 9 #include "components/dom_distiller/core/article_entry.h" |
10 #include "components/dom_distiller/core/distilled_content_store.h" | |
10 #include "components/dom_distiller/core/fake_distiller.h" | 11 #include "components/dom_distiller/core/fake_distiller.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 using testing::Return; | 14 using testing::Return; |
14 using testing::_; | 15 using testing::_; |
15 | 16 |
16 namespace dom_distiller { | 17 namespace dom_distiller { |
17 namespace test { | 18 namespace test { |
18 | 19 |
19 class FakeViewRequestDelegate : public ViewRequestDelegate { | 20 class FakeViewRequestDelegate : public ViewRequestDelegate { |
20 public: | 21 public: |
21 virtual ~FakeViewRequestDelegate() {} | 22 virtual ~FakeViewRequestDelegate() {} |
22 MOCK_METHOD1(OnArticleReady, | 23 MOCK_METHOD1(OnArticleReady, |
23 void(const DistilledArticleProto* article_proto)); | 24 void(const DistilledArticleProto* article_proto)); |
24 MOCK_METHOD1(OnArticleUpdated, | 25 MOCK_METHOD1(OnArticleUpdated, |
25 void(ArticleDistillationUpdate article_update)); | 26 void(ArticleDistillationUpdate article_update)); |
26 }; | 27 }; |
27 | 28 |
29 class MockContentStore : public DistilledContentStore { | |
30 public: | |
31 MOCK_CONST_METHOD2(LoadContent, | |
32 void(const ArticleEntry& entry, LoadCallback callback)); | |
33 MOCK_METHOD3(SaveContent, | |
34 void(const ArticleEntry& entry, | |
35 const DistilledArticleProto& proto, | |
36 SaveCallback callback)); | |
37 }; | |
38 | |
28 class TestCancelCallback { | 39 class TestCancelCallback { |
29 public: | 40 public: |
30 TestCancelCallback() : cancelled_(false) {} | 41 TestCancelCallback() : cancelled_(false) {} |
31 TaskTracker::CancelCallback GetCallback() { | 42 TaskTracker::CancelCallback GetCallback() { |
32 return base::Bind(&TestCancelCallback::Cancel, base::Unretained(this)); | 43 return base::Bind(&TestCancelCallback::Cancel, base::Unretained(this)); |
33 } | 44 } |
34 void Cancel(TaskTracker*) { cancelled_ = true; } | 45 void Cancel(TaskTracker*) { cancelled_ = true; } |
35 bool Cancelled() { return cancelled_; } | 46 bool Cancelled() { return cancelled_; } |
36 | 47 |
37 private: | 48 private: |
(...skipping 28 matching lines...) Expand all Loading... | |
66 protected: | 77 protected: |
67 scoped_ptr<base::MessageLoop> message_loop_; | 78 scoped_ptr<base::MessageLoop> message_loop_; |
68 std::string entry_id_; | 79 std::string entry_id_; |
69 GURL page_0_url_; | 80 GURL page_0_url_; |
70 GURL page_1_url_; | 81 GURL page_1_url_; |
71 }; | 82 }; |
72 | 83 |
73 TEST_F(DomDistillerTaskTrackerTest, TestHasEntryId) { | 84 TEST_F(DomDistillerTaskTrackerTest, TestHasEntryId) { |
74 MockDistillerFactory distiller_factory; | 85 MockDistillerFactory distiller_factory; |
75 TestCancelCallback cancel_callback; | 86 TestCancelCallback cancel_callback; |
76 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 87 TaskTracker task_tracker( |
88 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); | |
77 EXPECT_TRUE(task_tracker.HasEntryId(entry_id_)); | 89 EXPECT_TRUE(task_tracker.HasEntryId(entry_id_)); |
78 EXPECT_FALSE(task_tracker.HasEntryId("other_id")); | 90 EXPECT_FALSE(task_tracker.HasEntryId("other_id")); |
79 } | 91 } |
80 | 92 |
81 TEST_F(DomDistillerTaskTrackerTest, TestHasUrl) { | 93 TEST_F(DomDistillerTaskTrackerTest, TestHasUrl) { |
82 MockDistillerFactory distiller_factory; | 94 MockDistillerFactory distiller_factory; |
83 TestCancelCallback cancel_callback; | 95 TestCancelCallback cancel_callback; |
84 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 96 TaskTracker task_tracker( |
97 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); | |
85 EXPECT_TRUE(task_tracker.HasUrl(page_0_url_)); | 98 EXPECT_TRUE(task_tracker.HasUrl(page_0_url_)); |
86 EXPECT_TRUE(task_tracker.HasUrl(page_1_url_)); | 99 EXPECT_TRUE(task_tracker.HasUrl(page_1_url_)); |
87 EXPECT_FALSE(task_tracker.HasUrl(GURL("http://other.url/"))); | 100 EXPECT_FALSE(task_tracker.HasUrl(GURL("http://other.url/"))); |
88 } | 101 } |
89 | 102 |
90 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelled) { | 103 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelled) { |
91 MockDistillerFactory distiller_factory; | 104 MockDistillerFactory distiller_factory; |
92 TestCancelCallback cancel_callback; | 105 TestCancelCallback cancel_callback; |
93 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 106 TaskTracker task_tracker( |
107 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); | |
94 | 108 |
95 FakeViewRequestDelegate viewer_delegate; | 109 FakeViewRequestDelegate viewer_delegate; |
96 FakeViewRequestDelegate viewer_delegate2; | 110 FakeViewRequestDelegate viewer_delegate2; |
97 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | 111 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
98 scoped_ptr<ViewerHandle> handle2(task_tracker.AddViewer(&viewer_delegate2)); | 112 scoped_ptr<ViewerHandle> handle2(task_tracker.AddViewer(&viewer_delegate2)); |
99 | 113 |
100 EXPECT_FALSE(cancel_callback.Cancelled()); | 114 EXPECT_FALSE(cancel_callback.Cancelled()); |
101 handle.reset(); | 115 handle.reset(); |
102 EXPECT_FALSE(cancel_callback.Cancelled()); | 116 EXPECT_FALSE(cancel_callback.Cancelled()); |
103 handle2.reset(); | 117 handle2.reset(); |
104 EXPECT_TRUE(cancel_callback.Cancelled()); | 118 EXPECT_TRUE(cancel_callback.Cancelled()); |
105 } | 119 } |
106 | 120 |
107 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelledWithSaveRequest) { | 121 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelledWithSaveRequest) { |
108 MockDistillerFactory distiller_factory; | 122 MockDistillerFactory distiller_factory; |
109 TestCancelCallback cancel_callback; | 123 TestCancelCallback cancel_callback; |
110 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 124 TaskTracker task_tracker( |
125 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); | |
111 | 126 |
112 FakeViewRequestDelegate viewer_delegate; | 127 FakeViewRequestDelegate viewer_delegate; |
113 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | 128 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
114 EXPECT_FALSE(cancel_callback.Cancelled()); | 129 EXPECT_FALSE(cancel_callback.Cancelled()); |
115 | 130 |
116 MockSaveCallback save_callback; | 131 MockSaveCallback save_callback; |
117 task_tracker.AddSaveCallback( | 132 task_tracker.AddSaveCallback( |
118 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); | 133 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); |
119 handle.reset(); | 134 handle.reset(); |
120 | 135 |
121 // Since there is a pending save request, the task shouldn't be cancelled. | 136 // Since there is a pending save request, the task shouldn't be cancelled. |
122 EXPECT_FALSE(cancel_callback.Cancelled()); | 137 EXPECT_FALSE(cancel_callback.Cancelled()); |
123 } | 138 } |
124 | 139 |
125 TEST_F(DomDistillerTaskTrackerTest, TestViewerNotifiedOnDistillationComplete) { | 140 TEST_F(DomDistillerTaskTrackerTest, TestViewerNotifiedOnDistillationComplete) { |
126 MockDistillerFactory distiller_factory; | 141 MockDistillerFactory distiller_factory; |
127 FakeDistiller* distiller = new FakeDistiller(true); | 142 FakeDistiller* distiller = new FakeDistiller(true); |
128 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | 143 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
129 .WillOnce(Return(distiller)); | 144 .WillOnce(Return(distiller)); |
130 TestCancelCallback cancel_callback; | 145 TestCancelCallback cancel_callback; |
131 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 146 TaskTracker task_tracker( |
147 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); | |
132 | 148 |
133 FakeViewRequestDelegate viewer_delegate; | 149 FakeViewRequestDelegate viewer_delegate; |
134 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | 150 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
135 base::RunLoop().RunUntilIdle(); | 151 base::RunLoop().RunUntilIdle(); |
136 | 152 |
137 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); | 153 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); |
138 | 154 |
139 task_tracker.StartDistiller(&distiller_factory); | 155 task_tracker.StartDistiller(&distiller_factory); |
140 base::RunLoop().RunUntilIdle(); | 156 base::RunLoop().RunUntilIdle(); |
141 | 157 |
142 EXPECT_FALSE(cancel_callback.Cancelled()); | 158 EXPECT_FALSE(cancel_callback.Cancelled()); |
143 } | 159 } |
144 | 160 |
145 TEST_F(DomDistillerTaskTrackerTest, | 161 TEST_F(DomDistillerTaskTrackerTest, |
146 TestSaveCallbackCalledOnDistillationComplete) { | 162 TestSaveCallbackCalledOnDistillationComplete) { |
147 MockDistillerFactory distiller_factory; | 163 MockDistillerFactory distiller_factory; |
148 FakeDistiller* distiller = new FakeDistiller(true); | 164 FakeDistiller* distiller = new FakeDistiller(true); |
149 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | 165 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
150 .WillOnce(Return(distiller)); | 166 .WillOnce(Return(distiller)); |
151 TestCancelCallback cancel_callback; | 167 TestCancelCallback cancel_callback; |
152 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 168 TaskTracker task_tracker( |
169 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); | |
153 | 170 |
154 MockSaveCallback save_callback; | 171 MockSaveCallback save_callback; |
155 task_tracker.AddSaveCallback( | 172 task_tracker.AddSaveCallback( |
156 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); | 173 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); |
157 base::RunLoop().RunUntilIdle(); | 174 base::RunLoop().RunUntilIdle(); |
158 | 175 |
159 EXPECT_CALL(save_callback, Save(_, _, _)); | 176 EXPECT_CALL(save_callback, Save(_, _, _)); |
160 | 177 |
161 task_tracker.StartDistiller(&distiller_factory); | 178 task_tracker.StartDistiller(&distiller_factory); |
162 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
163 | 180 |
164 EXPECT_TRUE(cancel_callback.Cancelled()); | 181 EXPECT_TRUE(cancel_callback.Cancelled()); |
165 } | 182 } |
166 | 183 |
184 DistilledArticleProto CreateDistilledArticleForEntry( | |
185 const ArticleEntry& entry) { | |
186 DistilledArticleProto article; | |
187 for (int i = 0; i < entry.pages_size(); ++i) { | |
188 DistilledPageProto* page = article.add_pages(); | |
189 page->set_url(entry.pages(i).url()); | |
190 page->set_html("<div>" + entry.pages(i).url() + "</div>"); | |
191 } | |
192 return article; | |
193 } | |
194 | |
195 TEST_F(DomDistillerTaskTrackerTest, TestBlobFetcher) { | |
196 ArticleEntry entry_with_blob = GetDefaultEntry(); | |
197 DistilledArticleProto stored_distilled_article = | |
198 CreateDistilledArticleForEntry(entry_with_blob); | |
199 InMemoryContentStore content_store; | |
200 content_store.InjectContent(entry_with_blob, stored_distilled_article); | |
201 TestCancelCallback cancel_callback; | |
202 | |
203 TaskTracker task_tracker( | |
204 entry_with_blob, cancel_callback.GetCallback(), &content_store); | |
205 | |
206 FakeViewRequestDelegate viewer_delegate; | |
207 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | |
208 base::RunLoop().RunUntilIdle(); | |
209 | |
210 const DistilledArticleProto* distilled_article; | |
211 | |
212 EXPECT_CALL(viewer_delegate, OnArticleReady(_)) | |
213 .WillOnce(testing::SaveArg<0>(&distilled_article)); | |
214 | |
215 task_tracker.StartBlobFetcher(); | |
216 base::RunLoop().RunUntilIdle(); | |
217 | |
218 EXPECT_EQ(stored_distilled_article.SerializeAsString(), | |
219 distilled_article->SerializeAsString()); | |
220 | |
221 EXPECT_FALSE(cancel_callback.Cancelled()); | |
222 } | |
223 | |
224 TEST_F(DomDistillerTaskTrackerTest, TestBlobFetcherFinishesFirst) { | |
225 MockDistillerFactory distiller_factory; | |
226 FakeDistiller* distiller = new FakeDistiller(false); | |
227 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | |
228 .WillOnce(Return(distiller)); | |
229 | |
230 ArticleEntry entry_with_blob = GetDefaultEntry(); | |
231 DistilledArticleProto stored_distilled_article = | |
232 CreateDistilledArticleForEntry(entry_with_blob); | |
233 InMemoryContentStore content_store; | |
234 content_store.InjectContent(entry_with_blob, stored_distilled_article); | |
235 TestCancelCallback cancel_callback; | |
236 TaskTracker task_tracker( | |
237 entry_with_blob, cancel_callback.GetCallback(), &content_store); | |
238 | |
239 FakeViewRequestDelegate viewer_delegate; | |
240 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | |
241 base::RunLoop().RunUntilIdle(); | |
242 | |
243 DistilledArticleProto distilled_article; | |
244 | |
245 EXPECT_CALL(viewer_delegate, OnArticleReady(_)) | |
246 .WillOnce(testing::SaveArgPointee<0>(&distilled_article)); | |
247 bool distiller_destroyed = false; | |
248 EXPECT_CALL(*distiller, Die()) | |
249 .WillOnce(testing::Assign(&distiller_destroyed, true)); | |
250 | |
251 task_tracker.StartDistiller(&distiller_factory); | |
252 task_tracker.StartBlobFetcher(); | |
253 base::RunLoop().RunUntilIdle(); | |
254 | |
255 testing::Mock::VerifyAndClearExpectations(&viewer_delegate); | |
256 EXPECT_EQ(stored_distilled_article.SerializeAsString(), | |
257 distilled_article.SerializeAsString()); | |
258 | |
259 EXPECT_TRUE(distiller_destroyed); | |
260 EXPECT_FALSE(cancel_callback.Cancelled()); | |
261 base::RunLoop().RunUntilIdle(); | |
262 } | |
263 | |
264 | |
nyquist
2014/04/14 22:42:22
Nit: some extra newlines here.
| |
265 | |
266 TEST_F(DomDistillerTaskTrackerTest, TestBlobFetcherWithoutBlob) { | |
267 MockDistillerFactory distiller_factory; | |
268 FakeDistiller* distiller = new FakeDistiller(false); | |
269 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | |
270 .WillOnce(Return(distiller)); | |
271 | |
272 ArticleEntry entry(GetDefaultEntry()); | |
273 InMemoryContentStore content_store; | |
274 scoped_ptr<DistilledArticleProto> distilled_article( | |
275 new DistilledArticleProto(CreateDistilledArticleForEntry(entry))); | |
276 | |
277 TestCancelCallback cancel_callback; | |
278 TaskTracker task_tracker( | |
279 GetDefaultEntry(), cancel_callback.GetCallback(), &content_store); | |
280 | |
281 FakeViewRequestDelegate viewer_delegate; | |
282 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | |
283 base::RunLoop().RunUntilIdle(); | |
284 | |
285 task_tracker.StartBlobFetcher(); | |
286 task_tracker.StartDistiller(&distiller_factory); | |
287 | |
288 // OnArticleReady shouldn't be called until distillation finishes (i.e. the | |
289 // blob fetcher shouldn't return distilled content). | |
290 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); | |
291 base::RunLoop().RunUntilIdle(); | |
292 | |
293 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); | |
294 distiller->RunDistillerCallback(distilled_article.Pass()); | |
295 base::RunLoop().RunUntilIdle(); | |
296 | |
297 EXPECT_FALSE(cancel_callback.Cancelled()); | |
298 } | |
299 | |
300 TEST_F(DomDistillerTaskTrackerTest, TestDistillerFailsFirst) { | |
301 MockDistillerFactory distiller_factory; | |
302 FakeDistiller* distiller = new FakeDistiller(false); | |
303 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | |
304 .WillOnce(Return(distiller)); | |
305 | |
306 ArticleEntry entry(GetDefaultEntry()); | |
307 MockContentStore content_store; | |
308 | |
309 TestCancelCallback cancel_callback; | |
310 TaskTracker task_tracker( | |
311 GetDefaultEntry(), cancel_callback.GetCallback(), &content_store); | |
312 | |
313 FakeViewRequestDelegate viewer_delegate; | |
314 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | |
315 | |
316 DistilledContentStore::LoadCallback content_store_load_callback; | |
317 EXPECT_CALL(content_store, LoadContent(_, _)).WillOnce( | |
318 testing::SaveArg<1>(&content_store_load_callback)); | |
319 | |
320 task_tracker.StartDistiller(&distiller_factory); | |
321 task_tracker.StartBlobFetcher(); | |
322 | |
323 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); | |
324 distiller->RunDistillerCallback( | |
325 scoped_ptr<DistilledArticleProto>(new DistilledArticleProto)); | |
326 base::RunLoop().RunUntilIdle(); | |
327 | |
328 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); | |
329 content_store_load_callback.Run( | |
330 true, | |
331 scoped_ptr<DistilledArticleProto>( | |
332 new DistilledArticleProto(CreateDistilledArticleForEntry(entry)))); | |
333 base::RunLoop().RunUntilIdle(); | |
334 | |
335 EXPECT_FALSE(cancel_callback.Cancelled()); | |
336 } | |
337 | |
338 TEST_F(DomDistillerTaskTrackerTest, ContentIsSaved) { | |
339 MockDistillerFactory distiller_factory; | |
340 FakeDistiller* distiller = new FakeDistiller(false); | |
341 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | |
342 .WillOnce(Return(distiller)); | |
343 | |
344 ArticleEntry entry(GetDefaultEntry()); | |
345 DistilledArticleProto distilled_article = | |
346 CreateDistilledArticleForEntry(entry); | |
347 | |
348 MockContentStore content_store; | |
349 TestCancelCallback cancel_callback; | |
350 TaskTracker task_tracker( | |
351 GetDefaultEntry(), cancel_callback.GetCallback(), &content_store); | |
352 | |
353 FakeViewRequestDelegate viewer_delegate; | |
354 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | |
355 | |
356 DistilledArticleProto stored_distilled_article; | |
357 DistilledContentStore::LoadCallback content_store_load_callback; | |
358 EXPECT_CALL(content_store, SaveContent(_, _, _)) | |
359 .WillOnce(testing::SaveArg<1>(&stored_distilled_article)); | |
360 | |
361 task_tracker.StartDistiller(&distiller_factory); | |
362 | |
363 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); | |
364 distiller->RunDistillerCallback(scoped_ptr<DistilledArticleProto>( | |
365 new DistilledArticleProto(distilled_article))); | |
366 base::RunLoop().RunUntilIdle(); | |
367 | |
368 ASSERT_EQ(stored_distilled_article.SerializeAsString(), | |
369 distilled_article.SerializeAsString()); | |
370 EXPECT_FALSE(cancel_callback.Cancelled()); | |
371 } | |
372 | |
167 } // namespace test | 373 } // namespace test |
168 } // namespace dom_distiller | 374 } // namespace dom_distiller |
OLD | NEW |