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

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
« no previous file with comments | « chrome/browser/interests/interests_fetcher.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 const char kEmptyResponse[] =
34 "{\n"
35 " \"interests\": []\n"
36 "}\n";
37 const char kSuccessfulResponse[] =
38 "{\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 const char kAccountId[] = "account@gmail.com";
53
54 std::vector<InterestsFetcher::Interest> GetExpectedEmptyResponse() {
55 return std::vector<InterestsFetcher::Interest>();
56 }
57
58 std::vector<InterestsFetcher::Interest> GetExpectedSuccessfulResponse() {
59 std::vector<InterestsFetcher::Interest> res;
60 res.push_back(InterestsFetcher::Interest{
61 "Google", GURL("https://fake.com/fake.png"), 0.9});
62 res.push_back(InterestsFetcher::Interest{
63 "Google Chrome", GURL("https://fake.com/fake.png"), 0.98});
64 return res;
65 }
66 } // namespace
67
68 class InterestsFetcherTest : public testing::Test {
69 public:
70 InterestsFetcherTest()
71 : request_context_(new net::TestURLRequestContextGetter(
72 base::ThreadTaskRunnerHandle::Get())),
73 url_fetcher_factory_(new net::TestURLFetcherFactory()) {
74 token_service_.UpdateCredentials(kAccountId, "refresh_token");
75
76 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
77
78 command_line->AppendSwitchASCII(switches::kInterestsURL, kInterestsURL);
79 }
80
81 MOCK_METHOD1(OnReceivedInterests,
82 void(const std::vector<InterestsFetcher::Interest>&));
83
84 protected:
85 void RequestInterests() {
86 request_.reset(new InterestsFetcher(&token_service_,
87 kAccountId,
88 request_context_.get()));
89
90 request_->FetchInterests(base::Bind(
91 &InterestsFetcherTest::OnReceivedInterests, base::Unretained(this)));
92 }
93
94 net::TestURLFetcher* GetURLFetcher() {
95 net::TestURLFetcher* url_fetcher =
96 url_fetcher_factory_->GetFetcherByID(kInterestsFetcherURLFetcherID);
97 EXPECT_TRUE(url_fetcher);
98 return url_fetcher;
99 }
100
101 void IssueAccessTokenErrors() {
102 token_service_.IssueErrorForAllPendingRequestsForAccount(
103 kAccountId, GoogleServiceAuthError::FromServiceError("Error!"));
104 }
105
106 void IssueAccessTokens() {
107 token_service_.IssueAllTokensForAccount(
108 kAccountId, "access_token",
109 base::Time::Now() + base::TimeDelta::FromHours(1));
110 }
111
112 void SendResponse(net::Error error,
113 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
171 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())).Times(0);
172 IssueAccessTokens();
173 SendAuthorizationError();
174
175 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
176 IssueAccessTokens();
177 SendValidResponse(kEmptyResponse);
178 }
179
180 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) {
181 RequestInterests();
182
183 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())).Times(0);
184 IssueAccessTokens();
185 SendAuthorizationError();
186
187 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
188 IssueAccessTokens();
189 SendAuthorizationError();
190 }
OLDNEW
« no previous file with comments | « chrome/browser/interests/interests_fetcher.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698