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

Side by Side Diff: components/doodle/doodle_fetcher_unittest.cc

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Tests work. Created 3 years, 10 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 unified diff | Download patch
OLDNEW
(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/sequenced_task_runner_handle.h"
22 #include "base/threading/thread_task_runner_handle.h"
23 #include "base/values.h"
24 #include "components/safe_json/testing_json_parser.h"
25 #include "net/http/http_status_code.h"
26 #include "net/url_request/test_url_fetcher_factory.h"
27 #include "net/url_request/url_request_status.h"
28 #include "net/url_request/url_request_test_util.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 using testing::Eq;
33 using testing::IsEmpty;
34
35 namespace doodle {
36
37 namespace {
38
39 const char* kSampleResponse =
40 ")]}'{"
41 "\"ddljson\": {"
42 "\"alt_text\":\"Mouseover Text\","
43 "\"doodle_type\":\"SIMPLE\","
44 "\"gallery_url\":\"//www.google.com/doodles/burning-man-festival\","
45 "\"header_layout\":\"LEGACY\","
46 "\"hires_image\": {"
47 "\"alternate_url\":\"\","
48 "\"background_color\":\"#ffffff\","
49 "\"height\":450,"
50 "\"image_id\":0,"
51 "\"image_name\":\"\","
52 "\"is_animated_gif\":true,"
53 "\"is_cta\":false,"
54 "\"offset_x\":0,"
55 "\"offset_y\":0,"
56 "\"overlay_icon_color\":\"\","
57 "\"size_type\":\"LARGE_2X\","
58 "\"slot\":8,"
59 "\"url\":\"/logos/doodles/2015/"
60 "new-years-eve-2015-5985438795825152-hp2x.gif\","
61 "\"width\":978"
62 "},"
63 "\"id\":0,"
64 "\"intent\":\"BUILTIN\","
65 "\"large_image\": {"
66 "\"alternate_url\":\"\","
67 "\"background_color\":\"#ffffff\","
68 "\"height\":225,"
69 "\"image_id\":0,"
70 "\"image_name\":\"\","
71 "\"is_animated_gif\":true,"
72 "\"is_cta\":false,"
73 "\"offset_x\":0,"
74 "\"offset_y\":0,"
75 "\"overlay_icon_color\":\"\","
76 "\"size_type\":\"LARGE\","
77 "\"slot\":1,"
78 "\"url\":\"/logos/doodles/2015/"
79 "new-years-eve-2015-5985438795825152-hp.gif\","
80 "\"width\":489"
81 "},"
82 "\"medium_image\": {"
83 "\"alternate_url\":\"\","
84 "\"background_color\":\"#fff1b9\","
85 "\"height\":128,"
86 "\"image_id\":0,"
87 "\"image_name\":\"\","
88 "\"is_animated_gif\":false,"
89 "\"is_cta\":true,"
90 "\"offset_x\":0,"
91 "\"offset_y\":0,"
92 "\"overlay_icon_color\":\"#ff0f15\","
93 "\"size_type\":\"LARGE_MOBILE_SRP\","
94 "\"slot\":3,"
95 "\"url\":\"/logos/doodles/2015/new-years-eve-2015-5985438795825152-m.png\","
96 "\"width\":291"
97 "},"
98 "\"search_url\":\"/search?q\u003dtest\","
99 "\"share_text\":\"Share Text #GoogleDoodle\nhttps://g.co/doodle/8hfqzq\","
100 "\"short_link\":\"//g.co/doodle/8hfqzq\","
101 "\"show_now_header_search_affordance\":false,"
102 "\"show_now_header_share_button\":true,"
103 "\"small_image\": {"
104 "\"alternate_url\":\"\","
105 "\"background_color\":\"\","
106 "\"height\":41,"
107 "\"image_id\":0,"
108 "\"image_name\":\"\","
109 "\"is_animated_gif\":false,"
110 "\"is_cta\":false,"
111 "\"offset_x\":0,"
112 "\"offset_y\":0,"
113 "\"overlay_icon_color\":\"\","
114 "\"size_type\":\"SMALL_DESKTOP_SRP\","
115 "\"slot\":4,"
116 "\"url\":\"/logos/doodles/2015/"
117 "new-years-eve-2015-5985438795825152.2-res.png\","
118 "\"width\":95"
119 "},"
120 "\"target_url\":\"/"
121 "search?"
122 "q\u003dtest\u0026sa\u003dX\u0026ved\u003d0ahUKEwjm9vmF8enRAhUJvBQKHX1CC54Q"
123 "NggB\","
124 "\"time_to_live_ms\":514220215000,"
125 "\"transparent_large_image\": {"
126 "\"alternate_url\":\"\","
127 "\"background_color\":\"\","
128 "\"height\":225,"
129 "\"image_id\":0,"
130 "\"image_name\":\"\","
131 "\"is_animated_gif\":false,"
132 "\"is_cta\":false,"
133 "\"offset_x\":0,"
134 "\"offset_y\":0,"
135 "\"overlay_icon_color\":\"\","
136 "\"size_type\":\"LARGE\","
137 "\"slot\":2,"
138 "\"url\":\"/logos/doodles/2015/"
139 "new-years-eve-2015-5985438795825152-thp.png\","
140 "\"width\":510"
141 "}"
142 "}"
143 "}";
144
145 } // namespace
146
147 class DoodleFetcherTest : public ::testing::Test {
148 protected:
149 DoodleFetcherTest()
150 : url_(kDoodleConfigUrl),
151 url_fetcher_factory_(nullptr),
152 mock_task_runner_(new base::TestSimpleTaskRunner()),
153 mock_task_runner_handle_(mock_task_runner_),
154 context_getter(
155 new net::TestURLRequestContextGetter(mock_task_runner_.get())) {}
156
157 void RespondWithJSON() {
158 base::DictionaryValue config;
159 std::unique_ptr<base::DictionaryValue> ddljson =
160 base::MakeUnique<base::DictionaryValue>();
161 ddljson->SetString("search_url", "?q=test");
162 ddljson->SetString("fullpage_interactive_url", "full/?q=test");
163 std::unique_ptr<base::DictionaryValue> image =
164 base::MakeUnique<base::DictionaryValue>();
165 image->SetString("url", "/doodle-url");
166 image->SetInteger("height", 300);
167 image->SetInteger("width", 1200);
168 image->SetString("background_color", "#00FFEE");
169 ddljson->Set("large_image", std::move(image));
170 config.Set("ddljson", std::move(ddljson));
171 std::string json_string;
172 base::JSONWriter::Write(config, &json_string);
173 url_fetcher_factory_.SetFakeResponse(GURL(url_), json_string, net::HTTP_OK,
174 net::URLRequestStatus::SUCCESS);
175 }
176
177 void RespondWithData(const std::string& data) {
178 url_fetcher_factory_.SetFakeResponse(GURL(url_), data, net::HTTP_OK,
179 net::URLRequestStatus::SUCCESS);
180 }
181
182 void RespondUnsuccessfully() {
183 url_fetcher_factory_.SetFakeResponse(GURL(url_), "", net::HTTP_NOT_FOUND,
184 net::URLRequestStatus::FAILED);
185 }
186
187 DoodleConfig FetchDoodle() {
188 DoodleFetcher fetcher(context_getter.get(),
189 GURL("https://www.google.com/"));
190
191 DoodleConfig doodle;
192 fetcher.FetchDoodle(base::Bind(
193 [](DoodleConfig* doodle, const DoodleConfig& config) {
194 *doodle = std::move(config);
195 LOG(ERROR) << "Run callback";
196 },
197 &doodle));
198 LOG(ERROR) << "Callback started. Wait for tasks ...";
199 mock_task_runner_->RunUntilIdle();
200 LOG(ERROR) << "... finished tasks.";
201 return doodle;
202 }
203
204 std::string url_;
205 net::FakeURLFetcherFactory url_fetcher_factory_;
206 scoped_refptr<base::TestSimpleTaskRunner> mock_task_runner_;
207 base::ThreadTaskRunnerHandle mock_task_runner_handle_;
208 scoped_refptr<net::TestURLRequestContextGetter> context_getter;
209 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override;
210 };
211
212 TEST_F(DoodleFetcherTest, ReturnsFromFetchWithoutError) {
213 RespondWithData(kSampleResponse);
214
215 EXPECT_TRUE(FetchDoodle().state == DoodleState::AVAILABLE);
216 }
217
218 TEST_F(DoodleFetcherTest, ReturnsFrom404FetchWithError) {
219 RespondUnsuccessfully();
220
221 EXPECT_TRUE(FetchDoodle().state == DoodleState::DOWNLOAD_ERROR);
222 }
223
224 TEST_F(DoodleFetcherTest, ResponseContainsValidLargeImageUrl) {
225 RespondWithData(kSampleResponse);
226
227 DoodleConfig response = FetchDoodle();
228
229 EXPECT_TRUE(response.large_image.url.is_valid());
230 }
231
232 } // namespace doodle
OLDNEW
« components/doodle/doodle_fetcher.cc ('K') | « components/doodle/doodle_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698