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 TEST_F(DomDistillerTaskTrackerTest, TestBlobFetcherWithoutBlob) { |
| 265 MockDistillerFactory distiller_factory; |
| 266 FakeDistiller* distiller = new FakeDistiller(false); |
| 267 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
| 268 .WillOnce(Return(distiller)); |
| 269 |
| 270 ArticleEntry entry(GetDefaultEntry()); |
| 271 InMemoryContentStore content_store; |
| 272 scoped_ptr<DistilledArticleProto> distilled_article( |
| 273 new DistilledArticleProto(CreateDistilledArticleForEntry(entry))); |
| 274 |
| 275 TestCancelCallback cancel_callback; |
| 276 TaskTracker task_tracker( |
| 277 GetDefaultEntry(), cancel_callback.GetCallback(), &content_store); |
| 278 |
| 279 FakeViewRequestDelegate viewer_delegate; |
| 280 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 281 base::RunLoop().RunUntilIdle(); |
| 282 |
| 283 task_tracker.StartBlobFetcher(); |
| 284 task_tracker.StartDistiller(&distiller_factory); |
| 285 |
| 286 // OnArticleReady shouldn't be called until distillation finishes (i.e. the |
| 287 // blob fetcher shouldn't return distilled content). |
| 288 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); |
| 289 base::RunLoop().RunUntilIdle(); |
| 290 |
| 291 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); |
| 292 distiller->RunDistillerCallback(distilled_article.Pass()); |
| 293 base::RunLoop().RunUntilIdle(); |
| 294 |
| 295 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 296 } |
| 297 |
| 298 TEST_F(DomDistillerTaskTrackerTest, TestDistillerFailsFirst) { |
| 299 MockDistillerFactory distiller_factory; |
| 300 FakeDistiller* distiller = new FakeDistiller(false); |
| 301 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
| 302 .WillOnce(Return(distiller)); |
| 303 |
| 304 ArticleEntry entry(GetDefaultEntry()); |
| 305 MockContentStore content_store; |
| 306 |
| 307 TestCancelCallback cancel_callback; |
| 308 TaskTracker task_tracker( |
| 309 GetDefaultEntry(), cancel_callback.GetCallback(), &content_store); |
| 310 |
| 311 FakeViewRequestDelegate viewer_delegate; |
| 312 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 313 |
| 314 DistilledContentStore::LoadCallback content_store_load_callback; |
| 315 EXPECT_CALL(content_store, LoadContent(_, _)).WillOnce( |
| 316 testing::SaveArg<1>(&content_store_load_callback)); |
| 317 |
| 318 task_tracker.StartDistiller(&distiller_factory); |
| 319 task_tracker.StartBlobFetcher(); |
| 320 |
| 321 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); |
| 322 distiller->RunDistillerCallback( |
| 323 scoped_ptr<DistilledArticleProto>(new DistilledArticleProto)); |
| 324 base::RunLoop().RunUntilIdle(); |
| 325 |
| 326 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); |
| 327 content_store_load_callback.Run( |
| 328 true, |
| 329 scoped_ptr<DistilledArticleProto>( |
| 330 new DistilledArticleProto(CreateDistilledArticleForEntry(entry)))); |
| 331 base::RunLoop().RunUntilIdle(); |
| 332 |
| 333 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 334 } |
| 335 |
| 336 TEST_F(DomDistillerTaskTrackerTest, ContentIsSaved) { |
| 337 MockDistillerFactory distiller_factory; |
| 338 FakeDistiller* distiller = new FakeDistiller(false); |
| 339 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
| 340 .WillOnce(Return(distiller)); |
| 341 |
| 342 ArticleEntry entry(GetDefaultEntry()); |
| 343 DistilledArticleProto distilled_article = |
| 344 CreateDistilledArticleForEntry(entry); |
| 345 |
| 346 MockContentStore content_store; |
| 347 TestCancelCallback cancel_callback; |
| 348 TaskTracker task_tracker( |
| 349 GetDefaultEntry(), cancel_callback.GetCallback(), &content_store); |
| 350 |
| 351 FakeViewRequestDelegate viewer_delegate; |
| 352 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 353 |
| 354 DistilledArticleProto stored_distilled_article; |
| 355 DistilledContentStore::LoadCallback content_store_load_callback; |
| 356 EXPECT_CALL(content_store, SaveContent(_, _, _)) |
| 357 .WillOnce(testing::SaveArg<1>(&stored_distilled_article)); |
| 358 |
| 359 task_tracker.StartDistiller(&distiller_factory); |
| 360 |
| 361 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); |
| 362 distiller->RunDistillerCallback(scoped_ptr<DistilledArticleProto>( |
| 363 new DistilledArticleProto(distilled_article))); |
| 364 base::RunLoop().RunUntilIdle(); |
| 365 |
| 366 ASSERT_EQ(stored_distilled_article.SerializeAsString(), |
| 367 distilled_article.SerializeAsString()); |
| 368 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 369 } |
| 370 |
167 } // namespace test | 371 } // namespace test |
168 } // namespace dom_distiller | 372 } // namespace dom_distiller |
OLD | NEW |