| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ntp_snippets/breaking_news/subscription_manager.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "components/ntp_snippets/breaking_news/breaking_news_suggestions_provid
er.h" |
| 9 #include "components/ntp_snippets/pref_names.h" |
| 10 #include "components/prefs/testing_pref_service.h" |
| 11 #include "net/url_request/test_url_fetcher_factory.h" |
| 12 #include "net/url_request/url_request_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace ntp_snippets { |
| 16 |
| 17 class SubscriptionManagerTest : public testing::Test { |
| 18 public: |
| 19 SubscriptionManagerTest() |
| 20 : request_context_getter_( |
| 21 new net::TestURLRequestContextGetter(message_loop_.task_runner())), |
| 22 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()) {} |
| 23 |
| 24 void SetUp() override { |
| 25 BreakingNewsSuggestionsProvider::RegisterProfilePrefs( |
| 26 pref_service_->registry()); |
| 27 } |
| 28 |
| 29 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() { |
| 30 return request_context_getter_.get(); |
| 31 } |
| 32 |
| 33 PrefService* GetPrefService() { return pref_service_.get(); } |
| 34 |
| 35 net::TestURLFetcher* GetRunningFetcher() { |
| 36 // All created TestURLFetchers have ID 0 by default. |
| 37 net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 38 DCHECK(url_fetcher); |
| 39 return url_fetcher; |
| 40 } |
| 41 |
| 42 void RespondWithData(const std::string& data) { |
| 43 net::TestURLFetcher* url_fetcher = GetRunningFetcher(); |
| 44 url_fetcher->set_status(net::URLRequestStatus()); |
| 45 url_fetcher->set_response_code(net::HTTP_OK); |
| 46 url_fetcher->SetResponseString(data); |
| 47 // Call the URLFetcher delegate to continue the test. |
| 48 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 49 } |
| 50 |
| 51 void RespondWithError(int error_code) { |
| 52 net::TestURLFetcher* url_fetcher = GetRunningFetcher(); |
| 53 url_fetcher->set_status(net::URLRequestStatus::FromError(error_code)); |
| 54 url_fetcher->SetResponseString(std::string()); |
| 55 // Call the URLFetcher delegate to continue the test. |
| 56 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 57 } |
| 58 |
| 59 private: |
| 60 base::MessageLoop message_loop_; |
| 61 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 62 net::TestURLFetcherFactory url_fetcher_factory_; |
| 63 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 64 }; |
| 65 |
| 66 TEST_F(SubscriptionManagerTest, SubscribeWithoutErrors) { |
| 67 std::string token = "1234567890"; |
| 68 SubscriptionManager manager(GetRequestContext(), GetPrefService()); |
| 69 manager.Subscribe(token); |
| 70 RespondWithData(""); |
| 71 EXPECT_EQ(GetPrefService()->GetString( |
| 72 ntp_snippets::prefs::kContentSuggestionsSubscriptionToken), |
| 73 token); |
| 74 } |
| 75 |
| 76 TEST_F(SubscriptionManagerTest, SubscribeWithErrors) { |
| 77 std::string token = "1234567890"; |
| 78 SubscriptionManager manager(GetRequestContext(), GetPrefService()); |
| 79 manager.Subscribe(token); |
| 80 RespondWithError(net::ERR_TIMED_OUT); |
| 81 EXPECT_FALSE(GetPrefService()->HasPrefPath( |
| 82 ntp_snippets::prefs::kContentSuggestionsSubscriptionToken)); |
| 83 } |
| 84 |
| 85 } // namespace ntp_snippets |
| OLD | NEW |