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

Unified Diff: components/doodle/doodle_fetcher_unittest.cc

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Created 3 years, 11 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
« no previous file with comments | « components/doodle/doodle_fetcher.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/doodle/doodle_fetcher_unittest.cc
diff --git a/components/doodle/doodle_fetcher_unittest.cc b/components/doodle/doodle_fetcher_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6ac8e55fcaf0f1ef7da094f737575320522ca1df
--- /dev/null
+++ b/components/doodle/doodle_fetcher_unittest.cc
@@ -0,0 +1,124 @@
+// Copyright 2016 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 "components/doodle/doodle_fetcher.h"
+
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "base/bind.h"
+#include "base/files/file_util.h"
+#include "base/files/important_file_writer.h"
+#include "base/files/scoped_temp_dir.h"
+#include "base/json/json_reader.h"
+#include "base/json/json_writer.h"
+#include "base/memory/ptr_util.h"
+#include "base/run_loop.h"
+#include "base/test/sequenced_worker_pool_owner.h"
+#include "base/test/test_simple_task_runner.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "base/values.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.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::Eq;
+using testing::IsEmpty;
+
+namespace {
+
+void ParseJson(
+ const std::string& json,
+ const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback,
+ const base::Callback<void(const std::string&)>& error_callback) {
+ base::JSONReader json_reader;
+ std::unique_ptr<base::Value> value = json_reader.ReadToValue(json);
+ if (value) {
+ success_callback.Run(std::move(value));
+ } else {
+ error_callback.Run(json_reader.GetErrorMessage());
+ }
+}
+
+} // namespace
+
+class DoodleFetcherTest : public ::testing::Test {
+ protected:
+ DoodleFetcherTest()
+ : url_("https://www.google.com/async/ddljson?ogdeb=ct~2000000007"),
+ url_fetcher_factory_(nullptr),
+ mock_task_runner_(new base::TestSimpleTaskRunner()),
+ mock_task_runner_handle_(mock_task_runner_),
+ context_getter(
+ new net::TestURLRequestContextGetter(mock_task_runner_.get())) {}
+
+ void RespondWithJSON() {
+ base::DictionaryValue config;
+ config.SetString("search_url", "google.de");
+ config.SetString("fullpage_interactive_url", "google.de");
+ std::unique_ptr<base::DictionaryValue> image =
+ base::MakeUnique<base::DictionaryValue>();
+ image->SetString("url", "google.de/doodle");
+ image->SetInteger("height", 300);
+ image->SetInteger("width", 1200);
+ image->SetString("background_color", "#00FFEE");
+ config.Set("large_image", std::move(image));
+ std::string json_string;
+ base::JSONWriter::Write(config, &json_string);
+ url_fetcher_factory_.SetFakeResponse(GURL(url_), json_string, net::HTTP_OK,
+ net::URLRequestStatus::SUCCESS);
+ }
+
+ void RespondWithData(const std::string& data) {
+ url_fetcher_factory_.SetFakeResponse(GURL(url_), data, net::HTTP_OK,
+ net::URLRequestStatus::SUCCESS);
+ }
+
+ void RespondUnsuccessfully() {
+ url_fetcher_factory_.SetFakeResponse(GURL(url_), "", net::HTTP_NOT_FOUND,
+ net::URLRequestStatus::FAILED);
+ }
+
+ DoodleConfig FetchDoodle() {
+ DoodleFetcher fetcher(context_getter.get(), base::Bind(ParseJson));
+
+ DoodleConfig doodle;
+ fetcher.FetchDoodle(
+ base::Bind([](DoodleConfig* doodle,
+ DoodleConfig config) { *doodle = std::move(config); },
+ &doodle));
+ mock_task_runner_->RunUntilIdle();
+ return doodle;
+ }
+
+ std::string url_;
+ net::FakeURLFetcherFactory url_fetcher_factory_;
+ scoped_refptr<base::TestSimpleTaskRunner> mock_task_runner_;
+ base::ThreadTaskRunnerHandle mock_task_runner_handle_;
+ scoped_refptr<net::TestURLRequestContextGetter> context_getter;
+};
+
+TEST_F(DoodleFetcherTest, ReturnsFromFetchWithoutError) {
+ RespondWithJSON();
+
+ EXPECT_FALSE(FetchDoodle().error_during_fetch);
+}
+
+TEST_F(DoodleFetcherTest, ReturnsFrom404FetchWithError) {
+ RespondUnsuccessfully();
+
+ EXPECT_TRUE(FetchDoodle().error_during_fetch);
+}
+
+TEST_F(DoodleFetcherTest, ResponseContainsValidImageUrl) {
+ RespondWithJSON();
+
+ DoodleConfig response = FetchDoodle();
+
+ EXPECT_TRUE(GURL(response.large_image.url).is_valid());
+}
« no previous file with comments | « components/doodle/doodle_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698