| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), | 215 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), |
| 216 account_tracker_.get())), | 216 account_tracker_.get())), |
| 217 fake_token_service_(new FakeProfileOAuth2TokenService()) { | 217 fake_token_service_(new FakeProfileOAuth2TokenService()) { |
| 218 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); | 218 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); |
| 219 // Since no SuggestionsService is injected in tests, we need to force the | 219 // Since no SuggestionsService is injected in tests, we need to force the |
| 220 // service to fetch from all hosts. | 220 // service to fetch from all hosts. |
| 221 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 221 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 222 switches::kDontRestrict); | 222 switches::kDontRestrict); |
| 223 } | 223 } |
| 224 | 224 |
| 225 ~NTPSnippetsServiceTest() override {} | 225 ~NTPSnippetsServiceTest() override { |
| 226 if (service_) |
| 227 service_->Shutdown(); |
| 228 } |
| 226 | 229 |
| 227 void SetUp() override { | 230 void SetUp() override { |
| 228 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 231 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 229 CreateSnippetsService(/*enabled=*/true); | 232 CreateSnippetsService(/*enabled=*/true); |
| 230 } | 233 } |
| 231 | 234 |
| 232 void CreateSnippetsService(bool enabled) { | 235 void CreateSnippetsService(bool enabled) { |
| 236 if (service_) |
| 237 service_->Shutdown(); |
| 238 |
| 233 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 239 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 234 base::ThreadTaskRunnerHandle::Get()); | 240 base::ThreadTaskRunnerHandle::Get()); |
| 235 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 241 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 236 new net::TestURLRequestContextGetter(task_runner.get()); | 242 new net::TestURLRequestContextGetter(task_runner.get()); |
| 237 | 243 |
| 238 service_.reset(new NTPSnippetsService( | 244 service_.reset(new NTPSnippetsService( |
| 239 pref_service_.get(), nullptr, task_runner, std::string("fr"), | 245 pref_service_.get(), nullptr, task_runner, std::string("fr"), |
| 240 &scheduler_, | 246 &scheduler_, |
| 241 base::WrapUnique(new NTPSnippetsFetcher( | 247 base::WrapUnique(new NTPSnippetsFetcher( |
| 242 fake_signin_manager_.get(), fake_token_service_.get(), | 248 fake_signin_manager_.get(), fake_token_service_.get(), |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 EXPECT_THAT(service()->snippets(), IsEmpty()); | 473 EXPECT_THAT(service()->snippets(), IsEmpty()); |
| 468 LoadFromJSONString(json_str); | 474 LoadFromJSONString(json_str); |
| 469 EXPECT_THAT(service()->snippets(), SizeIs(1)); | 475 EXPECT_THAT(service()->snippets(), SizeIs(1)); |
| 470 } | 476 } |
| 471 | 477 |
| 472 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { | 478 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { |
| 473 LoadFromJSONString(GetTestJson()); | 479 LoadFromJSONString(GetTestJson()); |
| 474 | 480 |
| 475 // For the test, we need the snippet to get discarded. | 481 // For the test, we need the snippet to get discarded. |
| 476 ASSERT_TRUE(service()->DiscardSnippet("http://localhost/foobar")); | 482 ASSERT_TRUE(service()->DiscardSnippet("http://localhost/foobar")); |
| 477 const NTPSnippetsService::NTPSnippetStorage& snippets = | 483 const NTPSnippet::PtrVector& snippets = service()->discarded_snippets(); |
| 478 service()->discarded_snippets(); | |
| 479 EXPECT_EQ(1u, snippets.size()); | 484 EXPECT_EQ(1u, snippets.size()); |
| 480 for (auto& snippet : snippets) { | 485 for (auto& snippet : snippets) { |
| 481 EXPECT_EQ("http://localhost/foobar", snippet->id()); | 486 EXPECT_EQ("http://localhost/foobar", snippet->id()); |
| 482 } | 487 } |
| 483 | 488 |
| 484 // There should be no discarded snippet after clearing the list. | 489 // There should be no discarded snippet after clearing the list. |
| 485 service()->ClearDiscardedSnippets(); | 490 service()->ClearDiscardedSnippets(); |
| 486 EXPECT_EQ(0u, service()->discarded_snippets().size()); | 491 EXPECT_EQ(0u, service()->discarded_snippets().size()); |
| 487 } | 492 } |
| 488 | 493 |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 EXPECT_THAT( | 770 EXPECT_THAT( |
| 766 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 771 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
| 767 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 772 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| 768 // Recreating the service and loading from prefs shouldn't count as fetched | 773 // Recreating the service and loading from prefs shouldn't count as fetched |
| 769 // articles. | 774 // articles. |
| 770 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 775 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 771 CreateSnippetsService(/*enabled=*/true); | 776 CreateSnippetsService(/*enabled=*/true); |
| 772 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); | 777 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); |
| 773 } | 778 } |
| 774 } // namespace ntp_snippets | 779 } // namespace ntp_snippets |
| OLD | NEW |