Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 2162533002: Adding a request counter to the snippets fetcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@traffic-monitor
Patch Set: Rebase + rename to throttler Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 } // namespace 262 } // namespace
263 263
264 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase { 264 class NTPSnippetsServiceTest : public test::NTPSnippetsTestBase {
265 public: 265 public:
266 NTPSnippetsServiceTest() 266 NTPSnippetsServiceTest()
267 : fake_url_fetcher_factory_( 267 : fake_url_fetcher_factory_(
268 /*default_factory=*/&failing_url_fetcher_factory_), 268 /*default_factory=*/&failing_url_fetcher_factory_),
269 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, 269 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat,
270 google_apis::GetAPIKey().c_str())) { 270 google_apis::GetAPIKey().c_str())) {
271 NTPSnippetsService::RegisterProfilePrefs(pref_service()->registry()); 271 NTPSnippetsService::RegisterProfilePrefs(pref_service()->registry());
272 RequestThrottler::RegisterProfilePrefs(pref_service()->registry());
272 273
273 // Since no SuggestionsService is injected in tests, we need to force the 274 // Since no SuggestionsService is injected in tests, we need to force the
274 // service to fetch from all hosts. 275 // service to fetch from all hosts.
275 base::CommandLine::ForCurrentProcess()->AppendSwitch( 276 base::CommandLine::ForCurrentProcess()->AppendSwitch(
276 switches::kDontRestrict); 277 switches::kDontRestrict);
277 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); 278 EXPECT_TRUE(database_dir_.CreateUniqueTempDir());
278 } 279 }
279 280
280 ~NTPSnippetsServiceTest() override { 281 ~NTPSnippetsServiceTest() override {
281 if (service_) 282 if (service_)
(...skipping 22 matching lines...) Expand all
304 new net::TestURLRequestContextGetter(task_runner.get()); 305 new net::TestURLRequestContextGetter(task_runner.get());
305 306
306 // Delete the current service, so that the database is destroyed before we 307 // Delete the current service, so that the database is destroyed before we
307 // create the new one, otherwise opening the new database will fail. 308 // create the new one, otherwise opening the new database will fail.
308 service_.reset(); 309 service_.reset();
309 310
310 ResetSigninManager(); 311 ResetSigninManager();
311 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher = 312 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher =
312 base::MakeUnique<NTPSnippetsFetcher>( 313 base::MakeUnique<NTPSnippetsFetcher>(
313 fake_signin_manager(), fake_token_service_.get(), 314 fake_signin_manager(), fake_token_service_.get(),
314 std::move(request_context_getter), base::Bind(&ParseJson), 315 std::move(request_context_getter), pref_service(),
316 base::Bind(&ParseJson),
315 /*is_stable_channel=*/true); 317 /*is_stable_channel=*/true);
316 318
317 fake_signin_manager()->SignIn("foo@bar.com"); 319 fake_signin_manager()->SignIn("foo@bar.com");
318 snippets_fetcher->SetPersonalizationForTesting( 320 snippets_fetcher->SetPersonalizationForTesting(
319 NTPSnippetsFetcher::Personalization::kNonPersonal); 321 NTPSnippetsFetcher::Personalization::kNonPersonal);
320 322
321 // Add an initial fetch response, as the service tries to fetch when there 323 // Add an initial fetch response, as the service tries to fetch when there
322 // is nothing in the DB. 324 // is nothing in the DB.
323 SetUpFetchResponse(GetTestJson({GetSnippet()})); 325 SetUpFetchResponse(GetTestJson({GetSnippet()}));
324 326
(...skipping 15 matching lines...) Expand all
340 MockScheduler& mock_scheduler() { return scheduler_; } 342 MockScheduler& mock_scheduler() { return scheduler_; }
341 343
342 // Provide the json to be returned by the fake fetcher. 344 // Provide the json to be returned by the fake fetcher.
343 void SetUpFetchResponse(const std::string& json) { 345 void SetUpFetchResponse(const std::string& json) {
344 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK, 346 fake_url_fetcher_factory_.SetFakeResponse(test_url_, json, net::HTTP_OK,
345 net::URLRequestStatus::SUCCESS); 347 net::URLRequestStatus::SUCCESS);
346 } 348 }
347 349
348 void LoadFromJSONString(const std::string& json) { 350 void LoadFromJSONString(const std::string& json) {
349 SetUpFetchResponse(json); 351 SetUpFetchResponse(json);
350 service()->FetchSnippets(); 352 service()->FetchSnippets(true);
351 base::RunLoop().RunUntilIdle(); 353 base::RunLoop().RunUntilIdle();
352 } 354 }
353 355
354 private: 356 private:
355 base::MessageLoop message_loop_; 357 base::MessageLoop message_loop_;
356 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 358 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
357 // Instantiation of factory automatically sets itself as URLFetcher's factory. 359 // Instantiation of factory automatically sets itself as URLFetcher's factory.
358 net::FakeURLFetcherFactory fake_url_fetcher_factory_; 360 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
359 const GURL test_url_; 361 const GURL test_url_;
360 std::unique_ptr<OAuth2TokenService> fake_token_service_; 362 std::unique_ptr<OAuth2TokenService> fake_token_service_;
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 MockServiceObserver mock_observer; 866 MockServiceObserver mock_observer;
865 service()->AddObserver(&mock_observer); 867 service()->AddObserver(&mock_observer);
866 868
867 // Simulate user signed out 869 // Simulate user signed out
868 SetUpFetchResponse(GetTestJson({GetSnippet()})); 870 SetUpFetchResponse(GetTestJson({GetSnippet()}));
869 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabledReasonChanged( 871 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabledReasonChanged(
870 DisabledReason::SIGNED_OUT)); 872 DisabledReason::SIGNED_OUT));
871 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT); 873 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT);
872 base::RunLoop().RunUntilIdle(); 874 base::RunLoop().RunUntilIdle();
873 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); 875 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_);
874 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made. 876 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made.
875 877
876 // Simulate user sign in. The service should be ready again and load snippets. 878 // Simulate user sign in. The service should be ready again and load snippets.
877 SetUpFetchResponse(GetTestJson({GetSnippet()})); 879 SetUpFetchResponse(GetTestJson({GetSnippet()}));
878 EXPECT_CALL(mock_observer, 880 EXPECT_CALL(mock_observer,
879 NTPSnippetsServiceDisabledReasonChanged(DisabledReason::NONE)); 881 NTPSnippetsServiceDisabledReasonChanged(DisabledReason::NONE));
880 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 882 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
881 service()->OnDisabledReasonChanged(DisabledReason::NONE); 883 service()->OnDisabledReasonChanged(DisabledReason::NONE);
882 base::RunLoop().RunUntilIdle(); 884 base::RunLoop().RunUntilIdle();
883 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); 885 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_);
884 EXPECT_FALSE(service()->snippets().empty()); 886 EXPECT_FALSE(service()->snippets().empty());
885 887
886 service()->RemoveObserver(&mock_observer); 888 service()->RemoveObserver(&mock_observer);
887 } 889 }
888 890
889 } // namespace ntp_snippets 891 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698