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

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

Issue 1317513004: Add InterestsFetcher which retrieves a user's interests from the server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Get the access_token in the InterestsFetcher 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/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
13 #include "net/base/net_errors.h"
14 #include "net/http/http_status_code.h"
15 #include "net/url_request/test_url_fetcher_factory.h"
16 #include "net/url_request/url_request_status.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gmock/include/gmock/gmock-generated-function-mockers.h"
19 #include "testing/gmock/include/gmock/gmock-matchers.h"
20 #include "testing/gmock/include/gmock/gmock-more-matchers.h"
21 #include "testing/gmock/include/gmock/gmock-spec-builders.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 using testing::IsEmpty;
25 using testing::Not;
26
27 namespace {
28
29 const int kInterestsFetcherURLFetcherID = 0;
30 const char kEmptyResponse[] = "{\n"
31 "\"interests\": []\n"
32 "}\n";
33
34 const char kSuccessfulResponse[] = "{\n"
35 " \"interests\": [\n"
36 " {\n"
37 " \"name\": \"Google\",\n"
38 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
39 " \"relevance\": 0.9\n"
40 " },\n"
41 " {\n"
42 " \"name\": \"Google Chrome\",\n"
43 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
44 " \"relevance\": 0.98\n"
45 " }\n"
46 " ]\n"
47 "}\n";
48
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", "https://fake.com/fake.png", 0.9});
59 res.push_back(InterestsFetcher::Interest{
60 "Google Chrome", "https://fake.com/fake.png", 0.98});
61 return res;
62 }
63
64 } // namespace
65
66 class InterestsFetcherTest : public testing::Test {
67 public:
68 InterestsFetcherTest()
69 : request_context_(new net::TestURLRequestContextGetter(
70 base::ThreadTaskRunnerHandle::Get())),
71 url_fetcher_factory_(new net::TestURLFetcherFactory()) {
72 token_service_.UpdateCredentials(kAccountId, "refresh_token");
73 }
74
75 MOCK_METHOD1(OnReceivedInterests,
76 void(const std::vector<InterestsFetcher::Interest>&));
77
78 protected:
79 void RequestInterests() {
80 request_.reset(new InterestsFetcher(
81 &token_service_,
82 kAccountId,
83 request_context_.get()));
84
85
86 request_->Start(
87 base::Bind(&InterestsFetcherTest::OnReceivedInterests,
88 base::Unretained(this)));
89 }
90
91 net::TestURLFetcher* GetURLFetcher() {
92 net::TestURLFetcher* url_fetcher = url_fetcher_factory_->GetFetcherByID(
93 kInterestsFetcherURLFetcherID);
94 EXPECT_TRUE(url_fetcher);
95 return url_fetcher;
96 }
97
98 void IssueAccessTokenErrors() {
99 token_service_.IssueErrorForAllPendingRequestsForAccount(
100 kAccountId,
101 GoogleServiceAuthError::FromServiceError("Error!"));
102 }
103
104 void IssueAccessTokens() {
105 token_service_.IssueAllTokensForAccount(
106 kAccountId,
107 "access_token",
108 base::Time::Now() + base::TimeDelta::FromHours(1));
109 }
110
111 void SendResponse(net::Error error, const std::string& response) {
112 net::TestURLFetcher* url_fetcher = GetURLFetcher();
113 url_fetcher->set_status(net::URLRequestStatus::FromError(error));
114 url_fetcher->set_response_code(net::HTTP_OK);
115 url_fetcher->SetResponseString(response);
116 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
117 }
118
119
120 void SendValidResponse(const std::string& response) {
121 SendResponse(net::OK, response);
122 }
123
124
125 void SendFailedResponse() {
126 SendResponse(net::ERR_ABORTED, std::string());
127 }
128
129 base::MessageLoop message_loop_;
130 FakeProfileOAuth2TokenService token_service_;
131 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
132 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
133 scoped_ptr<InterestsFetcher> request_;
134
135 };
136
137 TEST_F(InterestsFetcherTest, EmptyResponse) {
138 RequestInterests();
139 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
140 IssueAccessTokens();
141 SendValidResponse(kEmptyResponse);
142 }
143
144 TEST_F(InterestsFetcherTest, SuccessfullResponse) {
145 RequestInterests();
146 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse()));
147 IssueAccessTokens();
148 SendValidResponse(kSuccessfulResponse);
149 }
150
151 TEST_F(InterestsFetcherTest, FailedResponse) {
152 RequestInterests();
153 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
154 IssueAccessTokens();
155 SendFailedResponse();
156 }
157
158 TEST_F(InterestsFetcherTest, FailedOAuthRequest) {
159 RequestInterests();
160 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
161 IssueAccessTokenErrors();
162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698