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