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