summaryrefslogtreecommitdiffstats
path: root/media/base/mock_filters.h
blob: bffba6ff9cad7c703a8a4ab8e17e32b5deb6c531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// 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.
//
// A new breed of mock media filters, this time using gmock!  Feel free to add
// actions if you need interesting side-effects (i.e., copying data to the
// buffer passed into MockDataSource::Read()).
//
// Don't forget you can use StrictMock<> and NiceMock<> if you want the mock
// filters to fail the test or do nothing when an unexpected method is called.
// http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks

#ifndef MEDIA_BASE_MOCK_FILTERS_H_
#define MEDIA_BASE_MOCK_FILTERS_H_

#include "media/base/factory.h"
#include "media/base/filters.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace media {

// Use this template to test for object destruction by setting expectations on
// the method OnDestroy().
//
// TODO(scherkus): not sure about the naming...  perhaps contribute this back
// to gmock itself!
template<class MockClass>
class Destroyable : public MockClass {
 public:
  Destroyable() {}

  MOCK_METHOD0(OnDestroy, void());

 protected:
  virtual ~Destroyable() {
    OnDestroy();
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(Destroyable);
};

class MockDataSource : public DataSource {
 public:
  MockDataSource() {}

  // MediaFilter implementation.
  MOCK_METHOD0(Stop, void());
  MOCK_METHOD1(SetPlaybackRate, void(float playback_rate));
  MOCK_METHOD1(Seek, void(base::TimeDelta time));

  // DataSource implementation.
  MOCK_METHOD1(Initialize, bool(const std::string& url));
  const MediaFormat& media_format() { return media_format_; }
  MOCK_METHOD2(Read, size_t(uint8* data, size_t size));
  MOCK_METHOD1(GetPosition, bool(int64* position_out));
  MOCK_METHOD1(SetPosition, bool(int64 position));
  MOCK_METHOD1(GetSize, bool(int64* size_out));
  MOCK_METHOD0(IsSeekable, bool());

 protected:
  virtual ~MockDataSource() {}

 private:
  MediaFormat media_format_;

  DISALLOW_COPY_AND_ASSIGN(MockDataSource);
};

class MockDemuxer : public Demuxer {
 public:
  MockDemuxer() {}

  // MediaFilter implementation.
  MOCK_METHOD0(Stop, void());
  MOCK_METHOD1(SetPlaybackRate, void(float playback_rate));
  MOCK_METHOD1(Seek, void(base::TimeDelta time));

  // Demuxer implementation.
  MOCK_METHOD1(Initialize, bool(DataSource* data_source));
  MOCK_METHOD0(GetNumberOfStreams, size_t());
  MOCK_METHOD1(GetStream, scoped_refptr<DemuxerStream>(int stream_id));

 protected:
  virtual ~MockDemuxer() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(MockDemuxer);
};

class MockDemuxerStream : public DemuxerStream {
 public:
  MockDemuxerStream() {}

  // Sets the mime type of this object's media format, which is usually checked
  // to determine the type of decoder to create.
  explicit MockDemuxerStream(const std::string& mime_type) {
    media_format_.SetAsString(MediaFormat::kMimeType, mime_type);
  }

  // DemuxerStream implementation.
  const MediaFormat& media_format() { return media_format_; }
  MOCK_METHOD1(Read, void(Callback1<Buffer*>::Type* read_callback));
  MOCK_METHOD1(QueryInterface, void*(const char* interface_id));

 protected:
  virtual ~MockDemuxerStream() {}

 private:
  MediaFormat media_format_;

  DISALLOW_COPY_AND_ASSIGN(MockDemuxerStream);
};

class MockVideoDecoder : public VideoDecoder {
 public:
  MockVideoDecoder() {}

  // MediaFilter implementation.
  MOCK_METHOD0(Stop, void());
  MOCK_METHOD1(SetPlaybackRate, void(float playback_rate));
  MOCK_METHOD1(Seek, void(base::TimeDelta time));

  // VideoDecoder implementation.
  MOCK_METHOD1(Initialize, bool(DemuxerStream* demuxer_stream));
  const MediaFormat& media_format() { return media_format_; }
  MOCK_METHOD1(Read, void(Callback1<VideoFrame*>::Type* read_callback));

 protected:
  virtual ~MockVideoDecoder() {}

 private:
  MediaFormat media_format_;

  DISALLOW_COPY_AND_ASSIGN(MockVideoDecoder);
};

class MockAudioDecoder : public AudioDecoder {
 public:
  MockAudioDecoder() {}

  // MediaFilter implementation.
  MOCK_METHOD0(Stop, void());
  MOCK_METHOD1(SetPlaybackRate, void(float playback_rate));
  MOCK_METHOD1(Seek, void(base::TimeDelta time));

  // AudioDecoder implementation.
  MOCK_METHOD1(Initialize, bool(DemuxerStream* demuxer_stream));
  const MediaFormat& media_format() { return media_format_; }
  MOCK_METHOD1(Read, void(Callback1<Buffer*>::Type* read_callback));

 protected:
  virtual ~MockAudioDecoder() {}

 private:
  MediaFormat media_format_;

  DISALLOW_COPY_AND_ASSIGN(MockAudioDecoder);
};

class MockVideoRenderer : public VideoRenderer {
 public:
  MockVideoRenderer() {}

  // MediaFilter implementation.
  MOCK_METHOD0(Stop, void());
  MOCK_METHOD1(SetPlaybackRate, void(float playback_rate));
  MOCK_METHOD1(Seek, void(base::TimeDelta time));

  // VideoRenderer implementation.
  MOCK_METHOD1(Initialize, bool(VideoDecoder* decoder));

 protected:
  virtual ~MockVideoRenderer() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(MockVideoRenderer);
};

class MockAudioRenderer : public AudioRenderer {
 public:
  MockAudioRenderer() {}

  // MediaFilter implementation.
  MOCK_METHOD0(Stop, void());
  MOCK_METHOD1(SetPlaybackRate, void(float playback_rate));
  MOCK_METHOD1(Seek, void(base::TimeDelta time));

  // AudioRenderer implementation.
  MOCK_METHOD1(Initialize, bool(AudioDecoder* decoder));
  MOCK_METHOD1(SetVolume, void(float volume));

 protected:
  virtual ~MockAudioRenderer() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(MockAudioRenderer);
};

// FilterFactory that returns canned instances of mock filters.  You can set
// expectations on the filters and then pass the factory into a pipeline.
class MockFilterFactory : public FilterFactory {
 public:
  MockFilterFactory()
      : creation_successful_(true),
        data_source_(new MockDataSource()),
        demuxer_(new MockDemuxer()),
        video_decoder_(new MockVideoDecoder()),
        audio_decoder_(new MockAudioDecoder()),
        video_renderer_(new MockVideoRenderer()),
        audio_renderer_(new MockAudioRenderer()) {
  }

  virtual ~MockFilterFactory() {}

  // Controls whether the Create() method is successful or not.
  void set_creation_successful(bool creation_successful) {
    creation_successful_ = creation_successful;
  }

  // Mock accessors.
  MockDataSource* data_source() const { return data_source_; }
  MockDemuxer* demuxer() const { return demuxer_; }
  MockVideoDecoder* video_decoder() const { return video_decoder_; }
  MockAudioDecoder* audio_decoder() const { return audio_decoder_; }
  MockVideoRenderer* video_renderer() const { return video_renderer_; }
  MockAudioRenderer* audio_renderer() const { return audio_renderer_; }

 protected:
  MediaFilter* Create(FilterType filter_type, const MediaFormat& media_format) {
    if (!creation_successful_) {
      return NULL;
    }

    switch (filter_type) {
      case FILTER_DATA_SOURCE:
        return data_source_;
      case FILTER_DEMUXER:
        return demuxer_;
      case FILTER_VIDEO_DECODER:
        return video_decoder_;
      case FILTER_AUDIO_DECODER:
        return audio_decoder_;
      case FILTER_VIDEO_RENDERER:
        return video_renderer_;
      case FILTER_AUDIO_RENDERER:
        return audio_renderer_;
      default:
        NOTREACHED() << "Unknown filter type: " << filter_type;
    }
    return NULL;
  }

 private:
  bool creation_successful_;
  scoped_refptr<MockDataSource> data_source_;
  scoped_refptr<MockDemuxer> demuxer_;
  scoped_refptr<MockVideoDecoder> video_decoder_;
  scoped_refptr<MockAudioDecoder> audio_decoder_;
  scoped_refptr<MockVideoRenderer> video_renderer_;
  scoped_refptr<MockAudioRenderer> audio_renderer_;

  DISALLOW_COPY_AND_ASSIGN(MockFilterFactory);
};

// Helper gmock action that calls InitializationComplete() on behalf of the
// provided filter.
ACTION_P(InitializationComplete, filter) {
  filter->host()->InitializationComplete();
}

// Helper gmock action that calls Error() on behalf of the provided filter.
ACTION_P2(Error, filter, error) {
  filter->host()->Error(error);
}

}  // namespace media

#endif  // MEDIA_BASE_MOCK_FILTERS_H_