mac: Don't limit GL_MAX_TEXTURE_SIZE on 10.9.0+.
[chromium-blink-merge.git] / net / url_request / sdch_dictionary_fetcher_unittest.cc
blobae76cbcc7f88b5f7806bfe9af3ad15bad768c629
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>
9 #include "base/bind.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "net/base/sdch_manager.h"
14 #include "net/url_request/url_request_data_job.h"
15 #include "net/url_request/url_request_filter.h"
16 #include "net/url_request/url_request_interceptor.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h"
21 namespace net {
23 static const char* kSampleBufferContext = "This is a sample buffer.";
24 static const char* kTestDomain = "top.domain.test";
26 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob {
27 public:
28 URLRequestSpecifiedResponseJob(URLRequest* request,
29 NetworkDelegate* network_delegate)
30 : URLRequestSimpleJob(request, network_delegate) {}
32 static void AddUrlHandler() {
33 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
34 jobs_requested_ = 0;
35 filter->AddHostnameHandler(
36 "http", kTestDomain, &URLRequestSpecifiedResponseJob::Factory);
39 static void RemoveUrlHandler() {
40 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
41 filter->RemoveHostnameHandler("http", kTestDomain);
42 jobs_requested_ = 0;
45 static URLRequestJob* Factory(URLRequest* request,
46 net::NetworkDelegate* network_delegate,
47 const std::string& scheme) {
48 ++jobs_requested_;
49 return new URLRequestSpecifiedResponseJob(request, network_delegate);
52 static std::string ExpectedResponseForURL(const GURL& url) {
53 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n",
54 url.spec().c_str(),
55 kSampleBufferContext,
56 url.spec().c_str());
59 static int jobs_requested() { return jobs_requested_; }
61 private:
62 ~URLRequestSpecifiedResponseJob() override{};
63 int GetData(std::string* mime_type,
64 std::string* charset,
65 std::string* data,
66 const CompletionCallback& callback) const override {
67 GURL url(request_->url());
68 *data = ExpectedResponseForURL(url);
69 return OK;
72 static int jobs_requested_;
75 int URLRequestSpecifiedResponseJob::jobs_requested_(0);
77 class SdchDictionaryFetcherTest : public ::testing::Test {
78 public:
79 struct DictionaryAdditions {
80 DictionaryAdditions(const std::string& dictionary_text,
81 const GURL& dictionary_url)
82 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {}
84 std::string dictionary_text;
85 GURL dictionary_url;
88 SdchDictionaryFetcherTest() {
89 URLRequestSpecifiedResponseJob::AddUrlHandler();
90 context_.reset(new TestURLRequestContext);
91 fetcher_.reset(new SdchDictionaryFetcher(
92 context_.get(),
93 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched,
94 base::Unretained(this))));
97 ~SdchDictionaryFetcherTest() {
98 URLRequestSpecifiedResponseJob::RemoveUrlHandler();
101 void OnDictionaryFetched(const std::string& dictionary_text,
102 const GURL& dictionary_url,
103 const BoundNetLog& net_log) {
104 dictionary_additions.push_back(
105 DictionaryAdditions(dictionary_text, dictionary_url));
108 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) {
109 out->swap(dictionary_additions);
110 dictionary_additions.clear();
113 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); }
115 // May not be called outside the SetUp()/TearDown() interval.
116 int JobsRequested() {
117 return URLRequestSpecifiedResponseJob::jobs_requested();
120 GURL PathToGurl(const char* path) {
121 std::string gurl_string("http://");
122 gurl_string += kTestDomain;
123 gurl_string += "/";
124 gurl_string += path;
125 return GURL(gurl_string);
128 private:
129 scoped_ptr<TestURLRequestContext> context_;
130 scoped_ptr<SdchDictionaryFetcher> fetcher_;
131 std::vector<DictionaryAdditions> dictionary_additions;
134 // Schedule a fetch and make sure it happens.
135 TEST_F(SdchDictionaryFetcherTest, Basic) {
136 GURL dictionary_url(PathToGurl("dictionary"));
137 fetcher()->Schedule(dictionary_url);
139 base::RunLoop().RunUntilIdle();
140 EXPECT_EQ(1, JobsRequested());
141 std::vector<DictionaryAdditions> additions;
142 GetDictionaryAdditions(&additions);
143 ASSERT_EQ(1u, additions.size());
144 EXPECT_EQ(
145 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
146 additions[0].dictionary_text);
149 // Multiple fetches of the same URL should result in only one request.
150 TEST_F(SdchDictionaryFetcherTest, Multiple) {
151 GURL dictionary_url(PathToGurl("dictionary"));
152 fetcher()->Schedule(dictionary_url);
153 fetcher()->Schedule(dictionary_url);
154 fetcher()->Schedule(dictionary_url);
155 base::RunLoop().RunUntilIdle();
157 EXPECT_EQ(1, JobsRequested());
158 std::vector<DictionaryAdditions> additions;
159 GetDictionaryAdditions(&additions);
160 ASSERT_EQ(1u, additions.size());
161 EXPECT_EQ(
162 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
163 additions[0].dictionary_text);
166 // A cancel should result in no actual requests being generated.
167 TEST_F(SdchDictionaryFetcherTest, Cancel) {
168 GURL dictionary_url_1(PathToGurl("dictionary_1"));
169 GURL dictionary_url_2(PathToGurl("dictionary_2"));
170 GURL dictionary_url_3(PathToGurl("dictionary_3"));
172 fetcher()->Schedule(dictionary_url_1);
173 fetcher()->Schedule(dictionary_url_2);
174 fetcher()->Schedule(dictionary_url_3);
175 fetcher()->Cancel();
176 base::RunLoop().RunUntilIdle();
178 // Synchronous execution may have resulted in a single job being scheduled.
179 EXPECT_GE(1, JobsRequested());