[BackgroundSync] Clean up some tests
[chromium-blink-merge.git] / media / base / test_helpers.h
blob712812fc4f6b257c8f3f7e8cdd133b2082a605ce
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 RunLoop;
21 class TimeDelta;
24 namespace media {
26 class AudioBuffer;
27 class DecoderBuffer;
29 // Return a callback that expects to be run once.
30 base::Closure NewExpectedClosure();
31 PipelineStatusCB NewExpectedStatusCB(PipelineStatus status);
33 // Helper class for running a message loop until a callback has run. Useful for
34 // testing classes that run on more than a single thread.
36 // Events are intended for single use and cannot be reset.
37 class WaitableMessageLoopEvent {
38 public:
39 WaitableMessageLoopEvent();
40 ~WaitableMessageLoopEvent();
42 // Returns a thread-safe closure that will signal |this| when executed.
43 base::Closure GetClosure();
44 PipelineStatusCB GetPipelineStatusCB();
46 // Runs the current message loop until |this| has been signaled.
48 // Fails the test if the timeout is reached.
49 void RunAndWait();
51 // Runs the current message loop until |this| has been signaled and asserts
52 // that the |expected| status was received.
54 // Fails the test if the timeout is reached.
55 void RunAndWaitForStatus(PipelineStatus expected);
57 bool is_signaled() const { return signaled_; }
59 private:
60 void OnCallback(PipelineStatus status);
61 void OnTimeout();
63 base::MessageLoop* message_loop_;
64 bool signaled_;
65 PipelineStatus status_;
66 scoped_ptr<base::RunLoop> run_loop_;
68 DISALLOW_COPY_AND_ASSIGN(WaitableMessageLoopEvent);
71 // Provides pre-canned VideoDecoderConfig. These types are used for tests that
72 // don't care about detailed parameters of the config.
73 class TestVideoConfig {
74 public:
75 // Returns a configuration that is invalid.
76 static VideoDecoderConfig Invalid();
78 static VideoDecoderConfig Normal();
79 static VideoDecoderConfig NormalEncrypted();
81 // Returns a configuration that is larger in dimensions than Normal().
82 static VideoDecoderConfig Large();
83 static VideoDecoderConfig LargeEncrypted();
85 // Returns coded size for Normal and Large config.
86 static gfx::Size NormalCodedSize();
87 static gfx::Size LargeCodedSize();
89 private:
90 DISALLOW_IMPLICIT_CONSTRUCTORS(TestVideoConfig);
93 // Create an AudioBuffer containing |frames| frames of data, where each sample
94 // is of type T. |start| and |increment| are used to specify the values for the
95 // samples, which are created in channel order. The value for frame and channel
96 // is determined by:
98 // |start| + |channel| * |frames| * |increment| + index * |increment|
100 // E.g., for a stereo buffer the values in channel 0 will be:
101 // start
102 // start + increment
103 // start + 2 * increment, ...
105 // While, values in channel 1 will be:
106 // start + frames * increment
107 // start + (frames + 1) * increment
108 // start + (frames + 2) * increment, ...
110 // |start_time| will be used as the start time for the samples.
111 template <class T>
112 scoped_refptr<AudioBuffer> MakeAudioBuffer(SampleFormat format,
113 ChannelLayout channel_layout,
114 size_t channel_count,
115 int sample_rate,
116 T start,
117 T increment,
118 size_t frames,
119 base::TimeDelta timestamp);
121 // Create a fake video DecoderBuffer for testing purpose. The buffer contains
122 // part of video decoder config info embedded so that the testing code can do
123 // some sanity check.
124 scoped_refptr<DecoderBuffer> CreateFakeVideoBufferForTest(
125 const VideoDecoderConfig& config,
126 base::TimeDelta timestamp,
127 base::TimeDelta duration);
129 // Verify if a fake video DecoderBuffer is valid.
130 bool VerifyFakeVideoBufferForTest(const scoped_refptr<DecoderBuffer>& buffer,
131 const VideoDecoderConfig& config);
133 // Used to verify that the each call to A() is followed by a call to B(),
134 // before the next call to A(). There may be any number of pairs (including 0).
135 class CallbackPairChecker {
136 public:
137 CallbackPairChecker();
138 ~CallbackPairChecker();
139 void RecordACalled();
140 void RecordBCalled();
142 private:
143 bool expecting_b_;
146 // Test implementation of a media log LogCB that sends media log messages to
147 // DVLOG(1).
148 void AddLogEntryForTest(MediaLog::MediaLogLevel level,
149 const std::string& message);
151 } // namespace media
153 #endif // MEDIA_BASE_TEST_HELPERS_H_