| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/test/test_timeouts.h" |
| 8 #include "base/format_macros.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "base/stringprintf.h" | |
| 12 #include "media/base/filters.h" | |
| 13 #include "media/base/mock_filter_host.h" | 8 #include "media/base/mock_filter_host.h" |
| 14 #include "media/base/mock_filters.h" | 9 #include "media/base/mock_filters.h" |
| 15 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 16 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | |
| 17 #include "third_party/WebKit/WebKit/chromium/public/WebFrameClient.h" | |
| 18 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" | |
| 19 #include "third_party/WebKit/WebKit/chromium/public/WebURLError.h" | 11 #include "third_party/WebKit/WebKit/chromium/public/WebURLError.h" |
| 20 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h" | |
| 21 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" | |
| 22 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" |
| 23 #include "third_party/WebKit/WebKit/chromium/public/WebView.h" | |
| 24 #include "webkit/glue/media/buffered_data_source.h" | 13 #include "webkit/glue/media/buffered_data_source.h" |
| 25 #include "webkit/glue/mock_webframe.h" | 14 #include "webkit/glue/mock_webframe.h" |
| 26 #include "webkit/glue/mock_weburlloader_impl.h" | |
| 27 | 15 |
| 28 using ::testing::_; | 16 using ::testing::_; |
| 29 using ::testing::Assign; | 17 using ::testing::Assign; |
| 30 using ::testing::AtLeast; | 18 using ::testing::AtLeast; |
| 31 using ::testing::DeleteArg; | 19 using ::testing::DeleteArg; |
| 32 using ::testing::DoAll; | 20 using ::testing::DoAll; |
| 33 using ::testing::InSequence; | 21 using ::testing::InSequence; |
| 34 using ::testing::Invoke; | 22 using ::testing::Invoke; |
| 35 using ::testing::InvokeWithoutArgs; | 23 using ::testing::InvokeWithoutArgs; |
| 36 using ::testing::NotNull; | 24 using ::testing::NotNull; |
| 37 using ::testing::Return; | 25 using ::testing::Return; |
| 38 using ::testing::ReturnRef; | 26 using ::testing::ReturnRef; |
| 39 using ::testing::SetArgumentPointee; | 27 using ::testing::SetArgumentPointee; |
| 40 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
| 41 using ::testing::NiceMock; | 29 using ::testing::NiceMock; |
| 42 using ::testing::WithArgs; | 30 using ::testing::WithArgs; |
| 43 | 31 |
| 44 using WebKit::WebURLError; | |
| 45 using WebKit::WebFrame; | |
| 46 using WebKit::WebFrameClient; | |
| 47 using WebKit::WebString; | |
| 48 using WebKit::WebURLLoader; | |
| 49 using WebKit::WebURLRequest; | |
| 50 using WebKit::WebURLResponse; | |
| 51 using WebKit::WebView; | |
| 52 | |
| 53 namespace { | 32 namespace { |
| 54 | 33 |
| 55 const char* kHttpUrl = "http://test"; | 34 const char* kHttpUrl = "http://test"; |
| 56 const char* kFileUrl = "file://test"; | 35 const char* kFileUrl = "file://test"; |
| 57 const int kDataSize = 1024; | 36 const int kDataSize = 1024; |
| 58 const int kHttpOK = 200; | |
| 59 const int kHttpPartialContent = 206; | |
| 60 | 37 |
| 61 enum NetworkState { | 38 enum NetworkState { |
| 62 NONE, | 39 NONE, |
| 63 LOADED, | 40 LOADED, |
| 64 LOADING | 41 LOADING |
| 65 }; | 42 }; |
| 66 | 43 |
| 67 } // namespace | 44 } // namespace |
| 68 | 45 |
| 69 namespace webkit_glue { | 46 namespace webkit_glue { |
| 70 | 47 |
| 71 // Submit a request completed event to the resource loader due to request | 48 // A mock BufferedDataSource to inject mock BufferedResourceLoader through |
| 72 // being canceled. Pretending the event is from external. | 49 // CreateResourceLoader() method. |
| 73 ACTION_P(RequestCanceled, loader) { | 50 class MockBufferedDataSource : public BufferedDataSource { |
| 74 WebURLError error; | |
| 75 error.reason = net::ERR_ABORTED; | |
| 76 error.domain = WebString::fromUTF8(net::kErrorDomain); | |
| 77 loader->didFail(NULL, error); | |
| 78 } | |
| 79 | |
| 80 class BufferedResourceLoaderTest : public testing::Test { | |
| 81 public: | 51 public: |
| 82 BufferedResourceLoaderTest() { | 52 MockBufferedDataSource( |
| 83 url_loader_ = new NiceMock<MockWebURLLoader>(); | 53 MessageLoop* message_loop, WebFrame* frame) |
| 84 | 54 : BufferedDataSource(message_loop, frame) { |
| 85 for (int i = 0; i < kDataSize; ++i) | |
| 86 data_[i] = i; | |
| 87 } | 55 } |
| 88 | 56 |
| 89 virtual ~BufferedResourceLoaderTest() { | 57 virtual base::TimeDelta GetTimeoutMilliseconds() { |
| 90 ignore_result(frame_.release()); | 58 return base::TimeDelta::FromMilliseconds( |
| 59 TestTimeouts::short_timeout_ms()); |
| 91 } | 60 } |
| 92 | 61 |
| 93 void Initialize(const char* url, int first_position, int last_position) { | 62 MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*( |
| 94 gurl_ = GURL(url); | 63 int64 first_position, int64 last_position)); |
| 95 first_position_ = first_position; | |
| 96 last_position_ = last_position; | |
| 97 | |
| 98 frame_.reset(new NiceMock<MockWebFrame>()); | |
| 99 | |
| 100 loader_ = new BufferedResourceLoader(gurl_, | |
| 101 first_position_, last_position_); | |
| 102 loader_->SetURLLoaderForTest(url_loader_); | |
| 103 } | |
| 104 | |
| 105 void SetLoaderBuffer(size_t forward_capacity, size_t backward_capacity) { | |
| 106 loader_->buffer_.reset( | |
| 107 new media::SeekableBuffer(backward_capacity, forward_capacity)); | |
| 108 } | |
| 109 | |
| 110 void Start() { | |
| 111 InSequence s; | |
| 112 EXPECT_CALL(*url_loader_, loadAsynchronously(_, loader_.get())); | |
| 113 loader_->Start( | |
| 114 NewCallback(this, &BufferedResourceLoaderTest::StartCallback), | |
| 115 NewCallback(this, &BufferedResourceLoaderTest::NetworkCallback), | |
| 116 frame_.get()); | |
| 117 } | |
| 118 | |
| 119 void FullResponse(int64 instance_size) { | |
| 120 EXPECT_CALL(*this, StartCallback(net::OK)); | |
| 121 | |
| 122 WebURLResponse response(gurl_); | |
| 123 response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"), | |
| 124 WebString::fromUTF8(base::StringPrintf("%" | |
| 125 PRId64, instance_size))); | |
| 126 response.setExpectedContentLength(instance_size); | |
| 127 response.setHTTPStatusCode(kHttpOK); | |
| 128 loader_->didReceiveResponse(url_loader_, response); | |
| 129 EXPECT_EQ(instance_size, loader_->content_length()); | |
| 130 EXPECT_EQ(instance_size, loader_->instance_size()); | |
| 131 EXPECT_FALSE(loader_->partial_response()); | |
| 132 } | |
| 133 | |
| 134 void PartialResponse(int64 first_position, int64 last_position, | |
| 135 int64 instance_size) { | |
| 136 EXPECT_CALL(*this, StartCallback(net::OK)); | |
| 137 int64 content_length = last_position - first_position + 1; | |
| 138 | |
| 139 WebURLResponse response(gurl_); | |
| 140 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), | |
| 141 WebString::fromUTF8(base::StringPrintf("bytes " | |
| 142 "%" PRId64 "-%" PRId64 "/%" PRId64, | |
| 143 first_position, | |
| 144 last_position, | |
| 145 instance_size))); | |
| 146 response.setExpectedContentLength(content_length); | |
| 147 response.setHTTPStatusCode(kHttpPartialContent); | |
| 148 loader_->didReceiveResponse(url_loader_, response); | |
| 149 EXPECT_EQ(content_length, loader_->content_length()); | |
| 150 EXPECT_EQ(instance_size, loader_->instance_size()); | |
| 151 EXPECT_TRUE(loader_->partial_response()); | |
| 152 } | |
| 153 | |
| 154 void StopWhenLoad() { | |
| 155 InSequence s; | |
| 156 EXPECT_CALL(*url_loader_, cancel()) | |
| 157 .WillOnce(RequestCanceled(loader_)); | |
| 158 loader_->Stop(); | |
| 159 } | |
| 160 | |
| 161 // Helper method to write to |loader_| from |data_|. | |
| 162 void WriteLoader(int position, int size) { | |
| 163 EXPECT_CALL(*this, NetworkCallback()) | |
| 164 .RetiresOnSaturation(); | |
| 165 loader_->didReceiveData(url_loader_, | |
| 166 reinterpret_cast<char*>(data_ + position), size); | |
| 167 } | |
| 168 | |
| 169 // Helper method to read from |loader_|. | |
| 170 void ReadLoader(int64 position, int size, uint8* buffer) { | |
| 171 loader_->Read(position, size, buffer, | |
| 172 NewCallback(this, &BufferedResourceLoaderTest::ReadCallback)); | |
| 173 } | |
| 174 | |
| 175 // Verifis that data in buffer[0...size] is equal to data_[pos...pos+size]. | |
| 176 void VerifyBuffer(uint8* buffer, int pos, int size) { | |
| 177 EXPECT_EQ(0, memcmp(buffer, data_ + pos, size)); | |
| 178 } | |
| 179 | |
| 180 // Helper method to disallow deferring in |loader_|. | |
| 181 void DisallowLoaderDefer() { | |
| 182 if (loader_->deferred_) { | |
| 183 EXPECT_CALL(*url_loader_, setDefersLoading(false)); | |
| 184 EXPECT_CALL(*this, NetworkCallback()); | |
| 185 } | |
| 186 loader_->SetAllowDefer(false); | |
| 187 } | |
| 188 | |
| 189 // Helper method to allow deferring in |loader_|. | |
| 190 void AllowLoaderDefer() { | |
| 191 loader_->SetAllowDefer(true); | |
| 192 } | |
| 193 | |
| 194 MOCK_METHOD1(StartCallback, void(int error)); | |
| 195 MOCK_METHOD1(ReadCallback, void(int error)); | |
| 196 MOCK_METHOD0(NetworkCallback, void()); | |
| 197 | |
| 198 protected: | |
| 199 GURL gurl_; | |
| 200 int64 first_position_; | |
| 201 int64 last_position_; | |
| 202 | |
| 203 scoped_refptr<BufferedResourceLoader> loader_; | |
| 204 NiceMock<MockWebURLLoader>* url_loader_; | |
| 205 scoped_ptr<NiceMock<MockWebFrame> > frame_; | |
| 206 | |
| 207 uint8 data_[kDataSize]; | |
| 208 | 64 |
| 209 private: | 65 private: |
| 210 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest); | 66 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource); |
| 211 }; | 67 }; |
| 212 | 68 |
| 213 TEST_F(BufferedResourceLoaderTest, StartStop) { | |
| 214 Initialize(kHttpUrl, -1, -1); | |
| 215 Start(); | |
| 216 StopWhenLoad(); | |
| 217 } | |
| 218 | |
| 219 // Tests that a bad HTTP response is recived, e.g. file not found. | |
| 220 TEST_F(BufferedResourceLoaderTest, BadHttpResponse) { | |
| 221 Initialize(kHttpUrl, -1, -1); | |
| 222 Start(); | |
| 223 | |
| 224 EXPECT_CALL(*this, StartCallback(net::ERR_FAILED)); | |
| 225 EXPECT_CALL(*url_loader_, cancel()) | |
| 226 .WillOnce(RequestCanceled(loader_)); | |
| 227 | |
| 228 WebURLResponse response(gurl_); | |
| 229 response.setHTTPStatusCode(404); | |
| 230 response.setHTTPStatusText("Not Found\n"); | |
| 231 loader_->didReceiveResponse(url_loader_, response); | |
| 232 } | |
| 233 | |
| 234 // Tests that partial content is requested but not fulfilled. | |
| 235 TEST_F(BufferedResourceLoaderTest, NotPartialResponse) { | |
| 236 Initialize(kHttpUrl, 100, -1); | |
| 237 Start(); | |
| 238 FullResponse(1024); | |
| 239 StopWhenLoad(); | |
| 240 } | |
| 241 | |
| 242 // Tests that a 200 response is received. | |
| 243 TEST_F(BufferedResourceLoaderTest, FullResponse) { | |
| 244 Initialize(kHttpUrl, -1, -1); | |
| 245 Start(); | |
| 246 FullResponse(1024); | |
| 247 StopWhenLoad(); | |
| 248 } | |
| 249 | |
| 250 // Tests that a partial content response is received. | |
| 251 TEST_F(BufferedResourceLoaderTest, PartialResponse) { | |
| 252 Initialize(kHttpUrl, 100, 200); | |
| 253 Start(); | |
| 254 PartialResponse(100, 200, 1024); | |
| 255 StopWhenLoad(); | |
| 256 } | |
| 257 | |
| 258 // Tests that an invalid partial response is received. | |
| 259 TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) { | |
| 260 Initialize(kHttpUrl, 0, 10); | |
| 261 Start(); | |
| 262 | |
| 263 EXPECT_CALL(*this, StartCallback(net::ERR_INVALID_RESPONSE)); | |
| 264 EXPECT_CALL(*url_loader_, cancel()) | |
| 265 .WillOnce(RequestCanceled(loader_)); | |
| 266 | |
| 267 WebURLResponse response(gurl_); | |
| 268 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"), | |
| 269 WebString::fromUTF8(base::StringPrintf("bytes " | |
| 270 "%d-%d/%d", 1, 10, 1024))); | |
| 271 response.setExpectedContentLength(10); | |
| 272 response.setHTTPStatusCode(kHttpPartialContent); | |
| 273 loader_->didReceiveResponse(url_loader_, response); | |
| 274 } | |
| 275 | |
| 276 // Tests the logic of sliding window for data buffering and reading. | |
| 277 TEST_F(BufferedResourceLoaderTest, BufferAndRead) { | |
| 278 Initialize(kHttpUrl, 10, 29); | |
| 279 Start(); | |
| 280 PartialResponse(10, 29, 30); | |
| 281 | |
| 282 uint8 buffer[10]; | |
| 283 InSequence s; | |
| 284 | |
| 285 // Writes 10 bytes and read them back. | |
| 286 WriteLoader(10, 10); | |
| 287 EXPECT_CALL(*this, ReadCallback(10)); | |
| 288 ReadLoader(10, 10, buffer); | |
| 289 VerifyBuffer(buffer, 10, 10); | |
| 290 | |
| 291 // Writes 10 bytes and read 2 times. | |
| 292 WriteLoader(20, 10); | |
| 293 EXPECT_CALL(*this, ReadCallback(5)); | |
| 294 ReadLoader(20, 5, buffer); | |
| 295 VerifyBuffer(buffer, 20, 5); | |
| 296 EXPECT_CALL(*this, ReadCallback(5)); | |
| 297 ReadLoader(25, 5, buffer); | |
| 298 VerifyBuffer(buffer, 25, 5); | |
| 299 | |
| 300 // Read backward within buffer. | |
| 301 EXPECT_CALL(*this, ReadCallback(10)); | |
| 302 ReadLoader(10, 10, buffer); | |
| 303 VerifyBuffer(buffer, 10, 10); | |
| 304 | |
| 305 // Read backward outside buffer. | |
| 306 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | |
| 307 ReadLoader(9, 10, buffer); | |
| 308 | |
| 309 // Response has completed. | |
| 310 EXPECT_CALL(*this, NetworkCallback()); | |
| 311 loader_->didFinishLoading(url_loader_, 0); | |
| 312 | |
| 313 // Try to read 10 from position 25 will just return with 5 bytes. | |
| 314 EXPECT_CALL(*this, ReadCallback(5)); | |
| 315 ReadLoader(25, 10, buffer); | |
| 316 VerifyBuffer(buffer, 25, 5); | |
| 317 | |
| 318 // Try to read outside buffered range after request has completed. | |
| 319 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | |
| 320 ReadLoader(5, 10, buffer); | |
| 321 | |
| 322 // Try to read beyond the instance size. | |
| 323 EXPECT_CALL(*this, ReadCallback(0)); | |
| 324 ReadLoader(30, 10, buffer); | |
| 325 } | |
| 326 | |
| 327 TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) { | |
| 328 Initialize(kHttpUrl, 10, 0x00FFFFFF); | |
| 329 Start(); | |
| 330 PartialResponse(10, 0x00FFFFFF, 0x01000000); | |
| 331 | |
| 332 uint8 buffer[10]; | |
| 333 InSequence s; | |
| 334 | |
| 335 // Read very far aheard will get a cache miss. | |
| 336 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | |
| 337 ReadLoader(0x00FFFFFF, 1, buffer); | |
| 338 | |
| 339 // The following call will not call ReadCallback() because it is waiting for | |
| 340 // data to arrive. | |
| 341 ReadLoader(10, 10, buffer); | |
| 342 | |
| 343 // Writing to loader will fulfill the read request. | |
| 344 EXPECT_CALL(*this, ReadCallback(10)); | |
| 345 WriteLoader(10, 20); | |
| 346 VerifyBuffer(buffer, 10, 10); | |
| 347 | |
| 348 // The following call cannot be fulfilled now. | |
| 349 ReadLoader(25, 10, buffer); | |
| 350 | |
| 351 EXPECT_CALL(*this, ReadCallback(5)); | |
| 352 EXPECT_CALL(*this, NetworkCallback()); | |
| 353 loader_->didFinishLoading(url_loader_, 0); | |
| 354 } | |
| 355 | |
| 356 TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) { | |
| 357 Initialize(kHttpUrl, 10, 29); | |
| 358 Start(); | |
| 359 PartialResponse(10, 29, 30); | |
| 360 | |
| 361 uint8 buffer[10]; | |
| 362 InSequence s; | |
| 363 | |
| 364 ReadLoader(10, 10, buffer); | |
| 365 EXPECT_CALL(*this, ReadCallback(net::ERR_FAILED)); | |
| 366 EXPECT_CALL(*this, NetworkCallback()); | |
| 367 WebURLError error; | |
| 368 error.reason = net::ERR_FAILED; | |
| 369 loader_->didFail(url_loader_, error); | |
| 370 } | |
| 371 | |
| 372 // Tests the logic of caching data to disk when media is paused. | |
| 373 TEST_F(BufferedResourceLoaderTest, AllowDefer_NoDataReceived) { | |
| 374 Initialize(kHttpUrl, 10, 99); | |
| 375 SetLoaderBuffer(10, 20); | |
| 376 Start(); | |
| 377 PartialResponse(10, 99, 100); | |
| 378 | |
| 379 // Start in undeferred state, then disallow defer, then allow defer | |
| 380 // without receiving data in between. | |
| 381 DisallowLoaderDefer(); | |
| 382 AllowLoaderDefer(); | |
| 383 StopWhenLoad(); | |
| 384 } | |
| 385 | |
| 386 TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadSameWindow) { | |
| 387 Initialize(kHttpUrl, 10, 99); | |
| 388 SetLoaderBuffer(10, 20); | |
| 389 Start(); | |
| 390 PartialResponse(10, 99, 100); | |
| 391 | |
| 392 uint8 buffer[10]; | |
| 393 | |
| 394 // Start in undeferred state, disallow defer, receive data but don't shift | |
| 395 // buffer window, then allow defer and read. | |
| 396 DisallowLoaderDefer(); | |
| 397 WriteLoader(10, 10); | |
| 398 AllowLoaderDefer(); | |
| 399 | |
| 400 EXPECT_CALL(*this, ReadCallback(10)); | |
| 401 ReadLoader(10, 10, buffer); | |
| 402 VerifyBuffer(buffer, 10, 10); | |
| 403 StopWhenLoad(); | |
| 404 } | |
| 405 | |
| 406 TEST_F(BufferedResourceLoaderTest, AllowDefer_ReadPastWindow) { | |
| 407 Initialize(kHttpUrl, 10, 99); | |
| 408 SetLoaderBuffer(10, 20); | |
| 409 Start(); | |
| 410 PartialResponse(10, 99, 100); | |
| 411 | |
| 412 uint8 buffer[10]; | |
| 413 | |
| 414 // Not deferred, disallow defer, received data and shift buffer window, | |
| 415 // allow defer, then read in area outside of buffer window. | |
| 416 DisallowLoaderDefer(); | |
| 417 WriteLoader(10, 10); | |
| 418 WriteLoader(20, 50); | |
| 419 AllowLoaderDefer(); | |
| 420 | |
| 421 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | |
| 422 ReadLoader(10, 10, buffer); | |
| 423 StopWhenLoad(); | |
| 424 } | |
| 425 | |
| 426 TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredNoDataReceived) { | |
| 427 Initialize(kHttpUrl, 10, 99); | |
| 428 SetLoaderBuffer(10, 20); | |
| 429 Start(); | |
| 430 PartialResponse(10, 99, 100); | |
| 431 | |
| 432 uint8 buffer[10]; | |
| 433 | |
| 434 // Start in deferred state, then disallow defer, receive no data, and | |
| 435 // allow defer and read. | |
| 436 EXPECT_CALL(*url_loader_, setDefersLoading(true)); | |
| 437 EXPECT_CALL(*this, NetworkCallback()); | |
| 438 WriteLoader(10, 40); | |
| 439 | |
| 440 DisallowLoaderDefer(); | |
| 441 AllowLoaderDefer(); | |
| 442 | |
| 443 EXPECT_CALL(*this, ReadCallback(10)); | |
| 444 ReadLoader(20, 10, buffer); | |
| 445 VerifyBuffer(buffer, 20, 10); | |
| 446 StopWhenLoad(); | |
| 447 } | |
| 448 | |
| 449 TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadSameWindow) { | |
| 450 Initialize(kHttpUrl, 10, 99); | |
| 451 SetLoaderBuffer(10, 20); | |
| 452 Start(); | |
| 453 PartialResponse(10, 99, 100); | |
| 454 | |
| 455 uint8 buffer[10]; | |
| 456 | |
| 457 // Start in deferred state, disallow defer, receive data and shift buffer | |
| 458 // window, allow defer, and read in a place that's still in the window. | |
| 459 EXPECT_CALL(*url_loader_, setDefersLoading(true)); | |
| 460 EXPECT_CALL(*this, NetworkCallback()); | |
| 461 WriteLoader(10, 30); | |
| 462 | |
| 463 DisallowLoaderDefer(); | |
| 464 WriteLoader(40, 5); | |
| 465 AllowLoaderDefer(); | |
| 466 | |
| 467 EXPECT_CALL(*this, ReadCallback(10)); | |
| 468 ReadLoader(20, 10, buffer); | |
| 469 VerifyBuffer(buffer, 20, 10); | |
| 470 StopWhenLoad(); | |
| 471 } | |
| 472 | |
| 473 TEST_F(BufferedResourceLoaderTest, AllowDefer_DeferredReadPastWindow) { | |
| 474 Initialize(kHttpUrl, 10, 99); | |
| 475 SetLoaderBuffer(10, 20); | |
| 476 Start(); | |
| 477 PartialResponse(10, 99, 100); | |
| 478 | |
| 479 uint8 buffer[10]; | |
| 480 | |
| 481 // Start in deferred state, disallow defer, receive data and shift buffer | |
| 482 // window, allow defer, and read outside of the buffer window. | |
| 483 EXPECT_CALL(*url_loader_, setDefersLoading(true)); | |
| 484 EXPECT_CALL(*this, NetworkCallback()); | |
| 485 WriteLoader(10, 40); | |
| 486 | |
| 487 DisallowLoaderDefer(); | |
| 488 WriteLoader(50, 20); | |
| 489 WriteLoader(70, 40); | |
| 490 AllowLoaderDefer(); | |
| 491 | |
| 492 EXPECT_CALL(*this, ReadCallback(net::ERR_CACHE_MISS)); | |
| 493 ReadLoader(20, 5, buffer); | |
| 494 StopWhenLoad(); | |
| 495 } | |
| 496 // TODO(hclam): add unit test for defer loading. | |
| 497 | |
| 498 class MockBufferedResourceLoader : public BufferedResourceLoader { | 69 class MockBufferedResourceLoader : public BufferedResourceLoader { |
| 499 public: | 70 public: |
| 500 MockBufferedResourceLoader() : BufferedResourceLoader(GURL(), 0, 0) { | 71 MockBufferedResourceLoader() : BufferedResourceLoader(GURL(), 0, 0) { |
| 501 } | 72 } |
| 502 | 73 |
| 503 MOCK_METHOD3(Start, void(net::CompletionCallback* read_callback, | 74 MOCK_METHOD3(Start, void(net::CompletionCallback* read_callback, |
| 504 NetworkEventCallback* network_callback, | 75 NetworkEventCallback* network_callback, |
| 505 WebFrame* frame)); | 76 WebFrame* frame)); |
| 506 MOCK_METHOD0(Stop, void()); | 77 MOCK_METHOD0(Stop, void()); |
| 507 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, | 78 MOCK_METHOD4(Read, void(int64 position, int read_size, uint8* buffer, |
| 508 net::CompletionCallback* callback)); | 79 net::CompletionCallback* callback)); |
| 509 MOCK_METHOD0(content_length, int64()); | 80 MOCK_METHOD0(content_length, int64()); |
| 510 MOCK_METHOD0(instance_size, int64()); | 81 MOCK_METHOD0(instance_size, int64()); |
| 511 MOCK_METHOD0(partial_response, bool()); | 82 MOCK_METHOD0(partial_response, bool()); |
| 512 MOCK_METHOD0(network_activity, bool()); | 83 MOCK_METHOD0(network_activity, bool()); |
| 513 MOCK_METHOD0(url, const GURL&()); | 84 MOCK_METHOD0(url, const GURL&()); |
| 514 MOCK_METHOD0(GetBufferedFirstBytePosition, int64()); | 85 MOCK_METHOD0(GetBufferedFirstBytePosition, int64()); |
| 515 MOCK_METHOD0(GetBufferedLastBytePosition, int64()); | 86 MOCK_METHOD0(GetBufferedLastBytePosition, int64()); |
| 516 | 87 |
| 517 protected: | 88 protected: |
| 518 ~MockBufferedResourceLoader() {} | 89 ~MockBufferedResourceLoader() {} |
| 519 | 90 |
| 520 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); | 91 DISALLOW_COPY_AND_ASSIGN(MockBufferedResourceLoader); |
| 521 }; | 92 }; |
| 522 | 93 |
| 523 // A mock BufferedDataSource to inject mock BufferedResourceLoader through | |
| 524 // CreateResourceLoader() method. | |
| 525 class MockBufferedDataSource : public BufferedDataSource { | |
| 526 public: | |
| 527 MockBufferedDataSource( | |
| 528 MessageLoop* message_loop, WebFrame* frame) | |
| 529 : BufferedDataSource(message_loop, frame) { | |
| 530 } | |
| 531 | |
| 532 virtual base::TimeDelta GetTimeoutMilliseconds() { | |
| 533 // It is 100 ms because we don't want the test to run too long. | |
| 534 return base::TimeDelta::FromMilliseconds(100); | |
| 535 } | |
| 536 | |
| 537 MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*( | |
| 538 int64 first_position, int64 last_position)); | |
| 539 | |
| 540 private: | |
| 541 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource); | |
| 542 }; | |
| 543 | |
| 544 class BufferedDataSourceTest : public testing::Test { | 94 class BufferedDataSourceTest : public testing::Test { |
| 545 public: | 95 public: |
| 546 BufferedDataSourceTest() { | 96 BufferedDataSourceTest() { |
| 547 message_loop_ = MessageLoop::current(); | 97 message_loop_ = MessageLoop::current(); |
| 548 | 98 |
| 549 // Prepare test data. | 99 // Prepare test data. |
| 550 for (size_t i = 0; i < sizeof(data_); ++i) { | 100 for (size_t i = 0; i < sizeof(data_); ++i) { |
| 551 data_[i] = i; | 101 data_[i] = i; |
| 552 } | 102 } |
| 553 } | 103 } |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 944 StopDataSource(); | 494 StopDataSource(); |
| 945 } | 495 } |
| 946 | 496 |
| 947 TEST_F(BufferedDataSourceTest, FileHasLoadedState) { | 497 TEST_F(BufferedDataSourceTest, FileHasLoadedState) { |
| 948 InitializeDataSource(kFileUrl, net::OK, true, 1024, LOADED); | 498 InitializeDataSource(kFileUrl, net::OK, true, 1024, LOADED); |
| 949 ReadDataSourceTimesOut(20, 10); | 499 ReadDataSourceTimesOut(20, 10); |
| 950 StopDataSource(); | 500 StopDataSource(); |
| 951 } | 501 } |
| 952 | 502 |
| 953 } // namespace webkit_glue | 503 } // namespace webkit_glue |
| OLD | NEW |