OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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_retriever.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/gmock/include/gmock/internal/gmock-generated-internal-utils.h> | |
22 #include <testing/gtest/include/gtest/gtest.h> | |
23 #include <testing/gtest/include/gtest/internal/gtest-internal.h> | |
Marc Treib
2015/09/03 09:04:37
Is this one necessary?
tache
2015/09/03 13:26:29
Done.
| |
24 #include <testing/gtest/include/gtest/internal/gtest-port.h> | |
25 | |
26 using testing::IsEmpty; | |
27 using testing::Not; | |
28 using testing::internal::Notification; | |
Marc Treib
2015/09/03 09:04:37
This seems unnecessary as well.
tache
2015/09/03 13:26:29
Done.
| |
29 | |
30 namespace { | |
31 | |
32 const int kInterestsRetrieverURLFetcherID = 0; | |
33 const std::string kEmptyResponse = "{\n" | |
Marc Treib
2015/09/03 09:04:37
We don't use global vars of non-trivial types (i.e
tache
2015/09/03 13:26:29
Done.
| |
34 "\"interest\": []\n" | |
35 "}\n"; | |
36 | |
37 const std::vector<InterestsRetriever::Interest> kExpectedEmptyResponse; | |
38 const std::vector<InterestsRetriever::Interest> kExpectedSuccessfullResponse { | |
Marc Treib
2015/09/03 09:04:37
"Successful", one "l" :)
Also, again: No uniform i
tache
2015/09/03 13:26:29
Done.
| |
39 {"Google", "https://fake.com/fake.png", 0.9}, | |
40 {"Google Chrome", "https://fake.com/fake.png", 0.98} | |
41 }; | |
42 | |
43 const std::string kSuccessfulResponse = "{\n" | |
44 " \"interest\": [\n" | |
45 " {\n" | |
46 " \"name\": \"Google\",\n" | |
47 " \"imageUrl\": \"https://fake.com/fake.png\",\n" | |
48 " \"relevance\": 0.9\n" | |
49 " },\n" | |
50 " {\n" | |
51 " \"name\": \"Google Chrome\",\n" | |
52 " \"imageUrl\": \"https://fake.com/fake.png\",\n" | |
53 " \"relevance\": 0.98\n" | |
54 " }\n" | |
55 " ]\n" | |
56 "}\n"; | |
57 | |
58 } // namespace | |
59 | |
60 class InterestsRetrieverTest : public testing::Test { | |
61 public: | |
62 InterestsRetrieverTest() | |
63 : request_context_(new net::TestURLRequestContextGetter( | |
64 base::ThreadTaskRunnerHandle::Get())), | |
65 url_fetcher_factory_(new net::TestURLFetcherFactory()){} | |
66 | |
67 | |
68 MOCK_METHOD1(OnReceivedInterests, | |
69 void(std::vector<InterestsRetriever::Interest>)); | |
70 | |
71 protected: | |
72 | |
Marc Treib
2015/09/03 09:04:37
nit: there's a bunch of extra empty lines in this
tache
2015/09/03 13:26:29
Done.
| |
73 | |
74 void GetInterests() { | |
75 requests_.push_back(scoped_ptr<InterestsRetriever>(new InterestsRetriever( | |
76 request_context_.get(), | |
77 "secret token", | |
78 base::Bind(&InterestsRetrieverTest::OnReceivedInterests, | |
79 base::Unretained(this)), | |
80 url_fetcher_factory_.get()))); | |
81 } | |
82 | |
83 net::TestURLFetcher* GetURLFetcher() { | |
84 net::TestURLFetcher* url_fetcher = url_fetcher_factory_->GetFetcherByID( | |
85 kInterestsRetrieverURLFetcherID); | |
86 EXPECT_TRUE(url_fetcher); | |
87 return url_fetcher; | |
88 } | |
89 | |
90 | |
91 void SendResponse(net::Error error, const std::string& response) { | |
92 net::TestURLFetcher* url_fetcher = GetURLFetcher(); | |
93 url_fetcher->set_status(net::URLRequestStatus::FromError(error)); | |
94 url_fetcher->set_response_code(net::HTTP_OK); | |
95 url_fetcher->SetResponseString(response); | |
96 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | |
97 } | |
98 | |
99 | |
100 void SendValidResponse(const std::string &response) { | |
101 SendResponse(net::OK, response); | |
102 } | |
103 | |
104 | |
105 void SendFailedResponse() { | |
106 SendResponse(net::ERR_ABORTED, std::string()); | |
107 } | |
108 | |
109 | |
110 base::MessageLoop message_loop_; | |
111 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | |
112 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; | |
113 std::vector<scoped_ptr<InterestsRetriever>> requests_; | |
Marc Treib
2015/09/03 09:04:37
You can't put scoped_ptrs into a vector, because (
tache
2015/09/03 13:26:29
Switched to using a single scoped_ptr.
| |
114 }; | |
115 | |
116 TEST_F(InterestsRetrieverTest, EmptyResponse) { | |
117 GetInterests(); | |
118 EXPECT_CALL(*this, OnReceivedInterests(kExpectedEmptyResponse)); | |
119 SendValidResponse(kEmptyResponse); | |
120 } | |
121 | |
122 TEST_F(InterestsRetrieverTest, SuccessfullResponse) { | |
123 GetInterests(); | |
124 EXPECT_CALL(*this, OnReceivedInterests(kExpectedSuccessfullResponse)); | |
125 SendValidResponse(kSuccessfulResponse); | |
126 } | |
127 | |
128 TEST_F(InterestsRetrieverTest, FailedResponse) { | |
129 GetInterests(); | |
130 EXPECT_CALL(*this, OnReceivedInterests(kExpectedEmptyResponse)); | |
131 SendFailedResponse(); | |
132 } | |
133 | |
134 TEST_F(InterestsRetrieverTest, DefaultFakeResponse) { | |
135 GetInterests(); | |
136 EXPECT_CALL(*this, OnReceivedInterests(Not(IsEmpty()))); | |
137 SendValidResponse(InterestsRetriever::FakeResponse()); | |
138 } | |
OLD | NEW |