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

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

Issue 1384973002: Add InterestsFetcher which retrieves a user's interests from the server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 {
Bernhard Bauer 2015/10/06 08:56:10 Nit: empty line afterwards.
PEConn 2015/10/06 10:11:45 Done.
30 const int kInterestsFetcherURLFetcherID = 0;
31 const char kInterestsURL[] = "http://www.fake.com";
32 const char kEmptyResponse[] = "{\n"
33 "\"interests\": []\n"
Bernhard Bauer 2015/10/06 08:56:10 Alignment. Also, I would break immediately after t
PEConn 2015/10/06 10:11:45 Done.
34 "}\n";
35 const char kSuccessfulResponse[] = "{\n"
36 " \"interests\": [\n"
37 " {\n"
38 " \"name\": \"Google\",\n"
39 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
40 " \"relevance\": 0.9\n"
41 " },\n"
42 " {\n"
43 " \"name\": \"Google Chrome\",\n"
44 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
45 " \"relevance\": 0.98\n"
46 " }\n"
47 " ]\n"
48 "}\n";
49 const char kAccountId[] = "account@gmail.com";
50
51 std::vector<InterestsFetcher::Interest> GetExpectedEmptyResponse() {
52 return std::vector<InterestsFetcher::Interest>();
53 }
54
55 std::vector<InterestsFetcher::Interest> GetExpectedSuccessfulResponse() {
56 std::vector<InterestsFetcher::Interest> res;
57 res.push_back(
58 InterestsFetcher::Interest{"Google", GURL("https://fake.com/fake.png"), 0. 9});
Bernhard Bauer 2015/10/06 08:56:10 This appears to be over 80 columns.
PEConn 2015/10/06 10:11:45 Done.
59 res.push_back(InterestsFetcher::Interest{
60 "Google Chrome", GURL("https://fake.com/fake.png"), 0.98});
61 return res;
62 }
63 } // namespace
64
65 class InterestsFetcherTest : public testing::Test {
66 public:
67 InterestsFetcherTest()
68 : request_context_(new net::TestURLRequestContextGetter(
69 base::ThreadTaskRunnerHandle::Get())),
70 url_fetcher_factory_(new net::TestURLFetcherFactory()) {
71 token_service_.UpdateCredentials(kAccountId, "refresh_token");
72
73 base::CommandLine* command_line =
74 base::CommandLine::ForCurrentProcess();
75
76 command_line->AppendSwitchASCII(switches::kInterestsURL, kInterestsURL);
77 }
78
79 MOCK_METHOD1(OnReceivedInterests,
80 void(const std::vector<InterestsFetcher::Interest>&));
81 protected:
Marc Treib 2015/10/06 09:35:47 nit: Empty line before protected.
PEConn 2015/10/06 10:11:45 Done.
82 void RequestInterests() {
83 request_.reset(new InterestsFetcher(
84 &token_service_,
85 kAccountId,
86 request_context_.get()));
87
88 request_->FetchInterests(
89 base::Bind(&InterestsFetcherTest::OnReceivedInterests,
90 base::Unretained(this)));
91 }
92
93 net::TestURLFetcher* GetURLFetcher() {
94 net::TestURLFetcher* url_fetcher = url_fetcher_factory_->GetFetcherByID(
95 kInterestsFetcherURLFetcherID);
96 EXPECT_TRUE(url_fetcher);
97 return url_fetcher;
98 }
99
100 void IssueAccessTokenErrors() {
101 token_service_.IssueErrorForAllPendingRequestsForAccount(
102 kAccountId,
103 GoogleServiceAuthError::FromServiceError("Error!"));
104 }
105
106 void IssueAccessTokens() {
107 token_service_.IssueAllTokensForAccount(
108 kAccountId,
109 "access_token",
110 base::Time::Now() + base::TimeDelta::FromHours(1));
111 }
112
113 void SendResponse(net::Error error, int response_code,
114 const std::string& response) {
115 net::TestURLFetcher* url_fetcher = GetURLFetcher();
116 url_fetcher->set_status(net::URLRequestStatus::FromError(error));
117 url_fetcher->set_response_code(response_code);
118 url_fetcher->SetResponseString(response);
119 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
120 }
121
122 void SendValidResponse(const std::string& response) {
123 SendResponse(net::OK, net::HTTP_OK, response);
124 }
125
126 void SendFailedResponse() {
127 SendResponse(net::ERR_ABORTED, 0, std::string());
128 }
129
130 void SendAuthorizationError() {
131 SendResponse(net::OK, net::HTTP_UNAUTHORIZED, std::string());
132 }
133
134 base::MessageLoop message_loop_;
135 FakeProfileOAuth2TokenService token_service_;
136 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
137 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
138 scoped_ptr<InterestsFetcher> request_;
139 };
140
141 TEST_F(InterestsFetcherTest, EmptyResponse) {
142 RequestInterests();
143 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
144 IssueAccessTokens();
145 SendValidResponse(kEmptyResponse);
146 }
147
148 TEST_F(InterestsFetcherTest, SuccessfullResponse) {
149 RequestInterests();
150 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse()));
151 IssueAccessTokens();
152 SendValidResponse(kSuccessfulResponse);
153 }
154
155 TEST_F(InterestsFetcherTest, FailedResponse) {
156 RequestInterests();
157 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
158 IssueAccessTokens();
159 SendFailedResponse();
160 }
161
162 TEST_F(InterestsFetcherTest, FailedOAuthRequest) {
163 RequestInterests();
164 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
165 IssueAccessTokenErrors();
166 }
167
168 TEST_F(InterestsFetcherTest, RetryOnAuthorizationError) {
169 RequestInterests();
170 {
Bernhard Bauer 2015/10/06 08:56:10 I don' think this scope does anything, as you are
PEConn 2015/10/06 10:11:45 Done.
171 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()))
172 .Times(0);
173 IssueAccessTokens();
174 SendAuthorizationError();
175 }
176 {
177 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
178 IssueAccessTokens();
179 SendValidResponse(kEmptyResponse);
180 }
181 }
182
183 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) {
184 RequestInterests();
185 {
186 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()))
187 .Times(0);
188 IssueAccessTokens();
189 SendAuthorizationError();
190 }
191 {
192 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
193 IssueAccessTokens();
194 SendAuthorizationError();
195 }
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698