This source file includes following definitions.
- CorrectAcceptEncoding
- frame_
- Initialize
- SetLoaderBuffer
- Start
- FullResponse
- FullResponse
- PartialResponse
- PartialResponse
- Redirect
- StopWhenLoad
- WriteLoader
- WriteData
- WriteUntilThreshold
- ReadLoader
- VerifyBuffer
- ConfirmLoaderOffsets
- ConfirmBufferState
- ConfirmLoaderBufferBackwardCapacity
- ConfirmLoaderBufferForwardCapacity
- CheckBufferWindowBounds
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- ExpectContentRange
- ExpectContentRangeFailure
- ExpectContentRangeSuccess
- TEST
#include <algorithm>
#include <string>
#include "base/bind.h"
#include "base/format_macros.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "content/renderer/media/buffered_resource_loader.h"
#include "content/test/mock_webframeclient.h"
#include "content/test/mock_weburlloader.h"
#include "media/base/media_log.h"
#include "media/base/seekable_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_util.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURLError.h"
#include "third_party/WebKit/public/platform/WebURLRequest.h"
#include "third_party/WebKit/public/platform/WebURLResponse.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.h"
using ::testing::_;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::Truly;
using ::testing::NiceMock;
using blink::WebFrame;
using blink::WebString;
using blink::WebURLError;
using blink::WebURLResponse;
using blink::WebView;
namespace content {
static const char* kHttpUrl = "http://test";
static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing";
static const char kHttpRedirectToSameDomainUrl2[] = "http://test/ing2";
static const char kHttpRedirectToDifferentDomainUrl1[] = "http://test2";
static const int kDataSize = 1024;
static const int kHttpOK = 200;
static const int kHttpPartialContent = 206;
enum NetworkState {
NONE,
LOADED,
LOADING
};
static bool CorrectAcceptEncoding(const blink::WebURLRequest &request) {
std::string value = request.httpHeaderField(
WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding)).utf8();
return (value.find("identity;q=1") != std::string::npos) &&
(value.find("*;q=0") != std::string::npos);
}
class BufferedResourceLoaderTest : public testing::Test {
public:
BufferedResourceLoaderTest()
: view_(WebView::create(NULL)), frame_(WebFrame::create(&client_)) {
view_->setMainFrame(frame_);
for (int i = 0; i < kDataSize; ++i) {
data_[i] = i;
}
}
virtual ~BufferedResourceLoaderTest() {
view_->close();
frame_->close();
}
void Initialize(const char* url, int first_position, int last_position) {
gurl_ = GURL(url);
first_position_ = first_position;
last_position_ = last_position;
loader_.reset(new BufferedResourceLoader(
gurl_, BufferedResourceLoader::kUnspecified,
first_position_, last_position_,
BufferedResourceLoader::kCapacityDefer, 0, 0,
new media::MediaLog()));
url_loader_ = new NiceMock<MockWebURLLoader>();
loader_->test_loader_ = scoped_ptr<blink::WebURLLoader>(url_loader_);
}
void SetLoaderBuffer(int forward_capacity, int backward_capacity) {
loader_->buffer_.set_forward_capacity(forward_capacity);
loader_->buffer_.set_backward_capacity(backward_capacity);
loader_->buffer_.Clear();
}
void Start() {
InSequence s;
EXPECT_CALL(*url_loader_, loadAsynchronously(Truly(CorrectAcceptEncoding),
loader_.get()));
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
loader_->Start(
base::Bind(&BufferedResourceLoaderTest::StartCallback,
base::Unretained(this)),
base::Bind(&BufferedResourceLoaderTest::LoadingCallback,
base::Unretained(this)),
base::Bind(&BufferedResourceLoaderTest::ProgressCallback,
base::Unretained(this)),
view_->mainFrame());
}
void FullResponse(int64 instance_size) {
FullResponse(instance_size, BufferedResourceLoader::kOk);
}
void FullResponse(int64 instance_size,
BufferedResourceLoader::Status status) {
EXPECT_CALL(*this, StartCallback(status));
WebURLResponse response(gurl_);
response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
WebString::fromUTF8(base::StringPrintf("%"
PRId64, instance_size)));
response.setExpectedContentLength(instance_size);
response.setHTTPStatusCode(kHttpOK);
loader_->didReceiveResponse(url_loader_, response);
if (status == BufferedResourceLoader::kOk) {
EXPECT_EQ(instance_size, loader_->content_length());
EXPECT_EQ(instance_size, loader_->instance_size());
}
EXPECT_FALSE(loader_->range_supported());
}
void PartialResponse(int64 first_position, int64 last_position,
int64 instance_size) {
PartialResponse(first_position, last_position, instance_size, false, true);
}
void PartialResponse(int64 first_position, int64 last_position,
int64 instance_size, bool chunked, bool accept_ranges) {
EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kOk));
WebURLResponse response(gurl_);
response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"),
WebString::fromUTF8(base::StringPrintf("bytes "
"%" PRId64 "-%" PRId64 "/%" PRId64,
first_position,
last_position,
instance_size)));
int64 content_length = -1;
if (chunked) {
response.setHTTPHeaderField(WebString::fromUTF8("Transfer-Encoding"),
WebString::fromUTF8("chunked"));
} else {
content_length = last_position - first_position + 1;
}
response.setExpectedContentLength(content_length);
if (accept_ranges) {
response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
WebString::fromUTF8("bytes"));
}
response.setHTTPStatusCode(kHttpPartialContent);
loader_->didReceiveResponse(url_loader_, response);
EXPECT_EQ(content_length, loader_->content_length());
EXPECT_EQ(instance_size, loader_->instance_size());
EXPECT_TRUE(loader_->range_supported());
}
void Redirect(const char* url) {
GURL redirectUrl(url);
blink::WebURLRequest newRequest(redirectUrl);
blink::WebURLResponse redirectResponse(gurl_);
loader_->willSendRequest(url_loader_, newRequest, redirectResponse);
base::MessageLoop::current()->RunUntilIdle();
}
void StopWhenLoad() {
InSequence s;
EXPECT_CALL(*url_loader_, cancel());
loader_->Stop();
loader_.reset();
}
void WriteLoader(int position, int size) {
EXPECT_CALL(*this, ProgressCallback(position + size - 1));
loader_->didReceiveData(url_loader_,
reinterpret_cast<char*>(data_ + position),
size,
size);
}
void WriteData(int size) {
EXPECT_CALL(*this, ProgressCallback(_));
scoped_ptr<char[]> data(new char[size]);
loader_->didReceiveData(url_loader_, data.get(), size, size);
}
void WriteUntilThreshold() {
int buffered = loader_->buffer_.forward_bytes();
int capacity = loader_->buffer_.forward_capacity();
CHECK_LT(buffered, capacity);
EXPECT_CALL(*this, LoadingCallback(
BufferedResourceLoader::kLoadingDeferred));
WriteData(capacity - buffered);
}
void ReadLoader(int64 position, int size, uint8* buffer) {
loader_->Read(position, size, buffer,
base::Bind(&BufferedResourceLoaderTest::ReadCallback,
base::Unretained(this)));
}
void VerifyBuffer(uint8* buffer, int pos, int size) {
EXPECT_EQ(0, memcmp(buffer, data_ + pos, size));
}
void ConfirmLoaderOffsets(int64 expected_offset,
int expected_first_offset,
int expected_last_offset) {
EXPECT_EQ(loader_->offset_, expected_offset);
EXPECT_EQ(loader_->first_offset_, expected_first_offset);
EXPECT_EQ(loader_->last_offset_, expected_last_offset);
}
void ConfirmBufferState(int backward_bytes,
int backward_capacity,
int forward_bytes,
int forward_capacity) {
EXPECT_EQ(backward_bytes, loader_->buffer_.backward_bytes());
EXPECT_EQ(backward_capacity, loader_->buffer_.backward_capacity());
EXPECT_EQ(forward_bytes, loader_->buffer_.forward_bytes());
EXPECT_EQ(forward_capacity, loader_->buffer_.forward_capacity());
}
void ConfirmLoaderBufferBackwardCapacity(int expected_backward_capacity) {
EXPECT_EQ(loader_->buffer_.backward_capacity(),
expected_backward_capacity);
}
void ConfirmLoaderBufferForwardCapacity(int expected_forward_capacity) {
EXPECT_EQ(loader_->buffer_.forward_capacity(), expected_forward_capacity);
}
void CheckBufferWindowBounds() {
static const int kMinBufferCapacity = 2 * 1024 * 1024;
EXPECT_GE(loader_->buffer_.forward_capacity(), kMinBufferCapacity);
EXPECT_GE(loader_->buffer_.backward_capacity(), kMinBufferCapacity);
static const int kMaxBufferCapacity = 20 * 1024 * 1024;
EXPECT_LE(loader_->buffer_.forward_capacity(), kMaxBufferCapacity);
EXPECT_LE(loader_->buffer_.backward_capacity(), kMaxBufferCapacity);
}
MOCK_METHOD1(StartCallback, void(BufferedResourceLoader::Status));
MOCK_METHOD2(ReadCallback, void(BufferedResourceLoader::Status, int));
MOCK_METHOD1(LoadingCallback, void(BufferedResourceLoader::LoadingState));
MOCK_METHOD1(ProgressCallback, void(int64));
protected:
GURL gurl_;
int64 first_position_;
int64 last_position_;
scoped_ptr<BufferedResourceLoader> loader_;
NiceMock<MockWebURLLoader>* url_loader_;
MockWebFrameClient client_;
WebView* view_;
WebFrame* frame_;
base::MessageLoop message_loop_;
uint8 data_[kDataSize];
private:
DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoaderTest);
};
TEST_F(BufferedResourceLoaderTest, StartStop) {
Initialize(kHttpUrl, -1, -1);
Start();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BadHttpResponse) {
Initialize(kHttpUrl, -1, -1);
Start();
EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kFailed));
WebURLResponse response(gurl_);
response.setHTTPStatusCode(404);
response.setHTTPStatusText("Not Found\n");
loader_->didReceiveResponse(url_loader_, response);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, NotPartialResponse) {
Initialize(kHttpUrl, 100, -1);
Start();
FullResponse(1024, BufferedResourceLoader::kFailed);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, FullResponse) {
Initialize(kHttpUrl, -1, -1);
Start();
FullResponse(1024);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, PartialResponse) {
Initialize(kHttpUrl, 100, 200);
Start();
PartialResponse(100, 200, 1024);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, PartialResponse_Chunked) {
Initialize(kHttpUrl, 100, 200);
Start();
PartialResponse(100, 200, 1024, true, true);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, PartialResponse_NoAcceptRanges) {
Initialize(kHttpUrl, 100, 200);
Start();
PartialResponse(100, 200, 1024, false, false);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, PartialResponse_ChunkedNoAcceptRanges) {
Initialize(kHttpUrl, 100, 200);
Start();
PartialResponse(100, 200, 1024, true, false);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, InvalidPartialResponse) {
Initialize(kHttpUrl, 0, 10);
Start();
EXPECT_CALL(*this, StartCallback(BufferedResourceLoader::kFailed));
WebURLResponse response(gurl_);
response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"),
WebString::fromUTF8(base::StringPrintf("bytes "
"%d-%d/%d", 1, 10, 1024)));
response.setExpectedContentLength(10);
response.setHTTPStatusCode(kHttpPartialContent);
loader_->didReceiveResponse(url_loader_, response);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferAndRead) {
Initialize(kHttpUrl, 10, 29);
loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer);
Start();
PartialResponse(10, 29, 30);
uint8 buffer[10];
InSequence s;
WriteLoader(10, 10);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
ReadLoader(10, 10, buffer);
VerifyBuffer(buffer, 10, 10);
WriteLoader(20, 10);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
ReadLoader(20, 5, buffer);
VerifyBuffer(buffer, 20, 5);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
ReadLoader(25, 5, buffer);
VerifyBuffer(buffer, 25, 5);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
ReadLoader(10, 10, buffer);
VerifyBuffer(buffer, 10, 10);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
ReadLoader(9, 10, buffer);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFinished));
loader_->didFinishLoading(url_loader_, 0, -1);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
ReadLoader(25, 10, buffer);
VerifyBuffer(buffer, 25, 5);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
ReadLoader(5, 10, buffer);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 0));
ReadLoader(30, 10, buffer);
}
TEST_F(BufferedResourceLoaderTest, ReadExtendBuffer) {
Initialize(kHttpUrl, 10, 0x014FFFFFF);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 0x014FFFFFF, 0x015000000);
uint8 buffer[20];
InSequence s;
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
WriteLoader(10, 20);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 20));
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(10, 20, buffer);
VerifyBuffer(buffer, 10, 20);
ConfirmLoaderBufferForwardCapacity(10);
ReadLoader(30, 20, buffer);
ConfirmLoaderBufferForwardCapacity(20);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 20));
WriteLoader(30, 20);
VerifyBuffer(buffer, 30, 20);
ConfirmLoaderBufferForwardCapacity(10);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
ReadLoader(0x00300000, 1, buffer);
ConfirmLoaderBufferForwardCapacity(10);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
ReadLoader(30, 0x01400001, buffer);
ConfirmLoaderBufferForwardCapacity(10);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, ReadOutsideBuffer) {
Initialize(kHttpUrl, 10, 0x00FFFFFF);
Start();
PartialResponse(10, 0x00FFFFFF, 0x01000000);
uint8 buffer[10];
InSequence s;
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
ReadLoader(0x00FFFFFF, 1, buffer);
ReadLoader(10, 10, buffer);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
WriteLoader(10, 20);
VerifyBuffer(buffer, 10, 10);
ReadLoader(25, 10, buffer);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFinished));
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 5));
loader_->didFinishLoading(url_loader_, 0, -1);
}
TEST_F(BufferedResourceLoaderTest, RequestFailedWhenRead) {
Initialize(kHttpUrl, 10, 29);
Start();
PartialResponse(10, 29, 30);
uint8 buffer[10];
InSequence s;
ReadLoader(10, 10, buffer);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed));
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
WebURLError error;
error.reason = net::ERR_TIMED_OUT;
error.isCancellation = false;
loader_->didFail(url_loader_, error);
}
TEST_F(BufferedResourceLoaderTest, RequestFailedWithNoPendingReads) {
Initialize(kHttpUrl, 10, 29);
Start();
PartialResponse(10, 29, 30);
uint8 buffer[10];
InSequence s;
WriteLoader(10, 20);
WebURLError error;
error.reason = net::ERR_TIMED_OUT;
error.isCancellation = false;
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed));
loader_->didFail(url_loader_, error);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
ReadLoader(10, 10, buffer);
}
TEST_F(BufferedResourceLoaderTest, RequestCancelledWhenRead) {
Initialize(kHttpUrl, 10, 29);
Start();
PartialResponse(10, 29, 30);
uint8 buffer[10];
InSequence s;
ReadLoader(10, 10, buffer);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingFailed));
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kFailed, 0));
WebURLError error;
error.reason = 0;
error.isCancellation = true;
loader_->didFail(url_loader_, error);
}
TEST_F(BufferedResourceLoaderTest, NeverDeferStrategy) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
WriteLoader(10, 10);
WriteLoader(20, 50);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
ReadLoader(10, 10, buffer);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, ReadThenDeferStrategy) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
loader_->UpdateDeferStrategy(BufferedResourceLoader::kReadThenDefer);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
ReadLoader(10, 10, buffer);
WriteLoader(10, 9);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
WriteLoader(19, 1);
VerifyBuffer(buffer, 10, 10);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(20, 10, buffer);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
WriteLoader(20, 40);
VerifyBuffer(buffer, 20, 10);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(80, 10, buffer);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
WriteLoader(60, 40);
VerifyBuffer(buffer, 80, 10);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, ThresholdDeferStrategy) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 20);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[10];
InSequence s;
WriteData(5);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
WriteData(5);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1));
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(10, 1, buffer);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoadingDeferred));
WriteData(6);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, Tricky_ReadForwardsPastBuffered) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 10);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[256];
InSequence s;
WriteUntilThreshold();
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 1));
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(10, 1, buffer);
ConfirmBufferState(1, 10, 9, 10);
ConfirmLoaderOffsets(11, 0, 0);
ReadLoader(20, 4, buffer);
WriteData(2);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 4));
WriteData(2);
ConfirmBufferState(4, 10, 0, 10);
ConfirmLoaderOffsets(24, 0, 0);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, Tricky_ReadBackwardsPastBuffered) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 10);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[256];
InSequence s;
WriteUntilThreshold();
ConfirmBufferState(0, 10, 10, 10);
ConfirmLoaderOffsets(10, 0, 0);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
ReadLoader(9, 4, buffer);
ConfirmBufferState(0, 10, 10, 10);
ConfirmLoaderOffsets(10, 0, 0);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, Tricky_SmallReadWithinThreshold) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 10);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[256];
InSequence s;
WriteUntilThreshold();
ConfirmBufferState(0, 10, 10, 10);
ConfirmLoaderOffsets(10, 0, 0);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(24, 4, buffer);
ConfirmLoaderOffsets(20, 4, 8);
WriteData(4);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 4));
WriteData(4);
ConfirmBufferState(8, 10, 0, 10);
ConfirmLoaderOffsets(28, 0, 0);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadWithinThreshold) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 10);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[256];
InSequence s;
WriteUntilThreshold();
ConfirmBufferState(0, 10, 10, 10);
ConfirmLoaderOffsets(10, 0, 0);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(24, 12, buffer);
ConfirmLoaderOffsets(20, 4, 16);
ConfirmBufferState(10, 10, 0, 16);
WriteData(10);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 12));
WriteData(6);
ConfirmLoaderBufferForwardCapacity(10);
ConfirmBufferState(6, 10, 0, 10);
ConfirmLoaderOffsets(36, 0, 0);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, Tricky_LargeReadBackwards) {
Initialize(kHttpUrl, 10, 99);
SetLoaderBuffer(10, 10);
Start();
PartialResponse(10, 99, 100);
uint8 buffer[256];
InSequence s;
WriteUntilThreshold();
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 10));
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(10, 10, buffer);
WriteUntilThreshold();
ConfirmBufferState(10, 10, 10, 10);
ConfirmLoaderOffsets(20, 0, 0);
EXPECT_CALL(*this, LoadingCallback(BufferedResourceLoader::kLoading));
ReadLoader(16, 18, buffer);
ConfirmLoaderOffsets(16, 0, 18);
ConfirmBufferState(6, 10, 14, 18);
WriteData(2);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kOk, 18));
WriteData(2);
ConfirmLoaderBufferForwardCapacity(10);
ConfirmBufferState(4, 10, 0, 10);
ConfirmLoaderOffsets(34, 0, 0);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, Tricky_ReadPastThreshold) {
const int kSize = 5 * 1024 * 1024;
const int kThreshold = 2 * 1024 * 1024;
Initialize(kHttpUrl, 10, kSize);
SetLoaderBuffer(10, 10);
Start();
PartialResponse(10, kSize - 1, kSize);
uint8 buffer[256];
InSequence s;
WriteUntilThreshold();
ConfirmBufferState(0, 10, 10, 10);
ConfirmLoaderOffsets(10, 0, 0);
EXPECT_CALL(*this, ReadCallback(BufferedResourceLoader::kCacheMiss, 0));
ReadLoader(kThreshold + 20, 10, buffer);
ConfirmBufferState(0, 10, 10, 10);
ConfirmLoaderOffsets(10, 0, 0);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, HasSingleOrigin) {
Initialize(kHttpUrl, -1, -1);
Start();
FullResponse(1024);
EXPECT_TRUE(loader_->HasSingleOrigin());
StopWhenLoad();
Initialize(kHttpUrl, -1, -1);
Start();
Redirect(kHttpRedirectToSameDomainUrl1);
FullResponse(1024);
EXPECT_TRUE(loader_->HasSingleOrigin());
StopWhenLoad();
Initialize(kHttpUrl, -1, -1);
Start();
Redirect(kHttpRedirectToSameDomainUrl1);
Redirect(kHttpRedirectToSameDomainUrl2);
FullResponse(1024);
EXPECT_TRUE(loader_->HasSingleOrigin());
StopWhenLoad();
Initialize(kHttpUrl, -1, -1);
Start();
Redirect(kHttpRedirectToDifferentDomainUrl1);
FullResponse(1024);
EXPECT_FALSE(loader_->HasSingleOrigin());
StopWhenLoad();
Initialize(kHttpUrl, -1, -1);
Start();
Redirect(kHttpRedirectToSameDomainUrl1);
Redirect(kHttpRedirectToDifferentDomainUrl1);
FullResponse(1024);
EXPECT_FALSE(loader_->HasSingleOrigin());
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_Default) {
Initialize(kHttpUrl, -1, -1);
Start();
ConfirmLoaderBufferBackwardCapacity(2 * 1024 * 1024);
ConfirmLoaderBufferForwardCapacity(2 * 1024 * 1024);
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_Unknown) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetBitrate(0);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_BelowLowerBound) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetBitrate(1024 * 8);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_WithinBounds) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetBitrate(2 * 1024 * 1024 * 8);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_Bitrate_AboveUpperBound) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetBitrate(100 * 1024 * 1024 * 8);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_Negative) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetPlaybackRate(-10);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_Zero) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetPlaybackRate(0);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_BelowLowerBound) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetPlaybackRate(0.1f);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_WithinBounds) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetPlaybackRate(10);
CheckBufferWindowBounds();
StopWhenLoad();
}
TEST_F(BufferedResourceLoaderTest, BufferWindow_PlaybackRate_AboveUpperBound) {
Initialize(kHttpUrl, -1, -1);
Start();
loader_->SetPlaybackRate(100);
CheckBufferWindowBounds();
StopWhenLoad();
}
static void ExpectContentRange(
const std::string& str, bool expect_success,
int64 expected_first, int64 expected_last, int64 expected_size) {
int64 first, last, size;
ASSERT_EQ(expect_success, BufferedResourceLoader::ParseContentRange(
str, &first, &last, &size)) << str;
if (!expect_success)
return;
EXPECT_EQ(first, expected_first);
EXPECT_EQ(last, expected_last);
EXPECT_EQ(size, expected_size);
}
static void ExpectContentRangeFailure(const std::string& str) {
ExpectContentRange(str, false, 0, 0, 0);
}
static void ExpectContentRangeSuccess(
const std::string& str,
int64 expected_first, int64 expected_last, int64 expected_size) {
ExpectContentRange(str, true, expected_first, expected_last, expected_size);
}
TEST(BufferedResourceLoaderStandaloneTest, ParseContentRange) {
ExpectContentRangeFailure("cytes 0-499/500");
ExpectContentRangeFailure("bytes 0499/500");
ExpectContentRangeFailure("bytes 0-499500");
ExpectContentRangeFailure("bytes 0-499/500-blorg");
ExpectContentRangeFailure("bytes 0-499/500-1");
ExpectContentRangeFailure("bytes 0-499/400");
ExpectContentRangeFailure("bytes 0-/400");
ExpectContentRangeFailure("bytes -300/400");
ExpectContentRangeFailure("bytes 20-10/400");
ExpectContentRangeSuccess("bytes 0-499/500", 0, 499, 500);
ExpectContentRangeSuccess("bytes 0-0/500", 0, 0, 500);
ExpectContentRangeSuccess("bytes 10-11/50", 10, 11, 50);
ExpectContentRangeSuccess("bytes 10-11/*", 10, 11,
kPositionNotSpecified);
}
}