OLD | NEW |
(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_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/gtest/include/gtest/gtest.h" |
| 22 |
| 23 using testing::IsEmpty; |
| 24 using testing::Not; |
| 25 |
| 26 namespace { |
| 27 |
| 28 const int kInterestsRetrieverURLFetcherID = 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<InterestsRetriever::Interest> GetExpectedEmptyResponse() { |
| 49 return std::vector<InterestsRetriever::Interest>(); |
| 50 } |
| 51 |
| 52 std::vector<InterestsRetriever::Interest> GetExpectedSuccessfulResponse() { |
| 53 std::vector<InterestsRetriever::Interest> res; |
| 54 res.push_back( |
| 55 InterestsRetriever::Interest{"Google", "https://fake.com/fake.png", 0.9}); |
| 56 res.push_back(InterestsRetriever::Interest{ |
| 57 "Google Chrome", "https://fake.com/fake.png", 0.98}); |
| 58 return res; |
| 59 } |
| 60 |
| 61 } // namespace |
| 62 |
| 63 class InterestsRetrieverTest : public testing::Test { |
| 64 public: |
| 65 InterestsRetrieverTest() |
| 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<InterestsRetriever::Interest>&)); |
| 72 |
| 73 protected: |
| 74 void RequestInterests() { |
| 75 request_.reset(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 scoped_ptr<InterestsRetriever> request_; |
| 114 }; |
| 115 |
| 116 TEST_F(InterestsRetrieverTest, EmptyResponse) { |
| 117 RequestInterests(); |
| 118 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); |
| 119 SendValidResponse(kEmptyResponse); |
| 120 } |
| 121 |
| 122 TEST_F(InterestsRetrieverTest, SuccessfullResponse) { |
| 123 RequestInterests(); |
| 124 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse())); |
| 125 SendValidResponse(kSuccessfulResponse); |
| 126 } |
| 127 |
| 128 TEST_F(InterestsRetrieverTest, FailedResponse) { |
| 129 RequestInterests(); |
| 130 EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse())); |
| 131 SendFailedResponse(); |
| 132 } |
| 133 |
| 134 TEST_F(InterestsRetrieverTest, DefaultFakeResponse) { |
| 135 RequestInterests(); |
| 136 EXPECT_CALL(*this, OnReceivedInterests(Not(IsEmpty()))); |
| 137 SendValidResponse(InterestsRetriever::GetFakeResponseForTest()); |
| 138 } |
OLD | NEW |