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

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

Issue 1922083004: Allow fetching personalized snippets from ChromeReader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: After code review #2 Created 4 years, 7 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_fetcher.h" 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/test/histogram_tester.h" 11 #include "base/test/histogram_tester.h"
12 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
13 #include "components/signin/core/browser/account_tracker_service.h"
14 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
15 #include "components/signin/core/browser/fake_signin_manager.h"
16 #include "components/signin/core/browser/test_signin_client.h"
13 #include "google_apis/google_api_keys.h" 17 #include "google_apis/google_api_keys.h"
14 #include "net/url_request/test_url_fetcher_factory.h" 18 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_request_test_util.h" 19 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
18 22
19 namespace ntp_snippets { 23 namespace ntp_snippets {
20 namespace { 24 namespace {
21 25
22 using testing::ElementsAre; 26 using testing::ElementsAre;
(...skipping 21 matching lines...) Expand all
44 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, 48 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND,
45 net::URLRequestStatus::FAILED)); 49 net::URLRequestStatus::FAILED));
46 } 50 }
47 }; 51 };
48 52
49 class NTPSnippetsFetcherTest : public testing::Test { 53 class NTPSnippetsFetcherTest : public testing::Test {
50 public: 54 public:
51 NTPSnippetsFetcherTest() 55 NTPSnippetsFetcherTest()
52 : fake_url_fetcher_factory_( 56 : fake_url_fetcher_factory_(
53 /*default_factory=*/&failing_url_fetcher_factory_), 57 /*default_factory=*/&failing_url_fetcher_factory_),
54 snippets_fetcher_(scoped_refptr<net::TestURLRequestContextGetter>( 58 signin_client_(new TestSigninClient(nullptr)),
59 account_tracker_(new AccountTrackerService()),
60 snippets_fetcher_(new FakeSigninManagerBase(signin_client_.get(),
61 account_tracker_.get()),
62 new FakeProfileOAuth2TokenService(),
63 scoped_refptr<net::TestURLRequestContextGetter>(
55 new net::TestURLRequestContextGetter( 64 new net::TestURLRequestContextGetter(
56 base::ThreadTaskRunnerHandle::Get())), 65 base::ThreadTaskRunnerHandle::Get())),
57 /*is_stable_channel=*/true), 66 /*is_stable_channel=*/true),
58 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, 67 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat,
59 google_apis::GetAPIKey().c_str())) { 68 google_apis::GetAPIKey().c_str())) {
60 snippets_fetcher_subscription_ = snippets_fetcher_.AddCallback( 69 snippets_fetcher_subscription_ = snippets_fetcher_.AddCallback(
61 base::Bind(&MockSnippetsAvailableCallback::Run, 70 base::Bind(&MockSnippetsAvailableCallback::Run,
62 base::Unretained(&mock_callback_))); 71 base::Unretained(&mock_callback_)));
63 } 72 }
64 73
74
65 net::FakeURLFetcherFactory& fake_url_fetcher_factory() { 75 net::FakeURLFetcherFactory& fake_url_fetcher_factory() {
66 return fake_url_fetcher_factory_; 76 return fake_url_fetcher_factory_;
67 } 77 }
68 78
69 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } 79 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; }
70 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } 80 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
71 void RunUntilIdle() { message_loop_.RunUntilIdle(); } 81 void RunUntilIdle() { message_loop_.RunUntilIdle(); }
72 const GURL& test_url() { return test_url_; } 82 const GURL& test_url() { return test_url_; }
73 83
74 base::HistogramTester& histogram_tester() { return histogram_tester_; } 84 base::HistogramTester& histogram_tester() { return histogram_tester_; }
75 85
76 private: 86 private:
77 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 87 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
78 // Instantiation of factory automatically sets itself as URLFetcher's factory. 88 // Instantiation of factory automatically sets itself as URLFetcher's factory.
79 net::FakeURLFetcherFactory fake_url_fetcher_factory_; 89 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
90 std::unique_ptr<TestSigninClient> signin_client_;
91 std::unique_ptr<AccountTrackerService> account_tracker_;
80 // Needed to use ThreadTaskRunnerHandle. 92 // Needed to use ThreadTaskRunnerHandle.
81 base::MessageLoop message_loop_; 93 base::MessageLoop message_loop_;
82 NTPSnippetsFetcher snippets_fetcher_; 94 NTPSnippetsFetcher snippets_fetcher_;
83 MockSnippetsAvailableCallback mock_callback_; 95 MockSnippetsAvailableCallback mock_callback_;
84 std::unique_ptr< 96 std::unique_ptr<
85 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> 97 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription>
86 snippets_fetcher_subscription_; 98 snippets_fetcher_subscription_;
87 const GURL test_url_; 99 const GURL test_url_;
88 base::HistogramTester histogram_tester_; 100 base::HistogramTester histogram_tester_;
89 101
(...skipping 10 matching lines...) Expand all
100 112
101 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { 113 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) {
102 const std::string json_str = "{ \"recos\": [] }"; 114 const std::string json_str = "{ \"recos\": [] }";
103 fake_url_fetcher_factory().SetFakeResponse(test_url(), 115 fake_url_fetcher_factory().SetFakeResponse(test_url(),
104 /*data=*/json_str, net::HTTP_OK, 116 /*data=*/json_str, net::HTTP_OK,
105 net::URLRequestStatus::SUCCESS); 117 net::URLRequestStatus::SUCCESS);
106 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, 118 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
107 /*status_message=*/std::string())) 119 /*status_message=*/std::string()))
108 .Times(1); 120 .Times(1);
109 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 121 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
122 /*language_code=*/"en-US",
110 /*count=*/1); 123 /*count=*/1);
111 RunUntilIdle(); 124 RunUntilIdle();
112 EXPECT_THAT(histogram_tester().GetAllSamples( 125 EXPECT_THAT(histogram_tester().GetAllSamples(
113 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 126 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
114 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 127 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
115 } 128 }
116 129
117 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 130 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
118 fake_url_fetcher_factory().SetFakeResponse(test_url(), 131 fake_url_fetcher_factory().SetFakeResponse(test_url(),
119 /*data=*/std::string(), 132 /*data=*/std::string(),
120 net::HTTP_NOT_FOUND, 133 net::HTTP_NOT_FOUND,
121 net::URLRequestStatus::FAILED); 134 net::URLRequestStatus::FAILED);
122 EXPECT_CALL(mock_callback(), 135 EXPECT_CALL(mock_callback(),
123 Run(/*snippets_json=*/std::string(), 136 Run(/*snippets_json=*/std::string(),
124 /*status_message=*/"URLRequestStatus error -2")) 137 /*status_message=*/"URLRequestStatus error -2"))
125 .Times(1); 138 .Times(1);
126 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 139 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
140 /*language_code=*/"en-US",
127 /*count=*/1); 141 /*count=*/1);
128 RunUntilIdle(); 142 RunUntilIdle();
129 EXPECT_THAT(histogram_tester().GetAllSamples( 143 EXPECT_THAT(histogram_tester().GetAllSamples(
130 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 144 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
131 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 145 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
132 } 146 }
133 147
134 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 148 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
135 fake_url_fetcher_factory().SetFakeResponse(test_url(), 149 fake_url_fetcher_factory().SetFakeResponse(test_url(),
136 /*data=*/std::string(), 150 /*data=*/std::string(),
137 net::HTTP_NOT_FOUND, 151 net::HTTP_NOT_FOUND,
138 net::URLRequestStatus::SUCCESS); 152 net::URLRequestStatus::SUCCESS);
139 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(), 153 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
140 /*status_message=*/"HTTP error 404")) 154 /*status_message=*/"HTTP error 404"))
141 .Times(1); 155 .Times(1);
142 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 156 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
157 /*language_code=*/"en-US",
143 /*count=*/1); 158 /*count=*/1);
144 RunUntilIdle(); 159 RunUntilIdle();
145 EXPECT_THAT(histogram_tester().GetAllSamples( 160 EXPECT_THAT(histogram_tester().GetAllSamples(
146 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 161 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
147 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 162 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
148 } 163 }
149 164
150 // This test actually verifies that the test setup itself is sane, to prevent 165 // This test actually verifies that the test setup itself is sane, to prevent
151 // hard-to-reproduce test failures. 166 // hard-to-reproduce test failures.
152 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 167 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
153 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(), 168 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/std::string(),
154 /*status_message=*/Not(IsEmpty()))) 169 /*status_message=*/Not(IsEmpty())))
155 .Times(1); 170 .Times(1);
156 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 171 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
172 /*language_code=*/"en-US",
157 /*count=*/1); 173 /*count=*/1);
158 RunUntilIdle(); 174 RunUntilIdle();
159 } 175 }
160 176
161 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 177 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
162 const std::string json_str = "{ \"recos\": [] }"; 178 const std::string json_str = "{ \"recos\": [] }";
163 fake_url_fetcher_factory().SetFakeResponse(test_url(), 179 fake_url_fetcher_factory().SetFakeResponse(test_url(),
164 /*data=*/json_str, net::HTTP_OK, 180 /*data=*/json_str, net::HTTP_OK,
165 net::URLRequestStatus::SUCCESS); 181 net::URLRequestStatus::SUCCESS);
166 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str, 182 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/json_str,
167 /*status_message=*/std::string())) 183 /*status_message=*/std::string()))
168 .Times(1); 184 .Times(1);
169 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 185 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
186 /*language_code=*/"en-US",
170 /*count=*/1); 187 /*count=*/1);
171 // Second call to FetchSnippets() overrides/cancels the previous. Callback is 188 // Second call to FetchSnippets() overrides/cancels the previous. Callback is
172 // expected to be called once. 189 // expected to be called once.
173 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(), 190 snippets_fetcher().FetchSnippets(/*hosts=*/std::set<std::string>(),
191 /*language_code=*/"en-US",
174 /*count=*/1); 192 /*count=*/1);
175 RunUntilIdle(); 193 RunUntilIdle();
176 EXPECT_THAT(histogram_tester().GetAllSamples( 194 EXPECT_THAT(histogram_tester().GetAllSamples(
177 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 195 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
178 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 196 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
179 } 197 }
180 198
181 } // namespace 199 } // namespace
182 } // namespace ntp_snippets 200 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698