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