| 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 { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 protected: | 67 protected: |
| 67 scoped_ptr<base::MessageLoop> message_loop_; | 68 scoped_ptr<base::MessageLoop> message_loop_; |
| 68 std::string entry_id_; | 69 std::string entry_id_; |
| 69 GURL page_0_url_; | 70 GURL page_0_url_; |
| 70 GURL page_1_url_; | 71 GURL page_1_url_; |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 TEST_F(DomDistillerTaskTrackerTest, TestHasEntryId) { | 74 TEST_F(DomDistillerTaskTrackerTest, TestHasEntryId) { |
| 74 MockDistillerFactory distiller_factory; | 75 MockDistillerFactory distiller_factory; |
| 75 TestCancelCallback cancel_callback; | 76 TestCancelCallback cancel_callback; |
| 76 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 77 TaskTracker task_tracker( |
| 78 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); |
| 77 EXPECT_TRUE(task_tracker.HasEntryId(entry_id_)); | 79 EXPECT_TRUE(task_tracker.HasEntryId(entry_id_)); |
| 78 EXPECT_FALSE(task_tracker.HasEntryId("other_id")); | 80 EXPECT_FALSE(task_tracker.HasEntryId("other_id")); |
| 79 } | 81 } |
| 80 | 82 |
| 81 TEST_F(DomDistillerTaskTrackerTest, TestHasUrl) { | 83 TEST_F(DomDistillerTaskTrackerTest, TestHasUrl) { |
| 82 MockDistillerFactory distiller_factory; | 84 MockDistillerFactory distiller_factory; |
| 83 TestCancelCallback cancel_callback; | 85 TestCancelCallback cancel_callback; |
| 84 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 86 TaskTracker task_tracker( |
| 87 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); |
| 85 EXPECT_TRUE(task_tracker.HasUrl(page_0_url_)); | 88 EXPECT_TRUE(task_tracker.HasUrl(page_0_url_)); |
| 86 EXPECT_TRUE(task_tracker.HasUrl(page_1_url_)); | 89 EXPECT_TRUE(task_tracker.HasUrl(page_1_url_)); |
| 87 EXPECT_FALSE(task_tracker.HasUrl(GURL("http://other.url/"))); | 90 EXPECT_FALSE(task_tracker.HasUrl(GURL("http://other.url/"))); |
| 88 } | 91 } |
| 89 | 92 |
| 90 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelled) { | 93 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelled) { |
| 91 MockDistillerFactory distiller_factory; | 94 MockDistillerFactory distiller_factory; |
| 92 TestCancelCallback cancel_callback; | 95 TestCancelCallback cancel_callback; |
| 93 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 96 TaskTracker task_tracker( |
| 97 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); |
| 94 | 98 |
| 95 FakeViewRequestDelegate viewer_delegate; | 99 FakeViewRequestDelegate viewer_delegate; |
| 96 FakeViewRequestDelegate viewer_delegate2; | 100 FakeViewRequestDelegate viewer_delegate2; |
| 97 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | 101 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 98 scoped_ptr<ViewerHandle> handle2(task_tracker.AddViewer(&viewer_delegate2)); | 102 scoped_ptr<ViewerHandle> handle2(task_tracker.AddViewer(&viewer_delegate2)); |
| 99 | 103 |
| 100 EXPECT_FALSE(cancel_callback.Cancelled()); | 104 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 101 handle.reset(); | 105 handle.reset(); |
| 102 EXPECT_FALSE(cancel_callback.Cancelled()); | 106 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 103 handle2.reset(); | 107 handle2.reset(); |
| 104 EXPECT_TRUE(cancel_callback.Cancelled()); | 108 EXPECT_TRUE(cancel_callback.Cancelled()); |
| 105 } | 109 } |
| 106 | 110 |
| 107 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelledWithSaveRequest) { | 111 TEST_F(DomDistillerTaskTrackerTest, TestViewerCancelledWithSaveRequest) { |
| 108 MockDistillerFactory distiller_factory; | 112 MockDistillerFactory distiller_factory; |
| 109 TestCancelCallback cancel_callback; | 113 TestCancelCallback cancel_callback; |
| 110 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 114 TaskTracker task_tracker( |
| 115 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); |
| 111 | 116 |
| 112 FakeViewRequestDelegate viewer_delegate; | 117 FakeViewRequestDelegate viewer_delegate; |
| 113 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | 118 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 114 EXPECT_FALSE(cancel_callback.Cancelled()); | 119 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 115 | 120 |
| 116 MockSaveCallback save_callback; | 121 MockSaveCallback save_callback; |
| 117 task_tracker.AddSaveCallback( | 122 task_tracker.AddSaveCallback( |
| 118 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); | 123 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); |
| 119 handle.reset(); | 124 handle.reset(); |
| 120 | 125 |
| 121 // Since there is a pending save request, the task shouldn't be cancelled. | 126 // Since there is a pending save request, the task shouldn't be cancelled. |
| 122 EXPECT_FALSE(cancel_callback.Cancelled()); | 127 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 123 } | 128 } |
| 124 | 129 |
| 125 TEST_F(DomDistillerTaskTrackerTest, TestViewerNotifiedOnDistillationComplete) { | 130 TEST_F(DomDistillerTaskTrackerTest, TestViewerNotifiedOnDistillationComplete) { |
| 126 MockDistillerFactory distiller_factory; | 131 MockDistillerFactory distiller_factory; |
| 127 FakeDistiller* distiller = new FakeDistiller(true); | 132 FakeDistiller* distiller = new FakeDistiller(true); |
| 128 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | 133 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
| 129 .WillOnce(Return(distiller)); | 134 .WillOnce(Return(distiller)); |
| 130 TestCancelCallback cancel_callback; | 135 TestCancelCallback cancel_callback; |
| 131 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 136 TaskTracker task_tracker( |
| 137 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); |
| 132 | 138 |
| 133 FakeViewRequestDelegate viewer_delegate; | 139 FakeViewRequestDelegate viewer_delegate; |
| 134 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); | 140 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 135 base::RunLoop().RunUntilIdle(); | 141 base::RunLoop().RunUntilIdle(); |
| 136 | 142 |
| 137 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); | 143 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); |
| 138 | 144 |
| 139 task_tracker.StartDistiller(&distiller_factory); | 145 task_tracker.StartDistiller(&distiller_factory); |
| 140 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
| 141 | 147 |
| 142 EXPECT_FALSE(cancel_callback.Cancelled()); | 148 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 143 } | 149 } |
| 144 | 150 |
| 145 TEST_F(DomDistillerTaskTrackerTest, | 151 TEST_F(DomDistillerTaskTrackerTest, |
| 146 TestSaveCallbackCalledOnDistillationComplete) { | 152 TestSaveCallbackCalledOnDistillationComplete) { |
| 147 MockDistillerFactory distiller_factory; | 153 MockDistillerFactory distiller_factory; |
| 148 FakeDistiller* distiller = new FakeDistiller(true); | 154 FakeDistiller* distiller = new FakeDistiller(true); |
| 149 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) | 155 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
| 150 .WillOnce(Return(distiller)); | 156 .WillOnce(Return(distiller)); |
| 151 TestCancelCallback cancel_callback; | 157 TestCancelCallback cancel_callback; |
| 152 TaskTracker task_tracker(GetDefaultEntry(), cancel_callback.GetCallback()); | 158 TaskTracker task_tracker( |
| 159 GetDefaultEntry(), cancel_callback.GetCallback(), NULL); |
| 153 | 160 |
| 154 MockSaveCallback save_callback; | 161 MockSaveCallback save_callback; |
| 155 task_tracker.AddSaveCallback( | 162 task_tracker.AddSaveCallback( |
| 156 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); | 163 base::Bind(&MockSaveCallback::Save, base::Unretained(&save_callback))); |
| 157 base::RunLoop().RunUntilIdle(); | 164 base::RunLoop().RunUntilIdle(); |
| 158 | 165 |
| 159 EXPECT_CALL(save_callback, Save(_, _, _)); | 166 EXPECT_CALL(save_callback, Save(_, _, _)); |
| 160 | 167 |
| 161 task_tracker.StartDistiller(&distiller_factory); | 168 task_tracker.StartDistiller(&distiller_factory); |
| 162 base::RunLoop().RunUntilIdle(); | 169 base::RunLoop().RunUntilIdle(); |
| 163 | 170 |
| 164 EXPECT_TRUE(cancel_callback.Cancelled()); | 171 EXPECT_TRUE(cancel_callback.Cancelled()); |
| 165 } | 172 } |
| 166 | 173 |
| 174 DistilledArticleProto CreateDistilledArticleForEntry( |
| 175 const ArticleEntry& entry) { |
| 176 DistilledArticleProto article; |
| 177 for (int i = 0; i < entry.pages_size(); ++i) { |
| 178 DistilledPageProto* page = article.add_pages(); |
| 179 page->set_url(entry.pages(i).url()); |
| 180 page->set_html("<div>" + entry.pages(i).url() + "</div>"); |
| 181 } |
| 182 return article; |
| 183 } |
| 184 |
| 185 TEST_F(DomDistillerTaskTrackerTest, TestBlobFetcher) { |
| 186 ArticleEntry entry_with_blob = GetDefaultEntry(); |
| 187 DistilledArticleProto stored_distilled_article = |
| 188 CreateDistilledArticleForEntry(entry_with_blob); |
| 189 InMemoryContentStore content_store; |
| 190 content_store.InjectContent(entry_with_blob, stored_distilled_article); |
| 191 TestCancelCallback cancel_callback; |
| 192 |
| 193 TaskTracker task_tracker( |
| 194 entry_with_blob, cancel_callback.GetCallback(), &content_store); |
| 195 |
| 196 FakeViewRequestDelegate viewer_delegate; |
| 197 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 198 base::RunLoop().RunUntilIdle(); |
| 199 |
| 200 const DistilledArticleProto* distilled_article; |
| 201 |
| 202 EXPECT_CALL(viewer_delegate, OnArticleReady(_)) |
| 203 .WillOnce(testing::SaveArg<0>(&distilled_article)); |
| 204 |
| 205 task_tracker.StartBlobFetcher(); |
| 206 base::RunLoop().RunUntilIdle(); |
| 207 |
| 208 EXPECT_EQ(stored_distilled_article.SerializeAsString(), |
| 209 distilled_article->SerializeAsString()); |
| 210 |
| 211 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 212 } |
| 213 |
| 214 TEST_F(DomDistillerTaskTrackerTest, TestBlobFetcherFinishesFirst) { |
| 215 MockDistillerFactory distiller_factory; |
| 216 FakeDistiller* distiller = new FakeDistiller(false); |
| 217 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
| 218 .WillOnce(Return(distiller)); |
| 219 |
| 220 ArticleEntry entry_with_blob = GetDefaultEntry(); |
| 221 DistilledArticleProto stored_distilled_article = |
| 222 CreateDistilledArticleForEntry(entry_with_blob); |
| 223 InMemoryContentStore content_store; |
| 224 content_store.InjectContent(entry_with_blob, stored_distilled_article); |
| 225 TestCancelCallback cancel_callback; |
| 226 TaskTracker task_tracker( |
| 227 entry_with_blob, cancel_callback.GetCallback(), &content_store); |
| 228 |
| 229 FakeViewRequestDelegate viewer_delegate; |
| 230 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 231 base::RunLoop().RunUntilIdle(); |
| 232 |
| 233 const DistilledArticleProto* distilled_article; |
| 234 |
| 235 EXPECT_CALL(viewer_delegate, OnArticleReady(_)) |
| 236 .WillOnce(testing::SaveArg<0>(&distilled_article)); |
| 237 |
| 238 task_tracker.StartDistiller(&distiller_factory); |
| 239 task_tracker.StartBlobFetcher(); |
| 240 base::RunLoop().RunUntilIdle(); |
| 241 |
| 242 testing::Mock::VerifyAndClearExpectations(&viewer_delegate); |
| 243 EXPECT_EQ(stored_distilled_article.SerializeAsString(), |
| 244 distilled_article->SerializeAsString()); |
| 245 |
| 246 distiller->RunDistillerCallback( |
| 247 scoped_ptr<DistilledArticleProto>(new DistilledArticleProto)); |
| 248 |
| 249 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 250 |
| 251 base::RunLoop().RunUntilIdle(); |
| 252 } |
| 253 |
| 254 TEST_F(DomDistillerTaskTrackerTest, TestBlobFetcherWithoutBlob) { |
| 255 MockDistillerFactory distiller_factory; |
| 256 FakeDistiller* distiller = new FakeDistiller(false); |
| 257 EXPECT_CALL(distiller_factory, CreateDistillerImpl()) |
| 258 .WillOnce(Return(distiller)); |
| 259 |
| 260 ArticleEntry entry(GetDefaultEntry()); |
| 261 InMemoryContentStore content_store; |
| 262 scoped_ptr<DistilledArticleProto> distilled_article( |
| 263 new DistilledArticleProto(CreateDistilledArticleForEntry(entry))); |
| 264 |
| 265 TestCancelCallback cancel_callback; |
| 266 TaskTracker task_tracker( |
| 267 GetDefaultEntry(), cancel_callback.GetCallback(), &content_store); |
| 268 |
| 269 FakeViewRequestDelegate viewer_delegate; |
| 270 scoped_ptr<ViewerHandle> handle(task_tracker.AddViewer(&viewer_delegate)); |
| 271 base::RunLoop().RunUntilIdle(); |
| 272 |
| 273 task_tracker.StartBlobFetcher(); |
| 274 task_tracker.StartDistiller(&distiller_factory); |
| 275 |
| 276 // OnArticleReady shouldn't be called until distillation finishes (i.e. the |
| 277 // blob fetcher shouldn't return distilled content). |
| 278 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); |
| 279 base::RunLoop().RunUntilIdle(); |
| 280 |
| 281 EXPECT_CALL(viewer_delegate, OnArticleReady(_)); |
| 282 distiller->RunDistillerCallback(distilled_article.Pass()); |
| 283 base::RunLoop().RunUntilIdle(); |
| 284 |
| 285 EXPECT_FALSE(cancel_callback.Cancelled()); |
| 286 } |
| 287 |
| 167 } // namespace test | 288 } // namespace test |
| 168 } // namespace dom_distiller | 289 } // namespace dom_distiller |
| OLD | NEW |