Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / media / base / test_helpers.cc
blobbde12b4061aa83e4d3ae62671aacbae0400c0c1a
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 #include "media/base/test_helpers.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/pickle.h"
11 #include "base/run_loop.h"
12 #include "base/test/test_timeouts.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "media/base/audio_buffer.h"
16 #include "media/base/bind_to_current_loop.h"
17 #include "media/base/decoder_buffer.h"
18 #include "ui/gfx/geometry/rect.h"
20 using ::testing::_;
21 using ::testing::StrictMock;
23 namespace media {
25 // Utility mock for testing methods expecting Closures and PipelineStatusCBs.
26 class MockCallback : public base::RefCountedThreadSafe<MockCallback> {
27 public:
28 MockCallback();
29 MOCK_METHOD0(Run, void());
30 MOCK_METHOD1(RunWithStatus, void(PipelineStatus));
32 protected:
33 friend class base::RefCountedThreadSafe<MockCallback>;
34 virtual ~MockCallback();
36 private:
37 DISALLOW_COPY_AND_ASSIGN(MockCallback);
40 MockCallback::MockCallback() {}
41 MockCallback::~MockCallback() {}
43 base::Closure NewExpectedClosure() {
44 StrictMock<MockCallback>* callback = new StrictMock<MockCallback>();
45 EXPECT_CALL(*callback, Run());
46 return base::Bind(&MockCallback::Run, callback);
49 PipelineStatusCB NewExpectedStatusCB(PipelineStatus status) {
50 StrictMock<MockCallback>* callback = new StrictMock<MockCallback>();
51 EXPECT_CALL(*callback, RunWithStatus(status));
52 return base::Bind(&MockCallback::RunWithStatus, callback);
55 WaitableMessageLoopEvent::WaitableMessageLoopEvent()
56 : message_loop_(base::MessageLoop::current()),
57 signaled_(false),
58 status_(PIPELINE_OK) {
59 DCHECK(message_loop_);
62 WaitableMessageLoopEvent::~WaitableMessageLoopEvent() {}
64 base::Closure WaitableMessageLoopEvent::GetClosure() {
65 DCHECK_EQ(message_loop_, base::MessageLoop::current());
66 return BindToCurrentLoop(base::Bind(
67 &WaitableMessageLoopEvent::OnCallback, base::Unretained(this),
68 PIPELINE_OK));
71 PipelineStatusCB WaitableMessageLoopEvent::GetPipelineStatusCB() {
72 DCHECK_EQ(message_loop_, base::MessageLoop::current());
73 return BindToCurrentLoop(base::Bind(
74 &WaitableMessageLoopEvent::OnCallback, base::Unretained(this)));
77 void WaitableMessageLoopEvent::RunAndWait() {
78 RunAndWaitForStatus(PIPELINE_OK);
81 void WaitableMessageLoopEvent::RunAndWaitForStatus(PipelineStatus expected) {
82 DCHECK_EQ(message_loop_, base::MessageLoop::current());
83 if (signaled_) {
84 EXPECT_EQ(expected, status_);
85 return;
88 run_loop_.reset(new base::RunLoop());
89 base::Timer timer(false, false);
90 timer.Start(FROM_HERE, TestTimeouts::action_timeout(), base::Bind(
91 &WaitableMessageLoopEvent::OnTimeout, base::Unretained(this)));
93 run_loop_->Run();
94 EXPECT_TRUE(signaled_);
95 EXPECT_EQ(expected, status_);
96 run_loop_.reset();
99 void WaitableMessageLoopEvent::OnCallback(PipelineStatus status) {
100 DCHECK_EQ(message_loop_, base::MessageLoop::current());
101 signaled_ = true;
102 status_ = status;
104 // |run_loop_| may be null if the callback fires before RunAndWaitForStatus().
105 if (run_loop_)
106 run_loop_->Quit();
109 void WaitableMessageLoopEvent::OnTimeout() {
110 DCHECK_EQ(message_loop_, base::MessageLoop::current());
111 ADD_FAILURE() << "Timed out waiting for message loop to quit";
112 run_loop_->Quit();
115 static VideoDecoderConfig GetTestConfig(VideoCodec codec,
116 gfx::Size coded_size,
117 bool is_encrypted) {
118 gfx::Rect visible_rect(coded_size.width(), coded_size.height());
119 gfx::Size natural_size = coded_size;
121 return VideoDecoderConfig(codec, VIDEO_CODEC_PROFILE_UNKNOWN,
122 VideoFrame::YV12, coded_size, visible_rect, natural_size,
123 NULL, 0, is_encrypted);
126 static const gfx::Size kNormalSize(320, 240);
127 static const gfx::Size kLargeSize(640, 480);
129 VideoDecoderConfig TestVideoConfig::Invalid() {
130 return GetTestConfig(kUnknownVideoCodec, kNormalSize, false);
133 VideoDecoderConfig TestVideoConfig::Normal() {
134 return GetTestConfig(kCodecVP8, kNormalSize, false);
137 VideoDecoderConfig TestVideoConfig::NormalEncrypted() {
138 return GetTestConfig(kCodecVP8, kNormalSize, true);
141 VideoDecoderConfig TestVideoConfig::Large() {
142 return GetTestConfig(kCodecVP8, kLargeSize, false);
145 VideoDecoderConfig TestVideoConfig::LargeEncrypted() {
146 return GetTestConfig(kCodecVP8, kLargeSize, true);
149 gfx::Size TestVideoConfig::NormalCodedSize() {
150 return kNormalSize;
153 gfx::Size TestVideoConfig::LargeCodedSize() {
154 return kLargeSize;
157 template <class T>
158 scoped_refptr<AudioBuffer> MakeAudioBuffer(SampleFormat format,
159 ChannelLayout channel_layout,
160 size_t channel_count,
161 int sample_rate,
162 T start,
163 T increment,
164 size_t frames,
165 base::TimeDelta timestamp) {
166 const size_t channels = ChannelLayoutToChannelCount(channel_layout);
167 scoped_refptr<AudioBuffer> output =
168 AudioBuffer::CreateBuffer(format,
169 channel_layout,
170 static_cast<int>(channel_count),
171 sample_rate,
172 static_cast<int>(frames));
173 output->set_timestamp(timestamp);
175 const bool is_planar =
176 format == kSampleFormatPlanarS16 || format == kSampleFormatPlanarF32;
178 // Values in channel 0 will be:
179 // start
180 // start + increment
181 // start + 2 * increment, ...
182 // While, values in channel 1 will be:
183 // start + frames * increment
184 // start + (frames + 1) * increment
185 // start + (frames + 2) * increment, ...
186 for (size_t ch = 0; ch < channels; ++ch) {
187 T* buffer =
188 reinterpret_cast<T*>(output->channel_data()[is_planar ? ch : 0]);
189 const T v = static_cast<T>(start + ch * frames * increment);
190 for (size_t i = 0; i < frames; ++i) {
191 buffer[is_planar ? i : ch + i * channels] =
192 static_cast<T>(v + i * increment);
195 return output;
198 // Instantiate all the types of MakeAudioBuffer() and
199 // MakeAudioBuffer() needed.
200 #define DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(type) \
201 template scoped_refptr<AudioBuffer> MakeAudioBuffer<type>( \
202 SampleFormat format, \
203 ChannelLayout channel_layout, \
204 size_t channel_count, \
205 int sample_rate, \
206 type start, \
207 type increment, \
208 size_t frames, \
209 base::TimeDelta start_time)
210 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(uint8);
211 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(int16);
212 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(int32);
213 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(float);
215 static const char kFakeVideoBufferHeader[] = "FakeVideoBufferForTest";
217 scoped_refptr<DecoderBuffer> CreateFakeVideoBufferForTest(
218 const VideoDecoderConfig& config,
219 base::TimeDelta timestamp, base::TimeDelta duration) {
220 Pickle pickle;
221 pickle.WriteString(kFakeVideoBufferHeader);
222 pickle.WriteInt(config.coded_size().width());
223 pickle.WriteInt(config.coded_size().height());
224 pickle.WriteInt64(timestamp.InMilliseconds());
226 scoped_refptr<DecoderBuffer> buffer = DecoderBuffer::CopyFrom(
227 static_cast<const uint8*>(pickle.data()),
228 static_cast<int>(pickle.size()));
229 buffer->set_timestamp(timestamp);
230 buffer->set_duration(duration);
231 buffer->set_is_key_frame(true);
233 return buffer;
236 bool VerifyFakeVideoBufferForTest(
237 const scoped_refptr<DecoderBuffer>& buffer,
238 const VideoDecoderConfig& config) {
239 // Check if the input |buffer| matches the |config|.
240 PickleIterator pickle(Pickle(reinterpret_cast<const char*>(buffer->data()),
241 buffer->data_size()));
242 std::string header;
243 int width = 0;
244 int height = 0;
245 bool success = pickle.ReadString(&header) && pickle.ReadInt(&width) &&
246 pickle.ReadInt(&height);
247 return (success && header == kFakeVideoBufferHeader &&
248 width == config.coded_size().width() &&
249 height == config.coded_size().height());
252 CallbackPairChecker::CallbackPairChecker() : expecting_b_(false) {
255 CallbackPairChecker::~CallbackPairChecker() {
256 EXPECT_FALSE(expecting_b_);
259 void CallbackPairChecker::RecordACalled() {
260 EXPECT_FALSE(expecting_b_);
261 expecting_b_ = true;
264 void CallbackPairChecker::RecordBCalled() {
265 EXPECT_TRUE(expecting_b_);
266 expecting_b_ = false;
269 void AddLogEntryForTest(MediaLog::MediaLogLevel level,
270 const std::string& message) {
271 DVLOG(1) << "Media log (" << MediaLog::MediaLogLevelToString(level)
272 << "): " << message;
275 } // namespace media