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/handlers/audio/audio_directive_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "components/copresence/test/audio_test_support.h" |
| 10 #include "media/base/audio_bus.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using ::testing::_; |
| 15 using ::testing::Le; |
| 16 |
| 17 namespace copresence { |
| 18 |
| 19 class MockAudioDirectiveHandler : public AudioDirectiveHandler { |
| 20 public: |
| 21 MockAudioDirectiveHandler( |
| 22 const AudioDirectiveList::EncodeTokenCallback& encode_cb) |
| 23 : AudioDirectiveHandler(AudioRecorder::DecodeSamplesCallback(), |
| 24 encode_cb) {} |
| 25 virtual ~MockAudioDirectiveHandler() {} |
| 26 |
| 27 // Mock out the play/record methods. |
| 28 MOCK_METHOD2(PlayAudio, |
| 29 void(const scoped_refptr<media::AudioBusRefCounted>&, |
| 30 base::TimeDelta)); |
| 31 MOCK_METHOD1(RecordAudio, void(base::TimeDelta)); |
| 32 |
| 33 private: |
| 34 DISALLOW_COPY_AND_ASSIGN(MockAudioDirectiveHandler); |
| 35 }; |
| 36 |
| 37 class AudioDirectiveHandlerTest : public testing::Test { |
| 38 public: |
| 39 AudioDirectiveHandlerTest() |
| 40 : directive_handler_(new MockAudioDirectiveHandler( |
| 41 base::Bind(&AudioDirectiveHandlerTest::EncodeToken, |
| 42 base::Unretained(this)))) {} |
| 43 |
| 44 virtual ~AudioDirectiveHandlerTest() {} |
| 45 |
| 46 void DirectiveAdded() {} |
| 47 |
| 48 protected: |
| 49 void EncodeToken(const std::string& token, |
| 50 const AudioDirectiveList::SamplesCallback& callback) { |
| 51 callback.Run(token, CreateRandomAudioRefCounted(0x1337, 1, 0x7331)); |
| 52 } |
| 53 |
| 54 copresence::TokenInstruction CreateTransmitInstruction( |
| 55 const std::string& token) { |
| 56 copresence::TokenInstruction instruction; |
| 57 instruction.set_token_instruction_type(copresence::TRANSMIT); |
| 58 instruction.set_token_id(token); |
| 59 return instruction; |
| 60 } |
| 61 |
| 62 copresence::TokenInstruction CreateReceiveInstruction() { |
| 63 copresence::TokenInstruction instruction; |
| 64 instruction.set_token_instruction_type(copresence::RECEIVE); |
| 65 return instruction; |
| 66 } |
| 67 |
| 68 // This order is important. We want the message loop to get created before |
| 69 // our the audio directive handler since the directive list ctor (invoked |
| 70 // from the directive handler ctor) will post tasks. |
| 71 base::MessageLoop message_loop_; |
| 72 scoped_ptr<MockAudioDirectiveHandler> directive_handler_; |
| 73 |
| 74 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandlerTest); |
| 76 }; |
| 77 |
| 78 TEST_F(AudioDirectiveHandlerTest, Basic) { |
| 79 const base::TimeDelta kSmallTtl = base::TimeDelta::FromMilliseconds(0x1337); |
| 80 const base::TimeDelta kLargeTtl = base::TimeDelta::FromSeconds(0x7331); |
| 81 |
| 82 // Expect to play and record instructions for 'less' than the TTL specified, |
| 83 // since by the time that the token would have gotten encoded, we would |
| 84 // have (TTL - time_to_encode) left to play on that instruction. |
| 85 EXPECT_CALL(*directive_handler_, PlayAudio(_, testing::Le(kLargeTtl))) |
| 86 .Times(3); |
| 87 directive_handler_->AddInstruction(CreateTransmitInstruction("token1"), |
| 88 kLargeTtl); |
| 89 directive_handler_->AddInstruction(CreateTransmitInstruction("token2"), |
| 90 kLargeTtl); |
| 91 directive_handler_->AddInstruction(CreateTransmitInstruction("token3"), |
| 92 kSmallTtl); |
| 93 |
| 94 EXPECT_CALL(*directive_handler_, RecordAudio(Le(kLargeTtl))).Times(3); |
| 95 directive_handler_->AddInstruction(CreateReceiveInstruction(), kLargeTtl); |
| 96 directive_handler_->AddInstruction(CreateReceiveInstruction(), kSmallTtl); |
| 97 directive_handler_->AddInstruction(CreateReceiveInstruction(), kLargeTtl); |
| 98 } |
| 99 |
| 100 // TODO(rkc): When we are keeping track of which token we're currently playing, |
| 101 // add tests to make sure we don't replay if we get a token with a lower ttl |
| 102 // than the current active. |
| 103 |
| 104 } // namespace copresence |
OLD | NEW |