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/handlers/audio/audio_directive_handler.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(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 54 class AudioDirectiveHandlerTest : public testing::Test { 55 public: 56 AudioDirectiveHandlerTest() 57 : directive_handler_(new AudioDirectiveHandler( 58 AudioRecorder::DecodeSamplesCallback(), 59 base::Bind(&AudioDirectiveHandlerTest::EncodeToken, 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 } 65 virtual ~AudioDirectiveHandlerTest() {} 66 67 void DirectiveAdded() {} 68 69 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( 78 const std::string& token, 79 bool audible) { 80 copresence::TokenInstruction instruction; 81 instruction.set_token_instruction_type(copresence::TRANSMIT); 82 instruction.set_token_id(token); 83 instruction.set_medium(audible ? AUDIO_AUDIBLE_DTMF 84 : AUDIO_ULTRASOUND_PASSBAND); 85 return instruction; 86 } 87 88 copresence::TokenInstruction CreateReceiveInstruction() { 89 copresence::TokenInstruction instruction; 90 instruction.set_token_instruction_type(copresence::RECEIVE); 91 return instruction; 92 } 93 94 // 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 96 // from the directive handler ctor) will post tasks. 97 base::MessageLoop message_loop_; 98 scoped_ptr<AudioDirectiveHandler> directive_handler_; 99 100 private: 101 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); 102 }; 103 104 TEST_F(AudioDirectiveHandlerTest, Basic) { 105 const base::TimeDelta kTtl = base::TimeDelta::FromMilliseconds(9999); 106 directive_handler_->AddInstruction( 107 CreateTransmitInstruction("token", true), "op_id1", kTtl); 108 directive_handler_->AddInstruction( 109 CreateTransmitInstruction("token", false), "op_id1", kTtl); 110 directive_handler_->AddInstruction( 111 CreateTransmitInstruction("token", false), "op_id2", kTtl); 112 directive_handler_->AddInstruction( 113 CreateReceiveInstruction(), "op_id1", kTtl); 114 directive_handler_->AddInstruction( 115 CreateReceiveInstruction(), "op_id2", kTtl); 116 directive_handler_->AddInstruction( 117 CreateReceiveInstruction(), "op_id3", kTtl); 118 119 EXPECT_EQ(true, directive_handler_->player_audible_->IsPlaying()); 120 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying()); 121 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); 122 123 directive_handler_->RemoveInstructions("op_id1"); 124 EXPECT_FALSE(directive_handler_->player_audible_->IsPlaying()); 125 EXPECT_EQ(true, directive_handler_->player_inaudible_->IsPlaying()); 126 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); 127 128 directive_handler_->RemoveInstructions("op_id2"); 129 EXPECT_FALSE(directive_handler_->player_inaudible_->IsPlaying()); 130 EXPECT_EQ(true, directive_handler_->recorder_->IsRecording()); 131 132 directive_handler_->RemoveInstructions("op_id3"); 133 EXPECT_FALSE(directive_handler_->recorder_->IsRecording()); 134 } 135 136 // TODO(rkc): Write more tests that check more convoluted sequences of 137 // transmits/receives. 138 139 } // namespace copresence 140