audio_directive_handler_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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 14using ::testing::_; 15using ::testing::Le; 16 17namespace copresence { 18 19class 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 37class 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// TODO(rkc): Find and fix the memory leak here. 79#define MAYBE_Basic DISABLED_Basic 80 81TEST_F(AudioDirectiveHandlerTest, MAYBE_Basic) { 82 const base::TimeDelta kSmallTtl = base::TimeDelta::FromMilliseconds(0x1337); 83 const base::TimeDelta kLargeTtl = base::TimeDelta::FromSeconds(0x7331); 84 85 // Expect to play and record instructions for 'less' than the TTL specified, 86 // since by the time that the token would have gotten encoded, we would 87 // have (TTL - time_to_encode) left to play on that instruction. 88 EXPECT_CALL(*directive_handler_, PlayAudio(_, testing::Le(kLargeTtl))) 89 .Times(3); 90 directive_handler_->AddInstruction(CreateTransmitInstruction("token1"), 91 kLargeTtl); 92 directive_handler_->AddInstruction(CreateTransmitInstruction("token2"), 93 kLargeTtl); 94 directive_handler_->AddInstruction(CreateTransmitInstruction("token3"), 95 kSmallTtl); 96 97 EXPECT_CALL(*directive_handler_, RecordAudio(Le(kLargeTtl))).Times(3); 98 directive_handler_->AddInstruction(CreateReceiveInstruction(), kLargeTtl); 99 directive_handler_->AddInstruction(CreateReceiveInstruction(), kSmallTtl); 100 directive_handler_->AddInstruction(CreateReceiveInstruction(), kLargeTtl); 101} 102 103// TODO(rkc): When we are keeping track of which token we're currently playing, 104// add tests to make sure we don't replay if we get a token with a lower ttl 105// than the current active. 106 107} // namespace copresence 108