Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/copresence/handlers/audio/audio_directive_handler.h" | 5 #include "components/copresence/handlers/audio/audio_directive_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "components/copresence/mediums/audio/audio_player.h" | 10 #include "components/copresence/mediums/audio/audio_manager.h" |
| 10 #include "components/copresence/mediums/audio/audio_recorder.h" | |
| 11 #include "components/copresence/test/audio_test_support.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" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 13 |
| 16 using ::testing::_; | |
| 17 using ::testing::Le; | |
| 18 | |
| 19 namespace copresence { | 14 namespace copresence { |
| 20 | 15 |
| 21 class TestAudioPlayer : public AudioPlayer { | 16 namespace { |
| 17 | |
| 18 class TestAudioManager final : public AudioManager { | |
|
Daniel Erat
2014/10/21 19:43:22
nit: AudioManagerStub
rkc
2014/10/22 00:06:20
Done.
| |
| 22 public: | 19 public: |
| 23 TestAudioPlayer() {} | 20 TestAudioManager() {} |
| 24 virtual ~TestAudioPlayer() {} | 21 virtual ~TestAudioManager() {} |
| 25 | 22 |
| 26 // AudioPlayer overrides: | 23 // AudioManager overrides: |
| 27 virtual void Initialize() override {} | 24 virtual void Initialize(const DecodeSamplesCallback& decode_cb, |
| 28 virtual void Play( | 25 const EncodeTokenCallback& encode_cb) override {} |
| 29 const scoped_refptr<media::AudioBusRefCounted>& /* samples */) override { | 26 virtual void StartPlaying(AudioType type) override { playing_[type] = true; } |
| 30 set_is_playing(true); | 27 virtual void StopPlaying(AudioType type) override { playing_[type] = false; } |
| 28 virtual void StartRecording(AudioType type) override { | |
| 29 recording_[type] = true; | |
| 31 } | 30 } |
| 32 virtual void Stop() override { set_is_playing(false); } | 31 virtual void StopRecording(AudioType type) override { |
| 33 virtual void Finalize() override { delete this; } | 32 recording_[type] = false; |
| 33 } | |
| 34 virtual void SetToken(AudioType type, | |
| 35 const std::string& url_unsafe_token) override{}; | |
|
Daniel Erat
2014/10/21 19:43:22
nit: "override {}"
rkc
2014/10/22 00:06:20
Done.
| |
| 36 virtual const std::string& get_token(AudioType type) override { | |
| 37 return token_; | |
| 38 } | |
| 39 virtual bool IsRecording(AudioType type) override { return recording_[type]; } | |
| 40 virtual bool IsPlaying(AudioType type) override { return playing_[type]; } | |
| 34 | 41 |
| 35 private: | 42 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(TestAudioPlayer); | 43 // Indexed using enum AudioType. |
| 44 bool playing_[2]; | |
| 45 bool recording_[2]; | |
| 46 | |
| 47 std::string token_; | |
|
Daniel Erat
2014/10/21 19:43:22
did you mean to set this in SetToken()? it's alway
rkc
2014/10/22 00:06:20
This returns a reference pointer. It needs a real
| |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(TestAudioManager); | |
| 37 }; | 50 }; |
| 38 | 51 |
| 39 class TestAudioRecorder : public AudioRecorder { | 52 } // namespace |
| 40 public: | |
| 41 TestAudioRecorder() : AudioRecorder(AudioRecorder::DecodeSamplesCallback()) {} | |
| 42 virtual ~TestAudioRecorder() {} | |
| 43 | |
| 44 // AudioRecorder overrides: | |
| 45 virtual void Initialize() override {} | |
| 46 virtual void Record() override { set_is_recording(true); } | |
| 47 virtual void Stop() override { set_is_recording(false); } | |
| 48 virtual void Finalize() override { delete this; } | |
| 49 | |
| 50 private: | |
| 51 DISALLOW_COPY_AND_ASSIGN(TestAudioRecorder); | |
| 52 }; | |
| 53 | 53 |
| 54 class AudioDirectiveHandlerTest : public testing::Test { | 54 class AudioDirectiveHandlerTest : public testing::Test { |
| 55 public: | 55 public: |
| 56 AudioDirectiveHandlerTest() | 56 AudioDirectiveHandlerTest() |
| 57 : directive_handler_(new AudioDirectiveHandler( | 57 : directive_handler_(new AudioDirectiveHandler()) { |
| 58 AudioRecorder::DecodeSamplesCallback(), | 58 scoped_ptr<TestAudioManager> manager(new TestAudioManager); |
| 59 base::Bind(&AudioDirectiveHandlerTest::EncodeToken, | 59 manager_ptr_ = manager.get(); |
| 60 base::Unretained(this)))) { | 60 directive_handler_->set_audio_manager_for_testing(manager.Pass()); |
| 61 directive_handler_->set_player_audible_for_testing(new TestAudioPlayer()); | 61 directive_handler_->Initialize( |
| 62 directive_handler_->set_player_inaudible_for_testing(new TestAudioPlayer()); | 62 base::Bind(&AudioDirectiveHandlerTest::DecodeSamples, |
|
Daniel Erat
2014/10/21 19:43:22
just pass a callback around an empty function in t
rkc
2014/10/22 00:06:20
Done.
| |
| 63 directive_handler_->set_recorder_for_testing(new TestAudioRecorder()); | 63 base::Unretained(this)), |
| 64 base::Bind(&AudioDirectiveHandlerTest::EncodeToken, | |
| 65 base::Unretained(this))); | |
| 64 } | 66 } |
| 65 virtual ~AudioDirectiveHandlerTest() {} | 67 virtual ~AudioDirectiveHandlerTest() {} |
| 66 | 68 |
| 67 void DirectiveAdded() {} | 69 void DirectiveAdded() {} |
| 68 | 70 |
| 69 protected: | 71 protected: |
| 70 void EncodeToken(const std::string& token, | 72 // Callback stubs to pass into the directive handler. |
| 71 bool audible, | 73 void DecodeSamples(AudioType, const std::string&) {} |
| 72 const AudioDirectiveHandler::SamplesCallback& callback) { | 74 void EncodeToken(const std::string&, |
| 73 callback.Run( | 75 AudioType, |
| 74 token, audible, CreateRandomAudioRefCounted(0x1337, 1, 0x7331)); | 76 const AudioManager::SamplesCallback& callback) {} |
| 75 } | |
| 76 | 77 |
| 77 copresence::TokenInstruction CreateTransmitInstruction( | 78 copresence::TokenInstruction CreateTransmitInstruction( |
| 78 const std::string& token, | 79 const std::string& token, |
| 79 bool audible) { | 80 bool audible) { |
| 80 copresence::TokenInstruction instruction; | 81 copresence::TokenInstruction instruction; |
| 81 instruction.set_token_instruction_type(copresence::TRANSMIT); | 82 instruction.set_token_instruction_type(copresence::TRANSMIT); |
| 82 instruction.set_token_id(token); | 83 instruction.set_token_id(token); |
| 83 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | 84 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
| 84 : AUDIO_ULTRASOUND_PASSBAND); | 85 : AUDIO_ULTRASOUND_PASSBAND); |
| 85 return instruction; | 86 return instruction; |
| 86 } | 87 } |
| 87 | 88 |
| 88 copresence::TokenInstruction CreateReceiveInstruction() { | 89 copresence::TokenInstruction CreateReceiveInstruction(bool audible) { |
| 89 copresence::TokenInstruction instruction; | 90 copresence::TokenInstruction instruction; |
| 90 instruction.set_token_instruction_type(copresence::RECEIVE); | 91 instruction.set_token_instruction_type(copresence::RECEIVE); |
| 92 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | |
| 93 : AUDIO_ULTRASOUND_PASSBAND); | |
| 91 return instruction; | 94 return instruction; |
| 92 } | 95 } |
| 93 | 96 |
| 97 bool IsPlaying(AudioType type) { return manager_ptr_->IsPlaying(type); } | |
| 98 | |
| 99 bool IsRecording(AudioType type) { return manager_ptr_->IsRecording(type); } | |
| 100 | |
| 94 // This order is important. We want the message loop to get created before | 101 // This order is important. We want the message loop to get created before |
| 95 // our the audio directive handler since the directive list ctor (invoked | 102 // our the audio directive handler since the directive list ctor (invoked |
| 96 // from the directive handler ctor) will post tasks. | 103 // from the directive handler ctor) will post tasks. |
| 97 base::MessageLoop message_loop_; | 104 base::MessageLoop message_loop_; |
| 98 scoped_ptr<AudioDirectiveHandler> directive_handler_; | 105 scoped_ptr<AudioDirectiveHandler> directive_handler_; |
| 106 TestAudioManager* manager_ptr_; | |
|
Daniel Erat
2014/10/21 19:43:22
nit: document that this is unowned
rkc
2014/10/22 00:06:20
Done.
| |
| 99 | 107 |
| 100 private: | 108 private: |
| 101 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); | 109 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
| 102 }; | 110 }; |
| 103 | 111 |
| 104 TEST_F(AudioDirectiveHandlerTest, Basic) { | 112 TEST_F(AudioDirectiveHandlerTest, Basic) { |
| 105 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); | 113 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); |
| 106 directive_handler_->AddInstruction( | 114 directive_handler_->AddInstruction( |
| 107 CreateTransmitInstruction("token", true), "op_id1", kTtl); | 115 CreateTransmitInstruction("token", true), "op_id1", kTtl); |
| 108 directive_handler_->AddInstruction( | 116 directive_handler_->AddInstruction( |
| 109 CreateTransmitInstruction("token", false), "op_id1", kTtl); | 117 CreateTransmitInstruction("token", false), "op_id1", kTtl); |
| 110 directive_handler_->AddInstruction( | 118 directive_handler_->AddInstruction( |
| 111 CreateTransmitInstruction("token", false), "op_id2", kTtl); | 119 CreateTransmitInstruction("token", false), "op_id2", kTtl); |
| 112 directive_handler_->AddInstruction( | 120 directive_handler_->AddInstruction( |
| 113 CreateReceiveInstruction(), "op_id1", kTtl); | 121 CreateReceiveInstruction(false), "op_id1", kTtl); |
| 114 directive_handler_->AddInstruction( | 122 directive_handler_->AddInstruction( |
| 115 CreateReceiveInstruction(), "op_id2", kTtl); | 123 CreateReceiveInstruction(true), "op_id2", kTtl); |
| 116 directive_handler_->AddInstruction( | 124 directive_handler_->AddInstruction( |
| 117 CreateReceiveInstruction(), "op_id3", kTtl); | 125 CreateReceiveInstruction(false), "op_id3", kTtl); |
| 118 | 126 |
| 119 EXPECT_EQ(true, directive_handler_->player_audible_->IsPlaying()); | 127 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 120 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying()); | 128 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 121 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); | 129 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 130 EXPECT_TRUE(IsRecording(INAUDIBLE)); | |
| 122 | 131 |
| 123 directive_handler_->RemoveInstructions("op_id1"); | 132 directive_handler_->RemoveInstructions("op_id1"); |
| 124 EXPECT_FALSE(directive_handler_->player_audible_->IsPlaying()); | 133 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
| 125 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying()); | 134 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 126 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); | 135 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 136 EXPECT_TRUE(IsRecording(INAUDIBLE)); | |
| 127 | 137 |
| 128 directive_handler_->RemoveInstructions("op_id2"); | 138 directive_handler_->RemoveInstructions("op_id2"); |
| 129 EXPECT_FALSE(directive_handler_->player_inaudible_->IsPlaying()); | 139 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 130 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); | 140 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 141 EXPECT_TRUE(IsRecording(INAUDIBLE)); | |
| 131 | 142 |
| 132 directive_handler_->RemoveInstructions("op_id3"); | 143 directive_handler_->RemoveInstructions("op_id3"); |
| 133 EXPECT_FALSE(directive_handler_->recorder_->IsRecording()); | 144 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 134 } | 145 } |
| 135 | 146 |
| 136 // TODO(rkc): Write more tests that check more convoluted sequences of | 147 // TODO(rkc): Write more tests that check more convoluted sequences of |
| 137 // transmits/receives. | 148 // transmits/receives. |
| 149 // TODO(rkc): Write tests to move time forward and test functionality. | |
| 138 | 150 |
| 139 } // namespace copresence | 151 } // namespace copresence |
| OLD | NEW |