Delete unused downloads page asset.
[chromium-blink-merge.git] / media / base / test_helpers.h
blobf9df1bded6f53c66e99f6d2131dbfae2bcefa226
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 base::Callback<void(bool)> NewExpectedBoolCB(bool success);
32 PipelineStatusCB NewExpectedStatusCB(PipelineStatus status);
34 // Helper class for running a message loop until a callback has run. Useful for
35 // testing classes that run on more than a single thread.
37 // Events are intended for single use and cannot be reset.
38 class WaitableMessageLoopEvent {
39 public:
40 WaitableMessageLoopEvent();
41 ~WaitableMessageLoopEvent();
43 // Returns a thread-safe closure that will signal |this| when executed.
44 base::Closure GetClosure();
45 PipelineStatusCB GetPipelineStatusCB();
47 // Runs the current message loop until |this| has been signaled.
49 // Fails the test if the timeout is reached.
50 void RunAndWait();
52 // Runs the current message loop until |this| has been signaled and asserts
53 // that the |expected| status was received.
55 // Fails the test if the timeout is reached.
56 void RunAndWaitForStatus(PipelineStatus expected);
58 bool is_signaled() const { return signaled_; }
60 private:
61 void OnCallback(PipelineStatus status);
62 void OnTimeout();
64 base::MessageLoop* message_loop_;
65 bool signaled_;
66 PipelineStatus status_;
67 scoped_ptr<base::RunLoop> run_loop_;
69 DISALLOW_COPY_AND_ASSIGN(WaitableMessageLoopEvent);
72 // Provides pre-canned VideoDecoderConfig. These types are used for tests that
73 // don't care about detailed parameters of the config.
74 class TestVideoConfig {
75 public:
76 // Returns a configuration that is invalid.
77 static VideoDecoderConfig Invalid();
79 static VideoDecoderConfig Normal();
80 static VideoDecoderConfig NormalEncrypted();
82 // Returns a configuration that is larger in dimensions than Normal().
83 static VideoDecoderConfig Large();
84 static VideoDecoderConfig LargeEncrypted();
86 // Returns coded size for Normal and Large config.
87 static gfx::Size NormalCodedSize();
88 static gfx::Size LargeCodedSize();
90 private:
91 DISALLOW_IMPLICIT_CONSTRUCTORS(TestVideoConfig);
94 // Create an AudioBuffer containing |frames| frames of data, where each sample
95 // is of type T. |start| and |increment| are used to specify the values for the
96 // samples, which are created in channel order. The value for frame and channel
97 // is determined by:
99 // |start| + |channel| * |frames| * |increment| + index * |increment|
101 // E.g., for a stereo buffer the values in channel 0 will be:
102 // start
103 // start + increment
104 // start + 2 * increment, ...
106 // While, values in channel 1 will be:
107 // start + frames * increment
108 // start + (frames + 1) * increment
109 // start + (frames + 2) * increment, ...
111 // |start_time| will be used as the start time for the samples.
112 template <class T>
113 scoped_refptr<AudioBuffer> MakeAudioBuffer(SampleFormat format,
114 ChannelLayout channel_layout,
115 size_t channel_count,
116 int sample_rate,
117 T start,
118 T increment,
119 size_t frames,
120 base::TimeDelta timestamp);
122 // Create a fake video DecoderBuffer for testing purpose. The buffer contains
123 // part of video decoder config info embedded so that the testing code can do
124 // some sanity check.
125 scoped_refptr<DecoderBuffer> CreateFakeVideoBufferForTest(
126 const VideoDecoderConfig& config,
127 base::TimeDelta timestamp,
128 base::TimeDelta duration);
130 // Verify if a fake video DecoderBuffer is valid.
131 bool VerifyFakeVideoBufferForTest(const scoped_refptr<DecoderBuffer>& buffer,
132 const VideoDecoderConfig& config);
134 // Used to verify that the each call to A() is followed by a call to B(),
135 // before the next call to A(). There may be any number of pairs (including 0).
136 class CallbackPairChecker {
137 public:
138 CallbackPairChecker();
139 ~CallbackPairChecker();
140 void RecordACalled();
141 void RecordBCalled();
143 private:
144 bool expecting_b_;
147 } // namespace media
149 #endif // MEDIA_BASE_TEST_HELPERS_H_