| 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/dom_distiller_service.h" | 5 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 namespace dom_distiller { | 27 namespace dom_distiller { |
| 28 namespace test { | 28 namespace test { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 class FakeViewRequestDelegate : public ViewRequestDelegate { | 32 class FakeViewRequestDelegate : public ViewRequestDelegate { |
| 33 public: | 33 public: |
| 34 virtual ~FakeViewRequestDelegate() {} | 34 virtual ~FakeViewRequestDelegate() {} |
| 35 MOCK_METHOD1(OnArticleReady, void(const DistilledArticleProto* proto)); | 35 MOCK_METHOD1(OnArticleReady, void(const DistilledArticleProto* proto)); |
| 36 MOCK_METHOD1(OnArticleUpdated, |
| 37 void(ArticleDistillationUpdate article_update)); |
| 36 }; | 38 }; |
| 37 | 39 |
| 38 class MockDistillerObserver : public DomDistillerObserver { | 40 class MockDistillerObserver : public DomDistillerObserver { |
| 39 public: | 41 public: |
| 40 MOCK_METHOD1(ArticleEntriesUpdated, void(const std::vector<ArticleUpdate>&)); | 42 MOCK_METHOD1(ArticleEntriesUpdated, void(const std::vector<ArticleUpdate>&)); |
| 41 virtual ~MockDistillerObserver() {} | 43 virtual ~MockDistillerObserver() {} |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 class MockArticleAvailableCallback { | 46 class MockArticleAvailableCallback { |
| 45 public: | 47 public: |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 ArticleEntry entry; | 115 ArticleEntry entry; |
| 114 entry.set_entry_id(entry_id); | 116 entry.set_entry_id(entry_id); |
| 115 entry.add_pages()->set_url(url.spec()); | 117 entry.add_pages()->set_url(url.spec()); |
| 116 | 118 |
| 117 store_->AddEntry(entry); | 119 store_->AddEntry(entry); |
| 118 | 120 |
| 119 FakeViewRequestDelegate viewer_delegate; | 121 FakeViewRequestDelegate viewer_delegate; |
| 120 scoped_ptr<ViewerHandle> handle = | 122 scoped_ptr<ViewerHandle> handle = |
| 121 service_->ViewEntry(&viewer_delegate, entry_id); | 123 service_->ViewEntry(&viewer_delegate, entry_id); |
| 122 | 124 |
| 123 ASSERT_FALSE(distiller->GetCallback().is_null()); | 125 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 124 | 126 |
| 125 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); | 127 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); |
| 126 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); | 128 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); |
| 127 | 129 |
| 128 RunDistillerCallback(distiller, proto.Pass()); | 130 RunDistillerCallback(distiller, proto.Pass()); |
| 129 } | 131 } |
| 130 | 132 |
| 131 TEST_F(DomDistillerServiceTest, TestViewUrl) { | 133 TEST_F(DomDistillerServiceTest, TestViewUrl) { |
| 132 FakeDistiller* distiller = new FakeDistiller(false); | 134 FakeDistiller* distiller = new FakeDistiller(false); |
| 133 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 135 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 134 .WillOnce(Return(distiller)); | 136 .WillOnce(Return(distiller)); |
| 135 | 137 |
| 136 FakeViewRequestDelegate viewer_delegate; | 138 FakeViewRequestDelegate viewer_delegate; |
| 137 GURL url("http://www.example.com/p1"); | 139 GURL url("http://www.example.com/p1"); |
| 138 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); | 140 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); |
| 139 | 141 |
| 140 ASSERT_FALSE(distiller->GetCallback().is_null()); | 142 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 141 EXPECT_EQ(url, distiller->GetUrl()); | 143 EXPECT_EQ(url, distiller->GetUrl()); |
| 142 | 144 |
| 143 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); | 145 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); |
| 144 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); | 146 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); |
| 145 | 147 |
| 146 RunDistillerCallback(distiller, proto.Pass()); | 148 RunDistillerCallback(distiller, proto.Pass()); |
| 147 } | 149 } |
| 148 | 150 |
| 149 TEST_F(DomDistillerServiceTest, TestMultipleViewUrl) { | 151 TEST_F(DomDistillerServiceTest, TestMultipleViewUrl) { |
| 150 FakeDistiller* distiller = new FakeDistiller(false); | 152 FakeDistiller* distiller = new FakeDistiller(false); |
| 151 FakeDistiller* distiller2 = new FakeDistiller(false); | 153 FakeDistiller* distiller2 = new FakeDistiller(false); |
| 152 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 154 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 153 .WillOnce(Return(distiller)) | 155 .WillOnce(Return(distiller)) |
| 154 .WillOnce(Return(distiller2)); | 156 .WillOnce(Return(distiller2)); |
| 155 | 157 |
| 156 FakeViewRequestDelegate viewer_delegate; | 158 FakeViewRequestDelegate viewer_delegate; |
| 157 FakeViewRequestDelegate viewer_delegate2; | 159 FakeViewRequestDelegate viewer_delegate2; |
| 158 | 160 |
| 159 GURL url("http://www.example.com/p1"); | 161 GURL url("http://www.example.com/p1"); |
| 160 GURL url2("http://www.example.com/a/p1"); | 162 GURL url2("http://www.example.com/a/p1"); |
| 161 | 163 |
| 162 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); | 164 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); |
| 163 scoped_ptr<ViewerHandle> handle2 = service_->ViewUrl(&viewer_delegate2, url2); | 165 scoped_ptr<ViewerHandle> handle2 = service_->ViewUrl(&viewer_delegate2, url2); |
| 164 | 166 |
| 165 ASSERT_FALSE(distiller->GetCallback().is_null()); | 167 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 166 EXPECT_EQ(url, distiller->GetUrl()); | 168 EXPECT_EQ(url, distiller->GetUrl()); |
| 167 | 169 |
| 168 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); | 170 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); |
| 169 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); | 171 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); |
| 170 | 172 |
| 171 RunDistillerCallback(distiller, proto.Pass()); | 173 RunDistillerCallback(distiller, proto.Pass()); |
| 172 | 174 |
| 173 ASSERT_FALSE(distiller2->GetCallback().is_null()); | 175 ASSERT_FALSE(distiller2->GetArticleCallback().is_null()); |
| 174 EXPECT_EQ(url2, distiller2->GetUrl()); | 176 EXPECT_EQ(url2, distiller2->GetUrl()); |
| 175 | 177 |
| 176 scoped_ptr<DistilledArticleProto> proto2 = CreateDefaultArticle(); | 178 scoped_ptr<DistilledArticleProto> proto2 = CreateDefaultArticle(); |
| 177 EXPECT_CALL(viewer_delegate2, OnArticleReady(proto2.get())); | 179 EXPECT_CALL(viewer_delegate2, OnArticleReady(proto2.get())); |
| 178 | 180 |
| 179 RunDistillerCallback(distiller2, proto2.Pass()); | 181 RunDistillerCallback(distiller2, proto2.Pass()); |
| 180 } | 182 } |
| 181 | 183 |
| 182 TEST_F(DomDistillerServiceTest, TestViewUrlCancelled) { | 184 TEST_F(DomDistillerServiceTest, TestViewUrlCancelled) { |
| 183 FakeDistiller* distiller = new FakeDistiller(false); | 185 FakeDistiller* distiller = new FakeDistiller(false); |
| 184 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 186 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 185 .WillOnce(Return(distiller)); | 187 .WillOnce(Return(distiller)); |
| 186 | 188 |
| 187 bool distiller_destroyed = false; | 189 bool distiller_destroyed = false; |
| 188 EXPECT_CALL(*distiller, Die()) | 190 EXPECT_CALL(*distiller, Die()) |
| 189 .WillOnce(testing::Assign(&distiller_destroyed, true)); | 191 .WillOnce(testing::Assign(&distiller_destroyed, true)); |
| 190 | 192 |
| 191 FakeViewRequestDelegate viewer_delegate; | 193 FakeViewRequestDelegate viewer_delegate; |
| 192 GURL url("http://www.example.com/p1"); | 194 GURL url("http://www.example.com/p1"); |
| 193 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); | 195 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); |
| 194 | 196 |
| 195 ASSERT_FALSE(distiller->GetCallback().is_null()); | 197 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 196 EXPECT_EQ(url, distiller->GetUrl()); | 198 EXPECT_EQ(url, distiller->GetUrl()); |
| 197 | 199 |
| 198 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); | 200 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); |
| 199 | 201 |
| 200 EXPECT_FALSE(distiller_destroyed); | 202 EXPECT_FALSE(distiller_destroyed); |
| 201 | 203 |
| 202 handle.reset(); | 204 handle.reset(); |
| 203 base::RunLoop().RunUntilIdle(); | 205 base::RunLoop().RunUntilIdle(); |
| 204 EXPECT_TRUE(distiller_destroyed); | 206 EXPECT_TRUE(distiller_destroyed); |
| 205 } | 207 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 227 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 229 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 228 .WillOnce(Return(distiller)); | 230 .WillOnce(Return(distiller)); |
| 229 | 231 |
| 230 GURL url("http://www.example.com/p1"); | 232 GURL url("http://www.example.com/p1"); |
| 231 | 233 |
| 232 MockArticleAvailableCallback article_cb; | 234 MockArticleAvailableCallback article_cb; |
| 233 EXPECT_CALL(article_cb, DistillationCompleted(true)); | 235 EXPECT_CALL(article_cb, DistillationCompleted(true)); |
| 234 | 236 |
| 235 std::string entry_id = service_->AddToList(url, ArticleCallback(&article_cb)); | 237 std::string entry_id = service_->AddToList(url, ArticleCallback(&article_cb)); |
| 236 | 238 |
| 237 ASSERT_FALSE(distiller->GetCallback().is_null()); | 239 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 238 EXPECT_EQ(url, distiller->GetUrl()); | 240 EXPECT_EQ(url, distiller->GetUrl()); |
| 239 | 241 |
| 240 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); | 242 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); |
| 241 RunDistillerCallback(distiller, proto.Pass()); | 243 RunDistillerCallback(distiller, proto.Pass()); |
| 242 | 244 |
| 243 ArticleEntry entry; | 245 ArticleEntry entry; |
| 244 EXPECT_TRUE(store_->GetEntryByUrl(url, &entry)); | 246 EXPECT_TRUE(store_->GetEntryByUrl(url, &entry)); |
| 245 EXPECT_EQ(entry.entry_id(), entry_id); | 247 EXPECT_EQ(entry.entry_id(), entry_id); |
| 246 EXPECT_EQ(1u, store_->GetEntries().size()); | 248 EXPECT_EQ(1u, store_->GetEntries().size()); |
| 247 service_->RemoveEntry(entry_id); | 249 service_->RemoveEntry(entry_id); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 pages_url[page_num] = GURL(base_url + base::IntToString(page_num)); | 384 pages_url[page_num] = GURL(base_url + base::IntToString(page_num)); |
| 383 } | 385 } |
| 384 | 386 |
| 385 MockArticleAvailableCallback article_cb; | 387 MockArticleAvailableCallback article_cb; |
| 386 EXPECT_CALL(article_cb, DistillationCompleted(true)); | 388 EXPECT_CALL(article_cb, DistillationCompleted(true)); |
| 387 | 389 |
| 388 std::string entry_id = | 390 std::string entry_id = |
| 389 service_->AddToList(pages_url[0], ArticleCallback(&article_cb)); | 391 service_->AddToList(pages_url[0], ArticleCallback(&article_cb)); |
| 390 | 392 |
| 391 ArticleEntry entry; | 393 ArticleEntry entry; |
| 392 ASSERT_FALSE(distiller->GetCallback().is_null()); | 394 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 393 EXPECT_EQ(pages_url[0], distiller->GetUrl()); | 395 EXPECT_EQ(pages_url[0], distiller->GetUrl()); |
| 394 | 396 |
| 395 // Create the article with pages to pass to the distiller. | 397 // Create the article with pages to pass to the distiller. |
| 396 scoped_ptr<DistilledArticleProto> proto = | 398 scoped_ptr<DistilledArticleProto> proto = |
| 397 CreateArticleWithURL(pages_url[0].spec()); | 399 CreateArticleWithURL(pages_url[0].spec()); |
| 398 for (int page_num = 1; page_num < kPageCount; ++page_num) { | 400 for (int page_num = 1; page_num < kPageCount; ++page_num) { |
| 399 DistilledPageProto* distilled_page = proto->add_pages(); | 401 DistilledPageProto* distilled_page = proto->add_pages(); |
| 400 distilled_page->set_url(pages_url[page_num].spec()); | 402 distilled_page->set_url(pages_url[page_num].spec()); |
| 401 } | 403 } |
| 402 | 404 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 417 EXPECT_TRUE(store_->GetEntryByUrl(pages_url[page_num], &entry)); | 419 EXPECT_TRUE(store_->GetEntryByUrl(pages_url[page_num], &entry)); |
| 418 } | 420 } |
| 419 | 421 |
| 420 service_->RemoveEntry(entry_id); | 422 service_->RemoveEntry(entry_id); |
| 421 base::RunLoop().RunUntilIdle(); | 423 base::RunLoop().RunUntilIdle(); |
| 422 EXPECT_EQ(0u, store_->GetEntries().size()); | 424 EXPECT_EQ(0u, store_->GetEntries().size()); |
| 423 } | 425 } |
| 424 | 426 |
| 425 } // namespace test | 427 } // namespace test |
| 426 } // namespace dom_distiller | 428 } // namespace dom_distiller |
| OLD | NEW |