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(""); |
| 72 EXPECT_TRUE(manager.IsSubscribed()); |
70 EXPECT_EQ(GetPrefService()->GetString( | 73 EXPECT_EQ(GetPrefService()->GetString( |
71 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken), | 74 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken), |
72 token); | 75 token); |
73 } | 76 } |
74 | 77 |
75 TEST_F(SubscriptionManagerTest, SubscribeWithErrors) { | 78 TEST_F(SubscriptionManagerTest, SubscribeWithErrors) { |
76 std::string token = "1234567890"; | 79 std::string token = "1234567890"; |
77 SubscriptionManager manager(GetRequestContext(), GetPrefService(), | 80 SubscriptionManager manager(GetRequestContext(), GetPrefService(), GURL(url), |
78 GURL("http://valid-url.test")); | 81 GURL(url)); |
79 manager.Subscribe(token); | 82 manager.Subscribe(token); |
80 RespondWithError(net::ERR_TIMED_OUT); | 83 RespondWithError(net::ERR_TIMED_OUT); |
| 84 EXPECT_FALSE(manager.IsSubscribed()); |
81 EXPECT_FALSE(GetPrefService()->HasPrefPath( | 85 EXPECT_FALSE(GetPrefService()->HasPrefPath( |
82 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken)); | 86 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken)); |
83 } | 87 } |
84 | 88 |
| 89 TEST_F(SubscriptionManagerTest, UnsubscribeSuccessfully) { |
| 90 std::string token = "1234567890"; |
| 91 GetPrefService()->SetString( |
| 92 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken, token); |
| 93 SubscriptionManager manager(GetRequestContext(), GetPrefService(), GURL(url), |
| 94 GURL(url)); |
| 95 manager.Unsubscribe(token); |
| 96 RespondWithData(""); |
| 97 EXPECT_FALSE(manager.IsSubscribed()); |
| 98 EXPECT_FALSE(GetPrefService()->HasPrefPath( |
| 99 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken)); |
| 100 } |
| 101 |
| 102 TEST_F(SubscriptionManagerTest, UnsubscribeWithErrors) { |
| 103 std::string token = "1234567890"; |
| 104 GetPrefService()->SetString( |
| 105 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken, token); |
| 106 SubscriptionManager manager(GetRequestContext(), GetPrefService(), GURL(url), |
| 107 GURL(url)); |
| 108 manager.Unsubscribe(token); |
| 109 RespondWithError(net::ERR_TIMED_OUT); |
| 110 EXPECT_TRUE(manager.IsSubscribed()); |
| 111 EXPECT_EQ(GetPrefService()->GetString( |
| 112 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken), |
| 113 token); |
| 114 } |
| 115 |
85 } // namespace ntp_snippets | 116 } // namespace ntp_snippets |
OLD | NEW |