| 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" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "components/dom_distiller/core/article_entry.h" | 13 #include "components/dom_distiller/core/article_entry.h" |
| 14 #include "components/dom_distiller/core/dom_distiller_model.h" | 14 #include "components/dom_distiller/core/dom_distiller_model.h" |
| 15 #include "components/dom_distiller/core/dom_distiller_store.h" | 15 #include "components/dom_distiller/core/dom_distiller_store.h" |
| 16 #include "components/dom_distiller/core/dom_distiller_test_util.h" | 16 #include "components/dom_distiller/core/dom_distiller_test_util.h" |
| 17 #include "components/dom_distiller/core/fake_db.h" | 17 #include "components/dom_distiller/core/fake_db.h" |
| 18 #include "components/dom_distiller/core/fake_distiller.h" | 18 #include "components/dom_distiller/core/fake_distiller.h" |
| 19 #include "components/dom_distiller/core/fake_distiller_page.h" |
| 19 #include "components/dom_distiller/core/task_tracker.h" | 20 #include "components/dom_distiller/core/task_tracker.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using testing::Invoke; | 24 using testing::Invoke; |
| 24 using testing::Return; | 25 using testing::Return; |
| 25 using testing::_; | 26 using testing::_; |
| 26 | 27 |
| 27 namespace dom_distiller { | 28 namespace dom_distiller { |
| 28 namespace test { | 29 namespace test { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 } // namespace | 76 } // namespace |
| 76 | 77 |
| 77 class DomDistillerServiceTest : public testing::Test { | 78 class DomDistillerServiceTest : public testing::Test { |
| 78 public: | 79 public: |
| 79 virtual void SetUp() { | 80 virtual void SetUp() { |
| 80 main_loop_.reset(new base::MessageLoop()); | 81 main_loop_.reset(new base::MessageLoop()); |
| 81 FakeDB* fake_db = new FakeDB(&db_model_); | 82 FakeDB* fake_db = new FakeDB(&db_model_); |
| 82 FakeDB::EntryMap store_model; | 83 FakeDB::EntryMap store_model; |
| 83 store_ = test::util::CreateStoreWithFakeDB(fake_db, store_model); | 84 store_ = test::util::CreateStoreWithFakeDB(fake_db, store_model); |
| 84 distiller_factory_ = new MockDistillerFactory(); | 85 distiller_factory_ = new MockDistillerFactory(); |
| 86 distiller_page_factory_ = new MockDistillerPageFactory(); |
| 85 service_.reset(new DomDistillerService( | 87 service_.reset(new DomDistillerService( |
| 86 scoped_ptr<DomDistillerStoreInterface>(store_), | 88 scoped_ptr<DomDistillerStoreInterface>(store_), |
| 87 scoped_ptr<DistillerFactory>(distiller_factory_))); | 89 scoped_ptr<DistillerFactory>(distiller_factory_), |
| 90 scoped_ptr<DistillerPageFactory>(distiller_page_factory_))); |
| 88 fake_db->InitCallback(true); | 91 fake_db->InitCallback(true); |
| 89 fake_db->LoadCallback(true); | 92 fake_db->LoadCallback(true); |
| 90 } | 93 } |
| 91 | 94 |
| 92 virtual void TearDown() { | 95 virtual void TearDown() { |
| 93 base::RunLoop().RunUntilIdle(); | 96 base::RunLoop().RunUntilIdle(); |
| 94 store_ = NULL; | 97 store_ = NULL; |
| 95 distiller_factory_ = NULL; | 98 distiller_factory_ = NULL; |
| 96 service_.reset(); | 99 service_.reset(); |
| 97 } | 100 } |
| 98 | 101 |
| 99 protected: | 102 protected: |
| 100 // store is owned by service_. | 103 // store is owned by service_. |
| 101 DomDistillerStoreInterface* store_; | 104 DomDistillerStoreInterface* store_; |
| 102 MockDistillerFactory* distiller_factory_; | 105 MockDistillerFactory* distiller_factory_; |
| 106 MockDistillerPageFactory* distiller_page_factory_; |
| 103 scoped_ptr<DomDistillerService> service_; | 107 scoped_ptr<DomDistillerService> service_; |
| 104 scoped_ptr<base::MessageLoop> main_loop_; | 108 scoped_ptr<base::MessageLoop> main_loop_; |
| 105 FakeDB::EntryMap db_model_; | 109 FakeDB::EntryMap db_model_; |
| 106 }; | 110 }; |
| 107 | 111 |
| 108 TEST_F(DomDistillerServiceTest, TestViewEntry) { | 112 TEST_F(DomDistillerServiceTest, TestViewEntry) { |
| 109 FakeDistiller* distiller = new FakeDistiller(false); | 113 FakeDistiller* distiller = new FakeDistiller(false); |
| 110 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 114 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 111 .WillOnce(Return(distiller)); | 115 .WillOnce(Return(distiller)); |
| 112 | 116 |
| 113 GURL url("http://www.example.com/p1"); | 117 GURL url("http://www.example.com/p1"); |
| 114 std::string entry_id("id0"); | 118 std::string entry_id("id0"); |
| 115 ArticleEntry entry; | 119 ArticleEntry entry; |
| 116 entry.set_entry_id(entry_id); | 120 entry.set_entry_id(entry_id); |
| 117 entry.add_pages()->set_url(url.spec()); | 121 entry.add_pages()->set_url(url.spec()); |
| 118 | 122 |
| 119 store_->AddEntry(entry); | 123 store_->AddEntry(entry); |
| 120 | 124 |
| 121 FakeViewRequestDelegate viewer_delegate; | 125 FakeViewRequestDelegate viewer_delegate; |
| 122 scoped_ptr<ViewerHandle> handle = | 126 scoped_ptr<ViewerHandle> handle = service_->ViewEntry( |
| 123 service_->ViewEntry(&viewer_delegate, entry_id); | 127 &viewer_delegate, service_->CreateDefaultDistillerPage(), entry_id); |
| 124 | 128 |
| 125 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); | 129 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 126 | 130 |
| 127 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); | 131 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); |
| 128 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); | 132 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); |
| 129 | 133 |
| 130 RunDistillerCallback(distiller, proto.Pass()); | 134 RunDistillerCallback(distiller, proto.Pass()); |
| 131 } | 135 } |
| 132 | 136 |
| 133 TEST_F(DomDistillerServiceTest, TestViewUrl) { | 137 TEST_F(DomDistillerServiceTest, TestViewUrl) { |
| 134 FakeDistiller* distiller = new FakeDistiller(false); | 138 FakeDistiller* distiller = new FakeDistiller(false); |
| 135 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 139 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 136 .WillOnce(Return(distiller)); | 140 .WillOnce(Return(distiller)); |
| 137 | 141 |
| 138 FakeViewRequestDelegate viewer_delegate; | 142 FakeViewRequestDelegate viewer_delegate; |
| 139 GURL url("http://www.example.com/p1"); | 143 GURL url("http://www.example.com/p1"); |
| 140 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); | 144 scoped_ptr<ViewerHandle> handle = service_->ViewUrl( |
| 145 &viewer_delegate, service_->CreateDefaultDistillerPage(), url); |
| 141 | 146 |
| 142 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); | 147 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 143 EXPECT_EQ(url, distiller->GetUrl()); | 148 EXPECT_EQ(url, distiller->GetUrl()); |
| 144 | 149 |
| 145 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); | 150 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); |
| 146 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); | 151 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); |
| 147 | 152 |
| 148 RunDistillerCallback(distiller, proto.Pass()); | 153 RunDistillerCallback(distiller, proto.Pass()); |
| 149 } | 154 } |
| 150 | 155 |
| 151 TEST_F(DomDistillerServiceTest, TestMultipleViewUrl) { | 156 TEST_F(DomDistillerServiceTest, TestMultipleViewUrl) { |
| 152 FakeDistiller* distiller = new FakeDistiller(false); | 157 FakeDistiller* distiller = new FakeDistiller(false); |
| 153 FakeDistiller* distiller2 = new FakeDistiller(false); | 158 FakeDistiller* distiller2 = new FakeDistiller(false); |
| 154 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 159 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 155 .WillOnce(Return(distiller)) | 160 .WillOnce(Return(distiller)) |
| 156 .WillOnce(Return(distiller2)); | 161 .WillOnce(Return(distiller2)); |
| 157 | 162 |
| 158 FakeViewRequestDelegate viewer_delegate; | 163 FakeViewRequestDelegate viewer_delegate; |
| 159 FakeViewRequestDelegate viewer_delegate2; | 164 FakeViewRequestDelegate viewer_delegate2; |
| 160 | 165 |
| 161 GURL url("http://www.example.com/p1"); | 166 GURL url("http://www.example.com/p1"); |
| 162 GURL url2("http://www.example.com/a/p1"); | 167 GURL url2("http://www.example.com/a/p1"); |
| 163 | 168 |
| 164 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); | 169 scoped_ptr<ViewerHandle> handle = service_->ViewUrl( |
| 165 scoped_ptr<ViewerHandle> handle2 = service_->ViewUrl(&viewer_delegate2, url2); | 170 &viewer_delegate, service_->CreateDefaultDistillerPage(), url); |
| 171 scoped_ptr<ViewerHandle> handle2 = service_->ViewUrl( |
| 172 &viewer_delegate2, service_->CreateDefaultDistillerPage(), url2); |
| 166 | 173 |
| 167 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); | 174 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 168 EXPECT_EQ(url, distiller->GetUrl()); | 175 EXPECT_EQ(url, distiller->GetUrl()); |
| 169 | 176 |
| 170 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); | 177 scoped_ptr<DistilledArticleProto> proto = CreateDefaultArticle(); |
| 171 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); | 178 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); |
| 172 | 179 |
| 173 RunDistillerCallback(distiller, proto.Pass()); | 180 RunDistillerCallback(distiller, proto.Pass()); |
| 174 | 181 |
| 175 ASSERT_FALSE(distiller2->GetArticleCallback().is_null()); | 182 ASSERT_FALSE(distiller2->GetArticleCallback().is_null()); |
| 176 EXPECT_EQ(url2, distiller2->GetUrl()); | 183 EXPECT_EQ(url2, distiller2->GetUrl()); |
| 177 | 184 |
| 178 scoped_ptr<DistilledArticleProto> proto2 = CreateDefaultArticle(); | 185 scoped_ptr<DistilledArticleProto> proto2 = CreateDefaultArticle(); |
| 179 EXPECT_CALL(viewer_delegate2, OnArticleReady(proto2.get())); | 186 EXPECT_CALL(viewer_delegate2, OnArticleReady(proto2.get())); |
| 180 | 187 |
| 181 RunDistillerCallback(distiller2, proto2.Pass()); | 188 RunDistillerCallback(distiller2, proto2.Pass()); |
| 182 } | 189 } |
| 183 | 190 |
| 184 TEST_F(DomDistillerServiceTest, TestViewUrlCancelled) { | 191 TEST_F(DomDistillerServiceTest, TestViewUrlCancelled) { |
| 185 FakeDistiller* distiller = new FakeDistiller(false); | 192 FakeDistiller* distiller = new FakeDistiller(false); |
| 186 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 193 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 187 .WillOnce(Return(distiller)); | 194 .WillOnce(Return(distiller)); |
| 188 | 195 |
| 189 bool distiller_destroyed = false; | 196 bool distiller_destroyed = false; |
| 190 EXPECT_CALL(*distiller, Die()) | 197 EXPECT_CALL(*distiller, Die()) |
| 191 .WillOnce(testing::Assign(&distiller_destroyed, true)); | 198 .WillOnce(testing::Assign(&distiller_destroyed, true)); |
| 192 | 199 |
| 193 FakeViewRequestDelegate viewer_delegate; | 200 FakeViewRequestDelegate viewer_delegate; |
| 194 GURL url("http://www.example.com/p1"); | 201 GURL url("http://www.example.com/p1"); |
| 195 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); | 202 scoped_ptr<ViewerHandle> handle = service_->ViewUrl( |
| 203 &viewer_delegate, service_->CreateDefaultDistillerPage(), url); |
| 196 | 204 |
| 197 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); | 205 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 198 EXPECT_EQ(url, distiller->GetUrl()); | 206 EXPECT_EQ(url, distiller->GetUrl()); |
| 199 | 207 |
| 200 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); | 208 EXPECT_CALL(viewer_delegate, OnArticleReady(_)).Times(0); |
| 201 | 209 |
| 202 EXPECT_FALSE(distiller_destroyed); | 210 EXPECT_FALSE(distiller_destroyed); |
| 203 | 211 |
| 204 handle.reset(); | 212 handle.reset(); |
| 205 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
| 206 EXPECT_TRUE(distiller_destroyed); | 214 EXPECT_TRUE(distiller_destroyed); |
| 207 } | 215 } |
| 208 | 216 |
| 209 TEST_F(DomDistillerServiceTest, TestViewUrlDoesNotAddEntry) { | 217 TEST_F(DomDistillerServiceTest, TestViewUrlDoesNotAddEntry) { |
| 210 FakeDistiller* distiller = new FakeDistiller(false); | 218 FakeDistiller* distiller = new FakeDistiller(false); |
| 211 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 219 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 212 .WillOnce(Return(distiller)); | 220 .WillOnce(Return(distiller)); |
| 213 | 221 |
| 214 FakeViewRequestDelegate viewer_delegate; | 222 FakeViewRequestDelegate viewer_delegate; |
| 215 GURL url("http://www.example.com/p1"); | 223 GURL url("http://www.example.com/p1"); |
| 216 scoped_ptr<ViewerHandle> handle = service_->ViewUrl(&viewer_delegate, url); | 224 scoped_ptr<ViewerHandle> handle = service_->ViewUrl( |
| 225 &viewer_delegate, service_->CreateDefaultDistillerPage(), url); |
| 217 | 226 |
| 218 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); | 227 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); |
| 219 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); | 228 EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get())); |
| 220 | 229 |
| 221 RunDistillerCallback(distiller, proto.Pass()); | 230 RunDistillerCallback(distiller, proto.Pass()); |
| 222 base::RunLoop().RunUntilIdle(); | 231 base::RunLoop().RunUntilIdle(); |
| 223 // The entry should not be added to the store. | 232 // The entry should not be added to the store. |
| 224 EXPECT_EQ(0u, store_->GetEntries().size()); | 233 EXPECT_EQ(0u, store_->GetEntries().size()); |
| 225 } | 234 } |
| 226 | 235 |
| 227 TEST_F(DomDistillerServiceTest, TestAddAndRemoveEntry) { | 236 TEST_F(DomDistillerServiceTest, TestAddAndRemoveEntry) { |
| 228 FakeDistiller* distiller = new FakeDistiller(false); | 237 FakeDistiller* distiller = new FakeDistiller(false); |
| 229 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 238 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 230 .WillOnce(Return(distiller)); | 239 .WillOnce(Return(distiller)); |
| 231 | 240 |
| 232 GURL url("http://www.example.com/p1"); | 241 GURL url("http://www.example.com/p1"); |
| 233 | 242 |
| 234 MockArticleAvailableCallback article_cb; | 243 MockArticleAvailableCallback article_cb; |
| 235 EXPECT_CALL(article_cb, DistillationCompleted(true)); | 244 EXPECT_CALL(article_cb, DistillationCompleted(true)); |
| 236 | 245 |
| 237 std::string entry_id = service_->AddToList(url, ArticleCallback(&article_cb)); | 246 std::string entry_id = |
| 247 service_->AddToList(url, |
| 248 service_->CreateDefaultDistillerPage().Pass(), |
| 249 ArticleCallback(&article_cb)); |
| 238 | 250 |
| 239 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); | 251 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 240 EXPECT_EQ(url, distiller->GetUrl()); | 252 EXPECT_EQ(url, distiller->GetUrl()); |
| 241 | 253 |
| 242 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); | 254 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); |
| 243 RunDistillerCallback(distiller, proto.Pass()); | 255 RunDistillerCallback(distiller, proto.Pass()); |
| 244 | 256 |
| 245 ArticleEntry entry; | 257 ArticleEntry entry; |
| 246 EXPECT_TRUE(store_->GetEntryByUrl(url, &entry)); | 258 EXPECT_TRUE(store_->GetEntryByUrl(url, &entry)); |
| 247 EXPECT_EQ(entry.entry_id(), entry_id); | 259 EXPECT_EQ(entry.entry_id(), entry_id); |
| 248 EXPECT_EQ(1u, store_->GetEntries().size()); | 260 EXPECT_EQ(1u, store_->GetEntries().size()); |
| 249 service_->RemoveEntry(entry_id); | 261 service_->RemoveEntry(entry_id); |
| 250 base::RunLoop().RunUntilIdle(); | 262 base::RunLoop().RunUntilIdle(); |
| 251 EXPECT_EQ(0u, store_->GetEntries().size()); | 263 EXPECT_EQ(0u, store_->GetEntries().size()); |
| 252 } | 264 } |
| 253 | 265 |
| 254 TEST_F(DomDistillerServiceTest, TestCancellation) { | 266 TEST_F(DomDistillerServiceTest, TestCancellation) { |
| 255 FakeDistiller* distiller = new FakeDistiller(false); | 267 FakeDistiller* distiller = new FakeDistiller(false); |
| 256 MockDistillerObserver observer; | 268 MockDistillerObserver observer; |
| 257 service_->AddObserver(&observer); | 269 service_->AddObserver(&observer); |
| 258 | 270 |
| 259 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 271 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 260 .WillOnce(Return(distiller)); | 272 .WillOnce(Return(distiller)); |
| 261 | 273 |
| 262 MockArticleAvailableCallback article_cb; | 274 MockArticleAvailableCallback article_cb; |
| 263 EXPECT_CALL(article_cb, DistillationCompleted(false)); | 275 EXPECT_CALL(article_cb, DistillationCompleted(false)); |
| 264 | 276 |
| 265 GURL url("http://www.example.com/p1"); | 277 GURL url("http://www.example.com/p1"); |
| 266 std::string entry_id = service_->AddToList(url, ArticleCallback(&article_cb)); | 278 std::string entry_id = |
| 279 service_->AddToList(url, |
| 280 service_->CreateDefaultDistillerPage().Pass(), |
| 281 ArticleCallback(&article_cb)); |
| 267 | 282 |
| 268 // Remove entry will cause the |article_cb| to be called with false value. | 283 // Remove entry will cause the |article_cb| to be called with false value. |
| 269 service_->RemoveEntry(entry_id); | 284 service_->RemoveEntry(entry_id); |
| 270 base::RunLoop().RunUntilIdle(); | 285 base::RunLoop().RunUntilIdle(); |
| 271 } | 286 } |
| 272 | 287 |
| 273 TEST_F(DomDistillerServiceTest, TestMultipleObservers) { | 288 TEST_F(DomDistillerServiceTest, TestMultipleObservers) { |
| 274 FakeDistiller* distiller = new FakeDistiller(false); | 289 FakeDistiller* distiller = new FakeDistiller(false); |
| 275 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 290 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 276 .WillOnce(Return(distiller)); | 291 .WillOnce(Return(distiller)); |
| 277 | 292 |
| 278 const int kObserverCount = 5; | 293 const int kObserverCount = 5; |
| 279 MockDistillerObserver observers[kObserverCount]; | 294 MockDistillerObserver observers[kObserverCount]; |
| 280 for (int i = 0; i < kObserverCount; ++i) { | 295 for (int i = 0; i < kObserverCount; ++i) { |
| 281 service_->AddObserver(&observers[i]); | 296 service_->AddObserver(&observers[i]); |
| 282 } | 297 } |
| 283 | 298 |
| 284 DomDistillerService::ArticleAvailableCallback article_cb; | 299 DomDistillerService::ArticleAvailableCallback article_cb; |
| 285 GURL url("http://www.example.com/p1"); | 300 GURL url("http://www.example.com/p1"); |
| 286 std::string entry_id = service_->AddToList(url, article_cb); | 301 std::string entry_id = service_->AddToList( |
| 302 url, service_->CreateDefaultDistillerPage().Pass(), article_cb); |
| 287 | 303 |
| 288 // Distillation should notify all observers that article is added. | 304 // Distillation should notify all observers that article is added. |
| 289 std::vector<DomDistillerObserver::ArticleUpdate> expected_updates; | 305 std::vector<DomDistillerObserver::ArticleUpdate> expected_updates; |
| 290 DomDistillerObserver::ArticleUpdate update; | 306 DomDistillerObserver::ArticleUpdate update; |
| 291 update.entry_id = entry_id; | 307 update.entry_id = entry_id; |
| 292 update.update_type = DomDistillerObserver::ArticleUpdate::ADD; | 308 update.update_type = DomDistillerObserver::ArticleUpdate::ADD; |
| 293 expected_updates.push_back(update); | 309 expected_updates.push_back(update); |
| 294 | 310 |
| 295 for (int i = 0; i < kObserverCount; ++i) { | 311 for (int i = 0; i < kObserverCount; ++i) { |
| 296 EXPECT_CALL( | 312 EXPECT_CALL( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 318 TEST_F(DomDistillerServiceTest, TestMultipleCallbacks) { | 334 TEST_F(DomDistillerServiceTest, TestMultipleCallbacks) { |
| 319 FakeDistiller* distiller = new FakeDistiller(false); | 335 FakeDistiller* distiller = new FakeDistiller(false); |
| 320 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 336 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 321 .WillOnce(Return(distiller)); | 337 .WillOnce(Return(distiller)); |
| 322 | 338 |
| 323 const int kClientsCount = 5; | 339 const int kClientsCount = 5; |
| 324 MockArticleAvailableCallback article_cb[kClientsCount]; | 340 MockArticleAvailableCallback article_cb[kClientsCount]; |
| 325 // Adding a URL and then distilling calls all clients. | 341 // Adding a URL and then distilling calls all clients. |
| 326 GURL url("http://www.example.com/p1"); | 342 GURL url("http://www.example.com/p1"); |
| 327 const std::string entry_id = | 343 const std::string entry_id = |
| 328 service_->AddToList(url, ArticleCallback(&article_cb[0])); | 344 service_->AddToList(url, |
| 345 service_->CreateDefaultDistillerPage().Pass(), |
| 346 ArticleCallback(&article_cb[0])); |
| 329 EXPECT_CALL(article_cb[0], DistillationCompleted(true)); | 347 EXPECT_CALL(article_cb[0], DistillationCompleted(true)); |
| 330 | 348 |
| 331 for (int i = 1; i < kClientsCount; ++i) { | 349 for (int i = 1; i < kClientsCount; ++i) { |
| 332 EXPECT_EQ(entry_id, | 350 EXPECT_EQ(entry_id, |
| 333 service_->AddToList(url, ArticleCallback(&article_cb[i]))); | 351 service_->AddToList(url, |
| 352 service_->CreateDefaultDistillerPage().Pass(), |
| 353 ArticleCallback(&article_cb[i]))); |
| 334 EXPECT_CALL(article_cb[i], DistillationCompleted(true)); | 354 EXPECT_CALL(article_cb[i], DistillationCompleted(true)); |
| 335 } | 355 } |
| 336 | 356 |
| 337 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); | 357 scoped_ptr<DistilledArticleProto> proto = CreateArticleWithURL(url.spec()); |
| 338 RunDistillerCallback(distiller, proto.Pass()); | 358 RunDistillerCallback(distiller, proto.Pass()); |
| 339 | 359 |
| 340 // Add the same url again, all callbacks should be called with true. | 360 // Add the same url again, all callbacks should be called with true. |
| 341 for (int i = 0; i < kClientsCount; ++i) { | 361 for (int i = 0; i < kClientsCount; ++i) { |
| 342 EXPECT_CALL(article_cb[i], DistillationCompleted(true)); | 362 EXPECT_CALL(article_cb[i], DistillationCompleted(true)); |
| 343 EXPECT_EQ(entry_id, | 363 EXPECT_EQ(entry_id, |
| 344 service_->AddToList(url, ArticleCallback(&article_cb[i]))); | 364 service_->AddToList(url, |
| 365 service_->CreateDefaultDistillerPage().Pass(), |
| 366 ArticleCallback(&article_cb[i]))); |
| 345 } | 367 } |
| 346 | 368 |
| 347 base::RunLoop().RunUntilIdle(); | 369 base::RunLoop().RunUntilIdle(); |
| 348 } | 370 } |
| 349 | 371 |
| 350 TEST_F(DomDistillerServiceTest, TestMultipleCallbacksOnRemove) { | 372 TEST_F(DomDistillerServiceTest, TestMultipleCallbacksOnRemove) { |
| 351 FakeDistiller* distiller = new FakeDistiller(false); | 373 FakeDistiller* distiller = new FakeDistiller(false); |
| 352 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 374 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 353 .WillOnce(Return(distiller)); | 375 .WillOnce(Return(distiller)); |
| 354 | 376 |
| 355 const int kClientsCount = 5; | 377 const int kClientsCount = 5; |
| 356 MockArticleAvailableCallback article_cb[kClientsCount]; | 378 MockArticleAvailableCallback article_cb[kClientsCount]; |
| 357 // Adding a URL and remove the entry before distillation. Callback should be | 379 // Adding a URL and remove the entry before distillation. Callback should be |
| 358 // called with false. | 380 // called with false. |
| 359 GURL url("http://www.example.com/p1"); | 381 GURL url("http://www.example.com/p1"); |
| 360 const std::string entry_id = | 382 const std::string entry_id = |
| 361 service_->AddToList(url, ArticleCallback(&article_cb[0])); | 383 service_->AddToList(url, |
| 384 service_->CreateDefaultDistillerPage().Pass(), |
| 385 ArticleCallback(&article_cb[0])); |
| 362 | 386 |
| 363 EXPECT_CALL(article_cb[0], DistillationCompleted(false)); | 387 EXPECT_CALL(article_cb[0], DistillationCompleted(false)); |
| 364 for (int i = 1; i < kClientsCount; ++i) { | 388 for (int i = 1; i < kClientsCount; ++i) { |
| 365 EXPECT_EQ(entry_id, | 389 EXPECT_EQ(entry_id, |
| 366 service_->AddToList(url, ArticleCallback(&article_cb[i]))); | 390 service_->AddToList(url, |
| 391 service_->CreateDefaultDistillerPage().Pass(), |
| 392 ArticleCallback(&article_cb[i]))); |
| 367 EXPECT_CALL(article_cb[i], DistillationCompleted(false)); | 393 EXPECT_CALL(article_cb[i], DistillationCompleted(false)); |
| 368 } | 394 } |
| 369 | 395 |
| 370 service_->RemoveEntry(entry_id); | 396 service_->RemoveEntry(entry_id); |
| 371 base::RunLoop().RunUntilIdle(); | 397 base::RunLoop().RunUntilIdle(); |
| 372 } | 398 } |
| 373 | 399 |
| 374 TEST_F(DomDistillerServiceTest, TestMultiplePageArticle) { | 400 TEST_F(DomDistillerServiceTest, TestMultiplePageArticle) { |
| 375 FakeDistiller* distiller = new FakeDistiller(false); | 401 FakeDistiller* distiller = new FakeDistiller(false); |
| 376 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | 402 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 377 .WillOnce(Return(distiller)); | 403 .WillOnce(Return(distiller)); |
| 378 | 404 |
| 379 const int kPageCount = 8; | 405 const int kPageCount = 8; |
| 380 | 406 |
| 381 std::string base_url("http://www.example.com/p"); | 407 std::string base_url("http://www.example.com/p"); |
| 382 GURL pages_url[kPageCount]; | 408 GURL pages_url[kPageCount]; |
| 383 for (int page_num = 0; page_num < kPageCount; ++page_num) { | 409 for (int page_num = 0; page_num < kPageCount; ++page_num) { |
| 384 pages_url[page_num] = GURL(base_url + base::IntToString(page_num)); | 410 pages_url[page_num] = GURL(base_url + base::IntToString(page_num)); |
| 385 } | 411 } |
| 386 | 412 |
| 387 MockArticleAvailableCallback article_cb; | 413 MockArticleAvailableCallback article_cb; |
| 388 EXPECT_CALL(article_cb, DistillationCompleted(true)); | 414 EXPECT_CALL(article_cb, DistillationCompleted(true)); |
| 389 | 415 |
| 390 std::string entry_id = | 416 std::string entry_id = |
| 391 service_->AddToList(pages_url[0], ArticleCallback(&article_cb)); | 417 service_->AddToList(pages_url[0], |
| 418 service_->CreateDefaultDistillerPage().Pass(), |
| 419 ArticleCallback(&article_cb)); |
| 392 | 420 |
| 393 ArticleEntry entry; | 421 ArticleEntry entry; |
| 394 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); | 422 ASSERT_FALSE(distiller->GetArticleCallback().is_null()); |
| 395 EXPECT_EQ(pages_url[0], distiller->GetUrl()); | 423 EXPECT_EQ(pages_url[0], distiller->GetUrl()); |
| 396 | 424 |
| 397 // Create the article with pages to pass to the distiller. | 425 // Create the article with pages to pass to the distiller. |
| 398 scoped_ptr<DistilledArticleProto> proto = | 426 scoped_ptr<DistilledArticleProto> proto = |
| 399 CreateArticleWithURL(pages_url[0].spec()); | 427 CreateArticleWithURL(pages_url[0].spec()); |
| 400 for (int page_num = 1; page_num < kPageCount; ++page_num) { | 428 for (int page_num = 1; page_num < kPageCount; ++page_num) { |
| 401 DistilledPageProto* distilled_page = proto->add_pages(); | 429 DistilledPageProto* distilled_page = proto->add_pages(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 419 EXPECT_TRUE(store_->GetEntryByUrl(pages_url[page_num], &entry)); | 447 EXPECT_TRUE(store_->GetEntryByUrl(pages_url[page_num], &entry)); |
| 420 } | 448 } |
| 421 | 449 |
| 422 service_->RemoveEntry(entry_id); | 450 service_->RemoveEntry(entry_id); |
| 423 base::RunLoop().RunUntilIdle(); | 451 base::RunLoop().RunUntilIdle(); |
| 424 EXPECT_EQ(0u, store_->GetEntries().size()); | 452 EXPECT_EQ(0u, store_->GetEntries().size()); |
| 425 } | 453 } |
| 426 | 454 |
| 427 } // namespace test | 455 } // namespace test |
| 428 } // namespace dom_distiller | 456 } // namespace dom_distiller |
| OLD | NEW |