OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/interests/interests_fetcher.h" | 5 #include "chrome/browser/interests/interests_fetcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 : request_context_(new net::TestURLRequestContextGetter( | 71 : request_context_(new net::TestURLRequestContextGetter( |
72 base::ThreadTaskRunnerHandle::Get())), | 72 base::ThreadTaskRunnerHandle::Get())), |
73 url_fetcher_factory_(new net::TestURLFetcherFactory()) { | 73 url_fetcher_factory_(new net::TestURLFetcherFactory()) { |
74 token_service_.UpdateCredentials(kAccountId, "refresh_token"); | 74 token_service_.UpdateCredentials(kAccountId, "refresh_token"); |
75 | 75 |
76 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 76 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
77 | 77 |
78 command_line->AppendSwitchASCII(switches::kInterestsURL, kInterestsURL); | 78 command_line->AppendSwitchASCII(switches::kInterestsURL, kInterestsURL); |
79 } | 79 } |
80 | 80 |
81 MOCK_METHOD1(OnReceivedInterests, | 81 MOCK_METHOD0(OnSuccessfulResponse, void()); |
82 void(const std::vector<InterestsFetcher::Interest>&)); | 82 MOCK_METHOD0(OnEmptyResponse, void()); |
| 83 MOCK_METHOD0(OnFailedResponse, void()); |
| 84 |
| 85 void OnReceivedInterests( |
| 86 scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { |
| 87 if (!interests) { |
| 88 OnFailedResponse(); |
| 89 return; |
| 90 } |
| 91 |
| 92 if (*interests == GetExpectedEmptyResponse()) |
| 93 OnEmptyResponse(); |
| 94 else if (*interests == GetExpectedSuccessfulResponse()) |
| 95 OnSuccessfulResponse(); |
| 96 else |
| 97 FAIL() << "Unexpected interests response."; |
| 98 } |
83 | 99 |
84 protected: | 100 protected: |
85 void RequestInterests() { | 101 void RequestInterests() { |
86 request_.reset(new InterestsFetcher(&token_service_, | 102 request_.reset(new InterestsFetcher(&token_service_, |
87 kAccountId, | 103 kAccountId, |
88 request_context_.get())); | 104 request_context_.get())); |
89 | 105 |
90 request_->FetchInterests(base::Bind( | 106 request_->FetchInterests(base::Bind( |
91 &InterestsFetcherTest::OnReceivedInterests, base::Unretained(this))); | 107 &InterestsFetcherTest::OnReceivedInterests, base::Unretained(this))); |
92 } | 108 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 149 |
134 base::MessageLoop message_loop_; | 150 base::MessageLoop message_loop_; |
135 FakeProfileOAuth2TokenService token_service_; | 151 FakeProfileOAuth2TokenService token_service_; |
136 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 152 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
137 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; | 153 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; |
138 scoped_ptr<InterestsFetcher> request_; | 154 scoped_ptr<InterestsFetcher> request_; |
139 }; | 155 }; |
140 | 156 |
141 TEST_F(InterestsFetcherTest, EmptyResponse) { | 157 TEST_F(InterestsFetcherTest, EmptyResponse) { |
142 RequestInterests(); | 158 RequestInterests(); |
143 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 159 EXPECT_CALL(*this, OnEmptyResponse()); |
144 IssueAccessTokens(); | 160 IssueAccessTokens(); |
145 SendValidResponse(kEmptyResponse); | 161 SendValidResponse(kEmptyResponse); |
146 } | 162 } |
147 | 163 |
148 TEST_F(InterestsFetcherTest, SuccessfullResponse) { | 164 TEST_F(InterestsFetcherTest, SuccessfullResponse) { |
149 RequestInterests(); | 165 RequestInterests(); |
150 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse())); | 166 EXPECT_CALL(*this, OnSuccessfulResponse()); |
151 IssueAccessTokens(); | 167 IssueAccessTokens(); |
152 SendValidResponse(kSuccessfulResponse); | 168 SendValidResponse(kSuccessfulResponse); |
153 } | 169 } |
154 | 170 |
155 TEST_F(InterestsFetcherTest, FailedResponse) { | 171 TEST_F(InterestsFetcherTest, FailedResponse) { |
156 RequestInterests(); | 172 RequestInterests(); |
157 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 173 EXPECT_CALL(*this, OnFailedResponse()); |
158 IssueAccessTokens(); | 174 IssueAccessTokens(); |
159 SendFailedResponse(); | 175 SendFailedResponse(); |
160 } | 176 } |
161 | 177 |
162 TEST_F(InterestsFetcherTest, FailedOAuthRequest) { | 178 TEST_F(InterestsFetcherTest, FailedOAuthRequest) { |
163 RequestInterests(); | 179 RequestInterests(); |
164 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 180 EXPECT_CALL(*this, OnFailedResponse()); |
165 IssueAccessTokenErrors(); | 181 IssueAccessTokenErrors(); |
166 } | 182 } |
167 | 183 |
168 TEST_F(InterestsFetcherTest, RetryOnAuthorizationError) { | 184 TEST_F(InterestsFetcherTest, RetryOnAuthorizationError) { |
169 RequestInterests(); | 185 RequestInterests(); |
170 | 186 |
171 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())).Times(0); | 187 EXPECT_CALL(*this, OnEmptyResponse()).Times(0); |
172 IssueAccessTokens(); | 188 IssueAccessTokens(); |
173 SendAuthorizationError(); | 189 SendAuthorizationError(); |
174 | 190 |
175 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 191 EXPECT_CALL(*this, OnEmptyResponse()); |
176 IssueAccessTokens(); | 192 IssueAccessTokens(); |
177 SendValidResponse(kEmptyResponse); | 193 SendValidResponse(kEmptyResponse); |
178 } | 194 } |
179 | 195 |
180 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) { | 196 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) { |
181 RequestInterests(); | 197 RequestInterests(); |
182 | 198 |
183 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())).Times(0); | 199 EXPECT_CALL(*this, OnEmptyResponse()).Times(0); |
184 IssueAccessTokens(); | 200 IssueAccessTokens(); |
185 SendAuthorizationError(); | 201 SendAuthorizationError(); |
186 | 202 |
187 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 203 EXPECT_CALL(*this, OnFailedResponse()); |
188 IssueAccessTokens(); | 204 IssueAccessTokens(); |
189 SendAuthorizationError(); | 205 SendAuthorizationError(); |
190 } | 206 } |
OLD | NEW |