| Index: chrome/browser/interests/interests_retriever_unittest.cc
|
| diff --git a/chrome/browser/interests/interests_retriever_unittest.cc b/chrome/browser/interests/interests_retriever_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..576258e04baba6320bc26cf0b521f719fda179a4
|
| --- /dev/null
|
| +++ b/chrome/browser/interests/interests_retriever_unittest.cc
|
| @@ -0,0 +1,138 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/interests/interests_retriever.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/thread_task_runner_handle.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/http/http_status_code.h"
|
| +#include "net/url_request/test_url_fetcher_factory.h"
|
| +#include "net/url_request/url_request_status.h"
|
| +#include "net/url_request/url_request_test_util.h"
|
| +#include "testing/gmock/include/gmock/gmock-generated-function-mockers.h"
|
| +#include "testing/gmock/include/gmock/gmock-matchers.h"
|
| +#include "testing/gmock/include/gmock/gmock-more-matchers.h"
|
| +#include "testing/gmock/include/gmock/gmock-spec-builders.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using testing::IsEmpty;
|
| +using testing::Not;
|
| +
|
| +namespace {
|
| +
|
| +const int kInterestsRetrieverURLFetcherID = 0;
|
| +const char kEmptyResponse[] = "{\n"
|
| + "\"interest\": []\n"
|
| + "}\n";
|
| +
|
| +const char kSuccessfulResponse[] = "{\n"
|
| + " \"interest\": [\n"
|
| + " {\n"
|
| + " \"name\": \"Google\",\n"
|
| + " \"imageUrl\": \"https://fake.com/fake.png\",\n"
|
| + " \"relevance\": 0.9\n"
|
| + " },\n"
|
| + " {\n"
|
| + " \"name\": \"Google Chrome\",\n"
|
| + " \"imageUrl\": \"https://fake.com/fake.png\",\n"
|
| + " \"relevance\": 0.98\n"
|
| + " }\n"
|
| + " ]\n"
|
| + "}\n";
|
| +
|
| +std::vector<InterestsRetriever::Interest> GetExpectedEmptyResponse() {
|
| + return std::vector<InterestsRetriever::Interest>();
|
| +}
|
| +
|
| +std::vector<InterestsRetriever::Interest> GetExpectedSuccessfulResponse() {
|
| + std::vector<InterestsRetriever::Interest> res;
|
| + res.push_back(
|
| + InterestsRetriever::Interest{"Google", "https://fake.com/fake.png", 0.9});
|
| + res.push_back(InterestsRetriever::Interest{
|
| + "Google Chrome", "https://fake.com/fake.png", 0.98});
|
| + return res;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class InterestsRetrieverTest : public testing::Test {
|
| + public:
|
| + InterestsRetrieverTest()
|
| + : request_context_(new net::TestURLRequestContextGetter(
|
| + base::ThreadTaskRunnerHandle::Get())),
|
| + url_fetcher_factory_(new net::TestURLFetcherFactory()) {}
|
| +
|
| + MOCK_METHOD1(OnReceivedInterests,
|
| + void(const std::vector<InterestsRetriever::Interest>&));
|
| +
|
| + protected:
|
| + void RequestInterests() {
|
| + request_.reset(new InterestsRetriever(
|
| + request_context_.get(),
|
| + "secret token",
|
| + base::Bind(&InterestsRetrieverTest::OnReceivedInterests,
|
| + base::Unretained(this)),
|
| + url_fetcher_factory_.get()));
|
| + }
|
| +
|
| + net::TestURLFetcher* GetURLFetcher() {
|
| + net::TestURLFetcher* url_fetcher = url_fetcher_factory_->GetFetcherByID(
|
| + kInterestsRetrieverURLFetcherID);
|
| + EXPECT_TRUE(url_fetcher);
|
| + return url_fetcher;
|
| + }
|
| +
|
| +
|
| + void SendResponse(net::Error error, const std::string& response) {
|
| + net::TestURLFetcher* url_fetcher = GetURLFetcher();
|
| + url_fetcher->set_status(net::URLRequestStatus::FromError(error));
|
| + url_fetcher->set_response_code(net::HTTP_OK);
|
| + url_fetcher->SetResponseString(response);
|
| + url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
|
| + }
|
| +
|
| +
|
| + void SendValidResponse(const std::string& response) {
|
| + SendResponse(net::OK, response);
|
| + }
|
| +
|
| +
|
| + void SendFailedResponse() {
|
| + SendResponse(net::ERR_ABORTED, std::string());
|
| + }
|
| +
|
| +
|
| + base::MessageLoop message_loop_;
|
| + scoped_refptr<net::TestURLRequestContextGetter> request_context_;
|
| + scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
|
| + scoped_ptr<InterestsRetriever> request_;
|
| +};
|
| +
|
| +TEST_F(InterestsRetrieverTest, EmptyResponse) {
|
| + RequestInterests();
|
| + EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
|
| + SendValidResponse(kEmptyResponse);
|
| +}
|
| +
|
| +TEST_F(InterestsRetrieverTest, SuccessfullResponse) {
|
| + RequestInterests();
|
| + EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse()));
|
| + SendValidResponse(kSuccessfulResponse);
|
| +}
|
| +
|
| +TEST_F(InterestsRetrieverTest, FailedResponse) {
|
| + RequestInterests();
|
| + EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
|
| + SendFailedResponse();
|
| +}
|
| +
|
| +TEST_F(InterestsRetrieverTest, DefaultFakeResponse) {
|
| + RequestInterests();
|
| + EXPECT_CALL(*this, OnReceivedInterests(Not(IsEmpty())));
|
| + SendValidResponse(InterestsRetriever::GetFakeResponseForTest());
|
| +}
|
|
|