Commit d3ae70d1 authored by scherkus@chromium.org's avatar scherkus@chromium.org

Added new gmock-based MockFilterHost and deprecated the old one.

Updated FileDataSource tests to use the new MockFilterHost as a proof of concept, also because my pipeline refactoring completely broke the test.

BUG=16008
TEST=FileDataSourceTest.* should pass

Review URL: http://codereview.chromium.org/149350

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20219 0039d316-1c4b-4281-b951-d872f2087c98
parent 83548cfb
// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
// The corresponding FilterHost implementation for MockPipeline. Maintains a
// reference to the parent MockPipeline and a reference to the Filter its
// hosting. Common usage is to check if the hosted filter has initialized by
// calling IsInitialized().
//
// A FilterHost implementation based on gmock. Combined with setting a message
// loop on a filter, permits single-threaded testing of filters without
// requiring a pipeline.
//
// TODO(scherkus): Remove old_mocks::MockFilterHost as soon as other tests have
// transitioned over to the new gmock-based MockFilterHost.
#ifndef MEDIA_BASE_MOCK_FILTER_HOST_H_
#define MEDIA_BASE_MOCK_FILTER_HOST_H_
......@@ -19,10 +21,33 @@
#include "media/base/filters.h"
#include "media/base/media_format.h"
#include "media/base/mock_pipeline.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
class MockFilterHost : public FilterHost {
public:
MockFilterHost() {}
// FilterHost implementation.
MOCK_METHOD0(InitializationComplete, void());
MOCK_METHOD1(Error, void(PipelineError error));
MOCK_CONST_METHOD0(GetTime, base::TimeDelta());
MOCK_METHOD1(SetTime, void(base::TimeDelta time));
MOCK_METHOD1(SetDuration, void(base::TimeDelta duration));
MOCK_METHOD1(SetBufferedTime, void(base::TimeDelta buffered_time));
MOCK_METHOD1(SetTotalBytes, void(int64 total_bytes));
MOCK_METHOD1(SetBufferedBytes, void(int64 buffered_bytes));
MOCK_METHOD2(SetVideoSize, void(size_t width, size_t height));
private:
DISALLOW_COPY_AND_ASSIGN(MockFilterHost);
};
namespace old_mocks {
// This version is deprecated.
template <class Filter>
class MockFilterHost : public FilterHost {
public:
......@@ -121,6 +146,8 @@ class MockFilterHost : public FilterHost {
DISALLOW_COPY_AND_ASSIGN(MockFilterHost);
};
} // namespace old_mocks
} // namespace media
#endif // MEDIA_BASE_MOCK_FILTER_HOST_H_
......@@ -71,7 +71,8 @@ class FFmpegDemuxerTest : public testing::Test {
// Prepare a filter host and data source for the demuxer.
pipeline_.reset(new MockPipeline());
filter_host_.reset(new MockFilterHost<Demuxer>(pipeline_.get(), demuxer_));
filter_host_.reset(new old_mocks::MockFilterHost<Demuxer>(pipeline_.get(),
demuxer_));
data_source_ = new StrictMock<MockDataSource>();
// Initialize FFmpeg fixtures.
......@@ -148,7 +149,7 @@ class FFmpegDemuxerTest : public testing::Test {
scoped_refptr<FilterFactory> factory_;
scoped_refptr<FFmpegDemuxer> demuxer_;
scoped_ptr<MockPipeline> pipeline_;
scoped_ptr<MockFilterHost<Demuxer> > filter_host_;
scoped_ptr<old_mocks::MockFilterHost<Demuxer> > filter_host_;
scoped_refptr<StrictMock<MockDataSource> > data_source_;
MessageLoop message_loop_;
......
......@@ -85,8 +85,8 @@ class FFmpegVideoDecoderTest : public testing::Test {
// Prepare a filter host, pipeline and demuxer for the video decoder.
pipeline_.reset(new MockPipeline());
filter_host_.reset(new MockFilterHost<VideoDecoder>(pipeline_.get(),
decoder_));
filter_host_.reset(
new old_mocks::MockFilterHost<VideoDecoder>(pipeline_.get(), decoder_));
demuxer_ = new MockFFmpegDemuxerStream();
// Initialize FFmpeg fixtures.
......@@ -121,7 +121,7 @@ class FFmpegVideoDecoderTest : public testing::Test {
scoped_refptr<FilterFactory> factory_;
scoped_refptr<FFmpegVideoDecoder> decoder_;
scoped_ptr<MockPipeline> pipeline_;
scoped_ptr<MockFilterHost<VideoDecoder> > filter_host_;
scoped_ptr<old_mocks::MockFilterHost<VideoDecoder> > filter_host_;
scoped_refptr<MockFFmpegDemuxerStream> demuxer_;
scoped_refptr<DataBuffer> buffer_;
scoped_refptr<DataBuffer> end_of_stream_buffer_;
......
......@@ -9,14 +9,12 @@
#include "base/lock.h"
#include "media/base/filters.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
namespace media {
// Basic data source that treats the URL as a file path, and uses the file
// system to read data for a media pipeline.
// TODO(ralph): We will add a pure virtual interface so that the chrome
// media player delegate can give us the file handle, bytes downloaded so far,
// and file size.
class FileDataSource : public DataSource {
public:
// Public method to get a filter factory for the FileDataSource.
......@@ -37,11 +35,17 @@ class FileDataSource : public DataSource {
virtual bool IsSeekable();
private:
// Only allow factories and tests to create this object.
//
// TODO(scherkus): I'm getting tired of these factories getting in the way
// of my tests!!!
FRIEND_TEST(FileDataSourceTest, OpenFile);
FRIEND_TEST(FileDataSourceTest, ReadData);
friend class FilterFactoryImpl0<FileDataSource>;
FileDataSource();
virtual ~FileDataSource();
// File handle. Null if not initialized or an error occurs.
// File handle. NULL if not initialized or an error occurs.
FILE* file_;
// Size of the file in bytes.
......@@ -53,7 +57,7 @@ class FileDataSource : public DataSource {
// Critical section that protects all of the DataSource methods to prevent
// a Stop from happening while in the middle of a file I/O operation.
// TODO(ralphl): Ideally this would use asynchronous I/O or we will know
// that we will block for a short period of time in reads. Othewise, we can
// that we will block for a short period of time in reads. Otherwise, we can
// hang the pipeline Stop.
Lock lock_;
......
......@@ -6,34 +6,14 @@
#include "base/base_paths.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/string_util.h"
#include "base/task.h"
#include "base/waitable_event.h"
#include "media/base/buffers.h"
#include "media/base/pipeline_impl.h"
#include "media/base/media_format.h"
#include "media/base/filters.h"
#include "media/base/factory.h"
#include "media/base/filter_host.h"
#include "media/base/mock_filter_host.h"
#include "media/base/mock_pipeline.h"
#include "media/filters/file_data_source.h"
#include "media/base/mock_media_filters.h"
#include "testing/gtest/include/gtest/gtest.h"
using media::FileDataSource;
using media::FilterFactory;
using media::FilterFactoryCollection;
using media::MediaFormat;
using media::MockFilterHost;
using media::MockPipeline;
using media::PipelineImpl;
using media::old_mocks::InitializationHelper;
using media::old_mocks::MockFilterConfig;
using media::old_mocks::MockFilterFactory;
using ::testing::NiceMock;
using ::testing::StrictMock;
namespace {
namespace media {
// Returns a path to the test file which contains the string "0123456789"
// without the quotes or any trailing space or null termination. The file lives
......@@ -55,42 +35,29 @@ std::string TestFileURL() {
#endif
}
} // namespace
// Use the "real" pipeline to open the file.
// Test that FileDataSource call the appropriate methods on its filter host.
TEST(FileDataSourceTest, OpenFile) {
PipelineImpl pipeline;
MockFilterConfig config;
config.has_video = false;
scoped_refptr<FilterFactoryCollection> c = new FilterFactoryCollection();
c->AddFactory(FileDataSource::CreateFactory());
c->AddFactory(new MockFilterFactory(&config));
InitializationHelper h;
h.Start(&pipeline, c, TestFileURL());
EXPECT_EQ(pipeline.GetTotalBytes(), 10);
EXPECT_EQ(pipeline.GetBufferedBytes(), 10);
pipeline.Stop();
StrictMock<MockFilterHost> host;
EXPECT_CALL(host, SetTotalBytes(10));
EXPECT_CALL(host, SetBufferedBytes(10));
EXPECT_CALL(host, InitializationComplete());
scoped_refptr<FileDataSource> filter = new FileDataSource();
filter->SetFilterHost(&host);
EXPECT_TRUE(filter->Initialize(TestFileURL()));
}
// Use the mock filter host to directly call the Read and GetPosition methods.
TEST(FileDataSourceTest, ReadData) {
MediaFormat url_format;
int64 position;
int64 size;
uint8 ten_bytes[10];
std::string url = TestFileURL();
url_format.SetAsString(MediaFormat::kMimeType, media::mime_type::kURL);
url_format.SetAsString(MediaFormat::kURL, url);
// Create our data source.
scoped_refptr<FilterFactory> factory = FileDataSource::CreateFactory();
FileDataSource* filter = factory->Create<FileDataSource>(url_format);
EXPECT_TRUE(filter);
// Create our mock pipeline and filter host and initialize the data source.
MockPipeline pipeline;
MockFilterHost<FileDataSource> mock_host(&pipeline, filter);
EXPECT_TRUE(filter->Initialize(url));
// Create our mock filter host and initialize the data source.
NiceMock<MockFilterHost> host;
scoped_refptr<FileDataSource> filter = new FileDataSource();
filter->SetFilterHost(&host);
EXPECT_TRUE(filter->Initialize(TestFileURL()));
EXPECT_TRUE(filter->GetSize(&size));
EXPECT_EQ(10, size);
......@@ -112,3 +79,5 @@ TEST(FileDataSourceTest, ReadData) {
EXPECT_TRUE(filter->GetPosition(&position));
EXPECT_EQ(10, position);
}
} // namespace media
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment