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