Chromium Code Reviews| 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()); | |
| 83 | 84 |
| 85 void OnReceivedInterests( | |
| 86 scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { | |
| 87 | |
| 88 if (interests) { | |
|
Bernhard Bauer
2015/10/08 09:04:41
Early-return if (!interests)?
PEConn
2015/10/08 09:40:56
Done.
| |
| 89 if (*interests == GetExpectedEmptyResponse()) | |
| 90 OnEmptyResponse(); | |
| 91 else if (*interests == GetExpectedSuccessfulResponse()) | |
| 92 OnSuccessfulResponse(); | |
|
Bernhard Bauer
2015/10/08 09:04:41
Add an additional else that fails the test, in cas
PEConn
2015/10/08 09:40:56
Done.
| |
| 93 } else { | |
| 94 OnFailedResponse(); | |
| 95 } | |
| 96 } | |
| 84 protected: | 97 protected: |
| 85 void RequestInterests() { | 98 void RequestInterests() { |
| 86 request_.reset(new InterestsFetcher(&token_service_, | 99 request_.reset(new InterestsFetcher(&token_service_, |
| 87 kAccountId, | 100 kAccountId, |
| 88 request_context_.get())); | 101 request_context_.get())); |
| 89 | 102 |
| 90 request_->FetchInterests(base::Bind( | 103 request_->FetchInterests(base::Bind( |
| 91 &InterestsFetcherTest::OnReceivedInterests, base::Unretained(this))); | 104 &InterestsFetcherTest::OnReceivedInterests, base::Unretained(this))); |
| 92 } | 105 } |
| 93 | 106 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 | 146 |
| 134 base::MessageLoop message_loop_; | 147 base::MessageLoop message_loop_; |
| 135 FakeProfileOAuth2TokenService token_service_; | 148 FakeProfileOAuth2TokenService token_service_; |
| 136 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 149 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 137 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; | 150 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; |
| 138 scoped_ptr<InterestsFetcher> request_; | 151 scoped_ptr<InterestsFetcher> request_; |
| 139 }; | 152 }; |
| 140 | 153 |
| 141 TEST_F(InterestsFetcherTest, EmptyResponse) { | 154 TEST_F(InterestsFetcherTest, EmptyResponse) { |
| 142 RequestInterests(); | 155 RequestInterests(); |
| 143 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 156 EXPECT_CALL(*this, OnEmptyResponse()); |
| 144 IssueAccessTokens(); | 157 IssueAccessTokens(); |
| 145 SendValidResponse(kEmptyResponse); | 158 SendValidResponse(kEmptyResponse); |
| 146 } | 159 } |
| 147 | 160 |
| 148 TEST_F(InterestsFetcherTest, SuccessfullResponse) { | 161 TEST_F(InterestsFetcherTest, SuccessfullResponse) { |
| 149 RequestInterests(); | 162 RequestInterests(); |
| 150 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse())); | 163 EXPECT_CALL(*this, OnSuccessfulResponse()); |
| 151 IssueAccessTokens(); | 164 IssueAccessTokens(); |
| 152 SendValidResponse(kSuccessfulResponse); | 165 SendValidResponse(kSuccessfulResponse); |
| 153 } | 166 } |
| 154 | 167 |
| 155 TEST_F(InterestsFetcherTest, FailedResponse) { | 168 TEST_F(InterestsFetcherTest, FailedResponse) { |
| 156 RequestInterests(); | 169 RequestInterests(); |
| 157 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 170 EXPECT_CALL(*this, OnFailedResponse()); |
| 158 IssueAccessTokens(); | 171 IssueAccessTokens(); |
| 159 SendFailedResponse(); | 172 SendFailedResponse(); |
| 160 } | 173 } |
| 161 | 174 |
| 162 TEST_F(InterestsFetcherTest, FailedOAuthRequest) { | 175 TEST_F(InterestsFetcherTest, FailedOAuthRequest) { |
| 163 RequestInterests(); | 176 RequestInterests(); |
| 164 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 177 EXPECT_CALL(*this, OnFailedResponse()); |
| 165 IssueAccessTokenErrors(); | 178 IssueAccessTokenErrors(); |
| 166 } | 179 } |
| 167 | 180 |
| 168 TEST_F(InterestsFetcherTest, RetryOnAuthorizationError) { | 181 TEST_F(InterestsFetcherTest, RetryOnAuthorizationError) { |
| 169 RequestInterests(); | 182 RequestInterests(); |
| 170 | 183 |
| 171 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())).Times(0); | 184 EXPECT_CALL(*this, OnEmptyResponse()).Times(0); |
| 172 IssueAccessTokens(); | 185 IssueAccessTokens(); |
| 173 SendAuthorizationError(); | 186 SendAuthorizationError(); |
| 174 | 187 |
| 175 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 188 EXPECT_CALL(*this, OnEmptyResponse()); |
| 176 IssueAccessTokens(); | 189 IssueAccessTokens(); |
| 177 SendValidResponse(kEmptyResponse); | 190 SendValidResponse(kEmptyResponse); |
| 178 } | 191 } |
| 179 | 192 |
| 180 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) { | 193 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) { |
| 181 RequestInterests(); | 194 RequestInterests(); |
| 182 | 195 |
| 183 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())).Times(0); | 196 EXPECT_CALL(*this, OnEmptyResponse()).Times(0); |
| 184 IssueAccessTokens(); | 197 IssueAccessTokens(); |
| 185 SendAuthorizationError(); | 198 SendAuthorizationError(); |
| 186 | 199 |
| 187 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); | 200 EXPECT_CALL(*this, OnFailedResponse()); |
| 188 IssueAccessTokens(); | 201 IssueAccessTokens(); |
| 189 SendAuthorizationError(); | 202 SendAuthorizationError(); |
| 190 } | 203 } |
| OLD | NEW |