This source file includes following definitions.
- loading_
- CreateMockResourceLoader
- loading
- set_loading
- downloading
- set_downloading
- frame_
- Initialize
- InitializeWith206Response
- InitializeWithFileResponse
- Stop
- ExpectCreateResourceLoader
- Respond
- ReceiveData
- FinishLoading
- ReadAt
- loader
- url_loader
- preload
- defer_strategy
- data_source_bitrate
- data_source_playback_rate
- loader_bitrate
- loader_playback_rate
- 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
- SetTrue
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "content/renderer/media/buffered_data_source.h"
#include "content/renderer/media/test_response_generator.h"
#include "content/test/mock_webframeclient.h"
#include "content/test/mock_weburlloader.h"
#include "media/base/media_log.h"
#include "media/base/mock_data_source_host.h"
#include "media/base/mock_filters.h"
#include "media/base/test_helpers.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::Assign;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::NiceMock;
using ::testing::StrictMock;
using blink::WebFrame;
using blink::WebString;
using blink::WebURLLoader;
using blink::WebURLResponse;
using blink::WebView;
namespace content {
class MockBufferedDataSource : public BufferedDataSource {
public:
MockBufferedDataSource(
const scoped_refptr<base::MessageLoopProxy>& message_loop,
WebFrame* frame,
media::DataSourceHost* host)
: BufferedDataSource(message_loop, frame, new media::MediaLog(), host,
base::Bind(&MockBufferedDataSource::set_downloading,
base::Unretained(this))),
downloading_(false),
loading_(false) {
}
virtual ~MockBufferedDataSource() {}
MOCK_METHOD2(CreateResourceLoader, BufferedResourceLoader*(int64, int64));
BufferedResourceLoader* CreateMockResourceLoader(int64 first_byte_position,
int64 last_byte_position) {
CHECK(!loading_) << "Previous resource load wasn't cancelled";
BufferedResourceLoader* loader =
BufferedDataSource::CreateResourceLoader(first_byte_position,
last_byte_position);
NiceMock<MockWebURLLoader>* url_loader = new NiceMock<MockWebURLLoader>();
ON_CALL(*url_loader, loadAsynchronously(_, _))
.WillByDefault(Assign(&loading_, true));
ON_CALL(*url_loader, cancel())
.WillByDefault(Assign(&loading_, false));
loader->test_loader_ = scoped_ptr<WebURLLoader>(url_loader);
return loader;
}
bool loading() { return loading_; }
void set_loading(bool loading) { loading_ = loading; }
bool downloading() { return downloading_; }
void set_downloading(bool downloading) { downloading_ = downloading; }
private:
bool downloading_;
bool loading_;
DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSource);
};
static const int64 kFileSize = 5000000;
static const int64 kFarReadPosition = 4000000;
static const int kDataSize = 1024;
static const char kHttpUrl[] = "http://localhost/foo.webm";
static const char kFileUrl[] = "file:///tmp/bar.webm";
class BufferedDataSourceTest : public testing::Test {
public:
BufferedDataSourceTest()
: view_(WebView::create(NULL)), frame_(WebFrame::create(&client_)) {
view_->setMainFrame(frame_);
data_source_.reset(new MockBufferedDataSource(
message_loop_.message_loop_proxy(), view_->mainFrame(), &host_));
}
virtual ~BufferedDataSourceTest() {
view_->close();
frame_->close();
}
MOCK_METHOD1(OnInitialize, void(bool));
void Initialize(const char* url, bool expected) {
GURL gurl(url);
response_generator_.reset(new TestResponseGenerator(gurl, kFileSize));
ExpectCreateResourceLoader();
EXPECT_CALL(*this, OnInitialize(expected));
data_source_->Initialize(
gurl, BufferedResourceLoader::kUnspecified, base::Bind(
&BufferedDataSourceTest::OnInitialize, base::Unretained(this)));
message_loop_.RunUntilIdle();
bool is_http = gurl.SchemeIs(kHttpScheme) || gurl.SchemeIs(kHttpsScheme);
EXPECT_EQ(data_source_->downloading(), is_http);
}
void InitializeWith206Response() {
Initialize(kHttpUrl, true);
EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
Respond(response_generator_->Generate206(0));
}
void InitializeWithFileResponse() {
Initialize(kFileUrl, true);
EXPECT_CALL(host_, SetTotalBytes(kFileSize));
EXPECT_CALL(host_, AddBufferedByteRange(0, kFileSize));
Respond(response_generator_->GenerateFileResponse(0));
}
void Stop() {
if (data_source_->loading()) {
loader()->didFail(url_loader(), response_generator_->GenerateError());
message_loop_.RunUntilIdle();
}
data_source_->Stop(media::NewExpectedClosure());
message_loop_.RunUntilIdle();
}
void ExpectCreateResourceLoader() {
EXPECT_CALL(*data_source_, CreateResourceLoader(_, _))
.WillOnce(Invoke(data_source_.get(),
&MockBufferedDataSource::CreateMockResourceLoader));
message_loop_.RunUntilIdle();
}
void Respond(const WebURLResponse& response) {
loader()->didReceiveResponse(url_loader(), response);
message_loop_.RunUntilIdle();
}
void ReceiveData(int size) {
scoped_ptr<char[]> data(new char[size]);
memset(data.get(), 0xA5, size);
loader()->didReceiveData(url_loader(), data.get(), size, size);
message_loop_.RunUntilIdle();
}
void FinishLoading() {
data_source_->set_loading(false);
loader()->didFinishLoading(url_loader(), 0, -1);
message_loop_.RunUntilIdle();
}
MOCK_METHOD1(ReadCallback, void(int size));
void ReadAt(int64 position) {
data_source_->Read(position, kDataSize, buffer_,
base::Bind(&BufferedDataSourceTest::ReadCallback,
base::Unretained(this)));
message_loop_.RunUntilIdle();
}
BufferedResourceLoader* loader() {
return data_source_->loader_.get();
}
WebURLLoader* url_loader() {
return loader()->active_loader_->loader_.get();
}
Preload preload() { return data_source_->preload_; }
BufferedResourceLoader::DeferStrategy defer_strategy() {
return loader()->defer_strategy_;
}
int data_source_bitrate() { return data_source_->bitrate_; }
int data_source_playback_rate() { return data_source_->playback_rate_; }
int loader_bitrate() { return loader()->bitrate_; }
int loader_playback_rate() { return loader()->playback_rate_; }
scoped_ptr<MockBufferedDataSource> data_source_;
scoped_ptr<TestResponseGenerator> response_generator_;
MockWebFrameClient client_;
WebView* view_;
WebFrame* frame_;
StrictMock<media::MockDataSourceHost> host_;
base::MessageLoop message_loop_;
private:
uint8 buffer_[kDataSize];
DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceTest);
};
TEST_F(BufferedDataSourceTest, Range_Supported) {
Initialize(kHttpUrl, true);
EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
Respond(response_generator_->Generate206(0));
EXPECT_TRUE(data_source_->loading());
EXPECT_FALSE(data_source_->IsStreaming());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_InstanceSizeUnknown) {
Initialize(kHttpUrl, true);
Respond(response_generator_->Generate206(
0, TestResponseGenerator::kNoContentRangeInstanceSize));
EXPECT_TRUE(data_source_->loading());
EXPECT_TRUE(data_source_->IsStreaming());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_NotFound) {
Initialize(kHttpUrl, false);
Respond(response_generator_->Generate404());
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_NotSupported) {
Initialize(kHttpUrl, true);
EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
Respond(response_generator_->Generate200());
EXPECT_TRUE(data_source_->loading());
EXPECT_TRUE(data_source_->IsStreaming());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_SupportedButReturned200) {
Initialize(kHttpUrl, true);
EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
WebURLResponse response = response_generator_->Generate200();
response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
WebString::fromUTF8("bytes"));
Respond(response);
EXPECT_TRUE(data_source_->loading());
EXPECT_FALSE(data_source_->IsStreaming());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_MissingContentRange) {
Initialize(kHttpUrl, false);
Respond(response_generator_->Generate206(
0, TestResponseGenerator::kNoContentRange));
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_MissingContentLength) {
Initialize(kHttpUrl, true);
EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length()));
Respond(response_generator_->Generate206(
0, TestResponseGenerator::kNoContentLength));
EXPECT_TRUE(data_source_->loading());
EXPECT_FALSE(data_source_->IsStreaming());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_WrongContentRange) {
Initialize(kHttpUrl, false);
Respond(response_generator_->Generate206(1337));
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, Range_ServerLied) {
InitializeWith206Response();
ExpectCreateResourceLoader();
ReadAt(kFarReadPosition);
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
Respond(response_generator_->Generate200());
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, Http_AbortWhileReading) {
InitializeWith206Response();
ReadAt(0);
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
data_source_->Abort();
message_loop_.RunUntilIdle();
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, File_AbortWhileReading) {
InitializeWithFileResponse();
ReadAt(0);
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
data_source_->Abort();
message_loop_.RunUntilIdle();
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, Http_Retry) {
InitializeWith206Response();
EXPECT_CALL(*this, ReadCallback(kDataSize));
EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
ReadAt(0);
ReceiveData(kDataSize);
ReadAt(kDataSize);
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->Generate206(kDataSize));
EXPECT_CALL(*this, ReadCallback(kDataSize));
EXPECT_CALL(host_, AddBufferedByteRange(kDataSize, (kDataSize * 2) - 1));
ReceiveData(kDataSize);
EXPECT_TRUE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, File_Retry) {
InitializeWithFileResponse();
EXPECT_CALL(*this, ReadCallback(kDataSize));
ReadAt(0);
ReceiveData(kDataSize);
ReadAt(kDataSize);
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->GenerateFileResponse(kDataSize));
EXPECT_CALL(*this, ReadCallback(kDataSize));
ReceiveData(kDataSize);
EXPECT_TRUE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, Http_TooManyRetries) {
InitializeWith206Response();
ReadAt(0);
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->Generate206(0));
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->Generate206(0));
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->Generate206(0));
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
FinishLoading();
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, File_TooManyRetries) {
InitializeWithFileResponse();
ReadAt(0);
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->GenerateFileResponse(0));
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->GenerateFileResponse(0));
ExpectCreateResourceLoader();
FinishLoading();
Respond(response_generator_->GenerateFileResponse(0));
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
FinishLoading();
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, File_InstanceSizeUnknown) {
Initialize(kFileUrl, false);
EXPECT_FALSE(data_source_->downloading());
Respond(response_generator_->GenerateFileResponse(-1));
EXPECT_FALSE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, File_Successful) {
InitializeWithFileResponse();
EXPECT_TRUE(data_source_->loading());
EXPECT_FALSE(data_source_->IsStreaming());
Stop();
}
static void SetTrue(bool* value) {
*value = true;
}
TEST_F(BufferedDataSourceTest, StopDoesNotUseMessageLoopForCallback) {
InitializeWith206Response();
bool stop_done_called = false;
EXPECT_TRUE(data_source_->loading());
data_source_->Stop(base::Bind(&SetTrue, &stop_done_called));
EXPECT_TRUE(stop_done_called);
message_loop_.RunUntilIdle();
}
TEST_F(BufferedDataSourceTest, StopDuringRead) {
InitializeWith206Response();
uint8 buffer[256];
data_source_->Read(0, arraysize(buffer), buffer, base::Bind(
&BufferedDataSourceTest::ReadCallback, base::Unretained(this)));
{
InSequence s;
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
data_source_->Stop(media::NewExpectedClosure());
}
message_loop_.RunUntilIdle();
}
TEST_F(BufferedDataSourceTest, DefaultValues) {
InitializeWith206Response();
EXPECT_EQ(AUTO, preload());
EXPECT_EQ(BufferedResourceLoader::kCapacityDefer, defer_strategy());
EXPECT_EQ(0, data_source_bitrate());
EXPECT_EQ(0.0f, data_source_playback_rate());
EXPECT_EQ(0, loader_bitrate());
EXPECT_EQ(0.0f, loader_playback_rate());
EXPECT_TRUE(data_source_->loading());
Stop();
}
TEST_F(BufferedDataSourceTest, SetBitrate) {
InitializeWith206Response();
data_source_->SetBitrate(1234);
message_loop_.RunUntilIdle();
EXPECT_EQ(1234, data_source_bitrate());
EXPECT_EQ(1234, loader_bitrate());
BufferedResourceLoader* old_loader = loader();
ExpectCreateResourceLoader();
ReadAt(kFarReadPosition);
Respond(response_generator_->Generate206(kFarReadPosition));
EXPECT_NE(old_loader, loader());
EXPECT_EQ(1234, loader_bitrate());
EXPECT_TRUE(data_source_->loading());
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
Stop();
}
TEST_F(BufferedDataSourceTest, MediaPlaybackRateChanged) {
InitializeWith206Response();
data_source_->MediaPlaybackRateChanged(2.0f);
message_loop_.RunUntilIdle();
EXPECT_EQ(2.0f, data_source_playback_rate());
EXPECT_EQ(2.0f, loader_playback_rate());
BufferedResourceLoader* old_loader = loader();
ExpectCreateResourceLoader();
ReadAt(kFarReadPosition);
Respond(response_generator_->Generate206(kFarReadPosition));
EXPECT_NE(old_loader, loader());
EXPECT_TRUE(data_source_->loading());
EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError));
Stop();
}
TEST_F(BufferedDataSourceTest, Http_Read) {
InitializeWith206Response();
ReadAt(0);
EXPECT_CALL(host_, AddBufferedByteRange(0, (kDataSize / 2) - 1));
ReceiveData(kDataSize / 2);
EXPECT_CALL(*this, ReadCallback(kDataSize));
EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
ReceiveData(kDataSize / 2);
EXPECT_TRUE(data_source_->downloading());
Stop();
}
TEST_F(BufferedDataSourceTest, Http_Read_Seek) {
InitializeWith206Response();
ReadAt(0);
EXPECT_CALL(*this, ReadCallback(kDataSize));
EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize - 1));
ReceiveData(kDataSize);
ReadAt(kDataSize * 2);
EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2 - 1));
ReceiveData(kDataSize);
EXPECT_CALL(*this, ReadCallback(kDataSize));
EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 3 - 1));
ReceiveData(kDataSize);
EXPECT_TRUE(data_source_->downloading());
Stop();
}
TEST_F(BufferedDataSourceTest, File_Read) {
InitializeWithFileResponse();
ReadAt(0);
ReceiveData(kDataSize / 2);
EXPECT_CALL(*this, ReadCallback(kDataSize));
ReceiveData(kDataSize / 2);
Stop();
}
TEST_F(BufferedDataSourceTest, Http_FinishLoading) {
InitializeWith206Response();
EXPECT_TRUE(data_source_->downloading());
FinishLoading();
EXPECT_FALSE(data_source_->downloading());
Stop();
}
TEST_F(BufferedDataSourceTest, File_FinishLoading) {
InitializeWithFileResponse();
EXPECT_FALSE(data_source_->downloading());
FinishLoading();
EXPECT_FALSE(data_source_->downloading());
Stop();
}
}