OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "components/copresence/mediums/audio/audio_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "components/copresence/mediums/audio/audio_player.h" |
| 10 #include "components/copresence/mediums/audio/audio_recorder.h" |
| 11 #include "components/copresence/test/audio_test_support.h" |
| 12 #include "media/base/audio_bus.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 using ::testing::_; |
| 17 using ::testing::Le; |
| 18 |
| 19 namespace copresence { |
| 20 |
| 21 class TestAudioPlayer : public AudioPlayer { |
| 22 public: |
| 23 TestAudioPlayer() {} |
| 24 virtual ~TestAudioPlayer() {} |
| 25 |
| 26 // AudioPlayer overrides: |
| 27 virtual void Initialize() override {} |
| 28 virtual void Play( |
| 29 const scoped_refptr<media::AudioBusRefCounted>& /* samples */) override { |
| 30 set_is_playing(true); |
| 31 } |
| 32 virtual void Stop() override { set_is_playing(false); } |
| 33 virtual void Finalize() override { delete this; } |
| 34 |
| 35 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(TestAudioPlayer); |
| 37 }; |
| 38 |
| 39 class TestAudioRecorder : public AudioRecorder { |
| 40 public: |
| 41 TestAudioRecorder() : AudioRecorder() {} |
| 42 virtual ~TestAudioRecorder() {} |
| 43 |
| 44 // AudioRecorder overrides: |
| 45 virtual void Initialize(const RecordedSamplesCallback& cb) override { |
| 46 cb_ = cb; |
| 47 } |
| 48 virtual void Record() override { set_is_recording(true); } |
| 49 virtual void Stop() override { set_is_recording(false); } |
| 50 virtual void Finalize() override { delete this; } |
| 51 |
| 52 void TriggerDecodeRequest() { |
| 53 if (!cb_.is_null()) |
| 54 cb_.Run(std::string(7, 0x1337)); |
| 55 } |
| 56 |
| 57 private: |
| 58 RecordedSamplesCallback cb_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(TestAudioRecorder); |
| 61 }; |
| 62 |
| 63 class AudioManagerTest : public testing::Test { |
| 64 public: |
| 65 AudioManagerTest() |
| 66 : audio_manager_(new AudioManager()), |
| 67 audible_player_(new TestAudioPlayer), |
| 68 inaudible_player_(new TestAudioPlayer), |
| 69 recorder_(new TestAudioRecorder), |
| 70 last_received_decode_type_(UNKNOWN) { |
| 71 audio_manager_->set_player_for_testing(AUDIBLE, audible_player_); |
| 72 audio_manager_->set_player_for_testing(INAUDIBLE, inaudible_player_); |
| 73 audio_manager_->set_recorder_for_testing(recorder_); |
| 74 audio_manager_->Initialize( |
| 75 base::Bind(&AudioManagerTest::DecodeSamples, base::Unretained(this)), |
| 76 base::Bind(&AudioManagerTest::EncodeToken, base::Unretained(this))); |
| 77 } |
| 78 virtual ~AudioManagerTest() {} |
| 79 |
| 80 void DirectiveAdded() {} |
| 81 |
| 82 protected: |
| 83 void EncodeToken(const std::string& token, |
| 84 AudioType audible, |
| 85 const AudioManager::SamplesCallback& callback) { |
| 86 callback.Run( |
| 87 token, audible, CreateRandomAudioRefCounted(0x1337, 1, 0x7331)); |
| 88 } |
| 89 |
| 90 void DecodeSamples(AudioType type, const std::string& /* samples */) { |
| 91 last_received_decode_type_ = type; |
| 92 } |
| 93 |
| 94 base::MessageLoop message_loop_; |
| 95 scoped_ptr<AudioManager> audio_manager_; |
| 96 |
| 97 // These will be deleted by audio_manager_'s dtor calling finalize on them. |
| 98 TestAudioPlayer* audible_player_; |
| 99 TestAudioPlayer* inaudible_player_; |
| 100 TestAudioRecorder* recorder_; |
| 101 |
| 102 AudioType last_received_decode_type_; |
| 103 |
| 104 private: |
| 105 DISALLOW_COPY_AND_ASSIGN(AudioManagerTest); |
| 106 }; |
| 107 |
| 108 TEST_F(AudioManagerTest, Basic) { |
| 109 audio_manager_->StartPlaying(AUDIBLE); |
| 110 EXPECT_TRUE(audio_manager_->is_playing_for_testing(AUDIBLE)); |
| 111 EXPECT_FALSE(audio_manager_->is_playing_for_testing(INAUDIBLE)); |
| 112 |
| 113 audio_manager_->StopPlaying(AUDIBLE); |
| 114 EXPECT_FALSE(audio_manager_->is_playing_for_testing(AUDIBLE)); |
| 115 |
| 116 audio_manager_->StartRecording(INAUDIBLE); |
| 117 EXPECT_TRUE(audio_manager_->is_recording_for_testing(INAUDIBLE)); |
| 118 EXPECT_FALSE(audio_manager_->is_recording_for_testing(AUDIBLE)); |
| 119 |
| 120 audio_manager_->StopRecording(INAUDIBLE); |
| 121 EXPECT_FALSE(audio_manager_->is_recording_for_testing(INAUDIBLE)); |
| 122 } |
| 123 |
| 124 TEST_F(AudioManagerTest, EncodeToken) { |
| 125 audio_manager_->StartPlaying(AUDIBLE); |
| 126 // No token yet, player shouldn't be playing. |
| 127 EXPECT_FALSE(audible_player_->IsPlaying()); |
| 128 |
| 129 audio_manager_->SetToken(INAUDIBLE, "abcd"); |
| 130 // No *audible* token yet, so player still shouldn't be playing. |
| 131 EXPECT_FALSE(audible_player_->IsPlaying()); |
| 132 |
| 133 audio_manager_->SetToken(AUDIBLE, "abcd"); |
| 134 EXPECT_TRUE(audible_player_->IsPlaying()); |
| 135 } |
| 136 |
| 137 TEST_F(AudioManagerTest, Record) { |
| 138 recorder_->TriggerDecodeRequest(); |
| 139 EXPECT_EQ(UNKNOWN, last_received_decode_type_); |
| 140 |
| 141 audio_manager_->StartRecording(AUDIBLE); |
| 142 recorder_->TriggerDecodeRequest(); |
| 143 EXPECT_EQ(AUDIBLE, last_received_decode_type_); |
| 144 |
| 145 audio_manager_->StartRecording(INAUDIBLE); |
| 146 recorder_->TriggerDecodeRequest(); |
| 147 EXPECT_EQ(BOTH, last_received_decode_type_); |
| 148 |
| 149 audio_manager_->StopRecording(AUDIBLE); |
| 150 recorder_->TriggerDecodeRequest(); |
| 151 EXPECT_EQ(INAUDIBLE, last_received_decode_type_); |
| 152 } |
| 153 |
| 154 } // namespace copresence |
OLD | NEW |