Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: media/base/mock_filters.h

Issue 149423: Converted remaining tests to use gmock and deleted all old mocking code. (Closed)
Patch Set: Fix again Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/base/mock_ffmpeg.cc ('k') | media/base/mock_media_filters.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in the 2 // source code is governed by a BSD-style license that can be found in the
3 // LICENSE file. 3 // LICENSE file.
4 // 4 //
5 // A new breed of mock media filters, this time using gmock! Feel free to add 5 // A new breed of mock media filters, this time using gmock! Feel free to add
6 // actions if you need interesting side-effects (i.e., copying data to the 6 // actions if you need interesting side-effects (i.e., copying data to the
7 // buffer passed into MockDataSource::Read()). 7 // buffer passed into MockDataSource::Read()).
8 // 8 //
9 // Don't forget you can use StrictMock<> and NiceMock<> if you want the mock 9 // Don't forget you can use StrictMock<> and NiceMock<> if you want the mock
10 // filters to fail the test or do nothing when an unexpected method is called. 10 // filters to fail the test or do nothing when an unexpected method is called.
11 // http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks 11 // http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks
12 12
13 #ifndef MEDIA_BASE_MOCK_FILTERS_H_ 13 #ifndef MEDIA_BASE_MOCK_FILTERS_H_
14 #define MEDIA_BASE_MOCK_FILTERS_H_ 14 #define MEDIA_BASE_MOCK_FILTERS_H_
15 15
16 #include "media/base/factory.h" 16 #include "media/base/factory.h"
17 #include "media/base/filters.h" 17 #include "media/base/filters.h"
18 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 19
20 namespace media { 20 namespace media {
21 21
22 // Use this template to test for object destruction by setting expectations on
23 // the method OnDestroy().
24 //
25 // TODO(scherkus): not sure about the naming... perhaps contribute this back
26 // to gmock itself!
27 template<class MockClass>
28 class Destroyable : public MockClass {
29 public:
30 Destroyable() {}
31
32 MOCK_METHOD0(OnDestroy, void());
33
34 protected:
35 virtual ~Destroyable() {
36 OnDestroy();
37 }
38
39 private:
40 DISALLOW_COPY_AND_ASSIGN(Destroyable);
41 };
42
22 class MockDataSource : public DataSource { 43 class MockDataSource : public DataSource {
23 public: 44 public:
24 MockDataSource() {} 45 MockDataSource() {}
25 46
26 // MediaFilter implementation. 47 // MediaFilter implementation.
27 MOCK_METHOD0(Stop, void()); 48 MOCK_METHOD0(Stop, void());
28 MOCK_METHOD1(SetPlaybackRate, void(float playback_rate)); 49 MOCK_METHOD1(SetPlaybackRate, void(float playback_rate));
29 MOCK_METHOD1(Seek, void(base::TimeDelta time)); 50 MOCK_METHOD1(Seek, void(base::TimeDelta time));
30 51
31 // DataSource implementation. 52 // DataSource implementation.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 virtual ~MockDemuxer() {} 85 virtual ~MockDemuxer() {}
65 86
66 private: 87 private:
67 DISALLOW_COPY_AND_ASSIGN(MockDemuxer); 88 DISALLOW_COPY_AND_ASSIGN(MockDemuxer);
68 }; 89 };
69 90
70 class MockDemuxerStream : public DemuxerStream { 91 class MockDemuxerStream : public DemuxerStream {
71 public: 92 public:
72 MockDemuxerStream() {} 93 MockDemuxerStream() {}
73 94
95 // Sets the mime type of this object's media format, which is usually checked
96 // to determine the type of decoder to create.
97 explicit MockDemuxerStream(const std::string& mime_type) {
98 media_format_.SetAsString(MediaFormat::kMimeType, mime_type);
99 }
100
74 // DemuxerStream implementation. 101 // DemuxerStream implementation.
75 const MediaFormat& media_format() { return media_format_; } 102 const MediaFormat& media_format() { return media_format_; }
76 MOCK_METHOD1(Read, void(Callback1<Buffer*>::Type* read_callback)); 103 MOCK_METHOD1(Read, void(Callback1<Buffer*>::Type* read_callback));
77 MOCK_METHOD1(QueryInterface, void*(const char* interface_id)); 104 MOCK_METHOD1(QueryInterface, void*(const char* interface_id));
78 105
79 protected: 106 protected:
80 virtual ~MockDemuxerStream() {} 107 virtual ~MockDemuxerStream() {}
81 108
82 private: 109 private:
83 MediaFormat media_format_; 110 MediaFormat media_format_;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 195
169 private: 196 private:
170 DISALLOW_COPY_AND_ASSIGN(MockAudioRenderer); 197 DISALLOW_COPY_AND_ASSIGN(MockAudioRenderer);
171 }; 198 };
172 199
173 // FilterFactory that returns canned instances of mock filters. You can set 200 // FilterFactory that returns canned instances of mock filters. You can set
174 // expectations on the filters and then pass the factory into a pipeline. 201 // expectations on the filters and then pass the factory into a pipeline.
175 class MockFilterFactory : public FilterFactory { 202 class MockFilterFactory : public FilterFactory {
176 public: 203 public:
177 MockFilterFactory() 204 MockFilterFactory()
178 : data_source_(new MockDataSource()), 205 : creation_successful_(true),
206 data_source_(new MockDataSource()),
179 demuxer_(new MockDemuxer()), 207 demuxer_(new MockDemuxer()),
180 video_decoder_(new MockVideoDecoder()), 208 video_decoder_(new MockVideoDecoder()),
181 audio_decoder_(new MockAudioDecoder()), 209 audio_decoder_(new MockAudioDecoder()),
182 video_renderer_(new MockVideoRenderer()), 210 video_renderer_(new MockVideoRenderer()),
183 audio_renderer_(new MockAudioRenderer()) { 211 audio_renderer_(new MockAudioRenderer()) {
184 } 212 }
185 213
186 virtual ~MockFilterFactory() {} 214 virtual ~MockFilterFactory() {}
187 215
216 // Controls whether the Create() method is successful or not.
217 void set_creation_successful(bool creation_successful) {
218 creation_successful_ = creation_successful;
219 }
220
188 // Mock accessors. 221 // Mock accessors.
189 MockDataSource* data_source() const { return data_source_; } 222 MockDataSource* data_source() const { return data_source_; }
190 MockDemuxer* demuxer() const { return demuxer_; } 223 MockDemuxer* demuxer() const { return demuxer_; }
191 MockVideoDecoder* video_decoder() const { return video_decoder_; } 224 MockVideoDecoder* video_decoder() const { return video_decoder_; }
192 MockAudioDecoder* audio_decoder() const { return audio_decoder_; } 225 MockAudioDecoder* audio_decoder() const { return audio_decoder_; }
193 MockVideoRenderer* video_renderer() const { return video_renderer_; } 226 MockVideoRenderer* video_renderer() const { return video_renderer_; }
194 MockAudioRenderer* audio_renderer() const { return audio_renderer_; } 227 MockAudioRenderer* audio_renderer() const { return audio_renderer_; }
195 228
196 protected: 229 protected:
197 MediaFilter* Create(FilterType filter_type, const MediaFormat& media_format) { 230 MediaFilter* Create(FilterType filter_type, const MediaFormat& media_format) {
231 if (!creation_successful_) {
232 return NULL;
233 }
234
198 switch (filter_type) { 235 switch (filter_type) {
199 case FILTER_DATA_SOURCE: 236 case FILTER_DATA_SOURCE:
200 return data_source_; 237 return data_source_;
201 case FILTER_DEMUXER: 238 case FILTER_DEMUXER:
202 return demuxer_; 239 return demuxer_;
203 case FILTER_VIDEO_DECODER: 240 case FILTER_VIDEO_DECODER:
204 return video_decoder_; 241 return video_decoder_;
205 case FILTER_AUDIO_DECODER: 242 case FILTER_AUDIO_DECODER:
206 return audio_decoder_; 243 return audio_decoder_;
207 case FILTER_VIDEO_RENDERER: 244 case FILTER_VIDEO_RENDERER:
208 return video_renderer_; 245 return video_renderer_;
209 case FILTER_AUDIO_RENDERER: 246 case FILTER_AUDIO_RENDERER:
210 return audio_renderer_; 247 return audio_renderer_;
211 default: 248 default:
212 NOTREACHED() << "Unknown filter type: " << filter_type; 249 NOTREACHED() << "Unknown filter type: " << filter_type;
213 } 250 }
214 return NULL; 251 return NULL;
215 } 252 }
216 253
217 private: 254 private:
255 bool creation_successful_;
218 scoped_refptr<MockDataSource> data_source_; 256 scoped_refptr<MockDataSource> data_source_;
219 scoped_refptr<MockDemuxer> demuxer_; 257 scoped_refptr<MockDemuxer> demuxer_;
220 scoped_refptr<MockVideoDecoder> video_decoder_; 258 scoped_refptr<MockVideoDecoder> video_decoder_;
221 scoped_refptr<MockAudioDecoder> audio_decoder_; 259 scoped_refptr<MockAudioDecoder> audio_decoder_;
222 scoped_refptr<MockVideoRenderer> video_renderer_; 260 scoped_refptr<MockVideoRenderer> video_renderer_;
223 scoped_refptr<MockAudioRenderer> audio_renderer_; 261 scoped_refptr<MockAudioRenderer> audio_renderer_;
224 262
225 DISALLOW_COPY_AND_ASSIGN(MockFilterFactory); 263 DISALLOW_COPY_AND_ASSIGN(MockFilterFactory);
226 }; 264 };
227 265
266 // Helper gmock action that calls InitializationComplete() on behalf of the
267 // provided filter.
268 ACTION_P(InitializationComplete, filter) {
269 filter->host()->InitializationComplete();
270 }
271
272 // Helper gmock action that calls Error() on behalf of the provided filter.
273 ACTION_P2(Error, filter, error) {
274 filter->host()->Error(error);
275 }
276
228 } // namespace media 277 } // namespace media
229 278
230 #endif // MEDIA_BASE_MOCK_FILTERS_H_ 279 #endif // MEDIA_BASE_MOCK_FILTERS_H_
OLDNEW
« no previous file with comments | « media/base/mock_ffmpeg.cc ('k') | media/base/mock_media_filters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698