OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "media/blink/resource_multibuffer_data_provider.h" | 5 #include "media/blink/resource_multibuffer_data_provider.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "media/base/media_log.h" | 18 #include "media/base/media_log.h" |
19 #include "media/base/seekable_buffer.h" | 19 #include "media/base/seekable_buffer.h" |
20 #include "media/blink/mock_weburlloader.h" | 20 #include "media/blink/mock_webassociatedurlloader.h" |
21 #include "media/blink/url_index.h" | 21 #include "media/blink/url_index.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/http/http_request_headers.h" | 23 #include "net/http/http_request_headers.h" |
24 #include "net/http/http_util.h" | 24 #include "net/http/http_util.h" |
25 #include "third_party/WebKit/public/platform/WebString.h" | 25 #include "third_party/WebKit/public/platform/WebString.h" |
26 #include "third_party/WebKit/public/platform/WebURLError.h" | 26 #include "third_party/WebKit/public/platform/WebURLError.h" |
27 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 27 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
28 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 28 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
29 #include "third_party/WebKit/public/web/WebFrameClient.h" | 29 #include "third_party/WebKit/public/web/WebFrameClient.h" |
30 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 30 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 base::Unretained(this))); | 97 base::Unretained(this))); |
98 | 98 |
99 first_position_ = first_position; | 99 first_position_ = first_position; |
100 | 100 |
101 std::unique_ptr<ResourceMultiBufferDataProvider> loader( | 101 std::unique_ptr<ResourceMultiBufferDataProvider> loader( |
102 new ResourceMultiBufferDataProvider(url_data_.get(), first_position_)); | 102 new ResourceMultiBufferDataProvider(url_data_.get(), first_position_)); |
103 loader_ = loader.get(); | 103 loader_ = loader.get(); |
104 url_data_->multibuffer()->AddProvider(std::move(loader)); | 104 url_data_->multibuffer()->AddProvider(std::move(loader)); |
105 | 105 |
106 // |test_loader_| will be used when Start() is called. | 106 // |test_loader_| will be used when Start() is called. |
107 url_loader_ = new NiceMock<MockWebURLLoader>(); | 107 url_loader_ = new NiceMock<MockWebAssociatedURLLoader>(); |
108 loader_->test_loader_ = std::unique_ptr<blink::WebURLLoader>(url_loader_); | 108 loader_->test_loader_ = |
| 109 std::unique_ptr<blink::WebAssociatedURLLoader>(url_loader_); |
109 } | 110 } |
110 | 111 |
111 void Start() { | 112 void Start() { |
112 InSequence s; | 113 InSequence s; |
113 EXPECT_CALL( | 114 EXPECT_CALL( |
114 *url_loader_, | 115 *url_loader_, |
115 loadAsynchronously(Truly(CorrectAcceptEncodingAndEtag), loader_)); | 116 loadAsynchronously(Truly(CorrectAcceptEncodingAndEtag), loader_)); |
116 | 117 |
117 loader_->Start(); | 118 loader_->Start(); |
118 } | 119 } |
119 | 120 |
120 void FullResponse(int64_t instance_size, bool ok = true) { | 121 void FullResponse(int64_t instance_size, bool ok = true) { |
121 WebURLResponse response(gurl_); | 122 WebURLResponse response(gurl_); |
122 response.setHTTPHeaderField( | 123 response.setHTTPHeaderField( |
123 WebString::fromUTF8("Content-Length"), | 124 WebString::fromUTF8("Content-Length"), |
124 WebString::fromUTF8(base::StringPrintf("%" PRId64, instance_size))); | 125 WebString::fromUTF8(base::StringPrintf("%" PRId64, instance_size))); |
125 response.setExpectedContentLength(instance_size); | 126 response.setExpectedContentLength(instance_size); |
126 response.setHTTPStatusCode(kHttpOK); | 127 response.setHTTPStatusCode(kHttpOK); |
127 loader_->didReceiveResponse(url_loader_, response); | 128 loader_->didReceiveResponse(response); |
128 | 129 |
129 if (ok) { | 130 if (ok) { |
130 EXPECT_EQ(instance_size, url_data_->length()); | 131 EXPECT_EQ(instance_size, url_data_->length()); |
131 } | 132 } |
132 | 133 |
133 EXPECT_FALSE(url_data_->range_supported()); | 134 EXPECT_FALSE(url_data_->range_supported()); |
134 } | 135 } |
135 | 136 |
136 void PartialResponse(int64_t first_position, | 137 void PartialResponse(int64_t first_position, |
137 int64_t last_position, | 138 int64_t last_position, |
(...skipping 24 matching lines...) Expand all Loading... |
162 } | 163 } |
163 response.setExpectedContentLength(content_length); | 164 response.setExpectedContentLength(content_length); |
164 | 165 |
165 // A server isn't required to return Accept-Ranges even though it might. | 166 // A server isn't required to return Accept-Ranges even though it might. |
166 if (accept_ranges) { | 167 if (accept_ranges) { |
167 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), | 168 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"), |
168 WebString::fromUTF8("bytes")); | 169 WebString::fromUTF8("bytes")); |
169 } | 170 } |
170 | 171 |
171 response.setHTTPStatusCode(kHttpPartialContent); | 172 response.setHTTPStatusCode(kHttpPartialContent); |
172 loader_->didReceiveResponse(url_loader_, response); | 173 loader_->didReceiveResponse(response); |
173 | 174 |
174 EXPECT_EQ(instance_size, url_data_->length()); | 175 EXPECT_EQ(instance_size, url_data_->length()); |
175 | 176 |
176 // A valid partial response should always result in this being true. | 177 // A valid partial response should always result in this being true. |
177 EXPECT_TRUE(url_data_->range_supported()); | 178 EXPECT_TRUE(url_data_->range_supported()); |
178 } | 179 } |
179 | 180 |
180 void Redirect(const char* url) { | 181 void Redirect(const char* url) { |
181 GURL redirectUrl(url); | 182 GURL redirectUrl(url); |
182 blink::WebURLRequest newRequest(redirectUrl); | 183 blink::WebURLRequest newRequest(redirectUrl); |
183 blink::WebURLResponse redirectResponse(gurl_); | 184 blink::WebURLResponse redirectResponse(gurl_); |
184 | 185 |
185 EXPECT_CALL(*this, RedirectCallback(_)) | 186 EXPECT_CALL(*this, RedirectCallback(_)) |
186 .WillOnce( | 187 .WillOnce( |
187 Invoke(this, &ResourceMultiBufferDataProviderTest::SetUrlData)); | 188 Invoke(this, &ResourceMultiBufferDataProviderTest::SetUrlData)); |
188 | 189 |
189 loader_->willFollowRedirect(url_loader_, newRequest, redirectResponse); | 190 loader_->willFollowRedirect(newRequest, redirectResponse); |
190 | 191 |
191 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
192 } | 193 } |
193 | 194 |
194 void StopWhenLoad() { | 195 void StopWhenLoad() { |
195 InSequence s; | 196 InSequence s; |
196 EXPECT_CALL(*url_loader_, cancel()); | 197 EXPECT_CALL(*url_loader_, cancel()); |
197 loader_ = nullptr; | 198 loader_ = nullptr; |
198 url_data_ = nullptr; | 199 url_data_ = nullptr; |
199 } | 200 } |
200 | 201 |
201 // Helper method to write to |loader_| from |data_|. | 202 // Helper method to write to |loader_| from |data_|. |
202 void WriteLoader(int position, int size) { | 203 void WriteLoader(int position, int size) { |
203 loader_->didReceiveData(url_loader_, | 204 loader_->didReceiveData(reinterpret_cast<char*>(data_ + position), size); |
204 reinterpret_cast<char*>(data_ + position), size, | |
205 size, size); | |
206 } | 205 } |
207 | 206 |
208 void WriteData(int size) { | 207 void WriteData(int size) { |
209 std::unique_ptr<char[]> data(new char[size]); | 208 std::unique_ptr<char[]> data(new char[size]); |
210 loader_->didReceiveData(url_loader_, data.get(), size, size, size); | 209 loader_->didReceiveData(data.get(), size); |
211 } | 210 } |
212 | 211 |
213 // Verifies that data in buffer[0...size] is equal to data_[pos...pos+size]. | 212 // Verifies that data in buffer[0...size] is equal to data_[pos...pos+size]. |
214 void VerifyBuffer(uint8_t* buffer, int pos, int size) { | 213 void VerifyBuffer(uint8_t* buffer, int pos, int size) { |
215 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); | 214 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); |
216 } | 215 } |
217 | 216 |
218 bool HasActiveLoader() { return loader_->active_loader_ != nullptr; } | 217 bool HasActiveLoader() { return loader_->active_loader_ != nullptr; } |
219 MOCK_METHOD1(RedirectCallback, void(const scoped_refptr<UrlData>&)); | 218 MOCK_METHOD1(RedirectCallback, void(const scoped_refptr<UrlData>&)); |
220 | 219 |
221 void SetUrlData(const scoped_refptr<UrlData>& new_url_data) { | 220 void SetUrlData(const scoped_refptr<UrlData>& new_url_data) { |
222 url_data_ = new_url_data; | 221 url_data_ = new_url_data; |
223 } | 222 } |
224 | 223 |
225 protected: | 224 protected: |
226 GURL gurl_; | 225 GURL gurl_; |
227 int64_t first_position_; | 226 int64_t first_position_; |
228 | 227 |
229 std::unique_ptr<UrlIndex> url_index_; | 228 std::unique_ptr<UrlIndex> url_index_; |
230 scoped_refptr<UrlData> url_data_; | 229 scoped_refptr<UrlData> url_data_; |
231 scoped_refptr<UrlData> redirected_to_; | 230 scoped_refptr<UrlData> redirected_to_; |
232 // The loader is owned by the UrlData above. | 231 // The loader is owned by the UrlData above. |
233 ResourceMultiBufferDataProvider* loader_; | 232 ResourceMultiBufferDataProvider* loader_; |
234 NiceMock<MockWebURLLoader>* url_loader_; | 233 NiceMock<MockWebAssociatedURLLoader>* url_loader_; |
235 | 234 |
236 blink::WebFrameClient client_; | 235 blink::WebFrameClient client_; |
237 WebView* view_; | 236 WebView* view_; |
238 | 237 |
239 base::MessageLoop message_loop_; | 238 base::MessageLoop message_loop_; |
240 | 239 |
241 uint8_t data_[kDataSize]; | 240 uint8_t data_[kDataSize]; |
242 | 241 |
243 private: | 242 private: |
244 DISALLOW_COPY_AND_ASSIGN(ResourceMultiBufferDataProviderTest); | 243 DISALLOW_COPY_AND_ASSIGN(ResourceMultiBufferDataProviderTest); |
245 }; | 244 }; |
246 | 245 |
247 TEST_F(ResourceMultiBufferDataProviderTest, StartStop) { | 246 TEST_F(ResourceMultiBufferDataProviderTest, StartStop) { |
248 Initialize(kHttpUrl, 0); | 247 Initialize(kHttpUrl, 0); |
249 Start(); | 248 Start(); |
250 StopWhenLoad(); | 249 StopWhenLoad(); |
251 } | 250 } |
252 | 251 |
253 // Tests that a bad HTTP response is recived, e.g. file not found. | 252 // Tests that a bad HTTP response is recived, e.g. file not found. |
254 TEST_F(ResourceMultiBufferDataProviderTest, BadHttpResponse) { | 253 TEST_F(ResourceMultiBufferDataProviderTest, BadHttpResponse) { |
255 Initialize(kHttpUrl, 0); | 254 Initialize(kHttpUrl, 0); |
256 Start(); | 255 Start(); |
257 | 256 |
258 EXPECT_CALL(*this, RedirectCallback(scoped_refptr<UrlData>(nullptr))); | 257 EXPECT_CALL(*this, RedirectCallback(scoped_refptr<UrlData>(nullptr))); |
259 | 258 |
260 WebURLResponse response(gurl_); | 259 WebURLResponse response(gurl_); |
261 response.setHTTPStatusCode(404); | 260 response.setHTTPStatusCode(404); |
262 response.setHTTPStatusText("Not Found\n"); | 261 response.setHTTPStatusText("Not Found\n"); |
263 loader_->didReceiveResponse(url_loader_, response); | 262 loader_->didReceiveResponse(response); |
264 } | 263 } |
265 | 264 |
266 // Tests that partial content is requested but not fulfilled. | 265 // Tests that partial content is requested but not fulfilled. |
267 TEST_F(ResourceMultiBufferDataProviderTest, NotPartialResponse) { | 266 TEST_F(ResourceMultiBufferDataProviderTest, NotPartialResponse) { |
268 Initialize(kHttpUrl, 100); | 267 Initialize(kHttpUrl, 100); |
269 Start(); | 268 Start(); |
270 FullResponse(1024, false); | 269 FullResponse(1024, false); |
271 } | 270 } |
272 | 271 |
273 // Tests that a 200 response is received. | 272 // Tests that a 200 response is received. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 EXPECT_CALL(*this, RedirectCallback(scoped_refptr<UrlData>(nullptr))); | 315 EXPECT_CALL(*this, RedirectCallback(scoped_refptr<UrlData>(nullptr))); |
317 | 316 |
318 WebURLResponse response(gurl_); | 317 WebURLResponse response(gurl_); |
319 response.setHTTPHeaderField( | 318 response.setHTTPHeaderField( |
320 WebString::fromUTF8("Content-Range"), | 319 WebString::fromUTF8("Content-Range"), |
321 WebString::fromUTF8(base::StringPrintf("bytes " | 320 WebString::fromUTF8(base::StringPrintf("bytes " |
322 "%d-%d/%d", | 321 "%d-%d/%d", |
323 1, 10, 1024))); | 322 1, 10, 1024))); |
324 response.setExpectedContentLength(10); | 323 response.setExpectedContentLength(10); |
325 response.setHTTPStatusCode(kHttpPartialContent); | 324 response.setHTTPStatusCode(kHttpPartialContent); |
326 loader_->didReceiveResponse(url_loader_, response); | 325 loader_->didReceiveResponse(response); |
327 } | 326 } |
328 | 327 |
329 TEST_F(ResourceMultiBufferDataProviderTest, TestRedirects) { | 328 TEST_F(ResourceMultiBufferDataProviderTest, TestRedirects) { |
330 // Test redirect. | 329 // Test redirect. |
331 Initialize(kHttpUrl, 0); | 330 Initialize(kHttpUrl, 0); |
332 Start(); | 331 Start(); |
333 Redirect(kHttpRedirect); | 332 Redirect(kHttpRedirect); |
334 FullResponse(1024); | 333 FullResponse(1024); |
335 StopWhenLoad(); | 334 StopWhenLoad(); |
336 } | 335 } |
337 | 336 |
338 } // namespace media | 337 } // namespace media |
OLD | NEW |