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

Side by Side Diff: chrome/browser/interests/interests_fetcher_unittest.cc

Issue 1317513004: Add InterestsFetcher which retrieves a user's interests from the server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve handling of HTTP errors. Created 5 years, 2 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 2015 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 "chrome/browser/interests/interests_fetcher.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_status_code.h"
17 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "net/url_request/url_request_status.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gmock/include/gmock/gmock-generated-function-mockers.h"
21 #include "testing/gmock/include/gmock/gmock-matchers.h"
22 #include "testing/gmock/include/gmock/gmock-more-matchers.h"
23 #include "testing/gmock/include/gmock/gmock-spec-builders.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 using testing::IsEmpty;
27 using testing::Not;
28
29 namespace {
30
31 const int kInterestsFetcherURLFetcherID = 0;
32 const char kInterestsURL[] = "http://www.fake.com";
33
34 const char kEmptyResponse[] = "{\n"
35 "\"interests\": []\n"
36 "}\n";
37
38 const char kSuccessfulResponse[] = "{\n"
39 " \"interests\": [\n"
40 " {\n"
41 " \"name\": \"Google\",\n"
42 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
43 " \"relevance\": 0.9\n"
44 " },\n"
45 " {\n"
46 " \"name\": \"Google Chrome\",\n"
47 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
48 " \"relevance\": 0.98\n"
49 " }\n"
50 " ]\n"
51 "}\n";
52
53 const char kAccountId[] = "account@gmail.com";
54
55 std::vector<InterestsFetcher::Interest> GetExpectedEmptyResponse() {
56 return std::vector<InterestsFetcher::Interest>();
57 }
58
59 std::vector<InterestsFetcher::Interest> GetExpectedSuccessfulResponse() {
60 std::vector<InterestsFetcher::Interest> res;
61 res.push_back(
62 InterestsFetcher::Interest{"Google", GURL("https://fake.com/fake.png"), 0. 9});
63 res.push_back(InterestsFetcher::Interest{
64 "Google Chrome", GURL("https://fake.com/fake.png"), 0.98});
65 return res;
66 }
67
68 } // namespace
69
70 class InterestsFetcherTest : public testing::Test {
71 public:
72 InterestsFetcherTest()
73 : request_context_(new net::TestURLRequestContextGetter(
74 base::ThreadTaskRunnerHandle::Get())),
75 url_fetcher_factory_(new net::TestURLFetcherFactory()) {
76 token_service_.UpdateCredentials(kAccountId, "refresh_token");
77
78 base::CommandLine* command_line =
79 base::CommandLine::ForCurrentProcess();
80
81 command_line->AppendSwitchASCII(switches::kInterestsURL, kInterestsURL);
82
83 }
84
85 MOCK_METHOD1(OnReceivedInterests,
86 void(const std::vector<InterestsFetcher::Interest>&));
87
88 protected:
89 void RequestInterests() {
90 request_.reset(new InterestsFetcher(
91 &token_service_,
92 kAccountId,
93 request_context_.get()));
94
95
96 request_->Start(
97 base::Bind(&InterestsFetcherTest::OnReceivedInterests,
98 base::Unretained(this)));
99 }
100
101 net::TestURLFetcher* GetURLFetcher() {
102 net::TestURLFetcher* url_fetcher = url_fetcher_factory_->GetFetcherByID(
103 kInterestsFetcherURLFetcherID);
104 EXPECT_TRUE(url_fetcher);
105 return url_fetcher;
106 }
107
108 void IssueAccessTokenErrors() {
109 token_service_.IssueErrorForAllPendingRequestsForAccount(
110 kAccountId,
111 GoogleServiceAuthError::FromServiceError("Error!"));
112 }
113
114 void IssueAccessTokens() {
115 token_service_.IssueAllTokensForAccount(
116 kAccountId,
117 "access_token",
118 base::Time::Now() + base::TimeDelta::FromHours(1));
119 }
120
121 void SendResponse(net::Error error, int response_code,
122 const std::string& response) {
123 net::TestURLFetcher* url_fetcher = GetURLFetcher();
124 url_fetcher->set_status(net::URLRequestStatus::FromError(error));
125 url_fetcher->set_response_code(response_code);
126 url_fetcher->SetResponseString(response);
127 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
128 }
129
130 void SendValidResponse(const std::string& response) {
131 SendResponse(net::OK, net::HTTP_OK, response);
132 }
133
134
135 void SendFailedResponse() {
136 SendResponse(net::ERR_ABORTED, 0, std::string());
137 }
138
139 void SendAuthorizationError() {
140 SendResponse(net::OK, net::HTTP_UNAUTHORIZED, std::string());
141 }
142
143 base::MessageLoop message_loop_;
144 FakeProfileOAuth2TokenService token_service_;
145 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
146 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
147 scoped_ptr<InterestsFetcher> request_;
148
149 };
150
151 TEST_F(InterestsFetcherTest, EmptyResponse) {
152 RequestInterests();
153 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
154 IssueAccessTokens();
155 SendValidResponse(kEmptyResponse);
156 }
157
158 TEST_F(InterestsFetcherTest, SuccessfullResponse) {
159 RequestInterests();
160 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse()));
161 IssueAccessTokens();
162 SendValidResponse(kSuccessfulResponse);
163 }
164
165 TEST_F(InterestsFetcherTest, FailedResponse) {
166 RequestInterests();
167 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
168 IssueAccessTokens();
169 SendFailedResponse();
170 }
171
172 TEST_F(InterestsFetcherTest, FailedOAuthRequest) {
173 RequestInterests();
174 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
175 IssueAccessTokenErrors();
176 }
177
178 TEST_F(InterestsFetcherTest, RetryOnAuthorizationError) {
179 RequestInterests();
180 {
181 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()))
182 .Times(0);
183 IssueAccessTokens();
184 SendAuthorizationError();
185 }
186 {
187 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
188 IssueAccessTokens();
189 SendValidResponse(kEmptyResponse);
190 }
191 }
192
193 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) {
194 RequestInterests();
195 {
196 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()))
197 .Times(0);
198 IssueAccessTokens();
199 SendAuthorizationError();
200 }
201 {
202 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
203 IssueAccessTokens();
204 SendAuthorizationError();
205 }
206 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698