summaryrefslogtreecommitdiffstats
path: root/media
diff options
context:
space:
mode:
authorscherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-16 22:40:22 +0000
committerscherkus@chromium.org <scherkus@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-16 22:40:22 +0000
commit48ab88b24fecfd7b21955fb24fcfe1544da34d25 (patch)
tree72f379f40ac70e0dca5770bf904c2c68bf0613b7 /media
parent8267a7d1082e969da88b168f22178adabbd94cf3 (diff)
downloadchromium_src-48ab88b24fecfd7b21955fb24fcfe1544da34d25.zip
chromium_src-48ab88b24fecfd7b21955fb24fcfe1544da34d25.tar.gz
chromium_src-48ab88b24fecfd7b21955fb24fcfe1544da34d25.tar.bz2
Refactored, templatized, commented and moved TestReader to the fancy new MockReader.
Previously only used by FFmpegDemuxerTest, it's not templated so we can use it with FFmpegVideoDecoderTest and FFmpegAudioDecoderTest and any other sort of demuxer or decoder for that matter. Review URL: http://codereview.chromium.org/126027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18547 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r--media/base/mock_reader.h91
-rw-r--r--media/filters/ffmpeg_demuxer_unittest.cc68
-rw-r--r--media/media.gyp1
3 files changed, 100 insertions, 60 deletions
diff --git a/media/base/mock_reader.h b/media/base/mock_reader.h
new file mode 100644
index 0000000..ad8db88
--- /dev/null
+++ b/media/base/mock_reader.h
@@ -0,0 +1,91 @@
+// 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.
+
+#ifndef MEDIA_BASE_MOCK_READER_H_
+#define MEDIA_BASE_MOCK_READER_H_
+
+#include <string>
+
+#include "base/ref_counted.h"
+#include "base/waitable_event.h"
+#include "media/base/filters.h"
+
+namespace media {
+
+// Ref counted object so we can create callbacks for asynchronous Read()
+// methods for any filter type.
+template <class FilterType, class BufferType>
+class MockReader :
+ public base::RefCountedThreadSafe<MockReader<FilterType, BufferType> > {
+ public:
+ MockReader()
+ : called_(false),
+ expecting_call_(false),
+ wait_for_read_(false, false) {
+ }
+
+ virtual ~MockReader() {
+ }
+
+ // Prepares this object for another read.
+ void Reset() {
+ DCHECK(!expecting_call_);
+ expecting_call_ = false;
+ called_ = false;
+ buffer_ = NULL;
+ wait_for_read_.Reset();
+ }
+
+ // Executes an asynchronous read on the given filter.
+ void Read(FilterType* filter) {
+ DCHECK(!expecting_call_);
+ called_ = false;
+ expecting_call_ = true;
+ filter->Read(NewCallback(this, &MockReader::OnReadComplete));
+ }
+
+ // Waits 500ms for the read callback to be completed. Returns true if the
+ // read was completed, false otherwise.
+ bool WaitForRead() {
+ return wait_for_read_.TimedWait(base::TimeDelta::FromMilliseconds(500));
+ }
+
+ // Mock accessors.
+ BufferType* buffer() { return buffer_; }
+ bool called() { return called_; }
+ bool expecting_call() { return expecting_call_; }
+
+ private:
+ void OnReadComplete(BufferType* buffer) {
+ DCHECK(!called_);
+ DCHECK(expecting_call_);
+ expecting_call_ = false;
+ called_ = true;
+ buffer_ = buffer;
+ wait_for_read_.Signal();
+ }
+
+ // Reference to the buffer provided in the callback.
+ scoped_refptr<BufferType> buffer_;
+
+ // Whether or not the callback was executed.
+ bool called_;
+
+ // Whether or not this reader was expecting a callback.
+ bool expecting_call_;
+
+ // Used by tests to wait for the callback to be executed.
+ base::WaitableEvent wait_for_read_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockReader);
+};
+
+// Commonly used reader types.
+typedef MockReader<DemuxerStream, Buffer> DemuxerStreamReader;
+typedef MockReader<AudioDecoder, Buffer> AudioDecoderReader;
+typedef MockReader<VideoDecoder, VideoFrame> VideoDecoderReader;
+
+} // namespace media
+
+#endif // MEDIA_BASE_MOCK_READER_H_
diff --git a/media/filters/ffmpeg_demuxer_unittest.cc b/media/filters/ffmpeg_demuxer_unittest.cc
index 002f6d5..b689616 100644
--- a/media/filters/ffmpeg_demuxer_unittest.cc
+++ b/media/filters/ffmpeg_demuxer_unittest.cc
@@ -10,6 +10,7 @@
#include "media/base/filters.h"
#include "media/base/mock_filter_host.h"
#include "media/base/mock_media_filters.h"
+#include "media/base/mock_reader.h"
#include "media/filters/ffmpeg_common.h"
#include "media/filters/ffmpeg_demuxer.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -192,60 +193,7 @@ int av_seek_frame(AVFormatContext *format, int stream_index, int64_t timestamp,
} // extern "C"
-using namespace media;
-
-namespace {
-
-// Ref counted object so we can create callbacks to call DemuxerStream::Read().
-class TestReader : public base::RefCountedThreadSafe<TestReader> {
- public:
- TestReader()
- : called_(false),
- expecting_call_(false),
- wait_for_read_(false, false) {
- }
-
- virtual ~TestReader() {}
-
- void Reset() {
- EXPECT_FALSE(expecting_call_);
- expecting_call_ = false;
- called_ = false;
- buffer_ = NULL;
- wait_for_read_.Reset();
- }
-
- void Read(DemuxerStream* stream) {
- EXPECT_FALSE(expecting_call_);
- called_ = false;
- expecting_call_ = true;
- stream->Read(NewCallback(this, &TestReader::ReadComplete));
- }
-
- void ReadComplete(Buffer* buffer) {
- EXPECT_FALSE(called_);
- EXPECT_TRUE(expecting_call_);
- expecting_call_ = false;
- called_ = true;
- buffer_ = buffer;
- wait_for_read_.Signal();
- }
-
- bool WaitForRead() {
- return wait_for_read_.TimedWait(base::TimeDelta::FromMilliseconds(500));
- }
-
- // Mock getters/setters.
- Buffer* buffer() { return buffer_; }
- bool called() { return called_; }
- bool expecting_call() { return expecting_call_; }
-
- private:
- scoped_refptr<Buffer> buffer_;
- bool called_;
- bool expecting_call_;
- base::WaitableEvent wait_for_read_;
-};
+namespace media {
// Fixture class to facilitate writing tests. Takes care of setting up the
// FFmpeg, pipeline and filter host mocks.
@@ -476,7 +424,7 @@ TEST_F(FFmpegDemuxerTest, ReadAndSeek) {
PacketQueue::get()->Enqueue(kPacketAudio, kDataSize, audio_data);
// Attempt a read from the audio stream and run the message loop until done.
- scoped_refptr<TestReader> reader(new TestReader());
+ scoped_refptr<DemuxerStreamReader> reader(new DemuxerStreamReader());
reader->Read(audio_stream);
pipeline_->RunAllTasks();
EXPECT_TRUE(reader->WaitForRead());
@@ -523,7 +471,7 @@ TEST_F(FFmpegDemuxerTest, ReadAndSeek) {
PacketQueue::get()->Enqueue(kPacketAudio, kDataSize, audio_data);
// Audio read #1, should be discontinuous.
- reader = new TestReader();
+ reader = new DemuxerStreamReader();
reader->Read(audio_stream);
pipeline_->RunAllTasks();
EXPECT_TRUE(reader->WaitForRead());
@@ -583,7 +531,7 @@ TEST_F(FFmpegDemuxerTest, ReadAndSeek) {
// Attempt a read from video stream, which will force the demuxer to queue
// the audio packets preceding the video packet.
- reader = new TestReader();
+ reader = new DemuxerStreamReader();
reader->Read(video_stream);
pipeline_->RunAllTasks();
EXPECT_TRUE(reader->WaitForRead());
@@ -617,7 +565,7 @@ TEST_F(FFmpegDemuxerTest, ReadAndSeek) {
g_av_read_frame = AVERROR_IO;
// Attempt a read from the audio stream and run the message loop until done.
- reader = new TestReader();
+ reader = new DemuxerStreamReader();
reader->Read(audio_stream);
pipeline_->RunAllTasks();
EXPECT_TRUE(reader->WaitForRead());
@@ -665,7 +613,7 @@ TEST_F(FFmpegDemuxerTest, DISABLED_MP3Hack) {
// Audio read should perform a deep copy on the packet and instantly release
// the original packet. The data pointers should not be the same, but the
// contents should match.
- scoped_refptr<TestReader> reader = new TestReader();
+ scoped_refptr<DemuxerStreamReader> reader = new DemuxerStreamReader();
reader->Read(audio_stream);
pipeline_->RunAllTasks();
EXPECT_TRUE(reader->WaitForRead());
@@ -686,4 +634,4 @@ TEST_F(FFmpegDemuxerTest, DISABLED_MP3Hack) {
EXPECT_EQ(0, g_outstanding_packets_av_new_frame);
}
-} // namespace
+} // namespace media
diff --git a/media/media.gyp b/media/media.gyp
index b6c895b..e3f3b3a 100644
--- a/media/media.gyp
+++ b/media/media.gyp
@@ -152,6 +152,7 @@
'base/mock_filter_host.h',
'base/mock_media_filters.h',
'base/mock_pipeline.h',
+ 'base/mock_reader.h',
'base/pipeline_impl_unittest.cc',
'base/run_all_unittests.cc',
'base/seekable_buffer_unittest.cc',