| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/breaking_news/subscription_manager.h" | 5 #include "components/ntp_snippets/breaking_news/subscription_manager.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "components/ntp_snippets/pref_names.h" | 8 #include "components/ntp_snippets/pref_names.h" |
| 9 #include "components/prefs/testing_pref_service.h" | 9 #include "components/prefs/testing_pref_service.h" |
| 10 #include "net/url_request/test_url_fetcher_factory.h" | 10 #include "net/url_request/test_url_fetcher_factory.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 } | 47 } |
| 48 | 48 |
| 49 void RespondWithError(int error_code) { | 49 void RespondWithError(int error_code) { |
| 50 net::TestURLFetcher* url_fetcher = GetRunningFetcher(); | 50 net::TestURLFetcher* url_fetcher = GetRunningFetcher(); |
| 51 url_fetcher->set_status(net::URLRequestStatus::FromError(error_code)); | 51 url_fetcher->set_status(net::URLRequestStatus::FromError(error_code)); |
| 52 url_fetcher->SetResponseString(std::string()); | 52 url_fetcher->SetResponseString(std::string()); |
| 53 // Call the URLFetcher delegate to continue the test. | 53 // Call the URLFetcher delegate to continue the test. |
| 54 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 54 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 55 } | 55 } |
| 56 | 56 |
| 57 const std::string url{"http://valid-url.test"}; |
| 58 |
| 57 private: | 59 private: |
| 58 base::MessageLoop message_loop_; | 60 base::MessageLoop message_loop_; |
| 59 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 61 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 60 net::TestURLFetcherFactory url_fetcher_factory_; | 62 net::TestURLFetcherFactory url_fetcher_factory_; |
| 61 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 63 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 62 }; | 64 }; |
| 63 | 65 |
| 64 TEST_F(SubscriptionManagerTest, SubscribeSuccessfully) { | 66 TEST_F(SubscriptionManagerTest, SubscribeSuccessfully) { |
| 65 std::string token = "1234567890"; | 67 std::string token = "1234567890"; |
| 66 SubscriptionManager manager(GetRequestContext(), GetPrefService(), | 68 SubscriptionManager manager(GetRequestContext(), GetPrefService(), GURL(url), |
| 67 GURL("http://valid-url.test")); | 69 GURL(url)); |
| 68 manager.Subscribe(token); | 70 manager.Subscribe(token); |
| 69 RespondWithData(""); | 71 RespondWithData(""); |
| 70 EXPECT_EQ(GetPrefService()->GetString( | 72 EXPECT_EQ(GetPrefService()->GetString( |
| 71 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken), | 73 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken), |
| 72 token); | 74 token); |
| 73 } | 75 } |
| 74 | 76 |
| 75 TEST_F(SubscriptionManagerTest, SubscribeWithErrors) { | 77 TEST_F(SubscriptionManagerTest, SubscribeWithErrors) { |
| 76 std::string token = "1234567890"; | 78 std::string token = "1234567890"; |
| 77 SubscriptionManager manager(GetRequestContext(), GetPrefService(), | 79 SubscriptionManager manager(GetRequestContext(), GetPrefService(), GURL(url), |
| 78 GURL("http://valid-url.test")); | 80 GURL(url)); |
| 79 manager.Subscribe(token); | 81 manager.Subscribe(token); |
| 80 RespondWithError(net::ERR_TIMED_OUT); | 82 RespondWithError(net::ERR_TIMED_OUT); |
| 81 EXPECT_FALSE(GetPrefService()->HasPrefPath( | 83 EXPECT_FALSE(GetPrefService()->HasPrefPath( |
| 82 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken)); | 84 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken)); |
| 83 } | 85 } |
| 84 | 86 |
| 87 TEST_F(SubscriptionManagerTest, UnsubscribeSuccessfully) { |
| 88 std::string token = "1234567890"; |
| 89 GetPrefService()->SetString( |
| 90 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken, token); |
| 91 SubscriptionManager manager(GetRequestContext(), GetPrefService(), GURL(url), |
| 92 GURL(url)); |
| 93 manager.Unsubscribe(token); |
| 94 RespondWithData(""); |
| 95 EXPECT_FALSE(GetPrefService()->HasPrefPath( |
| 96 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken)); |
| 97 } |
| 98 |
| 99 TEST_F(SubscriptionManagerTest, UnsubscribeWithErrors) { |
| 100 std::string token = "1234567890"; |
| 101 GetPrefService()->SetString( |
| 102 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken, token); |
| 103 SubscriptionManager manager(GetRequestContext(), GetPrefService(), GURL(url), |
| 104 GURL(url)); |
| 105 manager.Unsubscribe(token); |
| 106 RespondWithError(net::ERR_TIMED_OUT); |
| 107 EXPECT_EQ(GetPrefService()->GetString( |
| 108 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken), |
| 109 token); |
| 110 } |
| 111 |
| 85 } // namespace ntp_snippets | 112 } // namespace ntp_snippets |
| OLD | NEW |