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.
4
5#ifndef MEDIA_BASE_TEST_HELPERS_H_
6#define MEDIA_BASE_TEST_HELPERS_H_
7
8#include "base/basictypes.h"
9#include "base/callback.h"
10#include "media/base/channel_layout.h"
11#include "media/base/pipeline_status.h"
12#include "media/base/sample_format.h"
13#include "media/base/video_decoder_config.h"
14#include "testing/gmock/include/gmock/gmock.h"
15#include "ui/gfx/size.h"
16
17namespace base {
18class MessageLoop;
19class TimeDelta;
20}
21
22namespace media {
23
24class AudioBuffer;
25class DecoderBuffer;
26
27// Return a callback that expects to be run once.
28base::Closure NewExpectedClosure();
29PipelineStatusCB NewExpectedStatusCB(PipelineStatus status);
30
31// Helper class for running a message loop until a callback has run. Useful for
32// testing classes that run on more than a single thread.
33//
34// Events are intended for single use and cannot be reset.
35class WaitableMessageLoopEvent {
36 public:
37  WaitableMessageLoopEvent();
38  ~WaitableMessageLoopEvent();
39
40  // Returns a thread-safe closure that will signal |this| when executed.
41  base::Closure GetClosure();
42  PipelineStatusCB GetPipelineStatusCB();
43
44  // Runs the current message loop until |this| has been signaled.
45  //
46  // Fails the test if the timeout is reached.
47  void RunAndWait();
48
49  // Runs the current message loop until |this| has been signaled and asserts
50  // that the |expected| status was received.
51  //
52  // Fails the test if the timeout is reached.
53  void RunAndWaitForStatus(PipelineStatus expected);
54
55 private:
56  void OnCallback(PipelineStatus status);
57  void OnTimeout();
58
59  base::MessageLoop* message_loop_;
60  bool signaled_;
61  PipelineStatus status_;
62
63  DISALLOW_COPY_AND_ASSIGN(WaitableMessageLoopEvent);
64};
65
66// Provides pre-canned VideoDecoderConfig. These types are used for tests that
67// don't care about detailed parameters of the config.
68class TestVideoConfig {
69 public:
70  // Returns a configuration that is invalid.
71  static VideoDecoderConfig Invalid();
72
73  static VideoDecoderConfig Normal();
74  static VideoDecoderConfig NormalEncrypted();
75
76  // Returns a configuration that is larger in dimensions than Normal().
77  static VideoDecoderConfig Large();
78  static VideoDecoderConfig LargeEncrypted();
79
80  // Returns coded size for Normal and Large config.
81  static gfx::Size NormalCodedSize();
82  static gfx::Size LargeCodedSize();
83
84 private:
85  DISALLOW_IMPLICIT_CONSTRUCTORS(TestVideoConfig);
86};
87
88// Create an AudioBuffer containing |frames| frames of data, where each sample
89// is of type T.  |start| and |increment| are used to specify the values for the
90// samples, which are created in channel order.  The value for frame and channel
91// is determined by:
92//
93//   |start| + |channel| * |frames| * |increment| + index * |increment|
94//
95// E.g., for a stereo buffer the values in channel 0 will be:
96//   start
97//   start + increment
98//   start + 2 * increment, ...
99//
100// While, values in channel 1 will be:
101//   start + frames * increment
102//   start + (frames + 1) * increment
103//   start + (frames + 2) * increment, ...
104//
105// |start_time| will be used as the start time for the samples.
106template <class T>
107scoped_refptr<AudioBuffer> MakeAudioBuffer(SampleFormat format,
108                                           ChannelLayout channel_layout,
109                                           size_t channel_count,
110                                           int sample_rate,
111                                           T start,
112                                           T increment,
113                                           size_t frames,
114                                           base::TimeDelta timestamp);
115
116// Create a fake video DecoderBuffer for testing purpose. The buffer contains
117// part of video decoder config info embedded so that the testing code can do
118// some sanity check.
119scoped_refptr<DecoderBuffer> CreateFakeVideoBufferForTest(
120    const VideoDecoderConfig& config,
121    base::TimeDelta timestamp,
122    base::TimeDelta duration);
123
124// Verify if a fake video DecoderBuffer is valid.
125bool VerifyFakeVideoBufferForTest(const scoped_refptr<DecoderBuffer>& buffer,
126                                  const VideoDecoderConfig& config);
127
128// Used to verify that the each call to A() is followed by a call to B(),
129// before the next call to A(). There may be any number of pairs (including 0).
130class CallbackPairChecker {
131 public:
132  CallbackPairChecker();
133  ~CallbackPairChecker();
134  void RecordACalled();
135  void RecordBCalled();
136
137 private:
138  bool expecting_b_;
139};
140
141}  // namespace media
142
143#endif  // MEDIA_BASE_TEST_HELPERS_H_
144