OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/browser/android/url_request_content_job.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/files/file_util.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/run_loop.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/test/test_file_util.h" | |
14 #include "net/url_request/url_request.h" | |
15 #include "net/url_request/url_request_test_util.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 | |
19 namespace content { | |
20 | |
21 namespace { | |
22 | |
23 // A URLRequestJobFactory that will return URLRequestContentJobWithCallbacks | |
24 // instances for content:// scheme URLs. | |
25 class CallbacksJobFactory : public net::URLRequestJobFactory { | |
26 public: | |
27 class JobObserver { | |
28 public: | |
29 virtual void OnJobCreated(URLRequestContentJob* job) = 0; | |
30 }; | |
31 | |
32 CallbacksJobFactory(const base::FilePath& path, JobObserver* observer) | |
33 : path_(path), observer_(observer) { | |
34 } | |
35 | |
36 ~CallbacksJobFactory() override {} | |
37 | |
38 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | |
39 const std::string& scheme, | |
40 net::URLRequest* request, | |
41 net::NetworkDelegate* network_delegate) const override { | |
42 URLRequestContentJob* job = | |
43 new URLRequestContentJob( | |
44 request, | |
45 network_delegate, | |
46 path_, | |
47 const_cast<base::MessageLoop*>(&message_loop_)->task_runner()); | |
48 observer_->OnJobCreated(job); | |
49 return job; | |
50 } | |
51 | |
52 net::URLRequestJob* MaybeInterceptRedirect( | |
53 net::URLRequest* request, | |
54 net::NetworkDelegate* network_delegate, | |
55 const GURL& location) const override { | |
56 return nullptr; | |
57 } | |
58 | |
59 net::URLRequestJob* MaybeInterceptResponse( | |
60 net::URLRequest* request, | |
61 net::NetworkDelegate* network_delegate) const override { | |
62 return nullptr; | |
63 } | |
64 | |
65 bool IsHandledProtocol(const std::string& scheme) const override { | |
66 return scheme == "content"; | |
67 } | |
68 | |
69 bool IsHandledURL(const GURL& url) const override { | |
70 return IsHandledProtocol(url.scheme()); | |
71 } | |
72 | |
73 bool IsSafeRedirectTarget(const GURL& location) const override { | |
74 return false; | |
75 } | |
76 | |
77 private: | |
78 base::MessageLoop message_loop_; | |
79 base::FilePath path_; | |
80 JobObserver* observer_; | |
81 }; | |
82 | |
83 class JobObserverImpl : public CallbacksJobFactory::JobObserver { | |
84 public: | |
85 void OnJobCreated(URLRequestContentJob* job) override { | |
86 jobs_.push_back(job); | |
87 } | |
88 | |
89 typedef std::vector<scoped_refptr<URLRequestContentJob> > JobList; | |
90 | |
91 const JobList& jobs() { return jobs_; } | |
92 | |
93 protected: | |
94 JobList jobs_; | |
95 }; | |
96 | |
97 // A simple holder for start/end used in http range requests. | |
98 struct Range { | |
99 int start; | |
100 int end; | |
101 | |
102 Range() { | |
no sievers
2014/11/25 20:50:18
nit: might as well remove this and explicitly run
qinmin
2014/11/25 23:37:02
Done.
| |
103 start = 0; | |
104 end = 0; | |
105 } | |
106 | |
107 Range(int start, int end) { | |
108 this->start = start; | |
109 this->end = end; | |
110 } | |
111 }; | |
112 | |
113 // A superclass for tests of the OnSeekComplete / OnReadComplete functions of | |
114 // URLRequestContentJob. | |
115 class URLRequestContentJobTest : public testing::Test { | |
116 public: | |
117 URLRequestContentJobTest(); | |
118 | |
119 protected: | |
120 // This inserts an image file into the android MediaStore and retrieves the | |
121 // content URI. Then creates and runs a URLRequestContentJob to get the | |
122 // contents out of it. If a Range is provided, this function will add the | |
123 // appropriate Range http header to the request and verify the bytes | |
124 // retrieved. | |
125 void RunRequest(const Range* range); | |
126 | |
127 JobObserverImpl observer_; | |
128 net::TestURLRequestContext context_; | |
129 net::TestDelegate delegate_; | |
130 }; | |
131 | |
132 URLRequestContentJobTest::URLRequestContentJobTest() {} | |
133 | |
134 void URLRequestContentJobTest::RunRequest(const Range* range) { | |
135 base::FilePath test_dir; | |
136 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); | |
137 test_dir = test_dir.AppendASCII("content"); | |
138 test_dir = test_dir.AppendASCII("test"); | |
139 test_dir = test_dir.AppendASCII("data"); | |
140 test_dir = test_dir.AppendASCII("android"); | |
141 ASSERT_TRUE(base::PathExists(test_dir)); | |
142 base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png")); | |
143 | |
144 // Insert the image into MediaStore. MediaStore will do some conversions, and | |
145 // return the content URI. | |
146 base::FilePath path = base::InsertImageIntoMediaStore(image_file); | |
147 EXPECT_TRUE(path.IsContentUri()); | |
148 EXPECT_TRUE(base::PathExists(path)); | |
149 int64 file_size; | |
150 EXPECT_TRUE(base::GetFileSize(path, &file_size)); | |
151 EXPECT_LT(0, file_size); | |
152 CallbacksJobFactory factory(path, &observer_); | |
153 context_.set_job_factory(&factory); | |
154 | |
155 scoped_ptr<net::URLRequest> request(context_.CreateRequest( | |
156 GURL(path.value()), net::DEFAULT_PRIORITY, &delegate_, NULL)); | |
157 int expected_length = file_size; | |
158 if (range) { | |
159 ASSERT_GE(range->start, 0); | |
160 ASSERT_GE(range->end, 0); | |
161 ASSERT_LE(range->start, range->end); | |
162 std::string range_value = | |
163 base::StringPrintf("bytes=%d-%d", range->start, range->end); | |
164 request->SetExtraRequestHeaderByName( | |
165 net::HttpRequestHeaders::kRange, range_value, true /*overwrite*/); | |
166 if (range->start <= file_size) | |
167 expected_length = | |
168 std::min(range->end, (int) file_size - 1) - range->start + 1; | |
no sievers
2014/11/25 20:50:18
nit: bad-style cast, you can just do file_size - 1
qinmin
2014/11/25 23:37:02
range->end is an int, so need to cast file_size or
| |
169 else | |
170 expected_length = 0; | |
171 } | |
172 request->Start(); | |
173 | |
174 base::RunLoop loop; | |
175 loop.Run(); | |
176 | |
177 EXPECT_FALSE(delegate_.request_failed()); | |
178 ASSERT_EQ(observer_.jobs().size(), 1u); | |
179 EXPECT_EQ(delegate_.bytes_received(), expected_length); | |
180 } | |
181 | |
182 TEST_F(URLRequestContentJobTest, ContentURIWithoutRange) { | |
183 RunRequest(NULL); | |
184 } | |
185 | |
186 TEST_F(URLRequestContentJobTest, ContentURIWithSmallRange) { | |
187 Range range(1, 10); | |
188 RunRequest(&range); | |
189 } | |
190 | |
191 TEST_F(URLRequestContentJobTest, ContentURIWithLargeRange) { | |
192 Range range(1, 100000); | |
193 RunRequest(&range); | |
194 } | |
195 | |
196 } // namespace | |
197 | |
198 } // namespace content | |
OLD | NEW |