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