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

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

Issue 2122993003: Remove unused Interests code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Forgot unit test target. Created 4 years, 5 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/threading/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_METHOD0(OnSuccessfulResponse, void());
82 MOCK_METHOD0(OnEmptyResponse, void());
83 MOCK_METHOD0(OnFailedResponse, void());
84
85 void OnReceivedInterests(
86 std::unique_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 }
99
100 protected:
101 void RequestInterests() {
102 request_.reset(new InterestsFetcher(&token_service_,
103 kAccountId,
104 request_context_.get()));
105
106 request_->FetchInterests(base::Bind(
107 &InterestsFetcherTest::OnReceivedInterests, base::Unretained(this)));
108 }
109
110 net::TestURLFetcher* GetURLFetcher() {
111 net::TestURLFetcher* url_fetcher =
112 url_fetcher_factory_->GetFetcherByID(kInterestsFetcherURLFetcherID);
113 EXPECT_TRUE(url_fetcher);
114 return url_fetcher;
115 }
116
117 void IssueAccessTokenErrors() {
118 token_service_.IssueErrorForAllPendingRequestsForAccount(
119 kAccountId, GoogleServiceAuthError::FromServiceError("Error!"));
120 }
121
122 void IssueAccessTokens() {
123 token_service_.IssueAllTokensForAccount(
124 kAccountId, "access_token",
125 base::Time::Now() + base::TimeDelta::FromHours(1));
126 }
127
128 void SendResponse(net::Error error,
129 int response_code,
130 const std::string& response) {
131 net::TestURLFetcher* url_fetcher = GetURLFetcher();
132 url_fetcher->set_status(net::URLRequestStatus::FromError(error));
133 url_fetcher->set_response_code(response_code);
134 url_fetcher->SetResponseString(response);
135 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
136 }
137
138 void SendValidResponse(const std::string& response) {
139 SendResponse(net::OK, net::HTTP_OK, response);
140 }
141
142 void SendFailedResponse() {
143 SendResponse(net::ERR_ABORTED, 0, std::string());
144 }
145
146 void SendAuthorizationError() {
147 SendResponse(net::OK, net::HTTP_UNAUTHORIZED, std::string());
148 }
149
150 base::MessageLoop message_loop_;
151 FakeProfileOAuth2TokenService token_service_;
152 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
153 std::unique_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
154 std::unique_ptr<InterestsFetcher> request_;
155 };
156
157 TEST_F(InterestsFetcherTest, EmptyResponse) {
158 RequestInterests();
159 EXPECT_CALL(*this, OnEmptyResponse());
160 IssueAccessTokens();
161 SendValidResponse(kEmptyResponse);
162 }
163
164 TEST_F(InterestsFetcherTest, SuccessfullResponse) {
165 RequestInterests();
166 EXPECT_CALL(*this, OnSuccessfulResponse());
167 IssueAccessTokens();
168 SendValidResponse(kSuccessfulResponse);
169 }
170
171 TEST_F(InterestsFetcherTest, FailedResponse) {
172 RequestInterests();
173 EXPECT_CALL(*this, OnFailedResponse());
174 IssueAccessTokens();
175 SendFailedResponse();
176 }
177
178 TEST_F(InterestsFetcherTest, FailedOAuthRequest) {
179 RequestInterests();
180 EXPECT_CALL(*this, OnFailedResponse());
181 IssueAccessTokenErrors();
182 }
183
184 TEST_F(InterestsFetcherTest, RetryOnAuthorizationError) {
185 RequestInterests();
186
187 EXPECT_CALL(*this, OnEmptyResponse()).Times(0);
188 IssueAccessTokens();
189 SendAuthorizationError();
190
191 EXPECT_CALL(*this, OnEmptyResponse());
192 IssueAccessTokens();
193 SendValidResponse(kEmptyResponse);
194 }
195
196 TEST_F(InterestsFetcherTest, RetryOnlyOnceOnAuthorizationError) {
197 RequestInterests();
198
199 EXPECT_CALL(*this, OnEmptyResponse()).Times(0);
200 IssueAccessTokens();
201 SendAuthorizationError();
202
203 EXPECT_CALL(*this, OnFailedResponse());
204 IssueAccessTokens();
205 SendAuthorizationError();
206 }
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