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.
6 #include "base/message_loop/message_loop.h"
7 #include "base/time/time.h"
8 #include "media/audio/audio_parameters.h"
9 #include "media/audio/fake_audio_worker.h"
10 #include "media/audio/simple_sources.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 static const int kTestCallbacks
= 5;
17 class FakeAudioWorkerTest
: public testing::Test
{
21 AudioParameters::AUDIO_FAKE
, CHANNEL_LAYOUT_STEREO
, 44100, 8, 128),
22 fake_worker_(message_loop_
.task_runner(), params_
),
24 time_between_callbacks_
= base::TimeDelta::FromMicroseconds(
25 params_
.frames_per_buffer() * base::Time::kMicrosecondsPerSecond
/
26 static_cast<float>(params_
.sample_rate()));
29 ~FakeAudioWorkerTest() override
{}
31 void CalledByFakeWorker() {
35 void RunOnAudioThread() {
36 ASSERT_TRUE(message_loop_
.task_runner()->BelongsToCurrentThread());
37 fake_worker_
.Start(base::Bind(
38 &FakeAudioWorkerTest::CalledByFakeWorker
, base::Unretained(this)));
41 void RunOnceOnAudioThread() {
42 ASSERT_TRUE(message_loop_
.task_runner()->BelongsToCurrentThread());
44 // Start() should immediately post a task to run the callback, so we
45 // should end up with only a single callback being run.
46 message_loop_
.PostTask(FROM_HERE
, base::Bind(
47 &FakeAudioWorkerTest::EndTest
, base::Unretained(this), 1));
50 void StopStartOnAudioThread() {
51 ASSERT_TRUE(message_loop_
.task_runner()->BelongsToCurrentThread());
56 void TimeCallbacksOnAudioThread(int callbacks
) {
57 ASSERT_TRUE(message_loop_
.task_runner()->BelongsToCurrentThread());
59 if (seen_callbacks_
== 0) {
61 start_time_
= base::TimeTicks::Now();
64 // Keep going until we've seen the requested number of callbacks.
65 if (seen_callbacks_
< callbacks
) {
66 message_loop_
.PostDelayedTask(FROM_HERE
, base::Bind(
67 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread
,
68 base::Unretained(this), callbacks
), time_between_callbacks_
/ 2);
70 end_time_
= base::TimeTicks::Now();
75 void EndTest(int callbacks
) {
76 ASSERT_TRUE(message_loop_
.task_runner()->BelongsToCurrentThread());
78 EXPECT_LE(callbacks
, seen_callbacks_
);
79 message_loop_
.PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
83 base::MessageLoop message_loop_
;
84 AudioParameters params_
;
85 FakeAudioWorker fake_worker_
;
86 base::TimeTicks start_time_
;
87 base::TimeTicks end_time_
;
88 base::TimeDelta time_between_callbacks_
;
92 DISALLOW_COPY_AND_ASSIGN(FakeAudioWorkerTest
);
95 // Ensure the worker runs on the audio thread and fires callbacks.
96 TEST_F(FakeAudioWorkerTest
, FakeBasicCallback
) {
97 message_loop_
.PostTask(FROM_HERE
, base::Bind(
98 &FakeAudioWorkerTest::RunOnceOnAudioThread
,
99 base::Unretained(this)));
103 // Ensure the time between callbacks is sane.
104 TEST_F(FakeAudioWorkerTest
, TimeBetweenCallbacks
) {
105 message_loop_
.PostTask(FROM_HERE
, base::Bind(
106 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread
,
107 base::Unretained(this), kTestCallbacks
));
110 // There are only (kTestCallbacks - 1) intervals between kTestCallbacks.
111 base::TimeDelta actual_time_between_callbacks
=
112 (end_time_
- start_time_
) / (seen_callbacks_
- 1);
114 // Ensure callback time is no faster than the expected time between callbacks.
115 EXPECT_GE(actual_time_between_callbacks
, time_between_callbacks_
);
117 // Softly check if the callback time is no slower than twice the expected time
118 // between callbacks. Since this test runs on the bots we can't be too strict
120 if (actual_time_between_callbacks
> 2 * time_between_callbacks_
)
121 LOG(ERROR
) << "Time between fake audio callbacks is too large!";
124 // Ensure Start()/Stop() on the worker doesn't generate too many callbacks. See
125 // http://crbug.com/159049.
126 TEST_F(FakeAudioWorkerTest
, StartStopClearsCallbacks
) {
127 message_loop_
.PostTask(FROM_HERE
, base::Bind(
128 &FakeAudioWorkerTest::TimeCallbacksOnAudioThread
,
129 base::Unretained(this), kTestCallbacks
));
131 // Issue a Stop() / Start() in between expected callbacks to maximize the
132 // chance of catching the worker doing the wrong thing.
133 message_loop_
.PostDelayedTask(FROM_HERE
, base::Bind(
134 &FakeAudioWorkerTest::StopStartOnAudioThread
,
135 base::Unretained(this)), time_between_callbacks_
/ 2);
137 // EndTest() will ensure the proper number of callbacks have occurred.