Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / url_request / sdch_dictionary_fetcher_unittest.cc
blobf6db83fc62efcb0bca58681aada137f92693f9d6
1 // Copyright 2014 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.
5 #include "net/url_request/sdch_dictionary_fetcher.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "net/base/load_flags.h"
18 #include "net/base/sdch_manager.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/url_request/url_request_data_job.h"
21 #include "net/url_request/url_request_filter.h"
22 #include "net/url_request/url_request_interceptor.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace net {
28 namespace {
30 const char kSampleBufferContext[] = "This is a sample buffer.";
31 const char kTestDomain[] = "top.domain.test";
33 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob {
34 public:
35 // Called on destruction with load flags used for this request.
36 typedef base::Callback<void(int)> DestructionCallback;
38 URLRequestSpecifiedResponseJob(
39 URLRequest* request,
40 NetworkDelegate* network_delegate,
41 const HttpResponseInfo& response_info_to_return,
42 const DestructionCallback& destruction_callback)
43 : URLRequestSimpleJob(request, network_delegate),
44 response_info_to_return_(response_info_to_return),
45 last_load_flags_seen_(request->load_flags()),
46 destruction_callback_(destruction_callback) {
47 DCHECK(!destruction_callback.is_null());
50 static std::string ExpectedResponseForURL(const GURL& url) {
51 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n",
52 url.spec().c_str(),
53 kSampleBufferContext,
54 url.spec().c_str());
57 // URLRequestJob
58 void GetResponseInfo(HttpResponseInfo* info) override {
59 *info = response_info_to_return_;
62 private:
63 ~URLRequestSpecifiedResponseJob() override {
64 destruction_callback_.Run(last_load_flags_seen_);
67 // URLRequestSimpleJob implementation:
68 int GetData(std::string* mime_type,
69 std::string* charset,
70 std::string* data,
71 const CompletionCallback& callback) const override {
72 GURL url(request_->url());
73 *data = ExpectedResponseForURL(url);
74 return OK;
77 const HttpResponseInfo response_info_to_return_;
78 int last_load_flags_seen_;
79 const DestructionCallback destruction_callback_;
81 DISALLOW_COPY_AND_ASSIGN(URLRequestSpecifiedResponseJob);
84 class SpecifiedResponseJobInterceptor : public URLRequestInterceptor {
85 public:
86 // A callback to be called whenever a URLRequestSpecifiedResponseJob is
87 // created or destroyed. The first argument will be the change in number of
88 // jobs (i.e. +1 for created, -1 for destroyed).
89 // The second argument will be undefined if the job is being created,
90 // and will contain the load flags passed to the request the
91 // job was created for if the job is being destroyed.
92 typedef base::Callback<void(int outstanding_job_delta,
93 int destruction_load_flags)> LifecycleCallback;
95 // |*info| will be returned from all child URLRequestSpecifiedResponseJobs.
96 // Note that: a) this pointer is shared with the caller, and the caller must
97 // guarantee that |*info| outlives the SpecifiedResponseJobInterceptor, and
98 // b) |*info| is mutable, and changes to should propagate to
99 // URLRequestSpecifiedResponseJobs created after any change.
100 SpecifiedResponseJobInterceptor(HttpResponseInfo* http_response_info,
101 const LifecycleCallback& lifecycle_callback)
102 : http_response_info_(http_response_info),
103 lifecycle_callback_(lifecycle_callback) {
104 DCHECK(!lifecycle_callback_.is_null());
106 ~SpecifiedResponseJobInterceptor() override {}
108 URLRequestJob* MaybeInterceptRequest(
109 URLRequest* request,
110 NetworkDelegate* network_delegate) const override {
111 lifecycle_callback_.Run(1, 0);
113 return new URLRequestSpecifiedResponseJob(
114 request, network_delegate, *http_response_info_,
115 base::Bind(lifecycle_callback_, -1));
118 // The caller must ensure that both |*http_response_info| and the
119 // callback remain valid for the lifetime of the
120 // SpecifiedResponseJobInterceptor (i.e. until Unregister() is called).
121 static void RegisterWithFilter(HttpResponseInfo* http_response_info,
122 const LifecycleCallback& lifecycle_callback) {
123 scoped_ptr<SpecifiedResponseJobInterceptor> interceptor(
124 new SpecifiedResponseJobInterceptor(http_response_info,
125 lifecycle_callback));
127 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
128 "http", kTestDomain, interceptor.Pass());
131 static void Unregister() {
132 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http",
133 kTestDomain);
136 private:
137 HttpResponseInfo* http_response_info_;
138 LifecycleCallback lifecycle_callback_;
139 DISALLOW_COPY_AND_ASSIGN(SpecifiedResponseJobInterceptor);
142 // Local test infrastructure
143 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns
144 // a different but derivable response for each URL (used for all
145 // url requests in this file). This class is initialized with
146 // the HttpResponseInfo to return (if any), as well as a callback
147 // that is called when the class is destroyed. That callback
148 // takes as arguemnt the load flags used for the request the
149 // job was created for.
150 // * SpecifiedResponseJobInterceptor: This class is a
151 // URLRequestInterceptor that generates the class above. It is constructed
152 // with a pointer to the (mutable) resposne info that should be
153 // returned from the URLRequestSpecifiedResponseJob children, as well as
154 // a callback that is run when URLRequestSpecifiedResponseJobs are
155 // created or destroyed.
156 // * SdchDictionaryFetcherTest: This class registers the above interceptor,
157 // tracks the number of jobs requested and the subset of those
158 // that are still outstanding. It exports an interface to wait until there
159 // are no jobs outstanding. It shares an HttpResponseInfo structure
160 // with the SpecifiedResponseJobInterceptor to control the response
161 // information returned by the jbos.
162 // The standard pattern for tests is to schedule a dictionary fetch, wait
163 // for no jobs outstanding, then test that the fetch results are as expected.
165 class SdchDictionaryFetcherTest : public ::testing::Test {
166 public:
167 struct DictionaryAdditions {
168 DictionaryAdditions(const std::string& dictionary_text,
169 const GURL& dictionary_url)
170 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {}
172 std::string dictionary_text;
173 GURL dictionary_url;
176 SdchDictionaryFetcherTest()
177 : jobs_requested_(0),
178 jobs_outstanding_(0),
179 last_load_flags_seen_(LOAD_NORMAL),
180 context_(new TestURLRequestContext),
181 fetcher_(new SdchDictionaryFetcher(context_.get())),
182 factory_(this) {
183 response_info_to_return_.request_time = base::Time::Now();
184 response_info_to_return_.response_time = base::Time::Now();
185 SpecifiedResponseJobInterceptor::RegisterWithFilter(
186 &response_info_to_return_,
187 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged,
188 factory_.GetWeakPtr()));
191 ~SdchDictionaryFetcherTest() override {
192 SpecifiedResponseJobInterceptor::Unregister();
195 void OnDictionaryFetched(const std::string& dictionary_text,
196 const GURL& dictionary_url,
197 const BoundNetLog& net_log,
198 bool was_from_cache) {
199 dictionary_additions_.push_back(
200 DictionaryAdditions(dictionary_text, dictionary_url));
203 // Return (in |*out|) all dictionary additions since the last time
204 // this function was called.
205 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) {
206 out->swap(dictionary_additions_);
207 dictionary_additions_.clear();
210 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); }
212 // May not be called outside the SetUp()/TearDown() interval.
213 int jobs_requested() const { return jobs_requested_; }
215 GURL PathToGurl(const char* path) const {
216 std::string gurl_string("http://");
217 gurl_string += kTestDomain;
218 gurl_string += "/";
219 gurl_string += path;
220 return GURL(gurl_string);
223 // Block until there are no outstanding URLRequestSpecifiedResponseJobs.
224 void WaitForNoJobs() {
225 if (jobs_outstanding_ == 0)
226 return;
228 run_loop_.reset(new base::RunLoop);
229 run_loop_->Run();
230 run_loop_.reset();
233 HttpResponseInfo* response_info_to_return() {
234 return &response_info_to_return_;
237 int last_load_flags_seen() const { return last_load_flags_seen_; }
239 const SdchDictionaryFetcher::OnDictionaryFetchedCallback
240 GetDefaultCallback() {
241 return base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched,
242 base::Unretained(this));
245 private:
246 void OnNumberJobsChanged(int outstanding_jobs_delta, int load_flags) {
247 DCHECK_NE(0, outstanding_jobs_delta);
248 if (outstanding_jobs_delta > 0)
249 jobs_requested_ += outstanding_jobs_delta;
250 else
251 last_load_flags_seen_ = load_flags;
252 jobs_outstanding_ += outstanding_jobs_delta;
253 if (jobs_outstanding_ == 0 && run_loop_)
254 run_loop_->Quit();
257 int jobs_requested_;
258 int jobs_outstanding_;
260 // Last load flags seen by the interceptor installed in
261 // SdchDictionaryFetcherTest(). These are available to test bodies and
262 // currently used for ensuring that certain loads are marked only-from-cache.
263 int last_load_flags_seen_;
265 scoped_ptr<base::RunLoop> run_loop_;
266 scoped_ptr<TestURLRequestContext> context_;
267 scoped_ptr<SdchDictionaryFetcher> fetcher_;
268 std::vector<DictionaryAdditions> dictionary_additions_;
270 // The request_time and response_time fields are filled in by the constructor
271 // for SdchDictionaryFetcherTest. Tests can fill the other fields of this
272 // member in to alter the HttpResponseInfo returned by the fetcher's
273 // URLRequestJob.
274 HttpResponseInfo response_info_to_return_;
276 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_;
278 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcherTest);
281 // Schedule a fetch and make sure it happens.
282 TEST_F(SdchDictionaryFetcherTest, Basic) {
283 GURL dictionary_url(PathToGurl("dictionary"));
284 fetcher()->Schedule(dictionary_url, GetDefaultCallback());
285 WaitForNoJobs();
287 EXPECT_EQ(1, jobs_requested());
288 std::vector<DictionaryAdditions> additions;
289 GetDictionaryAdditions(&additions);
290 ASSERT_EQ(1u, additions.size());
291 EXPECT_EQ(
292 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
293 additions[0].dictionary_text);
294 EXPECT_FALSE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
297 // Multiple fetches of the same URL should result in only one request.
298 TEST_F(SdchDictionaryFetcherTest, Multiple) {
299 GURL dictionary_url(PathToGurl("dictionary"));
300 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
301 EXPECT_FALSE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
302 EXPECT_FALSE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
303 WaitForNoJobs();
305 EXPECT_EQ(1, jobs_requested());
306 std::vector<DictionaryAdditions> additions;
307 GetDictionaryAdditions(&additions);
308 ASSERT_EQ(1u, additions.size());
309 EXPECT_EQ(
310 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
311 additions[0].dictionary_text);
314 // A cancel should result in no actual requests being generated.
315 TEST_F(SdchDictionaryFetcherTest, Cancel) {
316 GURL dictionary_url_1(PathToGurl("dictionary_1"));
317 GURL dictionary_url_2(PathToGurl("dictionary_2"));
318 GURL dictionary_url_3(PathToGurl("dictionary_3"));
320 fetcher()->Schedule(dictionary_url_1, GetDefaultCallback());
321 fetcher()->Schedule(dictionary_url_2, GetDefaultCallback());
322 fetcher()->Schedule(dictionary_url_3, GetDefaultCallback());
323 fetcher()->Cancel();
324 WaitForNoJobs();
326 // Synchronous execution may have resulted in a single job being scheduled.
327 EXPECT_GE(1, jobs_requested());
330 // Attempt to confuse the fetcher loop processing by scheduling a
331 // dictionary addition while another fetch is in process.
332 TEST_F(SdchDictionaryFetcherTest, LoopRace) {
333 GURL dictionary0_url(PathToGurl("dictionary0"));
334 GURL dictionary1_url(PathToGurl("dictionary1"));
335 fetcher()->Schedule(dictionary0_url, GetDefaultCallback());
336 fetcher()->Schedule(dictionary1_url, GetDefaultCallback());
337 WaitForNoJobs();
339 ASSERT_EQ(2, jobs_requested());
340 std::vector<DictionaryAdditions> additions;
341 GetDictionaryAdditions(&additions);
342 ASSERT_EQ(2u, additions.size());
343 EXPECT_EQ(
344 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url),
345 additions[0].dictionary_text);
346 EXPECT_EQ(
347 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url),
348 additions[1].dictionary_text);
351 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadLoadFlags) {
352 GURL dictionary_url(PathToGurl("dictionary"));
353 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback());
355 WaitForNoJobs();
356 EXPECT_EQ(1, jobs_requested());
357 std::vector<DictionaryAdditions> additions;
358 GetDictionaryAdditions(&additions);
359 ASSERT_EQ(1u, additions.size());
360 EXPECT_EQ(
361 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
362 additions[0].dictionary_text);
363 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
366 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadFresh) {
367 std::string raw_headers = "\0";
368 response_info_to_return()->headers = new HttpResponseHeaders(
369 HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size()));
370 response_info_to_return()->headers->AddHeader("Cache-Control: max-age=1000");
372 GURL dictionary_url(PathToGurl("dictionary"));
373 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback());
375 WaitForNoJobs();
376 EXPECT_EQ(1, jobs_requested());
377 std::vector<DictionaryAdditions> additions;
378 GetDictionaryAdditions(&additions);
379 ASSERT_EQ(1u, additions.size());
380 EXPECT_EQ(
381 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
382 additions[0].dictionary_text);
383 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
386 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadStale) {
387 response_info_to_return()->headers = new HttpResponseHeaders("");
388 response_info_to_return()->headers->AddHeader("Cache-Control: no-cache");
390 GURL dictionary_url(PathToGurl("dictionary"));
391 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback());
393 WaitForNoJobs();
394 EXPECT_EQ(1, jobs_requested());
395 std::vector<DictionaryAdditions> additions;
396 GetDictionaryAdditions(&additions);
397 EXPECT_EQ(0u, additions.size());
398 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
401 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadThenLoad) {
402 GURL dictionary_url(PathToGurl("dictionary"));
403 EXPECT_TRUE(fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback()));
404 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
406 WaitForNoJobs();
407 EXPECT_EQ(2, jobs_requested());
410 TEST_F(SdchDictionaryFetcherTest, ScheduleLoadThenReload) {
411 GURL dictionary_url(PathToGurl("dictionary"));
412 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
413 EXPECT_FALSE(fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback()));
415 WaitForNoJobs();
416 EXPECT_EQ(1, jobs_requested());
419 TEST_F(SdchDictionaryFetcherTest, CancelAllowsFutureFetches) {
420 GURL dictionary_url(PathToGurl("dictionary"));
421 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
422 EXPECT_FALSE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
424 WaitForNoJobs();
425 EXPECT_EQ(1, jobs_requested());
427 fetcher()->Cancel();
428 WaitForNoJobs();
429 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
431 WaitForNoJobs();
432 EXPECT_EQ(2, jobs_requested());
435 } // namespace
437 } // namespace net