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 // 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 // source code is governed by a BSD-style license that can be found in the
// LICENSE file. // LICENSE file.
//
// The corresponding FilterHost implementation for MockPipeline. Maintains a // A FilterHost implementation based on gmock. Combined with setting a message
// reference to the parent MockPipeline and a reference to the Filter its // loop on a filter, permits single-threaded testing of filters without
// hosting. Common usage is to check if the hosted filter has initialized by // requiring a pipeline.
// calling IsInitialized(). //
// 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_ #ifndef MEDIA_BASE_MOCK_FILTER_HOST_H_
#define MEDIA_BASE_MOCK_FILTER_HOST_H_ #define MEDIA_BASE_MOCK_FILTER_HOST_H_
...@@ -19,10 +21,33 @@ ...@@ -19,10 +21,33 @@
#include "media/base/filters.h" #include "media/base/filters.h"
#include "media/base/media_format.h" #include "media/base/media_format.h"
#include "media/base/mock_pipeline.h" #include "media/base/mock_pipeline.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace media { 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> template <class Filter>
class MockFilterHost : public FilterHost { class MockFilterHost : public FilterHost {
public: public:
...@@ -121,6 +146,8 @@ class MockFilterHost : public FilterHost { ...@@ -121,6 +146,8 @@ class MockFilterHost : public FilterHost {
DISALLOW_COPY_AND_ASSIGN(MockFilterHost); DISALLOW_COPY_AND_ASSIGN(MockFilterHost);
}; };
} // namespace old_mocks
} // namespace media } // namespace media
#endif // MEDIA_BASE_MOCK_FILTER_HOST_H_ #endif // MEDIA_BASE_MOCK_FILTER_HOST_H_
...@@ -71,7 +71,8 @@ class FFmpegDemuxerTest : public testing::Test { ...@@ -71,7 +71,8 @@ class FFmpegDemuxerTest : public testing::Test {
// Prepare a filter host and data source for the demuxer. // Prepare a filter host and data source for the demuxer.
pipeline_.reset(new MockPipeline()); 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>(); data_source_ = new StrictMock<MockDataSource>();
// Initialize FFmpeg fixtures. // Initialize FFmpeg fixtures.
...@@ -148,7 +149,7 @@ class FFmpegDemuxerTest : public testing::Test { ...@@ -148,7 +149,7 @@ class FFmpegDemuxerTest : public testing::Test {
scoped_refptr<FilterFactory> factory_; scoped_refptr<FilterFactory> factory_;
scoped_refptr<FFmpegDemuxer> demuxer_; scoped_refptr<FFmpegDemuxer> demuxer_;
scoped_ptr<MockPipeline> pipeline_; scoped_ptr<MockPipeline> pipeline_;
scoped_ptr<MockFilterHost<Demuxer> > filter_host_; scoped_ptr<old_mocks::MockFilterHost<Demuxer> > filter_host_;
scoped_refptr<StrictMock<MockDataSource> > data_source_; scoped_refptr<StrictMock<MockDataSource> > data_source_;
MessageLoop message_loop_; MessageLoop message_loop_;
......
...@@ -85,8 +85,8 @@ class FFmpegVideoDecoderTest : public testing::Test { ...@@ -85,8 +85,8 @@ class FFmpegVideoDecoderTest : public testing::Test {
// Prepare a filter host, pipeline and demuxer for the video decoder. // Prepare a filter host, pipeline and demuxer for the video decoder.
pipeline_.reset(new MockPipeline()); pipeline_.reset(new MockPipeline());
filter_host_.reset(new MockFilterHost<VideoDecoder>(pipeline_.get(), filter_host_.reset(
decoder_)); new old_mocks::MockFilterHost<VideoDecoder>(pipeline_.get(), decoder_));
demuxer_ = new MockFFmpegDemuxerStream(); demuxer_ = new MockFFmpegDemuxerStream();
// Initialize FFmpeg fixtures. // Initialize FFmpeg fixtures.
...@@ -121,7 +121,7 @@ class FFmpegVideoDecoderTest : public testing::Test { ...@@ -121,7 +121,7 @@ class FFmpegVideoDecoderTest : public testing::Test {
scoped_refptr<FilterFactory> factory_; scoped_refptr<FilterFactory> factory_;
scoped_refptr<FFmpegVideoDecoder> decoder_; scoped_refptr<FFmpegVideoDecoder> decoder_;
scoped_ptr<MockPipeline> pipeline_; scoped_ptr<MockPipeline> pipeline_;
scoped_ptr<MockFilterHost<VideoDecoder> > filter_host_; scoped_ptr<old_mocks::MockFilterHost<VideoDecoder> > filter_host_;
scoped_refptr<MockFFmpegDemuxerStream> demuxer_; scoped_refptr<MockFFmpegDemuxerStream> demuxer_;
scoped_refptr<DataBuffer> buffer_; scoped_refptr<DataBuffer> buffer_;
scoped_refptr<DataBuffer> end_of_stream_buffer_; scoped_refptr<DataBuffer> end_of_stream_buffer_;
......
...@@ -9,14 +9,12 @@ ...@@ -9,14 +9,12 @@
#include "base/lock.h" #include "base/lock.h"
#include "media/base/filters.h" #include "media/base/filters.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
namespace media { namespace media {
// Basic data source that treats the URL as a file path, and uses the file // Basic data source that treats the URL as a file path, and uses the file
// system to read data for a media pipeline. // 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 { class FileDataSource : public DataSource {
public: public:
// Public method to get a filter factory for the FileDataSource. // Public method to get a filter factory for the FileDataSource.
...@@ -37,11 +35,17 @@ class FileDataSource : public DataSource { ...@@ -37,11 +35,17 @@ class FileDataSource : public DataSource {
virtual bool IsSeekable(); virtual bool IsSeekable();
private: 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>; friend class FilterFactoryImpl0<FileDataSource>;
FileDataSource(); FileDataSource();
virtual ~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_; FILE* file_;
// Size of the file in bytes. // Size of the file in bytes.
...@@ -53,7 +57,7 @@ class FileDataSource : public DataSource { ...@@ -53,7 +57,7 @@ class FileDataSource : public DataSource {
// Critical section that protects all of the DataSource methods to prevent // 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. // 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 // 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. // hang the pipeline Stop.
Lock lock_; Lock lock_;
......
...@@ -6,34 +6,14 @@ ...@@ -6,34 +6,14 @@
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/file_path.h" #include "base/file_path.h"
#include "base/file_util.h"
#include "base/string_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_filter_host.h"
#include "media/base/mock_pipeline.h"
#include "media/filters/file_data_source.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 ::testing::NiceMock;
using media::FilterFactory; using ::testing::StrictMock;
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;
namespace { namespace media {
// Returns a path to the test file which contains the string "0123456789" // 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 // without the quotes or any trailing space or null termination. The file lives
...@@ -55,42 +35,29 @@ std::string TestFileURL() { ...@@ -55,42 +35,29 @@ std::string TestFileURL() {
#endif #endif
} }
} // namespace // Test that FileDataSource call the appropriate methods on its filter host.
// Use the "real" pipeline to open the file.
TEST(FileDataSourceTest, OpenFile) { TEST(FileDataSourceTest, OpenFile) {
PipelineImpl pipeline; StrictMock<MockFilterHost> host;
MockFilterConfig config; EXPECT_CALL(host, SetTotalBytes(10));
config.has_video = false; EXPECT_CALL(host, SetBufferedBytes(10));
scoped_refptr<FilterFactoryCollection> c = new FilterFactoryCollection(); EXPECT_CALL(host, InitializationComplete());
c->AddFactory(FileDataSource::CreateFactory());
c->AddFactory(new MockFilterFactory(&config)); scoped_refptr<FileDataSource> filter = new FileDataSource();
InitializationHelper h; filter->SetFilterHost(&host);
h.Start(&pipeline, c, TestFileURL()); EXPECT_TRUE(filter->Initialize(TestFileURL()));
EXPECT_EQ(pipeline.GetTotalBytes(), 10);
EXPECT_EQ(pipeline.GetBufferedBytes(), 10);
pipeline.Stop();
} }
// Use the mock filter host to directly call the Read and GetPosition methods. // Use the mock filter host to directly call the Read and GetPosition methods.
TEST(FileDataSourceTest, ReadData) { TEST(FileDataSourceTest, ReadData) {
MediaFormat url_format;
int64 position; int64 position;
int64 size; int64 size;
uint8 ten_bytes[10]; 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. // Create our mock filter host and initialize the data source.
scoped_refptr<FilterFactory> factory = FileDataSource::CreateFactory(); NiceMock<MockFilterHost> host;
FileDataSource* filter = factory->Create<FileDataSource>(url_format); scoped_refptr<FileDataSource> filter = new FileDataSource();
EXPECT_TRUE(filter); filter->SetFilterHost(&host);
EXPECT_TRUE(filter->Initialize(TestFileURL()));
// 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));
EXPECT_TRUE(filter->GetSize(&size)); EXPECT_TRUE(filter->GetSize(&size));
EXPECT_EQ(10, size); EXPECT_EQ(10, size);
...@@ -112,3 +79,5 @@ TEST(FileDataSourceTest, ReadData) { ...@@ -112,3 +79,5 @@ TEST(FileDataSourceTest, ReadData) {
EXPECT_TRUE(filter->GetPosition(&position)); EXPECT_TRUE(filter->GetPosition(&position));
EXPECT_EQ(10, 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