Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / media / base / test_helpers.h
bloba2c29544ffb4d47967c0b9e782ce5222b2d89299
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"
18 namespace base {
19 class MessageLoop;
20 class TimeDelta;
23 namespace media {
25 class AudioBuffer;
26 class DecoderBuffer;
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 {
37 public:
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.
48 void RunAndWait();
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);
56 private:
57 void OnCallback(PipelineStatus status);
58 void OnTimeout();
60 base::MessageLoop* message_loop_;
61 bool signaled_;
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 {
70 public:
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();
85 private:
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
92 // is determined by:
94 // |start| + |channel| * |frames| * |increment| + index * |increment|
96 // E.g., for a stereo buffer the values in channel 0 will be:
97 // start
98 // start + increment
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.
107 template <class T>
108 scoped_refptr<AudioBuffer> MakeAudioBuffer(SampleFormat format,
109 ChannelLayout channel_layout,
110 size_t channel_count,
111 int sample_rate,
112 T start,
113 T increment,
114 size_t frames,
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 {
132 public:
133 CallbackPairChecker();
134 ~CallbackPairChecker();
135 void RecordACalled();
136 void RecordBCalled();
138 private:
139 bool expecting_b_;
142 // Test implementation of a media log LogCB that sends media log messages to
143 // DVLOG(1).
144 void AddLogEntryForTest(MediaLog::MediaLogLevel level,
145 const std::string& message);
147 } // namespace media
149 #endif // MEDIA_BASE_TEST_HELPERS_H_