1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A new breed of mock media filters, this time using gmock! Feel free to add
6 // actions if you need interesting side-effects.
8 // Don't forget you can use StrictMock<> and NiceMock<> if you want the mock
9 // filters to fail the test or do nothing when an unexpected method is called.
10 // http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks
12 #ifndef MEDIA_BASE_MOCK_FILTERS_H_
13 #define MEDIA_BASE_MOCK_FILTERS_H_
17 #include "base/callback.h"
18 #include "media/base/audio_decoder.h"
19 #include "media/base/audio_decoder_config.h"
20 #include "media/base/audio_renderer.h"
21 #include "media/base/decoder_buffer.h"
22 #include "media/base/decryptor.h"
23 #include "media/base/decryptor_client.h"
24 #include "media/base/demuxer.h"
25 #include "media/base/filter_collection.h"
26 #include "media/base/pipeline_status.h"
27 #include "media/base/video_decoder.h"
28 #include "media/base/video_decoder_config.h"
29 #include "media/base/video_frame.h"
30 #include "media/base/video_renderer.h"
31 #include "testing/gmock/include/gmock/gmock.h"
35 // Use this template to test for object destruction by setting expectations on
36 // the method OnDestroy().
38 // TODO(scherkus): not sure about the naming... perhaps contribute this back
40 template<class MockClass
>
41 class Destroyable
: public MockClass
{
45 MOCK_METHOD0(OnDestroy
, void());
48 virtual ~Destroyable() {
53 DISALLOW_COPY_AND_ASSIGN(Destroyable
);
56 class MockDemuxer
: public Demuxer
{
60 // Demuxer implementation.
61 MOCK_METHOD2(Initialize
, void(DemuxerHost
* host
, const PipelineStatusCB
& cb
));
62 MOCK_METHOD1(SetPlaybackRate
, void(float playback_rate
));
63 MOCK_METHOD2(Seek
, void(base::TimeDelta time
, const PipelineStatusCB
& cb
));
64 MOCK_METHOD1(Stop
, void(const base::Closure
& callback
));
65 MOCK_METHOD0(OnAudioRendererDisabled
, void());
66 MOCK_METHOD1(GetStream
, scoped_refptr
<DemuxerStream
>(DemuxerStream::Type
));
67 MOCK_CONST_METHOD0(GetStartTime
, base::TimeDelta());
70 virtual ~MockDemuxer();
73 DISALLOW_COPY_AND_ASSIGN(MockDemuxer
);
76 class MockDemuxerStream
: public DemuxerStream
{
80 // DemuxerStream implementation.
81 MOCK_METHOD0(type
, Type());
82 MOCK_METHOD1(Read
, void(const ReadCB
& read_cb
));
83 MOCK_METHOD0(audio_decoder_config
, const AudioDecoderConfig
&());
84 MOCK_METHOD0(video_decoder_config
, const VideoDecoderConfig
&());
85 MOCK_METHOD0(EnableBitstreamConverter
, void());
88 virtual ~MockDemuxerStream();
91 DISALLOW_COPY_AND_ASSIGN(MockDemuxerStream
);
94 class MockVideoDecoder
: public VideoDecoder
{
98 // VideoDecoder implementation.
99 MOCK_METHOD3(Initialize
, void(const scoped_refptr
<DemuxerStream
>&,
100 const PipelineStatusCB
&,
101 const StatisticsCB
&));
102 MOCK_METHOD1(Read
, void(const ReadCB
&));
103 MOCK_METHOD1(Reset
, void(const base::Closure
&));
104 MOCK_METHOD1(Stop
, void(const base::Closure
&));
105 MOCK_CONST_METHOD0(HasAlpha
, bool());
108 virtual ~MockVideoDecoder();
111 DISALLOW_COPY_AND_ASSIGN(MockVideoDecoder
);
114 class MockAudioDecoder
: public AudioDecoder
{
118 // AudioDecoder implementation.
119 MOCK_METHOD3(Initialize
, void(const scoped_refptr
<DemuxerStream
>&,
120 const PipelineStatusCB
&,
121 const StatisticsCB
&));
122 MOCK_METHOD1(Read
, void(const ReadCB
&));
123 MOCK_METHOD0(bits_per_channel
, int(void));
124 MOCK_METHOD0(channel_layout
, ChannelLayout(void));
125 MOCK_METHOD0(samples_per_second
, int(void));
126 MOCK_METHOD1(Reset
, void(const base::Closure
&));
129 virtual ~MockAudioDecoder();
132 DISALLOW_COPY_AND_ASSIGN(MockAudioDecoder
);
135 class MockVideoRenderer
: public VideoRenderer
{
139 // VideoRenderer implementation.
140 MOCK_METHOD10(Initialize
, void(const scoped_refptr
<DemuxerStream
>& stream
,
141 const VideoDecoderList
& decoders
,
142 const PipelineStatusCB
& init_cb
,
143 const StatisticsCB
& statistics_cb
,
144 const TimeCB
& time_cb
,
145 const NaturalSizeChangedCB
& size_changed_cb
,
146 const base::Closure
& ended_cb
,
147 const PipelineStatusCB
& error_cb
,
148 const TimeDeltaCB
& get_time_cb
,
149 const TimeDeltaCB
& get_duration_cb
));
150 MOCK_METHOD1(Play
, void(const base::Closure
& callback
));
151 MOCK_METHOD1(Pause
, void(const base::Closure
& callback
));
152 MOCK_METHOD1(Flush
, void(const base::Closure
& callback
));
153 MOCK_METHOD2(Preroll
, void(base::TimeDelta time
, const PipelineStatusCB
& cb
));
154 MOCK_METHOD1(Stop
, void(const base::Closure
& callback
));
155 MOCK_METHOD1(SetPlaybackRate
, void(float playback_rate
));
158 virtual ~MockVideoRenderer();
161 DISALLOW_COPY_AND_ASSIGN(MockVideoRenderer
);
164 class MockAudioRenderer
: public AudioRenderer
{
168 // AudioRenderer implementation.
169 MOCK_METHOD9(Initialize
, void(const scoped_refptr
<DemuxerStream
>& stream
,
170 const AudioDecoderList
& decoders
,
171 const PipelineStatusCB
& init_cb
,
172 const StatisticsCB
& statistics_cb
,
173 const base::Closure
& underflow_cb
,
174 const TimeCB
& time_cb
,
175 const base::Closure
& ended_cb
,
176 const base::Closure
& disabled_cb
,
177 const PipelineStatusCB
& error_cb
));
178 MOCK_METHOD1(Play
, void(const base::Closure
& callback
));
179 MOCK_METHOD1(Pause
, void(const base::Closure
& callback
));
180 MOCK_METHOD1(Flush
, void(const base::Closure
& callback
));
181 MOCK_METHOD1(Stop
, void(const base::Closure
& callback
));
182 MOCK_METHOD1(SetPlaybackRate
, void(float playback_rate
));
183 MOCK_METHOD2(Preroll
, void(base::TimeDelta time
, const PipelineStatusCB
& cb
));
184 MOCK_METHOD1(SetVolume
, void(float volume
));
185 MOCK_METHOD1(ResumeAfterUnderflow
, void(bool buffer_more_audio
));
188 virtual ~MockAudioRenderer();
191 DISALLOW_COPY_AND_ASSIGN(MockAudioRenderer
);
194 class MockDecryptor
: public Decryptor
{
197 virtual ~MockDecryptor();
199 MOCK_METHOD4(GenerateKeyRequest
, bool(const std::string
& key_system
,
200 const std::string
& type
,
201 const uint8
* init_data
,
202 int init_data_length
));
203 MOCK_METHOD6(AddKey
, void(const std::string
& key_system
,
206 const uint8
* init_data
,
207 int init_data_length
,
208 const std::string
& session_id
));
209 MOCK_METHOD2(CancelKeyRequest
, void(const std::string
& key_system
,
210 const std::string
& session_id
));
211 MOCK_METHOD3(Decrypt
, void(StreamType stream_type
,
212 const scoped_refptr
<DecoderBuffer
>& encrypted
,
213 const DecryptCB
& decrypt_cb
));
214 MOCK_METHOD1(CancelDecrypt
, void(StreamType stream_type
));
215 // TODO(xhwang): The following two methods are workarounds of the issue that
216 // move-only parameters are not supported in mocked methods. Remove when the
217 // issue is fixed: http://code.google.com/p/googletest/issues/detail?id=395
218 MOCK_METHOD3(InitializeAudioDecoderMock
,
219 void(const AudioDecoderConfig
& config
,
220 const DecoderInitCB
& init_cb
,
221 const KeyAddedCB
& key_added_cb
));
222 MOCK_METHOD3(InitializeVideoDecoderMock
,
223 void(const VideoDecoderConfig
& config
,
224 const DecoderInitCB
& init_cb
,
225 const KeyAddedCB
& key_added_cb
));
226 MOCK_METHOD2(DecryptAndDecodeAudio
,
227 void(const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
228 const AudioDecodeCB
& audio_decode_cb
));
229 MOCK_METHOD2(DecryptAndDecodeVideo
,
230 void(const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
231 const VideoDecodeCB
& video_decode_cb
));
232 MOCK_METHOD1(ResetDecoder
, void(StreamType stream_type
));
233 MOCK_METHOD1(DeinitializeDecoder
, void(StreamType stream_type
));
235 virtual void InitializeAudioDecoder(scoped_ptr
<AudioDecoderConfig
> config
,
236 const DecoderInitCB
& init_cb
,
237 const KeyAddedCB
& key_added_cb
) OVERRIDE
;
238 virtual void InitializeVideoDecoder(scoped_ptr
<VideoDecoderConfig
> config
,
239 const DecoderInitCB
& init_cb
,
240 const KeyAddedCB
& key_added_cb
) OVERRIDE
;
243 DISALLOW_COPY_AND_ASSIGN(MockDecryptor
);
246 class MockDecryptorClient
: public DecryptorClient
{
248 MockDecryptorClient();
249 virtual ~MockDecryptorClient();
251 MOCK_METHOD2(KeyAdded
, void(const std::string
&, const std::string
&));
252 MOCK_METHOD4(KeyError
, void(const std::string
&, const std::string
&,
253 Decryptor::KeyError
, int));
254 // TODO(xhwang): This is a workaround of the issue that move-only parameters
255 // are not supported in mocked methods. Remove this when the issue is fixed
256 // (http://code.google.com/p/googletest/issues/detail?id=395) or when we use
257 // std::string instead of scoped_array<uint8> (http://crbug.com/130689).
258 MOCK_METHOD5(KeyMessageMock
, void(const std::string
& key_system
,
259 const std::string
& session_id
,
260 const uint8
* message
,
262 const std::string
& default_url
));
263 MOCK_METHOD5(NeedKeyMock
, void(const std::string
& key_system
,
264 const std::string
& session_id
,
265 const std::string
& type
,
266 const uint8
* init_data
,
267 int init_data_length
));
268 virtual void KeyMessage(const std::string
& key_system
,
269 const std::string
& session_id
,
270 scoped_array
<uint8
> message
,
272 const std::string
& default_url
) OVERRIDE
;
273 virtual void NeedKey(const std::string
& key_system
,
274 const std::string
& session_id
,
275 const std::string
& type
,
276 scoped_array
<uint8
> init_data
,
277 int init_data_length
) OVERRIDE
;
280 DISALLOW_COPY_AND_ASSIGN(MockDecryptorClient
);
283 // FilterFactory that returns canned instances of mock filters. You can set
284 // expectations on the filters and then pass the collection into a pipeline.
285 class MockFilterCollection
{
287 MockFilterCollection();
288 virtual ~MockFilterCollection();
291 MockDemuxer
* demuxer() const { return demuxer_
; }
292 MockVideoDecoder
* video_decoder() const { return video_decoder_
; }
293 MockAudioDecoder
* audio_decoder() const { return audio_decoder_
; }
294 MockVideoRenderer
* video_renderer() const { return video_renderer_
; }
295 MockAudioRenderer
* audio_renderer() const { return audio_renderer_
; }
297 // Creates the FilterCollection containing the mocks.
298 scoped_ptr
<FilterCollection
> Create();
301 scoped_refptr
<MockDemuxer
> demuxer_
;
302 scoped_refptr
<MockVideoDecoder
> video_decoder_
;
303 scoped_refptr
<MockAudioDecoder
> audio_decoder_
;
304 scoped_refptr
<MockVideoRenderer
> video_renderer_
;
305 scoped_refptr
<MockAudioRenderer
> audio_renderer_
;
307 DISALLOW_COPY_AND_ASSIGN(MockFilterCollection
);
314 // Helper mock statistics callback.
315 class MockStatisticsCB
{
320 MOCK_METHOD1(OnStatistics
, void(const media::PipelineStatistics
& statistics
));
325 #endif // MEDIA_BASE_MOCK_FILTERS_H_