crazy linker: convert relocation unpacking to Android style.
[chromium-blink-merge.git] / media / audio / fake_audio_worker_unittest.cc
blob49a69d2b08641175675f4a6c72f736e409c74696
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 "base/bind.h"
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"
13 namespace media {
15 static const int kTestCallbacks = 5;
17 class FakeAudioWorkerTest : public testing::Test {
18 public:
19 FakeAudioWorkerTest()
20 : params_(
21 AudioParameters::AUDIO_FAKE, CHANNEL_LAYOUT_STEREO, 44100, 8, 128),
22 fake_worker_(message_loop_.message_loop_proxy(), params_),
23 seen_callbacks_(0) {
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() {
32 seen_callbacks_++;
35 void RunOnAudioThread() {
36 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
37 fake_worker_.Start(base::Bind(
38 &FakeAudioWorkerTest::CalledByFakeWorker, base::Unretained(this)));
41 void RunOnceOnAudioThread() {
42 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
43 RunOnAudioThread();
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_.message_loop_proxy()->BelongsToCurrentThread());
52 fake_worker_.Stop();
53 RunOnAudioThread();
56 void TimeCallbacksOnAudioThread(int callbacks) {
57 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
59 if (seen_callbacks_ == 0) {
60 RunOnAudioThread();
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);
69 } else {
70 end_time_ = base::TimeTicks::Now();
71 EndTest(callbacks);
75 void EndTest(int callbacks) {
76 ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
77 fake_worker_.Stop();
78 EXPECT_LE(callbacks, seen_callbacks_);
79 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
82 protected:
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_;
89 int seen_callbacks_;
91 private:
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)));
100 message_loop_.Run();
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));
108 message_loop_.Run();
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
119 // with the bounds.
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.
138 message_loop_.Run();
141 } // namespace media