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

Side by Side Diff: components/ntp_snippets/breaking_news/subscription_manager_unittest.cc

Issue 2914263002: [NTP::Push] Adding Breaking News Subscription Manager (Closed)
Patch Set: Subscription endpoint as a parameter. Created 3 years, 6 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
(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/pref_names.h"
9 #include "components/prefs/testing_pref_service.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace ntp_snippets {
15
16 class SubscriptionManagerTest : public testing::Test {
17 public:
18 SubscriptionManagerTest()
19 : request_context_getter_(
20 new net::TestURLRequestContextGetter(message_loop_.task_runner())),
21 pref_service_(base::MakeUnique<TestingPrefServiceSimple>()) {}
22
23 void SetUp() override {
24 SubscriptionManager::RegisterProfilePrefs(pref_service_->registry());
25 }
26
27 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() {
28 return request_context_getter_.get();
29 }
30
31 PrefService* GetPrefService() { return pref_service_.get(); }
32
33 net::TestURLFetcher* GetRunningFetcher() {
34 // All created TestURLFetchers have ID 0 by default.
35 net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(0);
36 DCHECK(url_fetcher);
37 return url_fetcher;
38 }
39
40 void RespondWithData(const std::string& data) {
41 net::TestURLFetcher* url_fetcher = GetRunningFetcher();
42 url_fetcher->set_status(net::URLRequestStatus());
43 url_fetcher->set_response_code(net::HTTP_OK);
44 url_fetcher->SetResponseString(data);
45 // Call the URLFetcher delegate to continue the test.
46 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
47 }
48
49 void RespondWithError(int error_code) {
50 net::TestURLFetcher* url_fetcher = GetRunningFetcher();
51 url_fetcher->set_status(net::URLRequestStatus::FromError(error_code));
52 url_fetcher->SetResponseString(std::string());
53 // Call the URLFetcher delegate to continue the test.
54 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
55 }
56
57 private:
58 base::MessageLoop message_loop_;
59 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
60 net::TestURLFetcherFactory url_fetcher_factory_;
61 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
62 };
63
64 TEST_F(SubscriptionManagerTest, SubscribeSuccessfully) {
65 std::string token = "1234567890";
66 SubscriptionManager manager(GetRequestContext(), GetPrefService(),
67 GURL("http://valid-url.test"));
68 manager.Subscribe(token);
69 RespondWithData("");
70 EXPECT_EQ(GetPrefService()->GetString(
71 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken),
72 token);
73 }
74
75 TEST_F(SubscriptionManagerTest, SubscribeWithErrors) {
76 std::string token = "1234567890";
77 SubscriptionManager manager(GetRequestContext(), GetPrefService(),
78 GURL("http://valid-url.test"));
79 manager.Subscribe(token);
80 RespondWithError(net::ERR_TIMED_OUT);
81 EXPECT_FALSE(GetPrefService()->HasPrefPath(
82 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken));
83 }
84
85 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/breaking_news/subscription_manager.cc ('k') | components/ntp_snippets/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698