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" | |
| 12 #include "media/base/audio_bus.h" | |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 11 #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::_; | 14 using ::testing::_; |
| 17 using ::testing::Le; | 15 using ::testing::Le; |
| 18 | 16 |
| 19 namespace copresence { | 17 namespace copresence { |
| 20 | 18 |
| 21 class TestAudioPlayer : public AudioPlayer { | 19 class TestAudioManager : public AudioManager { |
|
Daniel Erat
2014/10/18 21:21:18
i agree that you should create an pure virtual Aud
rkc
2014/10/20 16:45:37
Though I agree that there are benefits to having a
Charlie
2014/10/20 20:06:54
It's a little hard to talk about this without goin
rkc
2014/10/20 20:17:53
Its complexity may not grow at the same rate as th
Charlie
2014/10/20 20:33:05
We talked about this already. See crbug/424781.
rkc
2014/10/21 01:25:04
I don't necessarily agree with everything said in
| |
| 22 public: | 20 public: |
| 23 TestAudioPlayer() {} | 21 TestAudioManager() : AudioManager() {} |
| 24 virtual ~TestAudioPlayer() {} | 22 virtual ~TestAudioManager() {} |
| 25 | 23 |
| 26 // AudioPlayer overrides: | 24 // AudioManager overrides: |
| 27 virtual void Initialize() override {} | 25 virtual void Initialize(const DecodeSamplesCallback&, |
|
Daniel Erat
2014/10/18 21:21:18
it's cleaner to keep Initialize methods in the imp
rkc
2014/10/20 16:45:38
I'm confused about how this would work, maybe I am
Charlie
2014/10/20 20:06:54
Let's call it AudioManager or AudioManagerInterfac
rkc
2014/10/20 20:17:53
That doesn't fix the issue. The issue is that the
Charlie
2014/10/20 20:33:05
By "container class", do you mean AudioDirectiveHa
rkc
2014/10/21 01:25:04
What's the difference between this and just creati
Charlie
2014/10/21 16:27:09
set_*_for_testing() methods are sub-optimal becaus
| |
| 28 virtual void Play( | 26 const EncodeTokenCallback&) override {} |
| 29 const scoped_refptr<media::AudioBusRefCounted>& /* samples */) override { | 27 virtual void StartPlaying(AudioType type) override { |
| 30 set_is_playing(true); | 28 set_playing_for_testing(type, true); |
|
Daniel Erat
2014/10/18 21:21:18
it would also be cleaner if this stub class had it
rkc
2014/10/20 16:45:37
The TestAudioPlayer/Recorder are technically deleg
| |
| 31 } | 29 } |
| 32 virtual void Stop() override { set_is_playing(false); } | 30 virtual void StopPlaying(AudioType type) override { |
| 33 virtual void Finalize() override { delete this; } | 31 set_playing_for_testing(type, false); |
| 32 } | |
| 33 virtual void StartRecording(AudioType type) override { | |
| 34 set_recording_for_testing(type, true); | |
| 35 } | |
| 36 virtual void StopRecording(AudioType type) override { | |
| 37 set_recording_for_testing(type, false); | |
| 38 } | |
| 39 virtual void SetToken(AudioType, const std::string&) override {} | |
| 34 | 40 |
| 35 private: | 41 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(TestAudioPlayer); | 42 DISALLOW_COPY_AND_ASSIGN(TestAudioManager); |
| 37 }; | |
| 38 | |
| 39 class TestAudioRecorder : public AudioRecorder { | |
| 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 }; | 43 }; |
| 53 | 44 |
| 54 class AudioDirectiveHandlerTest : public testing::Test { | 45 class AudioDirectiveHandlerTest : public testing::Test { |
| 55 public: | 46 public: |
| 56 AudioDirectiveHandlerTest() | 47 AudioDirectiveHandlerTest() |
| 57 : directive_handler_(new AudioDirectiveHandler( | 48 : directive_handler_(new AudioDirectiveHandler()) { |
| 58 AudioRecorder::DecodeSamplesCallback(), | 49 directive_handler_->set_audio_manager_for_testing( |
| 59 base::Bind(&AudioDirectiveHandlerTest::EncodeToken, | 50 make_scoped_ptr(new TestAudioManager())); |
| 60 base::Unretained(this)))) { | |
| 61 directive_handler_->set_player_audible_for_testing(new TestAudioPlayer()); | |
| 62 directive_handler_->set_player_inaudible_for_testing(new TestAudioPlayer()); | |
| 63 directive_handler_->set_recorder_for_testing(new TestAudioRecorder()); | |
| 64 } | 51 } |
| 65 virtual ~AudioDirectiveHandlerTest() {} | 52 virtual ~AudioDirectiveHandlerTest() {} |
| 66 | 53 |
| 67 void DirectiveAdded() {} | 54 void DirectiveAdded() {} |
| 68 | 55 |
| 69 protected: | 56 protected: |
| 70 void EncodeToken(const std::string& token, | |
| 71 bool audible, | |
| 72 const AudioDirectiveHandler::SamplesCallback& callback) { | |
| 73 callback.Run( | |
| 74 token, audible, CreateRandomAudioRefCounted(0x1337, 1, 0x7331)); | |
| 75 } | |
| 76 | |
| 77 copresence::TokenInstruction CreateTransmitInstruction( | 57 copresence::TokenInstruction CreateTransmitInstruction( |
| 78 const std::string& token, | 58 const std::string& token, |
| 79 bool audible) { | 59 bool audible) { |
| 80 copresence::TokenInstruction instruction; | 60 copresence::TokenInstruction instruction; |
| 81 instruction.set_token_instruction_type(copresence::TRANSMIT); | 61 instruction.set_token_instruction_type(copresence::TRANSMIT); |
| 82 instruction.set_token_id(token); | 62 instruction.set_token_id(token); |
| 83 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | 63 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF |
| 84 : AUDIO_ULTRASOUND_PASSBAND); | 64 : AUDIO_ULTRASOUND_PASSBAND); |
| 85 return instruction; | 65 return instruction; |
| 86 } | 66 } |
| 87 | 67 |
| 88 copresence::TokenInstruction CreateReceiveInstruction() { | 68 copresence::TokenInstruction CreateReceiveInstruction(bool audible) { |
| 89 copresence::TokenInstruction instruction; | 69 copresence::TokenInstruction instruction; |
| 90 instruction.set_token_instruction_type(copresence::RECEIVE); | 70 instruction.set_token_instruction_type(copresence::RECEIVE); |
| 71 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF | |
| 72 : AUDIO_ULTRASOUND_PASSBAND); | |
| 91 return instruction; | 73 return instruction; |
| 92 } | 74 } |
| 93 | 75 |
| 76 bool IsPlaying(AudioType type) { | |
| 77 return directive_handler_->audio_manager_->is_playing_for_testing(type); | |
| 78 } | |
| 79 | |
| 80 bool IsRecording(AudioType type) { | |
| 81 return directive_handler_->audio_manager_->is_recording_for_testing(type); | |
| 82 } | |
| 83 | |
| 94 // This order is important. We want the message loop to get created before | 84 // 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 | 85 // our the audio directive handler since the directive list ctor (invoked |
| 96 // from the directive handler ctor) will post tasks. | 86 // from the directive handler ctor) will post tasks. |
| 97 base::MessageLoop message_loop_; | 87 base::MessageLoop message_loop_; |
| 98 scoped_ptr<AudioDirectiveHandler> directive_handler_; | 88 scoped_ptr<AudioDirectiveHandler> directive_handler_; |
| 99 | 89 |
| 100 private: | 90 private: |
| 101 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); | 91 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
| 102 }; | 92 }; |
| 103 | 93 |
| 104 TEST_F(AudioDirectiveHandlerTest, Basic) { | 94 TEST_F(AudioDirectiveHandlerTest, Basic) { |
| 105 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); | 95 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); |
| 106 directive_handler_->AddInstruction( | 96 directive_handler_->AddInstruction( |
| 107 CreateTransmitInstruction("token", true), "op_id1", kTtl); | 97 CreateTransmitInstruction("token", true), "op_id1", kTtl); |
| 108 directive_handler_->AddInstruction( | 98 directive_handler_->AddInstruction( |
| 109 CreateTransmitInstruction("token", false), "op_id1", kTtl); | 99 CreateTransmitInstruction("token", false), "op_id1", kTtl); |
| 110 directive_handler_->AddInstruction( | 100 directive_handler_->AddInstruction( |
| 111 CreateTransmitInstruction("token", false), "op_id2", kTtl); | 101 CreateTransmitInstruction("token", false), "op_id2", kTtl); |
| 112 directive_handler_->AddInstruction( | 102 directive_handler_->AddInstruction( |
| 113 CreateReceiveInstruction(), "op_id1", kTtl); | 103 CreateReceiveInstruction(false), "op_id1", kTtl); |
| 114 directive_handler_->AddInstruction( | 104 directive_handler_->AddInstruction( |
| 115 CreateReceiveInstruction(), "op_id2", kTtl); | 105 CreateReceiveInstruction(true), "op_id2", kTtl); |
| 116 directive_handler_->AddInstruction( | 106 directive_handler_->AddInstruction( |
| 117 CreateReceiveInstruction(), "op_id3", kTtl); | 107 CreateReceiveInstruction(false), "op_id3", kTtl); |
| 118 | 108 |
| 119 EXPECT_EQ(true, directive_handler_->player_audible_->IsPlaying()); | 109 EXPECT_TRUE(IsPlaying(AUDIBLE)); |
| 120 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying()); | 110 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 121 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); | 111 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 112 EXPECT_TRUE(IsRecording(INAUDIBLE)); | |
| 122 | 113 |
| 123 directive_handler_->RemoveInstructions("op_id1"); | 114 directive_handler_->RemoveInstructions("op_id1"); |
| 124 EXPECT_FALSE(directive_handler_->player_audible_->IsPlaying()); | 115 EXPECT_FALSE(IsPlaying(AUDIBLE)); |
| 125 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying()); | 116 EXPECT_TRUE(IsPlaying(INAUDIBLE)); |
| 126 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); | 117 EXPECT_TRUE(IsRecording(AUDIBLE)); |
| 118 EXPECT_TRUE(IsRecording(INAUDIBLE)); | |
| 127 | 119 |
| 128 directive_handler_->RemoveInstructions("op_id2"); | 120 directive_handler_->RemoveInstructions("op_id2"); |
| 129 EXPECT_FALSE(directive_handler_->player_inaudible_->IsPlaying()); | 121 EXPECT_FALSE(IsPlaying(INAUDIBLE)); |
| 130 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); | 122 EXPECT_FALSE(IsRecording(AUDIBLE)); |
| 123 EXPECT_TRUE(IsRecording(INAUDIBLE)); | |
| 131 | 124 |
| 132 directive_handler_->RemoveInstructions("op_id3"); | 125 directive_handler_->RemoveInstructions("op_id3"); |
| 133 EXPECT_FALSE(directive_handler_->recorder_->IsRecording()); | 126 EXPECT_FALSE(IsRecording(INAUDIBLE)); |
| 134 } | 127 } |
| 135 | 128 |
| 136 // TODO(rkc): Write more tests that check more convoluted sequences of | 129 // TODO(rkc): Write more tests that check more convoluted sequences of |
| 137 // transmits/receives. | 130 // transmits/receives. |
| 131 // TODO(rkc): Write tests to move time back and forth and test functionality. | |
| 138 | 132 |
| 139 } // namespace copresence | 133 } // namespace copresence |
| OLD | NEW |