| 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..17b4adb3847a228cb286198aab3215bc25cf59f5
|
| --- /dev/null
|
| +++ b/components/doodle/doodle_fetcher_unittest.cc
|
| @@ -0,0 +1,232 @@
|
| +// 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/sequenced_task_runner_handle.h"
|
| +#include "base/threading/thread_task_runner_handle.h"
|
| +#include "base/values.h"
|
| +#include "components/safe_json/testing_json_parser.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 doodle {
|
| +
|
| +namespace {
|
| +
|
| +const char* kSampleResponse =
|
| + ")]}'{"
|
| + "\"ddljson\": {"
|
| + "\"alt_text\":\"Mouseover Text\","
|
| + "\"doodle_type\":\"SIMPLE\","
|
| + "\"gallery_url\":\"//www.google.com/doodles/burning-man-festival\","
|
| + "\"header_layout\":\"LEGACY\","
|
| + "\"hires_image\": {"
|
| + "\"alternate_url\":\"\","
|
| + "\"background_color\":\"#ffffff\","
|
| + "\"height\":450,"
|
| + "\"image_id\":0,"
|
| + "\"image_name\":\"\","
|
| + "\"is_animated_gif\":true,"
|
| + "\"is_cta\":false,"
|
| + "\"offset_x\":0,"
|
| + "\"offset_y\":0,"
|
| + "\"overlay_icon_color\":\"\","
|
| + "\"size_type\":\"LARGE_2X\","
|
| + "\"slot\":8,"
|
| + "\"url\":\"/logos/doodles/2015/"
|
| + "new-years-eve-2015-5985438795825152-hp2x.gif\","
|
| + "\"width\":978"
|
| + "},"
|
| + "\"id\":0,"
|
| + "\"intent\":\"BUILTIN\","
|
| + "\"large_image\": {"
|
| + "\"alternate_url\":\"\","
|
| + "\"background_color\":\"#ffffff\","
|
| + "\"height\":225,"
|
| + "\"image_id\":0,"
|
| + "\"image_name\":\"\","
|
| + "\"is_animated_gif\":true,"
|
| + "\"is_cta\":false,"
|
| + "\"offset_x\":0,"
|
| + "\"offset_y\":0,"
|
| + "\"overlay_icon_color\":\"\","
|
| + "\"size_type\":\"LARGE\","
|
| + "\"slot\":1,"
|
| + "\"url\":\"/logos/doodles/2015/"
|
| + "new-years-eve-2015-5985438795825152-hp.gif\","
|
| + "\"width\":489"
|
| + "},"
|
| + "\"medium_image\": {"
|
| + "\"alternate_url\":\"\","
|
| + "\"background_color\":\"#fff1b9\","
|
| + "\"height\":128,"
|
| + "\"image_id\":0,"
|
| + "\"image_name\":\"\","
|
| + "\"is_animated_gif\":false,"
|
| + "\"is_cta\":true,"
|
| + "\"offset_x\":0,"
|
| + "\"offset_y\":0,"
|
| + "\"overlay_icon_color\":\"#ff0f15\","
|
| + "\"size_type\":\"LARGE_MOBILE_SRP\","
|
| + "\"slot\":3,"
|
| + "\"url\":\"/logos/doodles/2015/new-years-eve-2015-5985438795825152-m.png\","
|
| + "\"width\":291"
|
| + "},"
|
| + "\"search_url\":\"/search?q\u003dtest\","
|
| + "\"share_text\":\"Share Text #GoogleDoodle\nhttps://g.co/doodle/8hfqzq\","
|
| + "\"short_link\":\"//g.co/doodle/8hfqzq\","
|
| + "\"show_now_header_search_affordance\":false,"
|
| + "\"show_now_header_share_button\":true,"
|
| + "\"small_image\": {"
|
| + "\"alternate_url\":\"\","
|
| + "\"background_color\":\"\","
|
| + "\"height\":41,"
|
| + "\"image_id\":0,"
|
| + "\"image_name\":\"\","
|
| + "\"is_animated_gif\":false,"
|
| + "\"is_cta\":false,"
|
| + "\"offset_x\":0,"
|
| + "\"offset_y\":0,"
|
| + "\"overlay_icon_color\":\"\","
|
| + "\"size_type\":\"SMALL_DESKTOP_SRP\","
|
| + "\"slot\":4,"
|
| + "\"url\":\"/logos/doodles/2015/"
|
| + "new-years-eve-2015-5985438795825152.2-res.png\","
|
| + "\"width\":95"
|
| + "},"
|
| + "\"target_url\":\"/"
|
| + "search?"
|
| + "q\u003dtest\u0026sa\u003dX\u0026ved\u003d0ahUKEwjm9vmF8enRAhUJvBQKHX1CC54Q"
|
| + "NggB\","
|
| + "\"time_to_live_ms\":514220215000,"
|
| + "\"transparent_large_image\": {"
|
| + "\"alternate_url\":\"\","
|
| + "\"background_color\":\"\","
|
| + "\"height\":225,"
|
| + "\"image_id\":0,"
|
| + "\"image_name\":\"\","
|
| + "\"is_animated_gif\":false,"
|
| + "\"is_cta\":false,"
|
| + "\"offset_x\":0,"
|
| + "\"offset_y\":0,"
|
| + "\"overlay_icon_color\":\"\","
|
| + "\"size_type\":\"LARGE\","
|
| + "\"slot\":2,"
|
| + "\"url\":\"/logos/doodles/2015/"
|
| + "new-years-eve-2015-5985438795825152-thp.png\","
|
| + "\"width\":510"
|
| + "}"
|
| + "}"
|
| + "}";
|
| +
|
| +} // namespace
|
| +
|
| +class DoodleFetcherTest : public ::testing::Test {
|
| + protected:
|
| + DoodleFetcherTest()
|
| + : url_(kDoodleConfigUrl),
|
| + 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;
|
| + std::unique_ptr<base::DictionaryValue> ddljson =
|
| + base::MakeUnique<base::DictionaryValue>();
|
| + ddljson->SetString("search_url", "?q=test");
|
| + ddljson->SetString("fullpage_interactive_url", "full/?q=test");
|
| + std::unique_ptr<base::DictionaryValue> image =
|
| + base::MakeUnique<base::DictionaryValue>();
|
| + image->SetString("url", "/doodle-url");
|
| + image->SetInteger("height", 300);
|
| + image->SetInteger("width", 1200);
|
| + image->SetString("background_color", "#00FFEE");
|
| + ddljson->Set("large_image", std::move(image));
|
| + config.Set("ddljson", std::move(ddljson));
|
| + 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(),
|
| + GURL("https://www.google.com/"));
|
| +
|
| + DoodleConfig doodle;
|
| + fetcher.FetchDoodle(base::Bind(
|
| + [](DoodleConfig* doodle, const DoodleConfig& config) {
|
| + *doodle = std::move(config);
|
| + LOG(ERROR) << "Run callback";
|
| + },
|
| + &doodle));
|
| + LOG(ERROR) << "Callback started. Wait for tasks ...";
|
| + mock_task_runner_->RunUntilIdle();
|
| + LOG(ERROR) << "... finished tasks.";
|
| + 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;
|
| + safe_json::TestingJsonParser::ScopedFactoryOverride factory_override;
|
| +};
|
| +
|
| +TEST_F(DoodleFetcherTest, ReturnsFromFetchWithoutError) {
|
| + RespondWithData(kSampleResponse);
|
| +
|
| + EXPECT_TRUE(FetchDoodle().state == DoodleState::AVAILABLE);
|
| +}
|
| +
|
| +TEST_F(DoodleFetcherTest, ReturnsFrom404FetchWithError) {
|
| + RespondUnsuccessfully();
|
| +
|
| + EXPECT_TRUE(FetchDoodle().state == DoodleState::DOWNLOAD_ERROR);
|
| +}
|
| +
|
| +TEST_F(DoodleFetcherTest, ResponseContainsValidLargeImageUrl) {
|
| + RespondWithData(kSampleResponse);
|
| +
|
| + DoodleConfig response = FetchDoodle();
|
| +
|
| + EXPECT_TRUE(response.large_image.url.is_valid());
|
| +}
|
| +
|
| +} // namespace doodle
|
|
|