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

Side by Side Diff: net/url_request/url_request_file_job_unittest.cc

Issue 739033003: Support content scheme uri for Chrome on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressing comments Created 6 years, 1 month 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/url_request/url_request_file_job.h" 5 #include "net/url_request/url_request_file_job.h"
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/threading/sequenced_worker_pool.h" 12 #include "base/threading/sequenced_worker_pool.h"
13 #include "net/base/filename_util.h" 13 #include "net/base/filename_util.h"
14 #include "net/base/net_util.h" 14 #include "net/base/net_util.h"
15 #include "net/url_request/url_request.h" 15 #include "net/url_request/url_request.h"
16 #include "net/url_request/url_request_test_util.h" 16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 #if defined(OS_ANDROID)
20 #include "base/test/test_file_util.h"
21 #endif
22
19 namespace net { 23 namespace net {
20 24
21 namespace { 25 namespace {
22 26
23 // A URLRequestFileJob for testing OnSeekComplete / OnReadComplete callbacks. 27 // A URLRequestFileJob for testing OnSeekComplete / OnReadComplete callbacks.
24 class URLRequestFileJobWithCallbacks : public URLRequestFileJob { 28 class URLRequestFileJobWithCallbacks : public URLRequestFileJob {
25 public: 29 public:
26 URLRequestFileJobWithCallbacks( 30 URLRequestFileJobWithCallbacks(
27 URLRequest* request, 31 URLRequest* request,
28 NetworkDelegate* network_delegate, 32 NetworkDelegate* network_delegate,
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 return nullptr; 93 return nullptr;
90 } 94 }
91 95
92 net::URLRequestJob* MaybeInterceptResponse( 96 net::URLRequestJob* MaybeInterceptResponse(
93 net::URLRequest* request, 97 net::URLRequest* request,
94 net::NetworkDelegate* network_delegate) const override { 98 net::NetworkDelegate* network_delegate) const override {
95 return nullptr; 99 return nullptr;
96 } 100 }
97 101
98 bool IsHandledProtocol(const std::string& scheme) const override { 102 bool IsHandledProtocol(const std::string& scheme) const override {
103 #if defined(OS_ANDROID)
104 return scheme == "file" || scheme == "content";
105 #endif
99 return scheme == "file"; 106 return scheme == "file";
100 } 107 }
101 108
102 bool IsHandledURL(const GURL& url) const override { 109 bool IsHandledURL(const GURL& url) const override {
103 return IsHandledProtocol(url.scheme()); 110 return IsHandledProtocol(url.scheme());
104 } 111 }
105 112
106 bool IsSafeRedirectTarget(const GURL& location) const override { 113 bool IsSafeRedirectTarget(const GURL& location) const override {
107 return false; 114 return false;
108 } 115 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 172
166 protected: 173 protected:
167 // This creates a file with |content| as the contents, and then creates and 174 // This creates a file with |content| as the contents, and then creates and
168 // runs a URLRequestFileJobWithCallbacks job to get the contents out of it, 175 // runs a URLRequestFileJobWithCallbacks job to get the contents out of it,
169 // and makes sure that the callbacks observed the correct bytes. If a Range 176 // and makes sure that the callbacks observed the correct bytes. If a Range
170 // is provided, this function will add the appropriate Range http header to 177 // is provided, this function will add the appropriate Range http header to
171 // the request and verify that only the bytes in that range (inclusive) were 178 // the request and verify that only the bytes in that range (inclusive) were
172 // observed. 179 // observed.
173 void RunRequest(const std::string& content, const Range* range); 180 void RunRequest(const std::string& content, const Range* range);
174 181
182
183 // Helper methods called by RunRequest to run the url request and verify
184 // the returned results.
185 void RunRequestInternal(const base::FilePath& path,
186 const Range* range);
187 void VerifyResult(const std::string& content,
188 const Range* range);
189
175 JobObserverImpl observer_; 190 JobObserverImpl observer_;
176 TestURLRequestContext context_; 191 TestURLRequestContext context_;
177 TestDelegate delegate_; 192 TestDelegate delegate_;
178 }; 193 };
179 194
180 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} 195 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {}
181 196
182 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, 197 void URLRequestFileJobEventsTest::RunRequest(const std::string& content,
183 const Range* range) { 198 const Range* range) {
184 base::ScopedTempDir directory; 199 base::ScopedTempDir directory;
185 ASSERT_TRUE(directory.CreateUniqueTempDir()); 200 ASSERT_TRUE(directory.CreateUniqueTempDir());
186 base::FilePath path; 201 base::FilePath path;
187 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); 202 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path));
203 if (range)
204 ASSERT_LT(static_cast<unsigned int>(range->end), content.length());
asanka 2014/11/21 02:22:02 Why not test the behavior when a range request ext
205 RunRequestInternal(path, range);
206 VerifyResult(content, range);
207 }
208
209 void URLRequestFileJobEventsTest::RunRequestInternal(
210 const base::FilePath& path,
211 const Range* range) {
188 CallbacksJobFactory factory(path, &observer_); 212 CallbacksJobFactory factory(path, &observer_);
189 context_.set_job_factory(&factory); 213 context_.set_job_factory(&factory);
190 214
191 scoped_ptr<URLRequest> request(context_.CreateRequest( 215 scoped_ptr<URLRequest> request(context_.CreateRequest(
192 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_, NULL)); 216 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_, NULL));
193 if (range) { 217 if (range) {
194 ASSERT_GE(range->start, 0); 218 ASSERT_GE(range->start, 0);
195 ASSERT_GE(range->end, 0); 219 ASSERT_GE(range->end, 0);
196 ASSERT_LE(range->start, range->end); 220 ASSERT_LE(range->start, range->end);
197 ASSERT_LT(static_cast<unsigned int>(range->end), content.length());
198 std::string range_value = 221 std::string range_value =
199 base::StringPrintf("bytes=%d-%d", range->start, range->end); 222 base::StringPrintf("bytes=%d-%d", range->start, range->end);
200 request->SetExtraRequestHeaderByName( 223 request->SetExtraRequestHeaderByName(
201 HttpRequestHeaders::kRange, range_value, true /*overwrite*/); 224 HttpRequestHeaders::kRange, range_value, true /*overwrite*/);
202 } 225 }
203 request->Start(); 226 request->Start();
204 227
205 base::RunLoop loop; 228 base::RunLoop loop;
206 loop.Run(); 229 loop.Run();
230 }
231
232 void URLRequestFileJobEventsTest::VerifyResult(const std::string& content,
233 const Range* range) {
207 234
asanka 2014/11/21 02:22:02 extra whitespace.
208 EXPECT_FALSE(delegate_.request_failed()); 235 EXPECT_FALSE(delegate_.request_failed());
209 int expected_length = 236 int expected_length =
210 range ? (range->end - range->start + 1) : content.length(); 237 range ? (range->end - range->start + 1) : content.length();
211 EXPECT_EQ(delegate_.bytes_received(), expected_length); 238 EXPECT_EQ(delegate_.bytes_received(), expected_length);
212 239
213 std::string expected_content; 240 std::string expected_content;
214 if (range) { 241 if (range) {
215 expected_content.insert(0, content, range->start, expected_length); 242 expected_content.insert(0, content, range->start, expected_length);
216 } else { 243 } else {
217 expected_content = content; 244 expected_content = content;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 } 284 }
258 285
259 TEST_F(URLRequestFileJobEventsTest, Range) { 286 TEST_F(URLRequestFileJobEventsTest, Range) {
260 // Use a 15KB content file and read a range chosen somewhat arbitrarily but 287 // Use a 15KB content file and read a range chosen somewhat arbitrarily but
261 // not aligned on any likely page boundaries. 288 // not aligned on any likely page boundaries.
262 int size = 15 * 1024; 289 int size = 15 * 1024;
263 Range range(1701, (6 * 1024) + 3); 290 Range range(1701, (6 * 1024) + 3);
264 RunRequest(MakeContentOfSize(size), &range); 291 RunRequest(MakeContentOfSize(size), &range);
265 } 292 }
266 293
294 #if defined(OS_ANDROID)
295 TEST_F(URLRequestFileJobEventsTest, ContentUriFile) {
296 base::FilePath test_dir;
297 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir);
298 test_dir = test_dir.AppendASCII("net");
299 test_dir = test_dir.AppendASCII("data");
300 test_dir = test_dir.AppendASCII("url_request_unittest");
301 ASSERT_TRUE(base::PathExists(test_dir));
302 base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png"));
303
304 // Insert the image into MediaStore. MediaStore will do some conversions, and
305 // return the content URI.
306 base::FilePath path = base::InsertImageIntoMediaStore(image_file);
307 EXPECT_TRUE(path.IsContentUri());
308 EXPECT_TRUE(base::PathExists(path));
309 int64 file_size;
310 EXPECT_TRUE(base::GetFileSize(path, &file_size));
311 EXPECT_LT(0, file_size);
312
313 RunRequestInternal(path, NULL);
asanka 2014/11/21 02:22:02 This doesn't run the test with a content:// URI, d
314 EXPECT_FALSE(delegate_.request_failed());
315 EXPECT_EQ(delegate_.bytes_received(), file_size);
asanka 2014/11/21 02:22:02 Assuming the test will still look like this, the r
316 }
317 #endif
318
267 } // namespace 319 } // namespace
268 320
269 } // namespace net 321 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698