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 #ifndef MEDIA_BASE_TEST_HELPERS_H_
6 #define MEDIA_BASE_TEST_HELPERS_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "media/base/channel_layout.h"
11 #include "media/base/media_log.h"
12 #include "media/base/pipeline_status.h"
13 #include "media/base/sample_format.h"
14 #include "media/base/video_decoder_config.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "ui/gfx/geometry/size.h"
28 // Return a callback that expects to be run once.
29 base::Closure
NewExpectedClosure();
30 PipelineStatusCB
NewExpectedStatusCB(PipelineStatus status
);
32 // Helper class for running a message loop until a callback has run. Useful for
33 // testing classes that run on more than a single thread.
35 // Events are intended for single use and cannot be reset.
36 class WaitableMessageLoopEvent
{
38 WaitableMessageLoopEvent();
39 ~WaitableMessageLoopEvent();
41 // Returns a thread-safe closure that will signal |this| when executed.
42 base::Closure
GetClosure();
43 PipelineStatusCB
GetPipelineStatusCB();
45 // Runs the current message loop until |this| has been signaled.
47 // Fails the test if the timeout is reached.
50 // Runs the current message loop until |this| has been signaled and asserts
51 // that the |expected| status was received.
53 // Fails the test if the timeout is reached.
54 void RunAndWaitForStatus(PipelineStatus expected
);
57 void OnCallback(PipelineStatus status
);
60 base::MessageLoop
* message_loop_
;
62 PipelineStatus status_
;
64 DISALLOW_COPY_AND_ASSIGN(WaitableMessageLoopEvent
);
67 // Provides pre-canned VideoDecoderConfig. These types are used for tests that
68 // don't care about detailed parameters of the config.
69 class TestVideoConfig
{
71 // Returns a configuration that is invalid.
72 static VideoDecoderConfig
Invalid();
74 static VideoDecoderConfig
Normal();
75 static VideoDecoderConfig
NormalEncrypted();
77 // Returns a configuration that is larger in dimensions than Normal().
78 static VideoDecoderConfig
Large();
79 static VideoDecoderConfig
LargeEncrypted();
81 // Returns coded size for Normal and Large config.
82 static gfx::Size
NormalCodedSize();
83 static gfx::Size
LargeCodedSize();
86 DISALLOW_IMPLICIT_CONSTRUCTORS(TestVideoConfig
);
89 // Create an AudioBuffer containing |frames| frames of data, where each sample
90 // is of type T. |start| and |increment| are used to specify the values for the
91 // samples, which are created in channel order. The value for frame and channel
94 // |start| + |channel| * |frames| * |increment| + index * |increment|
96 // E.g., for a stereo buffer the values in channel 0 will be:
99 // start + 2 * increment, ...
101 // While, values in channel 1 will be:
102 // start + frames * increment
103 // start + (frames + 1) * increment
104 // start + (frames + 2) * increment, ...
106 // |start_time| will be used as the start time for the samples.
108 scoped_refptr
<AudioBuffer
> MakeAudioBuffer(SampleFormat format
,
109 ChannelLayout channel_layout
,
110 size_t channel_count
,
115 base::TimeDelta timestamp
);
117 // Create a fake video DecoderBuffer for testing purpose. The buffer contains
118 // part of video decoder config info embedded so that the testing code can do
119 // some sanity check.
120 scoped_refptr
<DecoderBuffer
> CreateFakeVideoBufferForTest(
121 const VideoDecoderConfig
& config
,
122 base::TimeDelta timestamp
,
123 base::TimeDelta duration
);
125 // Verify if a fake video DecoderBuffer is valid.
126 bool VerifyFakeVideoBufferForTest(const scoped_refptr
<DecoderBuffer
>& buffer
,
127 const VideoDecoderConfig
& config
);
129 // Used to verify that the each call to A() is followed by a call to B(),
130 // before the next call to A(). There may be any number of pairs (including 0).
131 class CallbackPairChecker
{
133 CallbackPairChecker();
134 ~CallbackPairChecker();
135 void RecordACalled();
136 void RecordBCalled();
142 // Test implementation of a media log LogCB that sends media log messages to
144 void AddLogEntryForTest(MediaLog::MediaLogLevel level
,
145 const std::string
& message
);
149 #endif // MEDIA_BASE_TEST_HELPERS_H_