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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/interests/interests_fetcher_unittest.cc
diff --git a/chrome/browser/interests/interests_fetcher_unittest.cc b/chrome/browser/interests/interests_fetcher_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..55c23b6bc64254c2112d30a4b9928b751e7ec85e
--- /dev/null
+++ b/chrome/browser/interests/interests_fetcher_unittest.cc
@@ -0,0 +1,132 @@
+// 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_fetcher.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 kInterestsFetcherURLFetcherID = 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<InterestsFetcher::Interest> GetExpectedEmptyResponse() {
+ return std::vector<InterestsFetcher::Interest>();
+}
+
+std::vector<InterestsFetcher::Interest> GetExpectedSuccessfulResponse() {
+ std::vector<InterestsFetcher::Interest> res;
+ res.push_back(
+ InterestsFetcher::Interest{"Google", "https://fake.com/fake.png", 0.9});
+ res.push_back(InterestsFetcher::Interest{
+ "Google Chrome", "https://fake.com/fake.png", 0.98});
+ return res;
+}
+
+} // namespace
+
+class InterestsFetcherTest : public testing::Test {
+ public:
+ InterestsFetcherTest()
+ : request_context_(new net::TestURLRequestContextGetter(
+ base::ThreadTaskRunnerHandle::Get())),
+ url_fetcher_factory_(new net::TestURLFetcherFactory()) {}
+
+ MOCK_METHOD1(OnReceivedInterests,
+ void(const std::vector<InterestsFetcher::Interest>&));
+
+ protected:
+ void RequestInterests() {
+ request_.reset(new InterestsFetcher(
+ request_context_.get(),
+ "secret token"));
+
+ request_->Start(
+ base::Bind(&InterestsFetcherTest::OnReceivedInterests,
+ base::Unretained(this)));
+ }
+
+ net::TestURLFetcher* GetURLFetcher() {
+ net::TestURLFetcher* url_fetcher = url_fetcher_factory_->GetFetcherByID(
+ kInterestsFetcherURLFetcherID);
+ 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<InterestsFetcher> request_;
+};
+
+TEST_F(InterestsFetcherTest, EmptyResponse) {
+ RequestInterests();
+ EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
+ SendValidResponse(kEmptyResponse);
+}
+
+TEST_F(InterestsFetcherTest, SuccessfullResponse) {
+ RequestInterests();
+ EXPECT_CALL(*this, OnReceivedInterests(GetExpectedSuccessfulResponse()));
+ SendValidResponse(kSuccessfulResponse);
+}
+
+TEST_F(InterestsFetcherTest, FailedResponse) {
+ RequestInterests();
+ EXPECT_CALL(*this, OnReceivedInterests(GetExpectedEmptyResponse()));
+ SendFailedResponse();
+}

Powered by Google App Engine
This is Rietveld 408576698