| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/doodle/doodle_fetcher.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/files/file_util.h" |
| 13 #include "base/files/important_file_writer.h" |
| 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/json/json_reader.h" |
| 16 #include "base/json/json_writer.h" |
| 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/run_loop.h" |
| 19 #include "base/test/sequenced_worker_pool_owner.h" |
| 20 #include "base/test/test_simple_task_runner.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "base/values.h" |
| 23 #include "net/http/http_status_code.h" |
| 24 #include "net/url_request/test_url_fetcher_factory.h" |
| 25 #include "net/url_request/url_request_status.h" |
| 26 #include "net/url_request/url_request_test_util.h" |
| 27 #include "testing/gmock/include/gmock/gmock.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 |
| 30 using testing::Eq; |
| 31 using testing::IsEmpty; |
| 32 |
| 33 namespace { |
| 34 |
| 35 void ParseJson( |
| 36 const std::string& json, |
| 37 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, |
| 38 const base::Callback<void(const std::string&)>& error_callback) { |
| 39 base::JSONReader json_reader; |
| 40 std::unique_ptr<base::Value> value = json_reader.ReadToValue(json); |
| 41 if (value) { |
| 42 success_callback.Run(std::move(value)); |
| 43 } else { |
| 44 error_callback.Run(json_reader.GetErrorMessage()); |
| 45 } |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 class DoodleFetcherTest : public ::testing::Test { |
| 51 protected: |
| 52 DoodleFetcherTest() |
| 53 : url_("https://www.google.com/async/ddljson?ogdeb=ct~2000000007"), |
| 54 url_fetcher_factory_(nullptr), |
| 55 mock_task_runner_(new base::TestSimpleTaskRunner()), |
| 56 mock_task_runner_handle_(mock_task_runner_), |
| 57 context_getter( |
| 58 new net::TestURLRequestContextGetter(mock_task_runner_.get())) {} |
| 59 |
| 60 void RespondWithJSON() { |
| 61 base::DictionaryValue config; |
| 62 config.SetString("search_url", "google.de"); |
| 63 config.SetString("fullpage_interactive_url", "google.de"); |
| 64 std::unique_ptr<base::DictionaryValue> image = |
| 65 base::MakeUnique<base::DictionaryValue>(); |
| 66 image->SetString("url", "google.de/doodle"); |
| 67 image->SetInteger("height", 300); |
| 68 image->SetInteger("width", 1200); |
| 69 image->SetString("background_color", "#00FFEE"); |
| 70 config.Set("large_image", std::move(image)); |
| 71 std::string json_string; |
| 72 base::JSONWriter::Write(config, &json_string); |
| 73 url_fetcher_factory_.SetFakeResponse(GURL(url_), json_string, net::HTTP_OK, |
| 74 net::URLRequestStatus::SUCCESS); |
| 75 } |
| 76 |
| 77 void RespondWithData(const std::string& data) { |
| 78 url_fetcher_factory_.SetFakeResponse(GURL(url_), data, net::HTTP_OK, |
| 79 net::URLRequestStatus::SUCCESS); |
| 80 } |
| 81 |
| 82 void RespondUnsuccessfully() { |
| 83 url_fetcher_factory_.SetFakeResponse(GURL(url_), "", net::HTTP_NOT_FOUND, |
| 84 net::URLRequestStatus::FAILED); |
| 85 } |
| 86 |
| 87 DoodleConfig FetchDoodle() { |
| 88 DoodleFetcher fetcher(context_getter.get(), base::Bind(ParseJson)); |
| 89 |
| 90 DoodleConfig doodle; |
| 91 fetcher.FetchDoodle( |
| 92 base::Bind([](DoodleConfig* doodle, |
| 93 DoodleConfig config) { *doodle = std::move(config); }, |
| 94 &doodle)); |
| 95 mock_task_runner_->RunUntilIdle(); |
| 96 return doodle; |
| 97 } |
| 98 |
| 99 std::string url_; |
| 100 net::FakeURLFetcherFactory url_fetcher_factory_; |
| 101 scoped_refptr<base::TestSimpleTaskRunner> mock_task_runner_; |
| 102 base::ThreadTaskRunnerHandle mock_task_runner_handle_; |
| 103 scoped_refptr<net::TestURLRequestContextGetter> context_getter; |
| 104 }; |
| 105 |
| 106 TEST_F(DoodleFetcherTest, ReturnsFromFetchWithoutError) { |
| 107 RespondWithJSON(); |
| 108 |
| 109 EXPECT_FALSE(FetchDoodle().error_during_fetch); |
| 110 } |
| 111 |
| 112 TEST_F(DoodleFetcherTest, ReturnsFrom404FetchWithError) { |
| 113 RespondUnsuccessfully(); |
| 114 |
| 115 EXPECT_TRUE(FetchDoodle().error_during_fetch); |
| 116 } |
| 117 |
| 118 TEST_F(DoodleFetcherTest, ResponseContainsValidImageUrl) { |
| 119 RespondWithJSON(); |
| 120 |
| 121 DoodleConfig response = FetchDoodle(); |
| 122 |
| 123 EXPECT_TRUE(GURL(response.large_image.url).is_valid()); |
| 124 } |
| OLD | NEW |