Chromium Code Reviews| Index: components/copresence/mediums/audio/audio_manager_unittest.cc |
| diff --git a/components/copresence/mediums/audio/audio_manager_unittest.cc b/components/copresence/mediums/audio/audio_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..798c4efa636fd873556f0c0148a7eef2f5a1411d |
| --- /dev/null |
| +++ b/components/copresence/mediums/audio/audio_manager_unittest.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright 2014 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 "components/copresence/mediums/audio/audio_manager.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "components/copresence/mediums/audio/audio_player.h" |
| +#include "components/copresence/mediums/audio/audio_recorder.h" |
| +#include "components/copresence/test/audio_test_support.h" |
| +#include "media/base/audio_bus.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using ::testing::_; |
| +using ::testing::Le; |
| + |
| +namespace copresence { |
| + |
| +class TestAudioPlayer : public AudioPlayer { |
| + public: |
| + TestAudioPlayer() {} |
| + virtual ~TestAudioPlayer() {} |
| + |
| + // AudioPlayer overrides: |
| + virtual void Initialize() override {} |
| + virtual void Play( |
| + const scoped_refptr<media::AudioBusRefCounted>& /* samples */) override { |
| + set_is_playing(true); |
| + } |
| + virtual void Stop() override { set_is_playing(false); } |
| + virtual void Finalize() override { delete this; } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestAudioPlayer); |
| +}; |
| + |
| +class TestAudioRecorder : public AudioRecorder { |
| + public: |
| + TestAudioRecorder() : AudioRecorder() {} |
| + virtual ~TestAudioRecorder() {} |
| + |
| + // AudioRecorder overrides: |
| + virtual void Initialize(const RecordedSamplesCallback& cb) override { |
| + cb_ = cb; |
| + } |
| + virtual void Record() override { set_is_recording(true); } |
| + virtual void Stop() override { set_is_recording(false); } |
| + virtual void Finalize() override { delete this; } |
| + |
| + void TriggerDecodeRequest() { |
| + if (!cb_.is_null()) |
| + cb_.Run(std::string(7, 0x1337)); |
| + } |
| + |
| + private: |
| + RecordedSamplesCallback cb_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestAudioRecorder); |
| +}; |
| + |
| +class AudioManagerTest : public testing::Test { |
| + public: |
| + AudioManagerTest() |
| + : audio_manager_(new AudioManager()), |
| + audible_player_(new TestAudioPlayer), |
| + inaudible_player_(new TestAudioPlayer), |
| + recorder_(new TestAudioRecorder), |
| + last_received_decode_type_(UNKNOWN) { |
| + audio_manager_->set_player_for_testing(AUDIBLE, audible_player_); |
| + audio_manager_->set_player_for_testing(INAUDIBLE, inaudible_player_); |
| + audio_manager_->set_recorder_for_testing(recorder_); |
| + audio_manager_->Initialize( |
| + base::Bind(&AudioManagerTest::DecodeSamples, base::Unretained(this)), |
| + base::Bind(&AudioManagerTest::EncodeToken, base::Unretained(this))); |
| + } |
| + virtual ~AudioManagerTest() {} |
| + |
| + void DirectiveAdded() {} |
| + |
| + protected: |
| + void EncodeToken(const std::string& token, |
| + AudioType audible, |
| + const AudioManager::SamplesCallback& callback) { |
| + callback.Run( |
| + token, audible, CreateRandomAudioRefCounted(0x1337, 1, 0x7331)); |
| + } |
| + |
| + void DecodeSamples(AudioType type, const std::string& /* samples */) { |
| + last_received_decode_type_ = type; |
| + } |
| + |
| + base::MessageLoop message_loop_; |
| + scoped_ptr<AudioManager> audio_manager_; |
| + |
| + // These will be deleted by audio_manager_'s dtor calling finalize on them. |
| + TestAudioPlayer* audible_player_; |
| + TestAudioPlayer* inaudible_player_; |
| + TestAudioRecorder* recorder_; |
| + |
| + AudioType last_received_decode_type_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(AudioManagerTest); |
| +}; |
| + |
| +TEST_F(AudioManagerTest, Basic) { |
| + audio_manager_->StartPlaying(AUDIBLE); |
| + EXPECT_TRUE(audio_manager_->is_playing_for_testing(AUDIBLE)); |
| + EXPECT_FALSE(audio_manager_->is_playing_for_testing(INAUDIBLE)); |
| + |
| + audio_manager_->StopPlaying(AUDIBLE); |
| + EXPECT_FALSE(audio_manager_->is_playing_for_testing(AUDIBLE)); |
| + |
| + audio_manager_->StartRecording(INAUDIBLE); |
| + EXPECT_TRUE(audio_manager_->is_recording_for_testing(INAUDIBLE)); |
| + EXPECT_FALSE(audio_manager_->is_recording_for_testing(AUDIBLE)); |
| + |
| + audio_manager_->StopRecording(INAUDIBLE); |
| + EXPECT_FALSE(audio_manager_->is_recording_for_testing(INAUDIBLE)); |
| +} |
| + |
| +TEST_F(AudioManagerTest, EncodeToken) { |
| + audio_manager_->StartPlaying(AUDIBLE); |
| + // No token yet, player shouldn't be playing. |
| + EXPECT_FALSE(audible_player_->IsPlaying()); |
| + |
| + audio_manager_->SetToken(INAUDIBLE, "abcd"); |
| + // No 'audible' token yet, so player still shouldn't be playing. |
|
Charlie
2014/10/17 22:42:26
*audible*
rkc
2014/10/18 00:21:55
Done.
|
| + EXPECT_FALSE(audible_player_->IsPlaying()); |
| + |
| + audio_manager_->SetToken(AUDIBLE, "abcd"); |
| + EXPECT_TRUE(audible_player_->IsPlaying()); |
| +} |
| + |
| +TEST_F(AudioManagerTest, Record) { |
| + recorder_->TriggerDecodeRequest(); |
| + EXPECT_EQ(UNKNOWN, last_received_decode_type_); |
| + |
| + audio_manager_->StartRecording(AUDIBLE); |
| + recorder_->TriggerDecodeRequest(); |
| + EXPECT_EQ(AUDIBLE, last_received_decode_type_); |
| + |
| + audio_manager_->StartRecording(INAUDIBLE); |
| + recorder_->TriggerDecodeRequest(); |
| + EXPECT_EQ(BOTH, last_received_decode_type_); |
| + |
| + audio_manager_->StopRecording(AUDIBLE); |
| + recorder_->TriggerDecodeRequest(); |
| + EXPECT_EQ(INAUDIBLE, last_received_decode_type_); |
| +} |
| + |
| +} // namespace copresence |