| Index: media/audio/fake_audio_provider_unittest.cc
|
| diff --git a/media/audio/fake_audio_consumer_unittest.cc b/media/audio/fake_audio_provider_unittest.cc
|
| similarity index 59%
|
| copy from media/audio/fake_audio_consumer_unittest.cc
|
| copy to media/audio/fake_audio_provider_unittest.cc
|
| index ab97eaea8c76c78fa263bd41793d26b58be50a00..ad50aa8e311018ce4b169f25563065d568b4a0f2 100644
|
| --- a/media/audio/fake_audio_consumer_unittest.cc
|
| +++ b/media/audio/fake_audio_provider_unittest.cc
|
| @@ -1,41 +1,53 @@
|
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| #include "base/bind.h"
|
| +#include "base/command_line.h"
|
| +#include "base/files/file_path.h"
|
| #include "base/message_loop/message_loop.h"
|
| #include "base/time/time.h"
|
| #include "media/audio/audio_parameters.h"
|
| -#include "media/audio/fake_audio_consumer.h"
|
| -#include "media/audio/simple_sources.h"
|
| +#include "media/audio/fake_audio_provider.h"
|
| +#include "media/base/media_switches.h"
|
| +#include "media/base/test_data_util.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace media {
|
|
|
| static const int kTestCallbacks = 5;
|
|
|
| -class FakeAudioConsumerTest : public testing::Test {
|
| +class FakeAudioProviderTest : public ::testing::TestWithParam<bool> {
|
| public:
|
| - FakeAudioConsumerTest()
|
| + FakeAudioProviderTest()
|
| : params_(
|
| AudioParameters::AUDIO_FAKE, CHANNEL_LAYOUT_STEREO, 44100, 8, 128),
|
| - fake_consumer_(message_loop_.message_loop_proxy(), params_),
|
| - source_(params_.channels(), 200.0, params_.sample_rate()) {
|
| - time_between_callbacks_ = base::TimeDelta::FromMicroseconds(
|
| - params_.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
|
| - static_cast<float>(params_.sample_rate()));
|
| + fake_provider_(message_loop_.message_loop_proxy(), params_),
|
| + num_callbacks_(0) {
|
| + time_between_callbacks_ = base::TimeDelta::FromMilliseconds(
|
| + params_.frames_per_buffer() * base::Time::kMillisecondsPerSecond /
|
| + params_.sample_rate());
|
| }
|
|
|
| - ~FakeAudioConsumerTest() override {}
|
| + ~FakeAudioProviderTest() override {}
|
|
|
| - void ConsumeData(AudioBus* audio_bus) {
|
| - source_.OnMoreData(audio_bus, 0);
|
| + void ReceiveInputData(AudioBus* audio_bus, int buffer_size) {
|
| + num_callbacks_++;
|
| }
|
|
|
| void RunOnAudioThread() {
|
| ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
|
| - fake_consumer_.Start(base::Bind(
|
| - &FakeAudioConsumerTest::ConsumeData, base::Unretained(this)));
|
| +
|
| + bool test_with_file = GetParam();
|
| + if (test_with_file) {
|
| + base::FilePath test_wav_file = media::GetTestDataFilePath("bear_pcm.wav");
|
| + fake_provider_.OpenInFileMode(test_wav_file);
|
| + } else {
|
| + fake_provider_.OpenInBeepMode();
|
| + }
|
| +
|
| + fake_provider_.Start(base::Bind(
|
| + &FakeAudioProviderTest::ReceiveInputData, base::Unretained(this)));
|
| }
|
|
|
| void RunOnceOnAudioThread() {
|
| @@ -44,27 +56,27 @@ class FakeAudioConsumerTest : public testing::Test {
|
| // Start() should immediately post a task to run the source callback, so we
|
| // should end up with only a single callback being run.
|
| message_loop_.PostTask(FROM_HERE, base::Bind(
|
| - &FakeAudioConsumerTest::EndTest, base::Unretained(this), 1));
|
| + &FakeAudioProviderTest::EndTest, base::Unretained(this), 1));
|
| }
|
|
|
| void StopStartOnAudioThread() {
|
| ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
|
| - fake_consumer_.Stop();
|
| + fake_provider_.Stop();
|
| RunOnAudioThread();
|
| }
|
|
|
| void TimeCallbacksOnAudioThread(int callbacks) {
|
| ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
|
|
|
| - if (source_.callbacks() == 0) {
|
| + if (num_callbacks_ == 0) {
|
| RunOnAudioThread();
|
| start_time_ = base::TimeTicks::Now();
|
| }
|
|
|
| // Keep going until we've seen the requested number of callbacks.
|
| - if (source_.callbacks() < callbacks) {
|
| + if (num_callbacks_ < callbacks) {
|
| message_loop_.PostDelayedTask(FROM_HERE, base::Bind(
|
| - &FakeAudioConsumerTest::TimeCallbacksOnAudioThread,
|
| + &FakeAudioProviderTest::TimeCallbacksOnAudioThread,
|
| base::Unretained(this), callbacks), time_between_callbacks_ / 2);
|
| } else {
|
| end_time_ = base::TimeTicks::Now();
|
| @@ -74,46 +86,46 @@ class FakeAudioConsumerTest : public testing::Test {
|
|
|
| void EndTest(int callbacks) {
|
| ASSERT_TRUE(message_loop_.message_loop_proxy()->BelongsToCurrentThread());
|
| - fake_consumer_.Stop();
|
| - EXPECT_LE(callbacks, source_.callbacks());
|
| + fake_provider_.Stop();
|
| + EXPECT_LE(callbacks, num_callbacks_);
|
| message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
|
| }
|
|
|
| protected:
|
| base::MessageLoop message_loop_;
|
| AudioParameters params_;
|
| - FakeAudioConsumer fake_consumer_;
|
| - SineWaveAudioSource source_;
|
| + FakeAudioProvider fake_provider_;
|
| base::TimeTicks start_time_;
|
| base::TimeTicks end_time_;
|
| base::TimeDelta time_between_callbacks_;
|
| + int num_callbacks_;
|
|
|
| private:
|
| - DISALLOW_COPY_AND_ASSIGN(FakeAudioConsumerTest);
|
| + DISALLOW_COPY_AND_ASSIGN(FakeAudioProviderTest);
|
| };
|
|
|
| // Ensure the fake audio stream runs on the audio thread and handles fires
|
| // callbacks to the AudioSourceCallback.
|
| -TEST_F(FakeAudioConsumerTest, FakeStreamBasicCallback) {
|
| +TEST_P(FakeAudioProviderTest, FakeStreamBasicCallback) {
|
| message_loop_.PostTask(FROM_HERE, base::Bind(
|
| - &FakeAudioConsumerTest::RunOnceOnAudioThread,
|
| + &FakeAudioProviderTest::RunOnceOnAudioThread,
|
| base::Unretained(this)));
|
| message_loop_.Run();
|
| }
|
|
|
| // Ensure the time between callbacks is sane.
|
| -TEST_F(FakeAudioConsumerTest, TimeBetweenCallbacks) {
|
| +TEST_P(FakeAudioProviderTest, TimeBetweenCallbacks) {
|
| message_loop_.PostTask(FROM_HERE, base::Bind(
|
| - &FakeAudioConsumerTest::TimeCallbacksOnAudioThread,
|
| + &FakeAudioProviderTest::TimeCallbacksOnAudioThread,
|
| base::Unretained(this), kTestCallbacks));
|
| message_loop_.Run();
|
|
|
| // There are only (kTestCallbacks - 1) intervals between kTestCallbacks.
|
| base::TimeDelta actual_time_between_callbacks =
|
| - (end_time_ - start_time_) / (source_.callbacks() - 1);
|
| + (end_time_ - start_time_) / (num_callbacks_ - 1);
|
|
|
| // Ensure callback time is no faster than the expected time between callbacks.
|
| - EXPECT_TRUE(actual_time_between_callbacks >= time_between_callbacks_);
|
| + EXPECT_GE(actual_time_between_callbacks, time_between_callbacks_);
|
|
|
| // Softly check if the callback time is no slower than twice the expected time
|
| // between callbacks. Since this test runs on the bots we can't be too strict
|
| @@ -122,21 +134,28 @@ TEST_F(FakeAudioConsumerTest, TimeBetweenCallbacks) {
|
| LOG(ERROR) << "Time between fake audio callbacks is too large!";
|
| }
|
|
|
| -// Ensure Start()/Stop() on the stream doesn't generate too many callbacks. See
|
| -// http://crbug.com/159049
|
| -TEST_F(FakeAudioConsumerTest, StartStopClearsCallbacks) {
|
| +// Ensure Start()/Stop() on the stream doesn't generate too many callbacks.
|
| +TEST_P(FakeAudioProviderTest, StartStopClearsCallbacks) {
|
| message_loop_.PostTask(FROM_HERE, base::Bind(
|
| - &FakeAudioConsumerTest::TimeCallbacksOnAudioThread,
|
| + &FakeAudioProviderTest::TimeCallbacksOnAudioThread,
|
| base::Unretained(this), kTestCallbacks));
|
|
|
| // Issue a Stop() / Start() in between expected callbacks to maximize the
|
| // chance of catching the FakeAudioOutputStream doing the wrong thing.
|
| message_loop_.PostDelayedTask(FROM_HERE, base::Bind(
|
| - &FakeAudioConsumerTest::StopStartOnAudioThread,
|
| + &FakeAudioProviderTest::StopStartOnAudioThread,
|
| base::Unretained(this)), time_between_callbacks_ / 2);
|
|
|
| // EndTest() will ensure the proper number of callbacks have occurred.
|
| message_loop_.Run();
|
| }
|
|
|
| +INSTANTIATE_TEST_CASE_P(WithBeepGenerator,
|
| + FakeAudioProviderTest,
|
| + testing::Values(false));
|
| +
|
| +INSTANTIATE_TEST_CASE_P(WithFileGenerator,
|
| + FakeAudioProviderTest,
|
| + testing::Values(true));
|
| +
|
| } // namespace media
|
|
|