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

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: Removed the use of FakeURLFetcher Created 5 years, 3 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 "net/base/net_errors.h"
13 #include "net/http/http_status_code.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_request_status.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gmock/include/gmock/gmock-generated-function-mockers.h"
18 #include "testing/gmock/include/gmock/gmock-matchers.h"
19 #include "testing/gmock/include/gmock/gmock-more-matchers.h"
20 #include "testing/gmock/include/gmock/gmock-spec-builders.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 using testing::IsEmpty;
24 using testing::Not;
25
26 namespace {
27
28 const int kInterestsFetcherURLFetcherID = 0;
29 const char kEmptyResponse[] = "{\n"
30 "\"interest\": []\n"
31 "}\n";
32
33 const char kSuccessfulResponse[] = "{\n"
34 " \"interest\": [\n"
35 " {\n"
36 " \"name\": \"Google\",\n"
37 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
38 " \"relevance\": 0.9\n"
39 " },\n"
40 " {\n"
41 " \"name\": \"Google Chrome\",\n"
42 " \"imageUrl\": \"https://fake.com/fake.png\",\n"
43 " \"relevance\": 0.98\n"
44 " }\n"
45 " ]\n"
46 "}\n";
47
48 std::vector<InterestsFetcher::Interest> GetExpectedEmptyResponse() {
49 return std::vector<InterestsFetcher::Interest>();
50 }
51
52 std::vector<InterestsFetcher::Interest> GetExpectedSuccessfulResponse() {
53 std::vector<InterestsFetcher::Interest> res;
54 res.push_back(
55 InterestsFetcher::Interest{"Google", "https://fake.com/fake.png", 0.9});
56 res.push_back(InterestsFetcher::Interest{
57 "Google Chrome", "https://fake.com/fake.png", 0.98});
58 return res;
59 }
60
61 } // namespace
62
63 class InterestsFetcherTest : public testing::Test {
64 public:
65 InterestsFetcherTest()
66 : request_context_(new net::TestURLRequestContextGetter(
67 base::ThreadTaskRunnerHandle::Get())),
68 url_fetcher_factory_(new net::TestURLFetcherFactory()) {}
69
70 MOCK_METHOD1(OnReceivedInterests,
71 void(const std::vector<InterestsFetcher::Interest>&));
72
73 protected:
74 void RequestInterests() {
75 request_.reset(new InterestsFetcher(
76 request_context_.get(),
77 "secret token"));
78
79 request_->Start(
80 base::Bind(&InterestsFetcherTest::OnReceivedInterests,
81 base::Unretained(this)));
82 }
83
84 net::TestURLFetcher* GetURLFetcher() {
85 net::TestURLFetcher* url_fetcher = url_fetcher_factory_->GetFetcherByID(
86 kInterestsFetcherURLFetcherID);
87 EXPECT_TRUE(url_fetcher);
88 return url_fetcher;
89 }
90
91
92 void SendResponse(net::Error error, const std::string& response) {
93 net::TestURLFetcher* url_fetcher = GetURLFetcher();
94 url_fetcher->set_status(net::URLRequestStatus::FromError(error));
95 url_fetcher->set_response_code(net::HTTP_OK);
96 url_fetcher->SetResponseString(response);
97 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
98 }
99
100
101 void SendValidResponse(const std::string& response) {
102 SendResponse(net::OK, response);
103 }
104
105
106 void SendFailedResponse() {
107 SendResponse(net::ERR_ABORTED, std::string());
108 }
109
110 base::MessageLoop message_loop_;
111 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
112 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
113 scoped_ptr<InterestsFetcher> request_;
114 };
115
116 TEST_F(InterestsFetcherTest, EmptyResponse) {
117 RequestInterests();
118 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
119 SendValidResponse(kEmptyResponse);
120 }
121
122 TEST_F(InterestsFetcherTest, SuccessfullResponse) {
123 RequestInterests();
124 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse()));
125 SendValidResponse(kSuccessfulResponse);
126 }
127
128 TEST_F(InterestsFetcherTest, FailedResponse) {
129 RequestInterests();
130 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
131 SendFailedResponse();
132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698