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

Unified Diff: chrome/browser/interests/interests_retriever_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: 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_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..61a02ae109af537f28574d8fc5ac6f41df9f40aa
--- /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> kExpectedEmptyResponse() {
Marc Treib 2015/09/03 13:54:55 s/k/Get/g, also below
tache 2015/09/03 15:21:52 Done.
+ return std::vector<InterestsRetriever::Interest>();
+}
+
+std::vector<InterestsRetriever::Interest> kExpectedSuccessfulResponse() {
+ 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(std::vector<InterestsRetriever::Interest>));
+
+ protected:
+ void GetInterests() {
Marc Treib 2015/09/03 13:54:55 RequestInterests? "Get" sounds like it would retur
tache 2015/09/03 15:21:53 Done.
+ request_ = scoped_ptr<InterestsRetriever>(new InterestsRetriever(
Marc Treib 2015/09/03 13:54:55 request_.reset(new ... is shorter :)
tache 2015/09/03 15:21:53 thx :D
+ 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) {
+ GetInterests();
+ EXPECT_CALL(*this, OnReceivedInterests(kExpectedEmptyResponse()));
+ SendValidResponse(kEmptyResponse);
+}
+
+TEST_F(InterestsRetrieverTest, SuccessfullResponse) {
+ GetInterests();
+ EXPECT_CALL(*this, OnReceivedInterests(kExpectedSuccessfulResponse()));
+ SendValidResponse(kSuccessfulResponse);
+}
+
+TEST_F(InterestsRetrieverTest, FailedResponse) {
+ GetInterests();
+ EXPECT_CALL(*this, OnReceivedInterests(kExpectedEmptyResponse()));
+ SendFailedResponse();
+}
+
+TEST_F(InterestsRetrieverTest, DefaultFakeResponse) {
+ GetInterests();
+ EXPECT_CALL(*this, OnReceivedInterests(Not(IsEmpty())));
+ SendValidResponse(InterestsRetriever::GetFakeResponseForTest());
+}

Powered by Google App Engine
This is Rietveld 408576698